cannam@148
|
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
|
cannam@148
|
2 // Licensed under the MIT License:
|
cannam@148
|
3 //
|
cannam@148
|
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
cannam@148
|
5 // of this software and associated documentation files (the "Software"), to deal
|
cannam@148
|
6 // in the Software without restriction, including without limitation the rights
|
cannam@148
|
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
cannam@148
|
8 // copies of the Software, and to permit persons to whom the Software is
|
cannam@148
|
9 // furnished to do so, subject to the following conditions:
|
cannam@148
|
10 //
|
cannam@148
|
11 // The above copyright notice and this permission notice shall be included in
|
cannam@148
|
12 // all copies or substantial portions of the Software.
|
cannam@148
|
13 //
|
cannam@148
|
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
cannam@148
|
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
cannam@148
|
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
cannam@148
|
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
cannam@148
|
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
cannam@148
|
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
cannam@148
|
20 // THE SOFTWARE.
|
cannam@148
|
21
|
cannam@148
|
22 #ifndef KJ_TEST_H_
|
cannam@148
|
23 #define KJ_TEST_H_
|
cannam@148
|
24
|
cannam@148
|
25 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
|
cannam@148
|
26 #pragma GCC system_header
|
cannam@148
|
27 #endif
|
cannam@148
|
28
|
cannam@148
|
29 #include "debug.h"
|
cannam@148
|
30 #include "vector.h"
|
cannam@148
|
31 #include "function.h"
|
cannam@148
|
32
|
cannam@148
|
33 namespace kj {
|
cannam@148
|
34
|
cannam@148
|
35 class TestRunner;
|
cannam@148
|
36
|
cannam@148
|
37 class TestCase {
|
cannam@148
|
38 public:
|
cannam@148
|
39 TestCase(const char* file, uint line, const char* description);
|
cannam@148
|
40 ~TestCase();
|
cannam@148
|
41
|
cannam@148
|
42 virtual void run() = 0;
|
cannam@148
|
43
|
cannam@148
|
44 private:
|
cannam@148
|
45 const char* file;
|
cannam@148
|
46 uint line;
|
cannam@148
|
47 const char* description;
|
cannam@148
|
48 TestCase* next;
|
cannam@148
|
49 TestCase** prev;
|
cannam@148
|
50 bool matchedFilter;
|
cannam@148
|
51
|
cannam@148
|
52 friend class TestRunner;
|
cannam@148
|
53 };
|
cannam@148
|
54
|
cannam@148
|
55 #define KJ_TEST(description) \
|
cannam@148
|
56 /* Make sure the linker fails if tests are not in anonymous namespaces. */ \
|
cannam@148
|
57 extern int KJ_CONCAT(YouMustWrapTestsInAnonymousNamespace, __COUNTER__) KJ_UNUSED; \
|
cannam@148
|
58 class KJ_UNIQUE_NAME(TestCase): public ::kj::TestCase { \
|
cannam@148
|
59 public: \
|
cannam@148
|
60 KJ_UNIQUE_NAME(TestCase)(): ::kj::TestCase(__FILE__, __LINE__, description) {} \
|
cannam@148
|
61 void run() override; \
|
cannam@148
|
62 } KJ_UNIQUE_NAME(testCase); \
|
cannam@148
|
63 void KJ_UNIQUE_NAME(TestCase)::run()
|
cannam@148
|
64
|
cannam@148
|
65 #if _MSC_VER
|
cannam@148
|
66 #define KJ_INDIRECT_EXPAND(m, vargs) m vargs
|
cannam@148
|
67 #define KJ_FAIL_EXPECT(...) \
|
cannam@148
|
68 KJ_INDIRECT_EXPAND(KJ_LOG, (ERROR , __VA_ARGS__));
|
cannam@148
|
69 #define KJ_EXPECT(cond, ...) \
|
cannam@148
|
70 if (cond); else KJ_INDIRECT_EXPAND(KJ_FAIL_EXPECT, ("failed: expected " #cond , __VA_ARGS__))
|
cannam@148
|
71 #else
|
cannam@148
|
72 #define KJ_FAIL_EXPECT(...) \
|
cannam@148
|
73 KJ_LOG(ERROR, ##__VA_ARGS__);
|
cannam@148
|
74 #define KJ_EXPECT(cond, ...) \
|
cannam@148
|
75 if (cond); else KJ_FAIL_EXPECT("failed: expected " #cond, ##__VA_ARGS__)
|
cannam@148
|
76 #endif
|
cannam@148
|
77
|
cannam@148
|
78 #define KJ_EXPECT_THROW_RECOVERABLE(type, code) \
|
cannam@148
|
79 do { \
|
cannam@148
|
80 KJ_IF_MAYBE(e, ::kj::runCatchingExceptions([&]() { code; })) { \
|
cannam@148
|
81 KJ_EXPECT(e->getType() == ::kj::Exception::Type::type, \
|
cannam@148
|
82 "code threw wrong exception type: " #code, e->getType()); \
|
cannam@148
|
83 } else { \
|
cannam@148
|
84 KJ_FAIL_EXPECT("code did not throw: " #code); \
|
cannam@148
|
85 } \
|
cannam@148
|
86 } while (false)
|
cannam@148
|
87
|
cannam@148
|
88 #define KJ_EXPECT_THROW_RECOVERABLE_MESSAGE(message, code) \
|
cannam@148
|
89 do { \
|
cannam@148
|
90 KJ_IF_MAYBE(e, ::kj::runCatchingExceptions([&]() { code; })) { \
|
cannam@148
|
91 KJ_EXPECT(::kj::_::hasSubstring(e->getDescription(), message), \
|
cannam@148
|
92 "exception description didn't contain expected substring", e->getDescription()); \
|
cannam@148
|
93 } else { \
|
cannam@148
|
94 KJ_FAIL_EXPECT("code did not throw: " #code); \
|
cannam@148
|
95 } \
|
cannam@148
|
96 } while (false)
|
cannam@148
|
97
|
cannam@148
|
98 #if KJ_NO_EXCEPTIONS
|
cannam@148
|
99 #define KJ_EXPECT_THROW(type, code) \
|
cannam@148
|
100 do { \
|
cannam@148
|
101 KJ_EXPECT(::kj::_::expectFatalThrow(type, nullptr, [&]() { code; })); \
|
cannam@148
|
102 } while (false)
|
cannam@148
|
103 #define KJ_EXPECT_THROW_MESSAGE(message, code) \
|
cannam@148
|
104 do { \
|
cannam@148
|
105 KJ_EXPECT(::kj::_::expectFatalThrow(nullptr, kj::StringPtr(message), [&]() { code; })); \
|
cannam@148
|
106 } while (false)
|
cannam@148
|
107 #else
|
cannam@148
|
108 #define KJ_EXPECT_THROW KJ_EXPECT_THROW_RECOVERABLE
|
cannam@148
|
109 #define KJ_EXPECT_THROW_MESSAGE KJ_EXPECT_THROW_RECOVERABLE_MESSAGE
|
cannam@148
|
110 #endif
|
cannam@148
|
111
|
cannam@148
|
112 #define KJ_EXPECT_LOG(level, substring) \
|
cannam@148
|
113 ::kj::_::LogExpectation KJ_UNIQUE_NAME(_kjLogExpectation)(::kj::LogSeverity::level, substring)
|
cannam@148
|
114 // Expects that a log message with the given level and substring text will be printed within
|
cannam@148
|
115 // the current scope. This message will not cause the test to fail, even if it is an error.
|
cannam@148
|
116
|
cannam@148
|
117 // =======================================================================================
|
cannam@148
|
118
|
cannam@148
|
119 namespace _ { // private
|
cannam@148
|
120
|
cannam@148
|
121 bool hasSubstring(kj::StringPtr haystack, kj::StringPtr needle);
|
cannam@148
|
122
|
cannam@148
|
123 #if KJ_NO_EXCEPTIONS
|
cannam@148
|
124 bool expectFatalThrow(Maybe<Exception::Type> type, Maybe<StringPtr> message,
|
cannam@148
|
125 Function<void()> code);
|
cannam@148
|
126 // Expects that the given code will throw a fatal exception matching the given type and/or message.
|
cannam@148
|
127 // Since exceptions are disabled, the test will fork() and run in a subprocess. On Windows, where
|
cannam@148
|
128 // fork() is not available, this always returns true.
|
cannam@148
|
129 #endif
|
cannam@148
|
130
|
cannam@148
|
131 class LogExpectation: public ExceptionCallback {
|
cannam@148
|
132 public:
|
cannam@148
|
133 LogExpectation(LogSeverity severity, StringPtr substring);
|
cannam@148
|
134 ~LogExpectation();
|
cannam@148
|
135
|
cannam@148
|
136 void logMessage(LogSeverity severity, const char* file, int line, int contextDepth,
|
cannam@148
|
137 String&& text) override;
|
cannam@148
|
138
|
cannam@148
|
139 private:
|
cannam@148
|
140 LogSeverity severity;
|
cannam@148
|
141 StringPtr substring;
|
cannam@148
|
142 bool seen;
|
cannam@148
|
143 UnwindDetector unwindDetector;
|
cannam@148
|
144 };
|
cannam@148
|
145
|
cannam@148
|
146 class GlobFilter {
|
cannam@148
|
147 // Implements glob filters for the --filter flag.
|
cannam@148
|
148 //
|
cannam@148
|
149 // Exposed in header only for testing.
|
cannam@148
|
150
|
cannam@148
|
151 public:
|
cannam@148
|
152 explicit GlobFilter(const char* pattern);
|
cannam@148
|
153 explicit GlobFilter(ArrayPtr<const char> pattern);
|
cannam@148
|
154
|
cannam@148
|
155 bool matches(StringPtr name);
|
cannam@148
|
156
|
cannam@148
|
157 private:
|
cannam@148
|
158 String pattern;
|
cannam@148
|
159 Vector<uint> states;
|
cannam@148
|
160
|
cannam@148
|
161 void applyState(char c, int state);
|
cannam@148
|
162 };
|
cannam@148
|
163
|
cannam@148
|
164 } // namespace _ (private)
|
cannam@148
|
165 } // namespace kj
|
cannam@148
|
166
|
cannam@148
|
167 #endif // KJ_TEST_H_
|