cannam@62: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors cannam@62: // Licensed under the MIT License: cannam@62: // cannam@62: // Permission is hereby granted, free of charge, to any person obtaining a copy cannam@62: // of this software and associated documentation files (the "Software"), to deal cannam@62: // in the Software without restriction, including without limitation the rights cannam@62: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cannam@62: // copies of the Software, and to permit persons to whom the Software is cannam@62: // furnished to do so, subject to the following conditions: cannam@62: // cannam@62: // The above copyright notice and this permission notice shall be included in cannam@62: // all copies or substantial portions of the Software. cannam@62: // cannam@62: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR cannam@62: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, cannam@62: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE cannam@62: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER cannam@62: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, cannam@62: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN cannam@62: // THE SOFTWARE. cannam@62: cannam@62: #ifndef KJ_TEST_H_ cannam@62: #define KJ_TEST_H_ cannam@62: cannam@62: #if defined(__GNUC__) && !KJ_HEADER_WARNINGS cannam@62: #pragma GCC system_header cannam@62: #endif cannam@62: cannam@62: #include "debug.h" cannam@62: #include "vector.h" cannam@62: #include "function.h" cannam@62: cannam@62: namespace kj { cannam@62: cannam@62: class TestRunner; cannam@62: cannam@62: class TestCase { cannam@62: public: cannam@62: TestCase(const char* file, uint line, const char* description); cannam@62: ~TestCase(); cannam@62: cannam@62: virtual void run() = 0; cannam@62: cannam@62: private: cannam@62: const char* file; cannam@62: uint line; cannam@62: const char* description; cannam@62: TestCase* next; cannam@62: TestCase** prev; cannam@62: bool matchedFilter; cannam@62: cannam@62: friend class TestRunner; cannam@62: }; cannam@62: cannam@62: #define KJ_TEST(description) \ cannam@62: /* Make sure the linker fails if tests are not in anonymous namespaces. */ \ cannam@62: extern int KJ_CONCAT(YouMustWrapTestsInAnonymousNamespace, __COUNTER__) KJ_UNUSED; \ cannam@62: class KJ_UNIQUE_NAME(TestCase): public ::kj::TestCase { \ cannam@62: public: \ cannam@62: KJ_UNIQUE_NAME(TestCase)(): ::kj::TestCase(__FILE__, __LINE__, description) {} \ cannam@62: void run() override; \ cannam@62: } KJ_UNIQUE_NAME(testCase); \ cannam@62: void KJ_UNIQUE_NAME(TestCase)::run() cannam@62: cannam@62: #if _MSC_VER cannam@62: #define KJ_INDIRECT_EXPAND(m, vargs) m vargs cannam@62: #define KJ_FAIL_EXPECT(...) \ cannam@62: KJ_INDIRECT_EXPAND(KJ_LOG, (ERROR , __VA_ARGS__)); cannam@62: #define KJ_EXPECT(cond, ...) \ cannam@62: if (cond); else KJ_INDIRECT_EXPAND(KJ_FAIL_EXPECT, ("failed: expected " #cond , __VA_ARGS__)) cannam@62: #else cannam@62: #define KJ_FAIL_EXPECT(...) \ cannam@62: KJ_LOG(ERROR, ##__VA_ARGS__); cannam@62: #define KJ_EXPECT(cond, ...) \ cannam@62: if (cond); else KJ_FAIL_EXPECT("failed: expected " #cond, ##__VA_ARGS__) cannam@62: #endif cannam@62: cannam@62: #define KJ_EXPECT_THROW_RECOVERABLE(type, code) \ cannam@62: do { \ cannam@62: KJ_IF_MAYBE(e, ::kj::runCatchingExceptions([&]() { code; })) { \ cannam@62: KJ_EXPECT(e->getType() == ::kj::Exception::Type::type, \ cannam@62: "code threw wrong exception type: " #code, e->getType()); \ cannam@62: } else { \ cannam@62: KJ_FAIL_EXPECT("code did not throw: " #code); \ cannam@62: } \ cannam@62: } while (false) cannam@62: cannam@62: #define KJ_EXPECT_THROW_RECOVERABLE_MESSAGE(message, code) \ cannam@62: do { \ cannam@62: KJ_IF_MAYBE(e, ::kj::runCatchingExceptions([&]() { code; })) { \ cannam@62: KJ_EXPECT(::kj::_::hasSubstring(e->getDescription(), message), \ cannam@62: "exception description didn't contain expected substring", e->getDescription()); \ cannam@62: } else { \ cannam@62: KJ_FAIL_EXPECT("code did not throw: " #code); \ cannam@62: } \ cannam@62: } while (false) cannam@62: cannam@62: #if KJ_NO_EXCEPTIONS cannam@62: #define KJ_EXPECT_THROW(type, code) \ cannam@62: do { \ cannam@62: KJ_EXPECT(::kj::_::expectFatalThrow(type, nullptr, [&]() { code; })); \ cannam@62: } while (false) cannam@62: #define KJ_EXPECT_THROW_MESSAGE(message, code) \ cannam@62: do { \ cannam@62: KJ_EXPECT(::kj::_::expectFatalThrow(nullptr, kj::StringPtr(message), [&]() { code; })); \ cannam@62: } while (false) cannam@62: #else cannam@62: #define KJ_EXPECT_THROW KJ_EXPECT_THROW_RECOVERABLE cannam@62: #define KJ_EXPECT_THROW_MESSAGE KJ_EXPECT_THROW_RECOVERABLE_MESSAGE cannam@62: #endif cannam@62: cannam@62: #define KJ_EXPECT_LOG(level, substring) \ cannam@62: ::kj::_::LogExpectation KJ_UNIQUE_NAME(_kjLogExpectation)(::kj::LogSeverity::level, substring) cannam@62: // Expects that a log message with the given level and substring text will be printed within cannam@62: // the current scope. This message will not cause the test to fail, even if it is an error. cannam@62: cannam@62: // ======================================================================================= cannam@62: cannam@62: namespace _ { // private cannam@62: cannam@62: bool hasSubstring(kj::StringPtr haystack, kj::StringPtr needle); cannam@62: cannam@62: #if KJ_NO_EXCEPTIONS cannam@62: bool expectFatalThrow(Maybe type, Maybe message, cannam@62: Function code); cannam@62: // Expects that the given code will throw a fatal exception matching the given type and/or message. cannam@62: // Since exceptions are disabled, the test will fork() and run in a subprocess. On Windows, where cannam@62: // fork() is not available, this always returns true. cannam@62: #endif cannam@62: cannam@62: class LogExpectation: public ExceptionCallback { cannam@62: public: cannam@62: LogExpectation(LogSeverity severity, StringPtr substring); cannam@62: ~LogExpectation(); cannam@62: cannam@62: void logMessage(LogSeverity severity, const char* file, int line, int contextDepth, cannam@62: String&& text) override; cannam@62: cannam@62: private: cannam@62: LogSeverity severity; cannam@62: StringPtr substring; cannam@62: bool seen; cannam@62: UnwindDetector unwindDetector; cannam@62: }; cannam@62: cannam@62: class GlobFilter { cannam@62: // Implements glob filters for the --filter flag. cannam@62: // cannam@62: // Exposed in header only for testing. cannam@62: cannam@62: public: cannam@62: explicit GlobFilter(const char* pattern); cannam@62: explicit GlobFilter(ArrayPtr pattern); cannam@62: cannam@62: bool matches(StringPtr name); cannam@62: cannam@62: private: cannam@62: String pattern; cannam@62: Vector states; cannam@62: cannam@62: void applyState(char c, int state); cannam@62: }; cannam@62: cannam@62: } // namespace _ (private) cannam@62: } // namespace kj cannam@62: cannam@62: #endif // KJ_TEST_H_