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