annotate osx/include/kj/test.h @ 169:223a55898ab9 tip default

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