annotate osx/include/kj/test.h @ 49:3ab5a40c4e3b

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