aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/testing/unittest/registar_ut.cpp
blob: 0426f366f2726dacbd3e92dd2c05d32b1abc876b (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include <library/cpp/testing/unittest/registar.h>

Y_UNIT_TEST_SUITE(TUnitTestMacroTest) {
    Y_UNIT_TEST(Assert) {
        auto unitAssert = [] {
            UNIT_ASSERT(false);
        };
        UNIT_ASSERT_TEST_FAILS(unitAssert());

        UNIT_ASSERT(true);
    }

    Y_UNIT_TEST(TypesEqual) {
        auto typesEqual = [] {
            UNIT_ASSERT_TYPES_EQUAL(int, long);
        };
        UNIT_ASSERT_TEST_FAILS(typesEqual());

        UNIT_ASSERT_TYPES_EQUAL(TString, TString);
    }

    Y_UNIT_TEST(DoublesEqual) {
        auto doublesEqual = [](double d1, double d2, double precision) {
            UNIT_ASSERT_DOUBLES_EQUAL(d1, d2, precision);
        };
        UNIT_ASSERT_TEST_FAILS(doublesEqual(0.0, 0.5, 0.1));
        UNIT_ASSERT_TEST_FAILS(doublesEqual(0.1, -0.1, 0.1));

        UNIT_ASSERT_DOUBLES_EQUAL(0.0, 0.01, 0.1);
        UNIT_ASSERT_DOUBLES_EQUAL(0.01, 0.0, 0.1);

        constexpr auto nan = std::numeric_limits<double>::quiet_NaN();
        UNIT_ASSERT_TEST_FAILS(doublesEqual(nan, 0.5, 0.1));
        UNIT_ASSERT_TEST_FAILS(doublesEqual(0.5, nan, 0.1));
        UNIT_ASSERT_DOUBLES_EQUAL(nan, nan, 0.1);
    }

    Y_UNIT_TEST(StringsEqual) {
        auto stringsEqual = [](auto s1, auto s2) {
            UNIT_ASSERT_STRINGS_EQUAL(s1, s2);
        };
        UNIT_ASSERT_TEST_FAILS(stringsEqual("q", "w"));
        UNIT_ASSERT_TEST_FAILS(stringsEqual("q", TString("w")));
        UNIT_ASSERT_TEST_FAILS(stringsEqual(TString("q"), "w"));
        UNIT_ASSERT_TEST_FAILS(stringsEqual(TString("a"), TString("b")));
        UNIT_ASSERT_TEST_FAILS(stringsEqual(TString("a"), TStringBuf("b")));
        UNIT_ASSERT_TEST_FAILS(stringsEqual("a", TStringBuf("b")));
        UNIT_ASSERT_TEST_FAILS(stringsEqual(TStringBuf("a"), "b"));

        TString empty;
        TStringBuf emptyBuf;
        UNIT_ASSERT_STRINGS_EQUAL("", empty);
        UNIT_ASSERT_STRINGS_EQUAL(empty, emptyBuf);
        UNIT_ASSERT_STRINGS_EQUAL("", static_cast<const char*>(nullptr));
    }

    Y_UNIT_TEST(StringContains) {
        auto stringContains = [](auto s, auto substr) {
            UNIT_ASSERT_STRING_CONTAINS(s, substr);
        };
        UNIT_ASSERT_TEST_FAILS(stringContains("", "a"));
        UNIT_ASSERT_TEST_FAILS(stringContains("lurkmore", "moar"));

        UNIT_ASSERT_STRING_CONTAINS("", "");
        UNIT_ASSERT_STRING_CONTAINS("a", "");
        UNIT_ASSERT_STRING_CONTAINS("failure", "fail");
        UNIT_ASSERT_STRING_CONTAINS("lurkmore", "more");
    }

    Y_UNIT_TEST(NoDiff) {
        auto noDiff = [](auto s1, auto s2) {
            UNIT_ASSERT_NO_DIFF(s1, s2);
        };
        UNIT_ASSERT_TEST_FAILS(noDiff("q", "w"));
        UNIT_ASSERT_TEST_FAILS(noDiff("q", ""));

        UNIT_ASSERT_NO_DIFF("", "");
        UNIT_ASSERT_NO_DIFF("a", "a");
    }

    Y_UNIT_TEST(StringsUnequal) {
        auto stringsUnequal = [](auto s1, auto s2) {
            UNIT_ASSERT_STRINGS_UNEQUAL(s1, s2);
        };
        UNIT_ASSERT_TEST_FAILS(stringsUnequal("1", "1"));
        UNIT_ASSERT_TEST_FAILS(stringsUnequal("", ""));
        UNIT_ASSERT_TEST_FAILS(stringsUnequal("42", TString("42")));
        UNIT_ASSERT_TEST_FAILS(stringsUnequal(TString("4"), "4"));
        UNIT_ASSERT_TEST_FAILS(stringsUnequal("d", TStringBuf("d")));
        UNIT_ASSERT_TEST_FAILS(stringsUnequal(TStringBuf("yandex"), "yandex"));
        UNIT_ASSERT_TEST_FAILS(stringsUnequal(TStringBuf("index"), TString("index")));
        UNIT_ASSERT_TEST_FAILS(stringsUnequal(TString("diff"), TStringBuf("diff")));

        UNIT_ASSERT_STRINGS_UNEQUAL("1", "2");
        UNIT_ASSERT_STRINGS_UNEQUAL("", "3");
        UNIT_ASSERT_STRINGS_UNEQUAL("green", TStringBuf("red"));
        UNIT_ASSERT_STRINGS_UNEQUAL(TStringBuf("solomon"), "golovan");
        UNIT_ASSERT_STRINGS_UNEQUAL("d", TString("f"));
        UNIT_ASSERT_STRINGS_UNEQUAL(TString("yandex"), "index");
        UNIT_ASSERT_STRINGS_UNEQUAL(TString("mail"), TStringBuf("yandex"));
        UNIT_ASSERT_STRINGS_UNEQUAL(TStringBuf("C++"), TString("python"));
    }

    Y_UNIT_TEST(Equal) {
        auto equal = [](auto v1, auto v2) {
            UNIT_ASSERT_EQUAL(v1, v2);
        };
        UNIT_ASSERT_TEST_FAILS(equal("1", TString("2")));
        UNIT_ASSERT_TEST_FAILS(equal(1, 2));
        UNIT_ASSERT_TEST_FAILS(equal(42ul, static_cast<unsigned short>(24)));

        UNIT_ASSERT_EQUAL("abc", TString("abc"));
        UNIT_ASSERT_EQUAL(12l, 12);
        UNIT_ASSERT_EQUAL(55, 55);
    }

    Y_UNIT_TEST(Unequal) {
        auto unequal = [](auto v1, auto v2) {
            UNIT_ASSERT_UNEQUAL(v1, v2);
        };
        UNIT_ASSERT_TEST_FAILS(unequal("x", TString("x")));
        UNIT_ASSERT_TEST_FAILS(unequal(1, 1));
        UNIT_ASSERT_TEST_FAILS(unequal(static_cast<unsigned short>(42), 42ul));

        UNIT_ASSERT_UNEQUAL("abc", TString("cba"));
        UNIT_ASSERT_UNEQUAL(12l, 10);
        UNIT_ASSERT_UNEQUAL(33, 50);
    }

    Y_UNIT_TEST(LessThan) { 
        auto lt = [](auto v1, auto v2) { 
            UNIT_ASSERT_LT(v1, v2); 
        }; 
 
        // less than 
        UNIT_ASSERT_LT(TStringBuf("1"), "2"); 
        UNIT_ASSERT_LT("2", TString("3")); 
        UNIT_ASSERT_LT("abc", TString("azz")); 
        UNIT_ASSERT_LT(2, 4); 
        UNIT_ASSERT_LT(42ul, static_cast<unsigned short>(48)); 
 
        // equals 
        UNIT_ASSERT_TEST_FAILS(lt(TStringBuf("2"), "2")); 
        UNIT_ASSERT_TEST_FAILS(lt("2", TString("2"))); 
        UNIT_ASSERT_TEST_FAILS(lt("abc", TString("abc"))); 
        UNIT_ASSERT_TEST_FAILS(lt(2, 2)); 
        UNIT_ASSERT_TEST_FAILS(lt(42ul, static_cast<unsigned short>(42))); 
 
        // greater than 
        UNIT_ASSERT_TEST_FAILS(lt(TStringBuf("2"), "1")); 
        UNIT_ASSERT_TEST_FAILS(lt("3", TString("2"))); 
        UNIT_ASSERT_TEST_FAILS(lt("azz", TString("abc"))); 
        UNIT_ASSERT_TEST_FAILS(lt(5, 2)); 
        UNIT_ASSERT_TEST_FAILS(lt(100ul, static_cast<unsigned short>(42))); 
    } 
 
    Y_UNIT_TEST(LessOrEqual) { 
        auto le = [](auto v1, auto v2) { 
            UNIT_ASSERT_LE(v1, v2); 
        }; 
 
        // less than 
        UNIT_ASSERT_LE(TStringBuf("1"), "2"); 
        UNIT_ASSERT_LE("2", TString("3")); 
        UNIT_ASSERT_LE("abc", TString("azz")); 
        UNIT_ASSERT_LE(2, 4); 
        UNIT_ASSERT_LE(42ul, static_cast<unsigned short>(48)); 
 
        // equals 
        UNIT_ASSERT_LE(TStringBuf("2"), "2"); 
        UNIT_ASSERT_LE("2", TString("2")); 
        UNIT_ASSERT_LE("abc", TString("abc")); 
        UNIT_ASSERT_LE(2, 2); 
        UNIT_ASSERT_LE(42ul, static_cast<unsigned short>(42)); 
 
        // greater than 
        UNIT_ASSERT_TEST_FAILS(le(TStringBuf("2"), "1")); 
        UNIT_ASSERT_TEST_FAILS(le("3", TString("2"))); 
        UNIT_ASSERT_TEST_FAILS(le("azz", TString("abc"))); 
        UNIT_ASSERT_TEST_FAILS(le(5, 2)); 
        UNIT_ASSERT_TEST_FAILS(le(100ul, static_cast<unsigned short>(42))); 
    } 
 
    Y_UNIT_TEST(GreaterThan) { 
        auto gt = [](auto v1, auto v2) { 
            UNIT_ASSERT_GT(v1, v2); 
        }; 
 
        // less than 
        UNIT_ASSERT_TEST_FAILS(gt(TStringBuf("1"), "2")); 
        UNIT_ASSERT_TEST_FAILS(gt("2", TString("3"))); 
        UNIT_ASSERT_TEST_FAILS(gt("abc", TString("azz"))); 
        UNIT_ASSERT_TEST_FAILS(gt(2, 4)); 
        UNIT_ASSERT_TEST_FAILS(gt(42ul, static_cast<unsigned short>(48))); 
 
        // equals 
        UNIT_ASSERT_TEST_FAILS(gt(TStringBuf("2"), "2")); 
        UNIT_ASSERT_TEST_FAILS(gt("2", TString("2"))); 
        UNIT_ASSERT_TEST_FAILS(gt("abc", TString("abc"))); 
        UNIT_ASSERT_TEST_FAILS(gt(2, 2)); 
        UNIT_ASSERT_TEST_FAILS(gt(42ul, static_cast<unsigned short>(42))); 
 
        // greater than 
        UNIT_ASSERT_GT(TStringBuf("2"), "1"); 
        UNIT_ASSERT_GT("3", TString("2")); 
        UNIT_ASSERT_GT("azz", TString("abc")); 
        UNIT_ASSERT_GT(5, 2); 
        UNIT_ASSERT_GT(100ul, static_cast<unsigned short>(42)); 
    } 
 
    Y_UNIT_TEST(GreaterOrEqual) { 
        auto ge = [](auto v1, auto v2) { 
            UNIT_ASSERT_GE(v1, v2); 
        }; 
 
        // less than 
        UNIT_ASSERT_TEST_FAILS(ge(TStringBuf("1"), "2")); 
        UNIT_ASSERT_TEST_FAILS(ge("2", TString("3"))); 
        UNIT_ASSERT_TEST_FAILS(ge("abc", TString("azz"))); 
        UNIT_ASSERT_TEST_FAILS(ge(2, 4)); 
        UNIT_ASSERT_TEST_FAILS(ge(42ul, static_cast<unsigned short>(48))); 
 
        // equals 
        UNIT_ASSERT_GE(TStringBuf("2"), "2"); 
        UNIT_ASSERT_GE("2", TString("2")); 
        UNIT_ASSERT_GE("abc", TString("abc")); 
        UNIT_ASSERT_GE(2, 2); 
        UNIT_ASSERT_GE(42ul, static_cast<unsigned short>(42)); 
 
        // greater than 
        UNIT_ASSERT_GE(TStringBuf("2"), "1"); 
        UNIT_ASSERT_GE("3", TString("2")); 
        UNIT_ASSERT_GE("azz", TString("abc")); 
        UNIT_ASSERT_GE(5, 2); 
        UNIT_ASSERT_GE(100ul, static_cast<unsigned short>(42)); 
    } 
 
    Y_UNIT_TEST(ValuesEqual) {
        auto valuesEqual = [](auto v1, auto v2) {
            UNIT_ASSERT_VALUES_EQUAL(v1, v2);
        };
        UNIT_ASSERT_TEST_FAILS(valuesEqual(1, 2));
        UNIT_ASSERT_TEST_FAILS(valuesEqual(1l, static_cast<short>(2)));

        UNIT_ASSERT_VALUES_EQUAL("yandex", TString("yandex"));
        UNIT_ASSERT_VALUES_EQUAL(1.0, 1.0);
    }

    Y_UNIT_TEST(ValuesUnequal) {
        auto valuesUnequal = [](auto v1, auto v2) {
            UNIT_ASSERT_VALUES_UNEQUAL(v1, v2);
        };
        UNIT_ASSERT_TEST_FAILS(valuesUnequal(5, 5));
        UNIT_ASSERT_TEST_FAILS(valuesUnequal(static_cast<char>(5), 5l));
        TString test("test");
        UNIT_ASSERT_TEST_FAILS(valuesUnequal("test", test.data()));

        UNIT_ASSERT_VALUES_UNEQUAL("UNIT_ASSERT_VALUES_UNEQUAL", "UNIT_ASSERT_VALUES_EQUAL");
        UNIT_ASSERT_VALUES_UNEQUAL(1.0, 1.1);
    }

    class TTestException: public yexception {
    public:
        TTestException(const TString& text = "test exception", bool throwMe = true)
            : ThrowMe(throwMe)
        {
            *this << text;
        }

        virtual ~TTestException() = default;

        virtual void Throw() {
            if (ThrowMe) {
                throw *this;
            }
        }

        std::string ThrowStr() {
            if (ThrowMe) {
                throw *this;
            }

            return {};
        }

        void AssertNoException() {
            UNIT_ASSERT_NO_EXCEPTION(Throw());
        }

        void AssertNoExceptionRet() {
            const TString res = UNIT_ASSERT_NO_EXCEPTION_RESULT(ThrowStr()); 
        }

        template <class TExpectedException>
        void AssertException() {
            UNIT_ASSERT_EXCEPTION(Throw(), TExpectedException);
        }

        template <class TExpectedException, class T>
        void AssertExceptionContains(const T& substr) {
            UNIT_ASSERT_EXCEPTION_CONTAINS(Throw(), TExpectedException, substr);
        }

        template <class TExpectedException, class P>
        void AssertExceptionSatisfies(const P& predicate) {
            UNIT_ASSERT_EXCEPTION_SATISFIES(Throw(), TExpectedException, predicate);
        }

        int GetValue() const {
            return 5;           // just some value for predicate testing
        }

        bool ThrowMe;
    };

    class TOtherTestException: public TTestException {
    public:
        using TTestException::TTestException;

        // Throws other type of exception
        void Throw() override {
            if (ThrowMe) {
                throw *this;
            }
        }
    };

    Y_UNIT_TEST(Exception) {
        UNIT_ASSERT_TEST_FAILS(TTestException("", false).AssertException<TTestException>());
        UNIT_ASSERT_TEST_FAILS(TTestException().AssertException<TOtherTestException>());

        UNIT_ASSERT_EXCEPTION(TOtherTestException().Throw(), TTestException);
        UNIT_ASSERT_EXCEPTION(TTestException().Throw(), TTestException);
    }

    Y_UNIT_TEST(ExceptionAssertionContainsOtherExceptionMessage) {
        NUnitTest::TUnitTestFailChecker checker;
        {
            auto guard = checker.InvokeGuard();
            TTestException("custom exception message").AssertException<TOtherTestException>();
        }
        UNIT_ASSERT(checker.Failed());
        UNIT_ASSERT_STRING_CONTAINS(checker.Msg(), "custom exception message");
    }

    Y_UNIT_TEST(NoException) {
        UNIT_ASSERT_TEST_FAILS(TTestException().AssertNoException());
        UNIT_ASSERT_TEST_FAILS(TTestException().AssertNoExceptionRet());

        UNIT_ASSERT_NO_EXCEPTION(TTestException("", false).Throw());
    }

    Y_UNIT_TEST(ExceptionContains) {
        UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TTestException>("cba"));
        UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TTestException>(TStringBuf("cba")));
        UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TTestException>(TString("cba")));
        UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TTestException>(TStringBuilder() << "cba"));

        UNIT_ASSERT_TEST_FAILS(TTestException("abc", false).AssertExceptionContains<TTestException>("bc"));

        UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TOtherTestException>("b"));

        UNIT_ASSERT_EXCEPTION_CONTAINS(TTestException("abc").Throw(), TTestException, "a");
    }

    Y_UNIT_TEST(ExceptionSatisfies) {
        const auto goodPredicate = [](const TTestException& e) { return e.GetValue() == 5; };
        const auto badPredicate  = [](const TTestException& e) { return e.GetValue() != 5; };
        UNIT_ASSERT_NO_EXCEPTION(TTestException().AssertExceptionSatisfies<TTestException>(goodPredicate));
        UNIT_ASSERT_TEST_FAILS(TTestException().AssertExceptionSatisfies<TTestException>(badPredicate));
        UNIT_ASSERT_TEST_FAILS(TTestException().AssertExceptionSatisfies<TOtherTestException>(goodPredicate));
    }
}