aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/scheme/tests/ut/scheme_merge_ut.cpp
blob: fe2bd6e43c98cdac61795833ee8c2e36eb396e83 (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
#include <library/cpp/scheme/scimpl_private.h>
#include <library/cpp/scheme/ut_utils/scheme_ut_utils.h>
#include <library/cpp/testing/unittest/registar.h>

#include <util/stream/null.h>
#include <library/cpp/string_utils/quote/quote.h>
#include <util/string/subst.h>
#include <util/string/util.h>

#include <type_traits>


Y_UNIT_TEST_SUITE(TSchemeMergeTest) {

    void DoTestReverseMerge(TStringBuf lhs, TStringBuf rhs, TStringBuf res) {
        NSc::TValue v = NSc::TValue::FromJson(lhs);
        v.ReverseMerge(NSc::TValue::FromJson(rhs));
        UNIT_ASSERT(NSc::TValue::Equal(v, NSc::TValue::FromJson(res)));
    }

    Y_UNIT_TEST(TestReverseMerge) {
        DoTestReverseMerge("{a:{x:y, b:c}}", "{a:{u:w, b:d}}", "{a:{u:w, x:y, b:c}}");
        DoTestReverseMerge("null", "{x:y}", "{x:y}");
        DoTestReverseMerge("null", "[b]", "[b]");
        DoTestReverseMerge("[a]", "[b]", "[a]");
        DoTestReverseMerge("{x:null}", "{x:b}", "{x:b}");
    } 
 
    Y_UNIT_TEST(TestMerge) {
        TStringBuf data = "{ a : [ { b : 1, d : { e : -1.e5 } }, { f : 0, g : [ h, i ] } ] }";
        NSc::TValue v = NSc::TValue::FromJson(data);
        UNIT_ASSERT_VALUES_EQUAL(v.ToJson(true), v.Clone().ToJson(true));
        UNIT_ASSERT(v.Has("a"));
        UNIT_ASSERT(v["a"].Has(1));
        UNIT_ASSERT(v["a"][0].Has("b"));
        UNIT_ASSERT(v["a"][0].Has("d"));
        UNIT_ASSERT(1 == v["a"][0]["b"]);
        UNIT_ASSERT(v["a"][0]["d"].Has("e"));
        UNIT_ASSERT(-1.e5 == v["a"][0]["d"]["e"]);
        UNIT_ASSERT(v["a"][1].Has("f"));
        UNIT_ASSERT(v["a"][1].Has("g"));
        UNIT_ASSERT(0. == v["a"][1]["f"]);
        UNIT_ASSERT(v["a"][1]["g"].IsArray());
        UNIT_ASSERT(v["a"][1]["g"].Has(1));
        UNIT_ASSERT(TStringBuf("h") == v["a"][1]["g"][0]);
        UNIT_ASSERT(TStringBuf("i") == v["a"][1]["g"][1]);

        {
            TStringBuf data = "{ a : [ { d : 42 }, { g : [ 3 ] } ], q : r }";

            NSc::TValue v1 = NSc::TValue::FromJson(data);
            UNIT_ASSERT_VALUES_EQUAL(v1.ToJson(true), v1.Clone().ToJson(true));
            UNIT_ASSERT(NSc::TValue::Equal(v1, v1.FromJson(v1.ToJson())));

            NSc::TValue v2;
            v2.MergeUpdate(v["a"]);
            UNIT_ASSERT_C(NSc::TValue::Equal(v["a"], v2), Sprintf("\n%s\n!=\n%s\n", v["a"].ToJson().data(), v2.ToJson().data()));

            v.MergeUpdate(v1);
            UNIT_ASSERT_C(!NSc::TValue::Equal(v["a"], v2), Sprintf("\n%s\n!=\n%s\n", v["a"].ToJson().data(), v2.ToJson().data()));
            v2.MergeUpdate(v1["a"]);
            UNIT_ASSERT_C(NSc::TValue::Equal(v["a"], v2), Sprintf("\n%s\n!=\n%s\n", v["a"].ToJson().data(), v2.ToJson().data()));
        }

        UNIT_ASSERT(v.Has("a"));
        UNIT_ASSERT(v.Has("q"));
        UNIT_ASSERT(TStringBuf("r") == v["q"]);
        UNIT_ASSERT(v["a"].Has(1));
        UNIT_ASSERT(!v["a"][0].Has("b"));
        UNIT_ASSERT(v["a"][0].Has("d"));
        UNIT_ASSERT(!v["a"][0]["d"].IsArray());
        UNIT_ASSERT(!v["a"][0]["d"].IsDict());
        UNIT_ASSERT(42 == v["a"][0]["d"]);
        UNIT_ASSERT(!v["a"][1].Has("f"));
        UNIT_ASSERT(v["a"][1].Has("g"));
        UNIT_ASSERT(v["a"][1]["g"].IsArray());
        UNIT_ASSERT(!v["a"][1]["g"].Has(1));
        UNIT_ASSERT(3 == v["a"][1]["g"][0]);
    }

    Y_UNIT_TEST(TestMerge1) {
        TStringBuf data = "[ { a : { b : d } } ]";

        NSc::TValue wcopy = NSc::TValue::FromJson(data);

        TStringBuf data1 = "[ { a : { b : c } } ]";

        wcopy.MergeUpdateJson(data1);

        {
            TString json = wcopy.ToJson(true);
            SubstGlobal(json, "\"", "");
            UNIT_ASSERT_VALUES_EQUAL(json, "[{a:{b:c}}]");
        }
    }

    Y_UNIT_TEST(TestMerge2) {
        TStringBuf data = "{ a : { b : c }, q : { x : y } }";

        NSc::TValue wcopy = NSc::TValue::FromJson(data);

        TStringBuf data1 = "{ a : { e : f } }";

        wcopy.MergeUpdateJson(data1);

        {
            TString json = wcopy.ToJson(true);
            SubstGlobal(json, "\"", "");
            UNIT_ASSERT_VALUES_EQUAL(json, "{a:{b:c,e:f},q:{x:y}}");
        }
    }

    Y_UNIT_TEST(TestMerge3) {
        TStringBuf data = "{ g : { x : { a : { b : c }, q : { x : y } }, y : fff } }";

        NSc::TValue wcopy = NSc::TValue::FromJson(data);

        TStringBuf data1 = "{ g : { x : { a : { e : f } } } }";

        wcopy.MergeUpdateJson(data1);

        {
            TString json = wcopy.ToJson(true);
            SubstGlobal(json, "\"", "");
            UNIT_ASSERT_VALUES_EQUAL(json, "{g:{x:{a:{b:c,e:f},q:{x:y}},y:fff}}");
        }
    }

    Y_UNIT_TEST(TestMerge4) {
        TStringBuf data = "{ a : 1, b : { c : 2, d : { q : f } } }";
        NSc::TValue val = NSc::TValue::FromJson(data);

        TStringBuf data1 = "{ a : 2, b : { c : 3, d : { q : e }, g : h } }";

        val.MergeUpdateJson(data1);

        {
            TString json = val.ToJson(true);
            SubstGlobal(json, "\"", "");

            UNIT_ASSERT_VALUES_EQUAL(json, "{a:2,b:{c:3,d:{q:e},g:h}}");
        }
    }

    Y_UNIT_TEST(TestMerge5) {
        NSc::TValue v0;
        v0.GetOrAdd("x").MergeUpdate(NSc::TValue(1));
        UNIT_ASSERT_VALUES_EQUAL(v0.ToJson(), "{\"x\":1}");
    }

    Y_UNIT_TEST(TestMerge6) {
        NSc::TValue va = NSc::TValue::FromJson("{\"x\":\"abc\",\"y\":\"def\"}");
        NSc::TValue vb = va.Get("y");
        NSc::TValue diff;
        diff["y"] = vb;
        va.MergeUpdate(diff);
        UNIT_ASSERT_VALUES_EQUAL(va.ToJson(), "{\"x\":\"abc\",\"y\":\"def\"}");
        UNIT_ASSERT_VALUES_EQUAL(vb.ToJson(), "\"def\"");
        UNIT_ASSERT_VALUES_EQUAL(diff.ToJson(), "{\"y\":\"def\"}");
    }

    Y_UNIT_TEST(TestMerge7) {
        NSc::TValue v;
        v["a"] = NSc::TValue::FromJson("[0.125,0.12,0.1,0.08,0.06]");
        UNIT_ASSERT_JSON_EQ_JSON(v, "{a:[0.125,0.12,0.1,0.08,0.06]}");

        NSc::TValue a = v.TrySelectOrAdd("a")->MergeUpdateJson("[1,2,3]");

        UNIT_ASSERT_JSON_EQ_JSON(a, "[1,2,3]");
        UNIT_ASSERT_JSON_EQ_JSON(v, "{a:[1,2,3]}");
    }
};