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