• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 #include "google/protobuf/reflection_mode.h"
8 
9 #include <gmock/gmock.h>
10 #include <gtest/gtest.h>
11 
12 namespace google {
13 namespace protobuf {
14 namespace internal {
15 
16 #ifndef PROTOBUF_NO_THREADLOCAL
17 
TEST(ReflectionModeTest,SimpleScopedReflection)18 TEST(ReflectionModeTest, SimpleScopedReflection) {
19   ASSERT_EQ(ScopedReflectionMode::current_reflection_mode(),
20             ReflectionMode::kDefault);
21   ScopedReflectionMode scope(ReflectionMode::kDiagnostics);
22   EXPECT_EQ(ScopedReflectionMode::current_reflection_mode(),
23             ReflectionMode::kDiagnostics);
24 }
25 
TEST(ReflectionModeTest,CleanNestedScopedReflection)26 TEST(ReflectionModeTest, CleanNestedScopedReflection) {
27   ASSERT_EQ(ScopedReflectionMode::current_reflection_mode(),
28             ReflectionMode::kDefault);
29   {
30     ScopedReflectionMode scope1(ReflectionMode::kDebugString);
31     EXPECT_EQ(ScopedReflectionMode::current_reflection_mode(),
32               ReflectionMode::kDebugString);
33     {
34       ScopedReflectionMode scope2(ReflectionMode::kDiagnostics);
35       EXPECT_EQ(ScopedReflectionMode::current_reflection_mode(),
36                 ReflectionMode::kDiagnostics);
37     }
38     EXPECT_EQ(ScopedReflectionMode::current_reflection_mode(),
39               ReflectionMode::kDebugString);
40   }
41   EXPECT_EQ(ScopedReflectionMode::current_reflection_mode(),
42             ReflectionMode::kDefault);
43 }
44 
TEST(ReflectionModeTest,UglyNestedScopedReflection)45 TEST(ReflectionModeTest, UglyNestedScopedReflection) {
46   ASSERT_EQ(ScopedReflectionMode::current_reflection_mode(),
47             ReflectionMode::kDefault);
48   ScopedReflectionMode scope1(ReflectionMode::kDebugString);
49   EXPECT_EQ(ScopedReflectionMode::current_reflection_mode(),
50             ReflectionMode::kDebugString);
51   ScopedReflectionMode scope2(ReflectionMode::kDiagnostics);
52   EXPECT_EQ(ScopedReflectionMode::current_reflection_mode(),
53             ReflectionMode::kDiagnostics);
54 }
55 
TEST(ReflectionModeTest,DebugStringModeDoesNotReplaceDiagnosticsMode)56 TEST(ReflectionModeTest, DebugStringModeDoesNotReplaceDiagnosticsMode) {
57   ASSERT_EQ(ScopedReflectionMode::current_reflection_mode(),
58             ReflectionMode::kDefault);
59   ScopedReflectionMode scope1(ReflectionMode::kDiagnostics);
60   {
61     ScopedReflectionMode scope2(ReflectionMode::kDebugString);
62     EXPECT_EQ(ScopedReflectionMode::current_reflection_mode(),
63               ReflectionMode::kDiagnostics);
64   }
65 }
66 
67 #else
68 
69 TEST(ReflectionModeTest, AlwaysReturnDefaultWhenNoThreadLocal) {
70   ASSERT_EQ(ScopedReflectionMode::current_reflection_mode(),
71             ReflectionMode::kDefault);
72   {
73     ScopedReflectionMode scope1(ReflectionMode::kDebugString);
74     EXPECT_EQ(ScopedReflectionMode::current_reflection_mode(),
75               ReflectionMode::kDefault);
76     {
77       ScopedReflectionMode scope2(ReflectionMode::kDiagnostics);
78       EXPECT_EQ(ScopedReflectionMode::current_reflection_mode(),
79                 ReflectionMode::kDefault);
80     }
81     EXPECT_EQ(ScopedReflectionMode::current_reflection_mode(),
82               ReflectionMode::kDefault);
83   }
84   EXPECT_EQ(ScopedReflectionMode::current_reflection_mode(),
85             ReflectionMode::kDefault);
86 }
87 
88 #endif
89 
90 }  // namespace internal
91 }  // namespace protobuf
92 }  // namespace google
93