comparison osx/include/kj/test.h @ 134:41e769c91eca

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