aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/scheme/ut_utils/scheme_ut_utils.h
blob: eb3ea15b2abfe3fcd6edab769617f2495d336ecb (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
#pragma once

#include <library/cpp/json/json_prettifier.h>
#include <library/cpp/scheme/scheme.h>
#include <library/cpp/json/json_value.h>
#include <library/cpp/json/json_writer.h>
#include <library/cpp/testing/unittest/registar.h>
#include <util/string/cast.h>

namespace NSc {
    namespace NUt {
        TValue AssertFromJson(TStringBuf json);

        inline TString NormalizeJson(const NSc::TValue& sc) {
            return sc.ToJson(true);
        }

        inline TString NormalizeJson(const NJson::TJsonValue& sc) {
            return NJson::WriteJson(sc, false, true, false);
        }

        template <class TStr>
        inline TString NormalizeJson(const TStr& val) {
            return AssertFromJson(val).ToJson(true);
        }

#define UNIT_ASSERT_JSON_EQ_JSON_C(A, B, c)                                                                                           \
    do {                                                                                                                              \
        const TString _a = NSc::NUt::NormalizeJson(A);                                                                                \
        const TString _b = NSc::NUt::NormalizeJson(B);                                                                                \
        if (_a != _b) {                                                                                                               \
            UNIT_FAIL_IMPL(                                                                                                           \
                "json values are different (" #A " != " #B ")",                                                                       \
                Sprintf("%s\n!=\n%s\n%s\n%s", _a.data(), _b.data(),                                                                               \
                        ::NUnitTest::ColoredDiff(NJson::PrettifyJson(_a), NJson::PrettifyJson(_b), " \t\n,:\"{}[]").data(), ToString(c).data())); \
        }                                                                                                                             \
    } while (false)

#define UNIT_ASSERT_JSON_EQ_JSON(A, B) UNIT_ASSERT_JSON_EQ_JSON_C(A, B, "")

        inline TString DumpJson(const TValue& json) {
            return NJson::CompactifyJson(json.ToJson(true), true, true);
        }

        // deprecated
        inline TString DumpJsonVS(const TValue& expected, const TValue& fact) {
            return DumpJson(expected) + "(expected) != (fact)" + DumpJson(fact);
        }

        void AssertScheme(const TValue& expected, const TValue& real);
        void AssertSchemeJson(TStringBuf expected, const TValue& real);
        void AssertJsonJson(TStringBuf expected, TStringBuf real);

    }
}