1 // Copyright 2019 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/location.h"
7 #include "base/trace_event/base_tracing.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 #if BUILDFLAG(ENABLE_BASE_TRACING)
12 #include "third_party/perfetto/include/perfetto/test/traced_value_test_support.h" // no-presubmit-check
13 #endif // BUILDFLAG(ENABLE_BASE_TRACING)
19 // This is a typical use: taking Location::Current as a default parameter.
20 // So even though this looks contrived, it confirms that such usage works as
22 Location WhereAmI(const Location& location = Location::Current()) {
28 TEST(LocationTest, CurrentYieldsCorrectValue) {
29 [[maybe_unused]] int previous_line = __LINE__;
30 Location here = WhereAmI();
31 EXPECT_NE(here.program_counter(), WhereAmI().program_counter());
32 EXPECT_THAT(here.file_name(), ::testing::EndsWith("location_unittest.cc"));
33 EXPECT_EQ(here.line_number(), previous_line + 1);
34 EXPECT_STREQ("TestBody", here.function_name());
37 #if BUILDFLAG(ENABLE_BASE_TRACING)
38 TEST(LocationTest, TracingSupport) {
39 EXPECT_EQ(perfetto::TracedValueToString(Location::CreateForTesting(
40 "func", "file", 42, WhereAmI().program_counter())),
41 "{function_name:func,file_name:file,line_number:42}");
43 #endif // BUILDFLAG(ENABLE_BASE_TRACING)