aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/protobuf/json/ut/string_transform_ut.cpp
blob: 3a4df5c022754c819dc7e835367872efa3931d41 (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
#include "json.h"

#include <library/cpp/testing/unittest/registar.h>
#include <library/cpp/protobuf/json/proto2json.h>

Y_UNIT_TEST_SUITE(TDoubleEscapeTransform) {
    Y_UNIT_TEST(TestEmptyString) {
        const NProtobufJson::IStringTransform& transform = NProtobufJson::TDoubleEscapeTransform();
        TString s;
        s = "";
        transform.Transform(s);
        UNIT_ASSERT_EQUAL(s, "");
    }

    Y_UNIT_TEST(TestAlphabeticString) {
        const NProtobufJson::IStringTransform& transform = NProtobufJson::TDoubleEscapeTransform();
        TString s;
        s = "abacaba";
        transform.Transform(s);
        UNIT_ASSERT_EQUAL(s, "abacaba");
    }

    Y_UNIT_TEST(TestRussianSymbols) {
        const NProtobufJson::IStringTransform& transform = NProtobufJson::TDoubleEscapeTransform();
        TString s;
        s = "тест";
        transform.Transform(s);
        UNIT_ASSERT_EQUAL(s, "\\\\321\\\\202\\\\320\\\\265\\\\321\\\\201\\\\321\\\\202");
    }

    Y_UNIT_TEST(TestEscapeSpecialSymbols) {
        const NProtobufJson::IStringTransform& transform = NProtobufJson::TDoubleEscapeTransform();
        TString s;
        s = "aba\\ca\"ba";
        transform.Transform(s);
        UNIT_ASSERT_EQUAL(s, "aba\\\\\\\\ca\\\\\\\"ba");
    }
}

Y_UNIT_TEST_SUITE(TDoubleUnescapeTransform) {
    Y_UNIT_TEST(TestEmptyString) {
        const NProtobufJson::IStringTransform& transform = NProtobufJson::TDoubleUnescapeTransform();
        TString s;
        s = "";
        transform.Transform(s);
        UNIT_ASSERT_EQUAL("", s);
    }

    Y_UNIT_TEST(TestAlphabeticString) {
        const NProtobufJson::IStringTransform& transform = NProtobufJson::TDoubleUnescapeTransform();
        TString s;
        s = "abacaba";
        transform.Transform(s);
        UNIT_ASSERT_EQUAL("abacaba", s);
    }

    Y_UNIT_TEST(TestRussianSymbols) {
        const NProtobufJson::IStringTransform& transform = NProtobufJson::TDoubleUnescapeTransform();
        TString s;
        s = "\\\\321\\\\202\\\\320\\\\265\\\\321\\\\201\\\\321\\\\202";
        transform.Transform(s);
        UNIT_ASSERT_EQUAL("тест", s);
    }

    Y_UNIT_TEST(TestEscapeSpecialSymbols) {
        const NProtobufJson::IStringTransform& transform = NProtobufJson::TDoubleUnescapeTransform();
        TString s;
        s = "aba\\\\\\\\ca\\\\\\\"ba";
        transform.Transform(s);
        UNIT_ASSERT_EQUAL("aba\\ca\"ba", s);
    }

    Y_UNIT_TEST(TestEscapeSpecialSymbolsDifficultCases) {
        const NProtobufJson::IStringTransform& transform = NProtobufJson::TDoubleUnescapeTransform();
        TString s;
        s = "\\\\\\\\\\\\\\\\";
        transform.Transform(s);
        UNIT_ASSERT_EQUAL("\\\\", s);

        s = "\\\\\\\\\\\\\\\"";
        transform.Transform(s);
        UNIT_ASSERT_EQUAL("\\\"", s);

        s = "\\\\\\\"\\\\\\\\";
        transform.Transform(s);
        UNIT_ASSERT_EQUAL("\"\\", s);

        s = "\\\\\\\"\\\\\\\"";
        transform.Transform(s);
        UNIT_ASSERT_EQUAL("\"\"", s);

        s = "\\\\\\\\\\\\\\\\\\\\\\\\";
        transform.Transform(s);
        UNIT_ASSERT_EQUAL("\\\\\\", s);

        s = "\\\\\\\\\\\\\\\\\\\\\\\\abacaba\\\\";
        transform.Transform(s);
        UNIT_ASSERT_EQUAL("\\\\\\abacaba", s);

        s = "\\\\\\\\\\\\\\\\\\\\\\\\abacaba\\\"";
        transform.Transform(s);
        UNIT_ASSERT_EQUAL("\\\\\\abacaba\"", s);
    }
}