aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/testing/unittest/gtest.h
blob: 7dc5eac4a2117988705aca54053ef995e70c7244 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#pragma once

// WARNING: this is a legacy header that tries to mimic the gtest interface while using unittest
// under the hood. Avoid using this interface -- use the genuine gtest instead (the GTEST macro).
// If you're already using GTEST macro and you've found yourself here, you probably meant
// to include `library/cpp/testing/gtest/gtest.h`.

#include "registar.h"

#include <util/generic/ymath.h>
#include <util/generic/ylimits.h>

namespace NUnitTest {
    namespace NPrivate {
        struct IGTestFactory: public ITestBaseFactory {
            ~IGTestFactory() override;

            virtual void AddTest(const char* name, void (*body)(TTestContext&), bool forceFork) = 0; 
        };

        IGTestFactory* ByName(const char* name);
    }
}

namespace NTesting {
    struct TTest {
        virtual void SetUp() {
        }

        virtual void TearDown() {
        }

        inline TTest* _This() noexcept {
            return this;
        }
    };
}

namespace testing {
    struct Test: public ::NTesting::TTest {
    };
}

#define TEST_IMPL(N, NN, FF)                                                         \
    void Test##N##NN(NUnitTest::TTestContext&);                                      \ 
    namespace NTestSuite##N##NN {                                                    \
        struct TReg {                                                                \
            inline TReg() {                                                          \
                ::NUnitTest::NPrivate::ByName(#N)->AddTest(#NN, &(Test##N##NN), FF); \
            }                                                                        \
        };                                                                           \
        static TReg reg;                                                             \
    }                                                                                \
    void Test##N##NN(NUnitTest::TTestContext&) 

#define TEST_F_IMPL(N, NN, FF)                \
    namespace NTestSuite##N##NN {             \
        struct TTestSuite: public N {         \
            inline TTestSuite() {             \
                this->_This()->SetUp();       \
            }                                 \
            inline ~TTestSuite() {            \
                this->_This()->TearDown();    \
            }                                 \
            void NN();                        \
        };                                    \
    };                                        \
    TEST_IMPL(N, NN, FF) {                    \
        NTestSuite##N##NN::TTestSuite().NN(); \
    }                                         \
    void NTestSuite##N##NN::TTestSuite::NN()

#define TEST(A, B) TEST_IMPL(A, B, false)
#define TEST_FORKED(A, B) TEST_IMPL(A, B, true)

#define TEST_F(A, B) TEST_F_IMPL(A, B, false)
#define TEST_F_FORKED(A, B) TEST_F_IMPL(A, B, true)

#define EXPECT_EQ(A, B) UNIT_ASSERT_VALUES_EQUAL(A, B)
#define EXPECT_NE(A, B) UNIT_ASSERT_UNEQUAL(A, B)
#define EXPECT_LE(A, B) UNIT_ASSERT((A) <= (B))
#define EXPECT_LT(A, B) UNIT_ASSERT((A) < (B))
#define EXPECT_GE(A, B) UNIT_ASSERT((A) >= (B))
#define EXPECT_GT(A, B) UNIT_ASSERT((A) > (B))
#define EXPECT_NO_THROW(A) UNIT_ASSERT_NO_EXCEPTION(A)
#define EXPECT_THROW(A, B) UNIT_ASSERT_EXCEPTION(A, B)
#define EXPECT_NEAR(A, B, D) UNIT_ASSERT_DOUBLES_EQUAL(A, B, D)
#define EXPECT_STREQ(A, B) UNIT_ASSERT_VALUES_EQUAL(A, B)

#define EXPECT_DOUBLE_EQ_TOLERANCE(A, B, tolerance) UNIT_ASSERT_C(fabs((A) - (B)) < tolerance * std::numeric_limits<decltype(A)>::epsilon(), TString("\n") + ToString(A) + " <> " + ToString(B))
#define EXPECT_DOUBLE_EQ(A, B) EXPECT_DOUBLE_EQ_TOLERANCE(A, B, 4.0)

//conflicts with util/system/defaults.h
#undef EXPECT_TRUE
#define EXPECT_TRUE(X) UNIT_ASSERT(X)
#undef EXPECT_FALSE
#define EXPECT_FALSE(X) UNIT_ASSERT(!(X))

#define ASSERT_EQ(A, B) EXPECT_EQ(A, B)
#define ASSERT_NE(A, B) EXPECT_NE(A, B)
#define ASSERT_GT(A, B) EXPECT_GT(A, B)
#define ASSERT_LT(A, B) EXPECT_LT(A, B)
#define ASSERT_FALSE(X) EXPECT_FALSE(X)
#define ASSERT_TRUE(X) EXPECT_TRUE(X)
#define ASSERT_THROW(A, B) EXPECT_THROW(A, B)
#define ASSERT_NO_THROW(A) EXPECT_NO_THROW(A)
#define ASSERT_DOUBLE_EQ(A, B) EXPECT_DOUBLE_EQ(A, B)
#define ASSERT_STREQ(A, B) EXPECT_STREQ(A, B)