annotate win64-msvc/include/kj/test.h @ 62:0994c39f1e94

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