• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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 #ifndef UTIL_TEST_TEST_H_
6 #define UTIL_TEST_TEST_H_
7 
8 #include <string.h>
9 
10 #include <sstream>
11 #include <string>
12 
13 // This is a minimal googletest-like testing framework. It's originally derived
14 // from Ninja's src/test.h. You might prefer that one if you have different
15 // tradeoffs (in particular, if you don't need to stream message to assertion
16 // failures, Ninja's is a bit simpler.)
17 namespace testing {
18 
19 class Test {
20  public:
Test()21   Test() : failed_(false) {}
~Test()22   virtual ~Test() {}
SetUp()23   virtual void SetUp() {}
TearDown()24   virtual void TearDown() {}
25   virtual void Run() = 0;
26 
Failed()27   bool Failed() const { return failed_; }
28 
29  private:
30   friend class TestResult;
31 
32   bool failed_;
33 };
34 
35 extern testing::Test* g_current_test;
36 
37 class TestResult {
38  public:
TestResult(bool condition,const char * error)39   TestResult(bool condition, const char* error)
40       : condition_(condition), error_(error) {
41     if (!condition)
42       g_current_test->failed_ = true;
43   }
44 
45   operator bool() const { return condition_; }
error()46   const char* error() const { return error_; }
47 
48  private:
49   bool condition_;
50   const char* error_;
51 };
52 
53 class Message {
54  public:
Message()55   Message() {}
~Message()56   ~Message() { printf("%s\n\n", ss_.str().c_str()); }
57 
58   template <typename T>
59   inline Message& operator<<(const T& val) {
60     ss_ << val;
61     return *this;
62   }
63 
64  private:
65   std::stringstream ss_;
66 };
67 
68 class AssertHelper {
69  public:
AssertHelper(const char * file,int line,const TestResult & test_result)70   AssertHelper(const char* file, int line, const TestResult& test_result)
71       : file_(file), line_(line), error_(test_result.error()) {}
72 
73   void operator=(const Message& message) const {
74     printf("\n*** FAILURE %s:%d: %s\n", file_, line_, error_);
75   }
76 
77  private:
78   const char* file_;
79   int line_;
80   const char* error_;
81 };
82 
83 }  // namespace testing
84 
85 void RegisterTest(testing::Test* (*)(), const char*);
86 
87 #define TEST_F_(x, y, name)                                                    \
88   struct y : public x {                                                        \
89     static testing::Test* Create() { return testing::g_current_test = new y; } \
90     virtual void Run();                                                        \
91   };                                                                           \
92   struct Register##y {                                                         \
93     Register##y() { RegisterTest(y::Create, name); }                           \
94   };                                                                           \
95   Register##y g_register_##y;                                                  \
96   void y::Run()
97 
98 #define TEST_F(x, y) TEST_F_(x, x##y, #x "." #y)
99 #define TEST(x, y) TEST_F_(testing::Test, x##y, #x "." #y)
100 
101 #define FRIEND_TEST(x, y) friend class x##y
102 
103 // Some compilers emit a warning if nested "if" statements are followed by an
104 // "else" statement and braces are not used to explicitly disambiguate the
105 // "else" binding.  This leads to problems with code like:
106 //
107 //   if (something)
108 //     ASSERT_TRUE(condition) << "Some message";
109 #define TEST_AMBIGUOUS_ELSE_BLOCKER_ \
110   switch (0)                         \
111   case 0:                            \
112   default:
113 
114 #define TEST_ASSERT_(expression, on_failure)                  \
115   TEST_AMBIGUOUS_ELSE_BLOCKER_                                \
116   if (const ::testing::TestResult test_result = (expression)) \
117     ;                                                         \
118   else                                                        \
119     on_failure(test_result)
120 
121 #define TEST_NONFATAL_FAILURE_(message) \
122   ::testing::AssertHelper(__FILE__, __LINE__, message) = ::testing::Message()
123 
124 #define TEST_FATAL_FAILURE_(message)                            \
125   return ::testing::AssertHelper(__FILE__, __LINE__, message) = \
126              ::testing::Message()
127 
128 #define EXPECT_EQ(a, b)                                     \
129   TEST_ASSERT_(::testing::TestResult(a == b, #a " == " #b), \
130                TEST_NONFATAL_FAILURE_)
131 
132 #define EXPECT_NE(a, b)                                     \
133   TEST_ASSERT_(::testing::TestResult(a != b, #a " != " #b), \
134                TEST_NONFATAL_FAILURE_)
135 
136 #define EXPECT_LT(a, b)                                   \
137   TEST_ASSERT_(::testing::TestResult(a < b, #a " < " #b), \
138                TEST_NONFATAL_FAILURE_)
139 
140 #define EXPECT_GT(a, b)                                   \
141   TEST_ASSERT_(::testing::TestResult(a > b, #a " > " #b), \
142                TEST_NONFATAL_FAILURE_)
143 
144 #define EXPECT_LE(a, b)                                     \
145   TEST_ASSERT_(::testing::TestResult(a <= b, #a " <= " #b), \
146                TEST_NONFATAL_FAILURE_)
147 
148 #define EXPECT_GE(a, b)                                     \
149   TEST_ASSERT_(::testing::TestResult(a >= b, #a " >= " #b), \
150                TEST_NONFATAL_FAILURE_)
151 
152 #define EXPECT_TRUE(a)                                          \
153   TEST_ASSERT_(::testing::TestResult(static_cast<bool>(a), #a), \
154                TEST_NONFATAL_FAILURE_)
155 
156 #define EXPECT_FALSE(a)                                          \
157   TEST_ASSERT_(::testing::TestResult(!static_cast<bool>(a), #a), \
158                TEST_NONFATAL_FAILURE_)
159 
160 #define EXPECT_STREQ(a, b)                                                \
161   TEST_ASSERT_(::testing::TestResult(strcmp(a, b) == 0, #a " str== " #b), \
162                TEST_NONFATAL_FAILURE_)
163 
164 #define ASSERT_EQ(a, b) \
165   TEST_ASSERT_(::testing::TestResult(a == b, #a " == " #b), TEST_FATAL_FAILURE_)
166 
167 #define ASSERT_NE(a, b) \
168   TEST_ASSERT_(::testing::TestResult(a != b, #a " != " #b), TEST_FATAL_FAILURE_)
169 
170 #define ASSERT_LT(a, b) \
171   TEST_ASSERT_(::testing::TestResult(a < b, #a " < " #b), TEST_FATAL_FAILURE_)
172 
173 #define ASSERT_GT(a, b) \
174   TEST_ASSERT_(::testing::TestResult(a > b, #a " > " #b), TEST_FATAL_FAILURE_)
175 
176 #define ASSERT_LE(a, b) \
177   TEST_ASSERT_(::testing::TestResult(a <= b, #a " <= " #b), TEST_FATAL_FAILURE_)
178 
179 #define ASSERT_GE(a, b) \
180   TEST_ASSERT_(::testing::TestResult(a >= b, #a " >= " #b), TEST_FATAL_FAILURE_)
181 
182 #define ASSERT_TRUE(a)                                          \
183   TEST_ASSERT_(::testing::TestResult(static_cast<bool>(a), #a), \
184                TEST_FATAL_FAILURE_)
185 
186 #define ASSERT_FALSE(a)                                          \
187   TEST_ASSERT_(::testing::TestResult(!static_cast<bool>(a), #a), \
188                TEST_FATAL_FAILURE_)
189 
190 #define ASSERT_STREQ(a, b)                                                \
191   TEST_ASSERT_(::testing::TestResult(strcmp(a, b) == 0, #a " str== " #b), \
192                TEST_FATAL_FAILURE_)
193 
194 #endif  // UTIL_TEST_TEST_H_
195