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 
5 #include "platform/base/location.h"
6 
7 #include "gmock/gmock.h"
8 #include "gtest/gtest.h"
9 
10 namespace openscreen {
11 using ::testing::MatchesRegex;
12 using ::testing::StartsWith;
13 
TEST(LocationTest,DefaultInitializedLocationIsNullptr)14 TEST(LocationTest, DefaultInitializedLocationIsNullptr) {
15   const Location loc;
16 
17   EXPECT_EQ(nullptr, loc.program_counter());
18   EXPECT_EQ("pc:NULL", loc.ToString());
19 }
20 
TEST(LocationTest,ExpresslyInitializedLocationIsValid)21 TEST(LocationTest, ExpresslyInitializedLocationIsValid) {
22   const void* void_ptr = reinterpret_cast<void*>(0x1337);
23   const Location loc(void_ptr);
24   EXPECT_EQ(void_ptr, loc.program_counter());
25   EXPECT_EQ("pc:0x1337", loc.ToString());
26 }
27 
TEST(LocationTest,LocationFromHereIsValid)28 TEST(LocationTest, LocationFromHereIsValid) {
29   const Location loc_from_here = openscreen::Location::CreateFromHere();
30   const Location loc_from_here_macro = CURRENT_LOCATION;
31 
32   EXPECT_NE(nullptr, loc_from_here.program_counter());
33   EXPECT_NE(nullptr, loc_from_here_macro.program_counter());
34 
35 // Some platforms have only limited Regex support, so we cannot have as
36 // thorough a test on those platforms.
37 #if GTEST_USES_POSIX_RE
38   EXPECT_THAT(loc_from_here.ToString(), MatchesRegex("pc:0x[0-9a-f]+"));
39   EXPECT_THAT(loc_from_here_macro.ToString(), MatchesRegex("pc:0x[0-9a-f]+"));
40 #else  // GTEST_USES_SIMPLE_RE = 1
41   EXPECT_THAT(loc_from_here.ToString(), StartsWith("pc:0x"));
42   EXPECT_THAT(loc_from_here_macro.ToString(), StartsWith("pc:0x"));
43 #endif
44 }
45 }  // namespace openscreen
46