jpayne@69
|
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
|
jpayne@69
|
2 // Licensed under the MIT License:
|
jpayne@69
|
3 //
|
jpayne@69
|
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
jpayne@69
|
5 // of this software and associated documentation files (the "Software"), to deal
|
jpayne@69
|
6 // in the Software without restriction, including without limitation the rights
|
jpayne@69
|
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
jpayne@69
|
8 // copies of the Software, and to permit persons to whom the Software is
|
jpayne@69
|
9 // furnished to do so, subject to the following conditions:
|
jpayne@69
|
10 //
|
jpayne@69
|
11 // The above copyright notice and this permission notice shall be included in
|
jpayne@69
|
12 // all copies or substantial portions of the Software.
|
jpayne@69
|
13 //
|
jpayne@69
|
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
jpayne@69
|
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
jpayne@69
|
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
jpayne@69
|
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
jpayne@69
|
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
jpayne@69
|
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
jpayne@69
|
20 // THE SOFTWARE.
|
jpayne@69
|
21
|
jpayne@69
|
22 #pragma once
|
jpayne@69
|
23
|
jpayne@69
|
24 #include "debug.h"
|
jpayne@69
|
25 #include "vector.h"
|
jpayne@69
|
26 #include "function.h"
|
jpayne@69
|
27 #include "windows-sanity.h" // work-around macro conflict with `ERROR`
|
jpayne@69
|
28
|
jpayne@69
|
29 KJ_BEGIN_HEADER
|
jpayne@69
|
30
|
jpayne@69
|
31 namespace kj {
|
jpayne@69
|
32
|
jpayne@69
|
33 class TestRunner;
|
jpayne@69
|
34
|
jpayne@69
|
35 class TestCase {
|
jpayne@69
|
36 public:
|
jpayne@69
|
37 TestCase(const char* file, uint line, const char* description);
|
jpayne@69
|
38 ~TestCase();
|
jpayne@69
|
39
|
jpayne@69
|
40 virtual void run() = 0;
|
jpayne@69
|
41
|
jpayne@69
|
42 protected:
|
jpayne@69
|
43 template <typename Func>
|
jpayne@69
|
44 void doBenchmark(Func&& func) {
|
jpayne@69
|
45 // Perform a benchmark with configurable iterations. func() will be called N times, where N
|
jpayne@69
|
46 // is set by the --benchmark CLI flag. This defaults to 1, so that when --benchmark is not
|
jpayne@69
|
47 // specified, we only test that the benchmark works.
|
jpayne@69
|
48 //
|
jpayne@69
|
49 // In the future, this could adaptively choose iteration count by running a few iterations to
|
jpayne@69
|
50 // find out how fast the benchmark is, then scaling.
|
jpayne@69
|
51
|
jpayne@69
|
52 for (size_t i = iterCount(); i-- > 0;) {
|
jpayne@69
|
53 func();
|
jpayne@69
|
54 }
|
jpayne@69
|
55 }
|
jpayne@69
|
56
|
jpayne@69
|
57 private:
|
jpayne@69
|
58 const char* file;
|
jpayne@69
|
59 uint line;
|
jpayne@69
|
60 const char* description;
|
jpayne@69
|
61 TestCase* next;
|
jpayne@69
|
62 TestCase** prev;
|
jpayne@69
|
63 bool matchedFilter;
|
jpayne@69
|
64
|
jpayne@69
|
65 static size_t iterCount();
|
jpayne@69
|
66
|
jpayne@69
|
67 friend class TestRunner;
|
jpayne@69
|
68 };
|
jpayne@69
|
69
|
jpayne@69
|
70 #define KJ_TEST(description) \
|
jpayne@69
|
71 /* Make sure the linker fails if tests are not in anonymous namespaces. */ \
|
jpayne@69
|
72 extern int KJ_CONCAT(YouMustWrapTestsInAnonymousNamespace, __COUNTER__) KJ_UNUSED; \
|
jpayne@69
|
73 class KJ_UNIQUE_NAME(TestCase): public ::kj::TestCase { \
|
jpayne@69
|
74 public: \
|
jpayne@69
|
75 KJ_UNIQUE_NAME(TestCase)(): ::kj::TestCase(__FILE__, __LINE__, description) {} \
|
jpayne@69
|
76 void run() override; \
|
jpayne@69
|
77 } KJ_UNIQUE_NAME(testCase); \
|
jpayne@69
|
78 void KJ_UNIQUE_NAME(TestCase)::run()
|
jpayne@69
|
79
|
jpayne@69
|
80 #if KJ_MSVC_TRADITIONAL_CPP
|
jpayne@69
|
81 #define KJ_INDIRECT_EXPAND(m, vargs) m vargs
|
jpayne@69
|
82 #define KJ_FAIL_EXPECT(...) \
|
jpayne@69
|
83 KJ_INDIRECT_EXPAND(KJ_LOG, (ERROR , __VA_ARGS__));
|
jpayne@69
|
84 #define KJ_EXPECT(cond, ...) \
|
jpayne@69
|
85 if (auto _kjCondition = ::kj::_::MAGIC_ASSERT << cond); \
|
jpayne@69
|
86 else KJ_INDIRECT_EXPAND(KJ_FAIL_EXPECT, ("failed: expected " #cond , _kjCondition, __VA_ARGS__))
|
jpayne@69
|
87 #else
|
jpayne@69
|
88 #define KJ_FAIL_EXPECT(...) \
|
jpayne@69
|
89 KJ_LOG(ERROR, ##__VA_ARGS__);
|
jpayne@69
|
90 #define KJ_EXPECT(cond, ...) \
|
jpayne@69
|
91 if (auto _kjCondition = ::kj::_::MAGIC_ASSERT << cond); \
|
jpayne@69
|
92 else KJ_FAIL_EXPECT("failed: expected " #cond, _kjCondition, ##__VA_ARGS__)
|
jpayne@69
|
93 #endif
|
jpayne@69
|
94
|
jpayne@69
|
95 #if _MSC_VER && !defined(__clang__)
|
jpayne@69
|
96 #define KJ_EXPECT_THROW_RECOVERABLE(type, code, ...) \
|
jpayne@69
|
97 do { \
|
jpayne@69
|
98 KJ_IF_MAYBE(e, ::kj::runCatchingExceptions([&]() { code; })) { \
|
jpayne@69
|
99 KJ_INDIRECT_EXPAND(KJ_EXPECT, (e->getType() == ::kj::Exception::Type::type, \
|
jpayne@69
|
100 "code threw wrong exception type: " #code, *e, __VA_ARGS__)); \
|
jpayne@69
|
101 } else { \
|
jpayne@69
|
102 KJ_INDIRECT_EXPAND(KJ_FAIL_EXPECT, ("code did not throw: " #code, __VA_ARGS__)); \
|
jpayne@69
|
103 } \
|
jpayne@69
|
104 } while (false)
|
jpayne@69
|
105
|
jpayne@69
|
106 #define KJ_EXPECT_THROW_RECOVERABLE_MESSAGE(message, code, ...) \
|
jpayne@69
|
107 do { \
|
jpayne@69
|
108 KJ_IF_MAYBE(e, ::kj::runCatchingExceptions([&]() { code; })) { \
|
jpayne@69
|
109 KJ_INDIRECT_EXPAND(KJ_EXPECT, (::kj::_::hasSubstring(e->getDescription(), message), \
|
jpayne@69
|
110 "exception description didn't contain expected substring", *e, __VA_ARGS__)); \
|
jpayne@69
|
111 } else { \
|
jpayne@69
|
112 KJ_INDIRECT_EXPAND(KJ_FAIL_EXPECT, ("code did not throw: " #code, __VA_ARGS__)); \
|
jpayne@69
|
113 } \
|
jpayne@69
|
114 } while (false)
|
jpayne@69
|
115 #else
|
jpayne@69
|
116 #define KJ_EXPECT_THROW_RECOVERABLE(type, code, ...) \
|
jpayne@69
|
117 do { \
|
jpayne@69
|
118 KJ_IF_MAYBE(e, ::kj::runCatchingExceptions([&]() { code; })) { \
|
jpayne@69
|
119 KJ_EXPECT(e->getType() == ::kj::Exception::Type::type, \
|
jpayne@69
|
120 "code threw wrong exception type: " #code, *e, ##__VA_ARGS__); \
|
jpayne@69
|
121 } else { \
|
jpayne@69
|
122 KJ_FAIL_EXPECT("code did not throw: " #code, ##__VA_ARGS__); \
|
jpayne@69
|
123 } \
|
jpayne@69
|
124 } while (false)
|
jpayne@69
|
125
|
jpayne@69
|
126 #define KJ_EXPECT_THROW_RECOVERABLE_MESSAGE(message, code, ...) \
|
jpayne@69
|
127 do { \
|
jpayne@69
|
128 KJ_IF_MAYBE(e, ::kj::runCatchingExceptions([&]() { code; })) { \
|
jpayne@69
|
129 KJ_EXPECT(::kj::_::hasSubstring(e->getDescription(), message), \
|
jpayne@69
|
130 "exception description didn't contain expected substring", *e, ##__VA_ARGS__); \
|
jpayne@69
|
131 } else { \
|
jpayne@69
|
132 KJ_FAIL_EXPECT("code did not throw: " #code, ##__VA_ARGS__); \
|
jpayne@69
|
133 } \
|
jpayne@69
|
134 } while (false)
|
jpayne@69
|
135 #endif
|
jpayne@69
|
136
|
jpayne@69
|
137 #if KJ_NO_EXCEPTIONS
|
jpayne@69
|
138 #define KJ_EXPECT_THROW(type, code, ...) \
|
jpayne@69
|
139 do { \
|
jpayne@69
|
140 KJ_EXPECT(::kj::_::expectFatalThrow(::kj::Exception::Type::type, nullptr, [&]() { code; })); \
|
jpayne@69
|
141 } while (false)
|
jpayne@69
|
142 #define KJ_EXPECT_THROW_MESSAGE(message, code, ...) \
|
jpayne@69
|
143 do { \
|
jpayne@69
|
144 KJ_EXPECT(::kj::_::expectFatalThrow(nullptr, kj::StringPtr(message), [&]() { code; })); \
|
jpayne@69
|
145 } while (false)
|
jpayne@69
|
146 #else
|
jpayne@69
|
147 #define KJ_EXPECT_THROW KJ_EXPECT_THROW_RECOVERABLE
|
jpayne@69
|
148 #define KJ_EXPECT_THROW_MESSAGE KJ_EXPECT_THROW_RECOVERABLE_MESSAGE
|
jpayne@69
|
149 #endif
|
jpayne@69
|
150
|
jpayne@69
|
151 #define KJ_EXPECT_EXIT(statusCode, code) \
|
jpayne@69
|
152 do { \
|
jpayne@69
|
153 KJ_EXPECT(::kj::_::expectExit(statusCode, [&]() { code; })); \
|
jpayne@69
|
154 } while (false)
|
jpayne@69
|
155 // Forks the code and expects it to exit with a given code.
|
jpayne@69
|
156
|
jpayne@69
|
157 #define KJ_EXPECT_SIGNAL(signal, code) \
|
jpayne@69
|
158 do { \
|
jpayne@69
|
159 KJ_EXPECT(::kj::_::expectSignal(signal, [&]() { code; })); \
|
jpayne@69
|
160 } while (false)
|
jpayne@69
|
161 // Forks the code and expects it to trigger a signal.
|
jpayne@69
|
162 // In the child resets all signal handlers as printStackTraceOnCrash sets.
|
jpayne@69
|
163
|
jpayne@69
|
164 #define KJ_EXPECT_LOG(level, substring) \
|
jpayne@69
|
165 ::kj::_::LogExpectation KJ_UNIQUE_NAME(_kjLogExpectation)(::kj::LogSeverity::level, substring)
|
jpayne@69
|
166 // Expects that a log message with the given level and substring text will be printed within
|
jpayne@69
|
167 // the current scope. This message will not cause the test to fail, even if it is an error.
|
jpayne@69
|
168
|
jpayne@69
|
169 // =======================================================================================
|
jpayne@69
|
170
|
jpayne@69
|
171 namespace _ { // private
|
jpayne@69
|
172
|
jpayne@69
|
173 bool hasSubstring(kj::StringPtr haystack, kj::StringPtr needle);
|
jpayne@69
|
174
|
jpayne@69
|
175 #if KJ_NO_EXCEPTIONS
|
jpayne@69
|
176 bool expectFatalThrow(Maybe<Exception::Type> type, Maybe<StringPtr> message,
|
jpayne@69
|
177 Function<void()> code);
|
jpayne@69
|
178 // Expects that the given code will throw a fatal exception matching the given type and/or message.
|
jpayne@69
|
179 // Since exceptions are disabled, the test will fork() and run in a subprocess. On Windows, where
|
jpayne@69
|
180 // fork() is not available, this always returns true.
|
jpayne@69
|
181 #endif
|
jpayne@69
|
182
|
jpayne@69
|
183 bool expectExit(Maybe<int> statusCode, FunctionParam<void()> code) noexcept;
|
jpayne@69
|
184 // Expects that the given code will exit with a given statusCode.
|
jpayne@69
|
185 // The test will fork() and run in a subprocess. On Windows, where fork() is not available,
|
jpayne@69
|
186 // this always returns true.
|
jpayne@69
|
187
|
jpayne@69
|
188 bool expectSignal(Maybe<int> signal, FunctionParam<void()> code) noexcept;
|
jpayne@69
|
189 // Expects that the given code will trigger a signal.
|
jpayne@69
|
190 // The test will fork() and run in a subprocess. On Windows, where fork() is not available,
|
jpayne@69
|
191 // this always returns true.
|
jpayne@69
|
192 // Resets signal handlers to default prior to running the code in the child process.
|
jpayne@69
|
193
|
jpayne@69
|
194 class LogExpectation: public ExceptionCallback {
|
jpayne@69
|
195 public:
|
jpayne@69
|
196 LogExpectation(LogSeverity severity, StringPtr substring);
|
jpayne@69
|
197 ~LogExpectation();
|
jpayne@69
|
198
|
jpayne@69
|
199 void logMessage(LogSeverity severity, const char* file, int line, int contextDepth,
|
jpayne@69
|
200 String&& text) override;
|
jpayne@69
|
201
|
jpayne@69
|
202 private:
|
jpayne@69
|
203 LogSeverity severity;
|
jpayne@69
|
204 StringPtr substring;
|
jpayne@69
|
205 bool seen;
|
jpayne@69
|
206 UnwindDetector unwindDetector;
|
jpayne@69
|
207 };
|
jpayne@69
|
208
|
jpayne@69
|
209 class GlobFilter {
|
jpayne@69
|
210 // Implements glob filters for the --filter flag.
|
jpayne@69
|
211 //
|
jpayne@69
|
212 // Exposed in header only for testing.
|
jpayne@69
|
213
|
jpayne@69
|
214 public:
|
jpayne@69
|
215 explicit GlobFilter(const char* pattern);
|
jpayne@69
|
216 explicit GlobFilter(ArrayPtr<const char> pattern);
|
jpayne@69
|
217
|
jpayne@69
|
218 bool matches(StringPtr name);
|
jpayne@69
|
219
|
jpayne@69
|
220 private:
|
jpayne@69
|
221 String pattern;
|
jpayne@69
|
222 Vector<uint> states;
|
jpayne@69
|
223
|
jpayne@69
|
224 void applyState(char c, int state);
|
jpayne@69
|
225 };
|
jpayne@69
|
226
|
jpayne@69
|
227 } // namespace _ (private)
|
jpayne@69
|
228 } // namespace kj
|
jpayne@69
|
229
|
jpayne@69
|
230 KJ_END_HEADER
|