cannam@62: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors cannam@62: // Licensed under the MIT License: cannam@62: // cannam@62: // Permission is hereby granted, free of charge, to any person obtaining a copy cannam@62: // of this software and associated documentation files (the "Software"), to deal cannam@62: // in the Software without restriction, including without limitation the rights cannam@62: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cannam@62: // copies of the Software, and to permit persons to whom the Software is cannam@62: // furnished to do so, subject to the following conditions: cannam@62: // cannam@62: // The above copyright notice and this permission notice shall be included in cannam@62: // all copies or substantial portions of the Software. cannam@62: // cannam@62: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR cannam@62: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, cannam@62: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE cannam@62: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER cannam@62: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, cannam@62: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN cannam@62: // THE SOFTWARE. cannam@62: cannam@62: #ifndef KJ_COMPAT_GTEST_H_ cannam@62: #define KJ_COMPAT_GTEST_H_ cannam@62: // This file defines compatibility macros converting Google Test tests into KJ tests. cannam@62: // cannam@62: // This is only intended to cover the most common functionality. Many tests will likely need cannam@62: // additional tweaks. For instance: cannam@62: // - Using operator<< to print information on failure is not supported. Instead, switch to cannam@62: // KJ_ASSERT/KJ_EXPECT and pass in stuff to print as additional parameters. cannam@62: // - Test fixtures are not supported. Allocate your "test fixture" on the stack instead. Do setup cannam@62: // in the constructor, teardown in the destructor. cannam@62: cannam@62: #include "../test.h" cannam@62: cannam@62: namespace kj { cannam@62: cannam@62: namespace _ { // private cannam@62: cannam@62: template cannam@62: T abs(T value) { return value < 0 ? -value : value; } cannam@62: cannam@62: inline bool floatAlmostEqual(float a, float b) { cannam@62: return a == b || abs(a - b) < (abs(a) + abs(b)) * 1e-5; cannam@62: } cannam@62: cannam@62: inline bool doubleAlmostEqual(double a, double b) { cannam@62: return a == b || abs(a - b) < (abs(a) + abs(b)) * 1e-12; cannam@62: } cannam@62: cannam@62: } // namespace _ (private) cannam@62: cannam@62: #define EXPECT_FALSE(x) KJ_EXPECT(!(x)) cannam@62: #define EXPECT_TRUE(x) KJ_EXPECT(x) cannam@62: #define EXPECT_EQ(x, y) KJ_EXPECT((x) == (y), x, y) cannam@62: #define EXPECT_NE(x, y) KJ_EXPECT((x) != (y), x, y) cannam@62: #define EXPECT_LE(x, y) KJ_EXPECT((x) <= (y), x, y) cannam@62: #define EXPECT_GE(x, y) KJ_EXPECT((x) >= (y), x, y) cannam@62: #define EXPECT_LT(x, y) KJ_EXPECT((x) < (y), x, y) cannam@62: #define EXPECT_GT(x, y) KJ_EXPECT((x) > (y), x, y) cannam@62: #define EXPECT_STREQ(x, y) KJ_EXPECT(::strcmp(x, y) == 0, x, y) cannam@62: #define EXPECT_FLOAT_EQ(x, y) KJ_EXPECT(::kj::_::floatAlmostEqual(y, x), y, x); cannam@62: #define EXPECT_DOUBLE_EQ(x, y) KJ_EXPECT(::kj::_::doubleAlmostEqual(y, x), y, x); cannam@62: cannam@62: #define ASSERT_FALSE(x) KJ_ASSERT(!(x)) cannam@62: #define ASSERT_TRUE(x) KJ_ASSERT(x) cannam@62: #define ASSERT_EQ(x, y) KJ_ASSERT((x) == (y), x, y) cannam@62: #define ASSERT_NE(x, y) KJ_ASSERT((x) != (y), x, y) cannam@62: #define ASSERT_LE(x, y) KJ_ASSERT((x) <= (y), x, y) cannam@62: #define ASSERT_GE(x, y) KJ_ASSERT((x) >= (y), x, y) cannam@62: #define ASSERT_LT(x, y) KJ_ASSERT((x) < (y), x, y) cannam@62: #define ASSERT_GT(x, y) KJ_ASSERT((x) > (y), x, y) cannam@62: #define ASSERT_STREQ(x, y) KJ_ASSERT(::strcmp(x, y) == 0, x, y) cannam@62: #define ASSERT_FLOAT_EQ(x, y) KJ_ASSERT(::kj::_::floatAlmostEqual(y, x), y, x); cannam@62: #define ASSERT_DOUBLE_EQ(x, y) KJ_ASSERT(::kj::_::doubleAlmostEqual(y, x), y, x); cannam@62: cannam@62: class AddFailureAdapter { cannam@62: public: cannam@62: AddFailureAdapter(const char* file, int line): file(file), line(line) {} cannam@62: cannam@62: ~AddFailureAdapter() { cannam@62: if (!handled) { cannam@62: _::Debug::log(file, line, LogSeverity::ERROR, "expectation failed"); cannam@62: } cannam@62: } cannam@62: cannam@62: template cannam@62: void operator<<(T&& info) { cannam@62: handled = true; cannam@62: _::Debug::log(file, line, LogSeverity::ERROR, "\"expectation failed\", info", cannam@62: "expectation failed", kj::fwd(info)); cannam@62: } cannam@62: cannam@62: private: cannam@62: bool handled = false; cannam@62: const char* file; cannam@62: int line; cannam@62: }; cannam@62: cannam@62: #define ADD_FAILURE() ::kj::AddFailureAdapter(__FILE__, __LINE__) cannam@62: cannam@62: #if KJ_NO_EXCEPTIONS cannam@62: #define EXPECT_ANY_THROW(code) \ cannam@62: KJ_EXPECT(::kj::_::expectFatalThrow(nullptr, nullptr, [&]() { code; })) cannam@62: #else cannam@62: #define EXPECT_ANY_THROW(code) \ cannam@62: KJ_EXPECT(::kj::runCatchingExceptions([&]() { code; }) != nullptr) cannam@62: #endif cannam@62: cannam@62: #define EXPECT_NONFATAL_FAILURE(code) \ cannam@62: EXPECT_TRUE(kj::runCatchingExceptions([&]() { code; }) != nullptr); cannam@62: cannam@62: #ifdef KJ_DEBUG cannam@62: #define EXPECT_DEBUG_ANY_THROW EXPECT_ANY_THROW cannam@62: #else cannam@62: #define EXPECT_DEBUG_ANY_THROW(EXP) cannam@62: #endif cannam@62: cannam@62: #define TEST(x, y) KJ_TEST("legacy test: " #x "/" #y) cannam@62: cannam@62: } // namespace kj cannam@62: cannam@62: #endif // KJ_COMPAT_GTEST_H_