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