annotate osx/include/kj/test.h @ 83:ae30d91d2ffe

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