annotate osx/include/kj/test.h @ 134:41e769c91eca

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