annotate osx/include/kj/compat/gtest.h @ 151:fe80428a60a5

Add AppImage files
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 26 Jun 2018 18:00:44 +0100
parents 45360b968bf4
children
rev   line source
cannam@147 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
cannam@147 2 // Licensed under the MIT License:
cannam@147 3 //
cannam@147 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@147 5 // of this software and associated documentation files (the "Software"), to deal
cannam@147 6 // in the Software without restriction, including without limitation the rights
cannam@147 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@147 8 // copies of the Software, and to permit persons to whom the Software is
cannam@147 9 // furnished to do so, subject to the following conditions:
cannam@147 10 //
cannam@147 11 // The above copyright notice and this permission notice shall be included in
cannam@147 12 // all copies or substantial portions of the Software.
cannam@147 13 //
cannam@147 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@147 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@147 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@147 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@147 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@147 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@147 20 // THE SOFTWARE.
cannam@147 21
cannam@147 22 #ifndef KJ_COMPAT_GTEST_H_
cannam@147 23 #define KJ_COMPAT_GTEST_H_
cannam@147 24 // This file defines compatibility macros converting Google Test tests into KJ tests.
cannam@147 25 //
cannam@147 26 // This is only intended to cover the most common functionality. Many tests will likely need
cannam@147 27 // additional tweaks. For instance:
cannam@147 28 // - Using operator<< to print information on failure is not supported. Instead, switch to
cannam@147 29 // KJ_ASSERT/KJ_EXPECT and pass in stuff to print as additional parameters.
cannam@147 30 // - Test fixtures are not supported. Allocate your "test fixture" on the stack instead. Do setup
cannam@147 31 // in the constructor, teardown in the destructor.
cannam@147 32
cannam@147 33 #include "../test.h"
cannam@147 34
cannam@147 35 namespace kj {
cannam@147 36
cannam@147 37 namespace _ { // private
cannam@147 38
cannam@147 39 template <typename T>
cannam@147 40 T abs(T value) { return value < 0 ? -value : value; }
cannam@147 41
cannam@147 42 inline bool floatAlmostEqual(float a, float b) {
cannam@147 43 return a == b || abs(a - b) < (abs(a) + abs(b)) * 1e-5;
cannam@147 44 }
cannam@147 45
cannam@147 46 inline bool doubleAlmostEqual(double a, double b) {
cannam@147 47 return a == b || abs(a - b) < (abs(a) + abs(b)) * 1e-12;
cannam@147 48 }
cannam@147 49
cannam@147 50 } // namespace _ (private)
cannam@147 51
cannam@147 52 #define EXPECT_FALSE(x) KJ_EXPECT(!(x))
cannam@147 53 #define EXPECT_TRUE(x) KJ_EXPECT(x)
cannam@147 54 #define EXPECT_EQ(x, y) KJ_EXPECT((x) == (y), x, y)
cannam@147 55 #define EXPECT_NE(x, y) KJ_EXPECT((x) != (y), x, y)
cannam@147 56 #define EXPECT_LE(x, y) KJ_EXPECT((x) <= (y), x, y)
cannam@147 57 #define EXPECT_GE(x, y) KJ_EXPECT((x) >= (y), x, y)
cannam@147 58 #define EXPECT_LT(x, y) KJ_EXPECT((x) < (y), x, y)
cannam@147 59 #define EXPECT_GT(x, y) KJ_EXPECT((x) > (y), x, y)
cannam@147 60 #define EXPECT_STREQ(x, y) KJ_EXPECT(::strcmp(x, y) == 0, x, y)
cannam@147 61 #define EXPECT_FLOAT_EQ(x, y) KJ_EXPECT(::kj::_::floatAlmostEqual(y, x), y, x);
cannam@147 62 #define EXPECT_DOUBLE_EQ(x, y) KJ_EXPECT(::kj::_::doubleAlmostEqual(y, x), y, x);
cannam@147 63
cannam@147 64 #define ASSERT_FALSE(x) KJ_ASSERT(!(x))
cannam@147 65 #define ASSERT_TRUE(x) KJ_ASSERT(x)
cannam@147 66 #define ASSERT_EQ(x, y) KJ_ASSERT((x) == (y), x, y)
cannam@147 67 #define ASSERT_NE(x, y) KJ_ASSERT((x) != (y), x, y)
cannam@147 68 #define ASSERT_LE(x, y) KJ_ASSERT((x) <= (y), x, y)
cannam@147 69 #define ASSERT_GE(x, y) KJ_ASSERT((x) >= (y), x, y)
cannam@147 70 #define ASSERT_LT(x, y) KJ_ASSERT((x) < (y), x, y)
cannam@147 71 #define ASSERT_GT(x, y) KJ_ASSERT((x) > (y), x, y)
cannam@147 72 #define ASSERT_STREQ(x, y) KJ_ASSERT(::strcmp(x, y) == 0, x, y)
cannam@147 73 #define ASSERT_FLOAT_EQ(x, y) KJ_ASSERT(::kj::_::floatAlmostEqual(y, x), y, x);
cannam@147 74 #define ASSERT_DOUBLE_EQ(x, y) KJ_ASSERT(::kj::_::doubleAlmostEqual(y, x), y, x);
cannam@147 75
cannam@147 76 class AddFailureAdapter {
cannam@147 77 public:
cannam@147 78 AddFailureAdapter(const char* file, int line): file(file), line(line) {}
cannam@147 79
cannam@147 80 ~AddFailureAdapter() {
cannam@147 81 if (!handled) {
cannam@147 82 _::Debug::log(file, line, LogSeverity::ERROR, "expectation failed");
cannam@147 83 }
cannam@147 84 }
cannam@147 85
cannam@147 86 template <typename T>
cannam@147 87 void operator<<(T&& info) {
cannam@147 88 handled = true;
cannam@147 89 _::Debug::log(file, line, LogSeverity::ERROR, "\"expectation failed\", info",
cannam@147 90 "expectation failed", kj::fwd<T>(info));
cannam@147 91 }
cannam@147 92
cannam@147 93 private:
cannam@147 94 bool handled = false;
cannam@147 95 const char* file;
cannam@147 96 int line;
cannam@147 97 };
cannam@147 98
cannam@147 99 #define ADD_FAILURE() ::kj::AddFailureAdapter(__FILE__, __LINE__)
cannam@147 100
cannam@147 101 #if KJ_NO_EXCEPTIONS
cannam@147 102 #define EXPECT_ANY_THROW(code) \
cannam@147 103 KJ_EXPECT(::kj::_::expectFatalThrow(nullptr, nullptr, [&]() { code; }))
cannam@147 104 #else
cannam@147 105 #define EXPECT_ANY_THROW(code) \
cannam@147 106 KJ_EXPECT(::kj::runCatchingExceptions([&]() { code; }) != nullptr)
cannam@147 107 #endif
cannam@147 108
cannam@147 109 #define EXPECT_NONFATAL_FAILURE(code) \
cannam@147 110 EXPECT_TRUE(kj::runCatchingExceptions([&]() { code; }) != nullptr);
cannam@147 111
cannam@147 112 #ifdef KJ_DEBUG
cannam@147 113 #define EXPECT_DEBUG_ANY_THROW EXPECT_ANY_THROW
cannam@147 114 #else
cannam@147 115 #define EXPECT_DEBUG_ANY_THROW(EXP)
cannam@147 116 #endif
cannam@147 117
cannam@147 118 #define TEST(x, y) KJ_TEST("legacy test: " #x "/" #y)
cannam@147 119
cannam@147 120 } // namespace kj
cannam@147 121
cannam@147 122 #endif // KJ_COMPAT_GTEST_H_