annotate win32-mingw/include/kj/test.h @ 135:38d1c0e7850b

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