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