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