annotate win32-mingw/include/kj/test.h @ 69:7aeed7906520

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