aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/getopt/ut/wrap.cpp
blob: 3444eea10232dad0dd21d2a540176f5e263a318a (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
#include <library/cpp/testing/unittest/registar.h>

#include <library/cpp/getopt/small/wrap.h>

Y_UNIT_TEST_SUITE(Wrap) {
    Y_UNIT_TEST(TestWrapping) {
        UNIT_ASSERT_STRINGS_EQUAL(
            NLastGetopt::Wrap(5, "a b c d eeffeff").Quote(),
            TString("a b c\nd\neeffeff").Quote()
        );

        UNIT_ASSERT_STRINGS_EQUAL(
            NLastGetopt::Wrap(5, "a b\nc d\neeffeff").Quote(),
            TString("a b\nc d\neeffeff").Quote()
        );

        UNIT_ASSERT_STRINGS_EQUAL(
            NLastGetopt::Wrap(5, "a b\n     c d\neeffeff").Quote(),
            TString("a b\n     c\nd\neeffeff").Quote()
        );

        UNIT_ASSERT_STRINGS_EQUAL(
            NLastGetopt::Wrap(5, "a b\nx     c d\neeffeff").Quote(),
            TString("a b\nx\nc d\neeffeff").Quote()
        );

        UNIT_ASSERT_STRINGS_EQUAL(
            NLastGetopt::Wrap(5, "a b\nx  \n   c d\neeffeff").Quote(),
            TString("a b\nx\n   c\nd\neeffeff").Quote()
        );

        UNIT_ASSERT_STRINGS_EQUAL(
            NLastGetopt::Wrap(5, "a b\nx      \n   c d\neeffeff").Quote(),
            TString("a b\nx\n   c\nd\neeffeff").Quote()
        );
    }

    Y_UNIT_TEST(TestWrappingIndent) {
        UNIT_ASSERT_STRINGS_EQUAL(
            NLastGetopt::Wrap(5, "a b c d", "|>").Quote(),
            TString("a b\n|>c d").Quote()
        );

        UNIT_ASSERT_STRINGS_EQUAL(
            NLastGetopt::Wrap(5, "a b\n\nc d", "|>").Quote(),
            TString("a b\n|>\n|>c d").Quote()
        );
    }

    Y_UNIT_TEST(TestWrappingAnsi) {
        UNIT_ASSERT_STRINGS_EQUAL(
            NLastGetopt::Wrap(5, "\033[1;2;3;4;5mx\033[1;2;3;4;5mx\033[1;2;3;4;5mx\033[1;2;3;4;5mx\033[1;2;3;4;5m").Quote(),
            TString("\033[1;2;3;4;5mx\033[1;2;3;4;5mx\033[1;2;3;4;5mx\033[1;2;3;4;5mx\033[1;2;3;4;5m").Quote()
        );

        UNIT_ASSERT_STRINGS_EQUAL(
            NLastGetopt::Wrap(5, "a \033[1;2;3;4;5mb c\033[1;2;3;4;5m \033[1;2;3;4;5md e f").Quote(),
            TString("a \033[1;2;3;4;5mb c\033[1;2;3;4;5m\n\033[1;2;3;4;5md e f").Quote()
        );

        UNIT_ASSERT_STRINGS_EQUAL(
            NLastGetopt::Wrap(5, "a b  \033[1;2;3;4;5m  c d").Quote(),
            TString("a b  \033[1;2;3;4;5m\nc d").Quote()
        );

        UNIT_ASSERT_STRINGS_EQUAL(
            NLastGetopt::Wrap(5, "a b       \033[1;2;3;4;5m  c d").Quote(),
            TString("a b\n\033[1;2;3;4;5m  c d").Quote()
        );
    }

    Y_UNIT_TEST(TestTextInfo) {
        size_t lastLineLen;
        bool hasParagraphs;

        NLastGetopt::Wrap(5, "a b c d e", "", &lastLineLen, &hasParagraphs);
        UNIT_ASSERT_VALUES_EQUAL(lastLineLen, 3);
        UNIT_ASSERT_VALUES_EQUAL(hasParagraphs, false);

        NLastGetopt::Wrap(5, "a b c\n\nd e f h", "", &lastLineLen, &hasParagraphs);
        UNIT_ASSERT_VALUES_EQUAL(lastLineLen, 1);
        UNIT_ASSERT_VALUES_EQUAL(hasParagraphs, true);

        NLastGetopt::Wrap(5, "a b c\n\n", "", &lastLineLen, &hasParagraphs);
        UNIT_ASSERT_VALUES_EQUAL(lastLineLen, 0);
        UNIT_ASSERT_VALUES_EQUAL(hasParagraphs, true);

        NLastGetopt::Wrap(5, "\n \na b c", "", &lastLineLen, &hasParagraphs);
        UNIT_ASSERT_VALUES_EQUAL(lastLineLen, 5);
        UNIT_ASSERT_VALUES_EQUAL(hasParagraphs, true);

        NLastGetopt::Wrap(5, "\nx\na b c", "", &lastLineLen, &hasParagraphs);
        UNIT_ASSERT_VALUES_EQUAL(lastLineLen, 5);
        UNIT_ASSERT_VALUES_EQUAL(hasParagraphs, false);
    }
}