aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/text/ExpressionFormatterTest.java
blob: 54cff243e24ee4f8a3d2ab9a53825659e14e8979 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.text;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 * @author bratseth
 */
public class ExpressionFormatterTest {

    @Test
    public void testBasic() {
        String expected =
                "foo(\n" +
                "  bar(\n" +
                "    baz(\n" +
                "    )\n" +
                "  )\n" +
                ")";
        assertPrettyPrint(expected, "foo(bar(baz()))", 0);
    }

    @Test
    public void testBasicDense() {
        assertPrettyPrint("foo(bar(baz()))", "foo(bar(baz()))", 50);
    }

    @Test
    public void testArgument() {
        String expected =
                "foo(\n" +
                "  bar(\n" +
                "    baz(\n" +
                "      hello world\n" +
                "    )\n" +
                "  )\n" +
                ")";
        assertPrettyPrint(expected, "foo(bar(baz(hello world)))", 0);
    }

    @Test
    public void testMultipleArguments() {
        String expected =
                "foo(\n" +
                "  bar(\n" +
                "    baz(\n" +
                "      hello world,\n" +
                "      37\n" +
                "    )\n" +
                "  )\n" +
                ")";
        assertPrettyPrint(expected, "foo(bar(baz(hello world,37)))", 0);
    }

    @Test
    public void testMultipleArgumentsSemiDense() {
        String expected =
                "foo(\n" +
                "  bar(\n" +
                "    baz(hi,37),\n" +
                "    baz(\n" +
                "      hello world,\n" +
                "      37\n" +
                "    )\n" +
                "  )\n" +
                ")";
        assertPrettyPrint(expected, "foo(bar(baz(hi,37),baz(hello world,37)))", 15);
    }

    @Test
    public void testUnmatchedStart() {
        String expected =
                "foo(\n" +
                "  (\n" +
                "    bar(\n" +
                "      baz(\n" +
                "      )\n" +
                "    )\n" +
                "  )";
        assertPrettyPrint(expected, "foo((bar(baz()))", 0);
    }

    @Test
    public void testUnmatchedEnd() {
        String expected =
                "foo(\n" +
                "  bar(\n" +
                "    baz(\n" +
                "    )\n" +
                "  )\n" +
                ")\n" +
                ")";
        assertPrettyPrint(expected, "foo(bar(baz())))", 0);
    }

    @Test
    public void testNoParenthesis() {
        String expected =
                "foo bar baz";
        assertPrettyPrint(expected, "foo bar baz", 0);
    }

    @Test
    public void testEmpty() {
        String expected =
                "";
        assertPrettyPrint(expected, "", 0);
    }

    @Test
    public void test2ColumnMode() {
        String expected =
                "1:  foo(\n" +
                "      bar(\n" +
                "        baz(\n" +
                "2:        hello world\n" +
                "        )\n" +
                "t(o   )\n" +
                "    )";
        ExpressionFormatter pp = ExpressionFormatter.inTwoColumnMode(3, 0);
        assertEquals(expected, pp.format("\t1:\tfoo(bar(baz(\t2:\thello world)\tt(o)@olong:\t))"));
    }

    @Test
    public void test2ColumnModeMultipleArguments() {
        String expected =
                "1:  foo(\n" +
                "      bar(\n" +
                "        baz(\n" +
                "2:        hello world,\n" +
                "3:        37\n" +
                "        )\n" +
                "t(o   )\n" +
                "    )";
        ExpressionFormatter pp = ExpressionFormatter.inTwoColumnMode(3, 0);
        assertEquals(expected, pp.format("\t1:\tfoo(bar(baz(\t2:\thello world,\t3:\t37)\tt(o)@olong:\t))"));
    }

    @Test
    public void test2ColumnModeMultipleArgumentsSemiDense() {
        String expected =
                "1:  foo(\n" +
                "      bar(\n" +
                "        baz(hi,37),\n" +
                "        boz(\n" +
                "2:        hello world,\n" +
                "3:        5\n" +
                "        )\n" +
                "t(o   )\n" +
                "    )";
        ExpressionFormatter pp = ExpressionFormatter.inTwoColumnMode(3, 15);
        assertEquals(expected, pp.format("\t1:\tfoo(bar(baz(hi,37),boz(\t2:\thello world,\t3:\t5)\tt(o)@olong:\t))"));
    }

    @Test
    public void test2ColumnModeMultipleArgumentsWithSpaces() {
        String expected =
                "    foo(\n" +
                "1:    bar(\n" +
                "        baz(\n" +
                "2:        hello world,\n" +
                "3:        37\n" +
                "        )\n" +
                "t(o   )\n" +
                "    )";
        ExpressionFormatter pp = ExpressionFormatter.inTwoColumnMode(3, 0);
        assertEquals(expected, pp.format("foo(\t1:\tbar(baz(\t2:\thello world, \t3:\t37)\tt(o)@olong:\t))"));
    }

    @Test
    public void testTwoColumnLambdaFunction() {
        String expected =
                "      join(\n" +
                "        a,\n" +
                "        join(\n" +
                "          b, c, f(a, b)(a * b)\n" +
                "        )\n" +
                "        , f(a, b)(a * b)\n" +
                "      )";
        ExpressionFormatter pp = ExpressionFormatter.inTwoColumnMode(5, 25);
        assertEquals(expected, pp.format("join(a, join(b, c, f(a, b)(a * b)), f(a, b)(a * b))"));
    }

    private void assertPrettyPrint(String expected, String expression, int lineLength) {
        assertEquals(expected, ExpressionFormatter.withLineLength(lineLength).format(expression));
    }

}