annotate win64-msvc/include/kj/test.h @ 64:eccd51b72864

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