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