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