summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/text/ExpressionFormatterTest.java
blob: 6dfb2e6fc8a6c6cda776263926732840008e3251 (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
// Copyright 2019 Oath Inc. 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" +
                ")\n";
        assertPrettyPrint(expected, "foo(bar(baz()))");
    }

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

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

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

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

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

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

    @Test
    public void test2ColumnMode() {
        String expected =
                "1:  foo(\n" +
                "      bar(\n" +
                "        baz(\n" +
                "2:        hello world\n" +
                "        )\n" +
                "t(o   )\n" +
                "    )\n";
        ExpressionFormatter pp = ExpressionFormatter.inTwoColumnMode(3);
        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" +
                "    )\n";
        ExpressionFormatter pp = ExpressionFormatter.inTwoColumnMode(3);
        assertEquals(expected, pp.format("\t1:\tfoo(bar(baz(\t2:\thello world,\t3:\t37)\tt(o)@olong:\t))"));
    }

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

    private void assertPrettyPrint(String expected, String expression) {
        assertEquals(expected, ExpressionFormatter.on(expression));
    }

}