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