location_unittest.cc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/location.h"
  5. #include "base/debug/debugging_buildflags.h"
  6. #include "base/trace_event/base_tracing.h"
  7. #include "testing/gmock/include/gmock/gmock.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. #if BUILDFLAG(ENABLE_BASE_TRACING)
  10. #include "third_party/perfetto/include/perfetto/test/traced_value_test_support.h" // no-presubmit-check
  11. #endif // BUILDFLAG(ENABLE_BASE_TRACING)
  12. namespace base {
  13. namespace {
  14. // This is a typical use: taking Location::Current as a default parameter.
  15. // So even though this looks contrived, it confirms that such usage works as
  16. // expected.
  17. Location WhereAmI(const Location& location = Location::Current()) {
  18. return location;
  19. }
  20. } // namespace
  21. TEST(LocationTest, CurrentYieldsCorrectValue) {
  22. [[maybe_unused]] int previous_line = __LINE__;
  23. Location here = WhereAmI();
  24. EXPECT_NE(here.program_counter(), WhereAmI().program_counter());
  25. #if SUPPORTS_LOCATION_BUILTINS
  26. EXPECT_THAT(here.file_name(), ::testing::EndsWith("location_unittest.cc"));
  27. #if BUILDFLAG(ENABLE_LOCATION_SOURCE)
  28. EXPECT_EQ(here.line_number(), previous_line + 1);
  29. EXPECT_STREQ("TestBody", here.function_name());
  30. #endif
  31. #elif defined(OFFICIAL_BUILD)
  32. #error Location builtins must be supported in official builds.
  33. #elif BUILDFLAG(FROM_HERE_USES_LOCATION_BUILTINS)
  34. #error FROM_HERE requires location builtins to be supported.
  35. #endif
  36. }
  37. #if BUILDFLAG(ENABLE_BASE_TRACING)
  38. TEST(LocationTest, TracingSupport) {
  39. EXPECT_EQ(perfetto::TracedValueToString(
  40. Location("func", "file", 42, WhereAmI().program_counter())),
  41. "{function_name:func,file_name:file,line_number:42}");
  42. }
  43. #endif // BUILDFLAG(ENABLE_BASE_TRACING)
  44. } // namespace base