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