aboutsummaryrefslogtreecommitdiffstats
path: root/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/parser/ScriptTestCase.java
blob: 164687c239cdbb6ebb78a1342bf370684551c4b5 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.indexinglanguage.parser;

import com.yahoo.vespa.indexinglanguage.expressions.ConstantExpression;
import com.yahoo.vespa.indexinglanguage.expressions.ScriptExpression;
import com.yahoo.vespa.indexinglanguage.expressions.StatementExpression;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

/**
 * @author Simon Thoresen Hult
 */
@SuppressWarnings({ "rawtypes" })
public class ScriptTestCase {

    @Test
    public void requireThatRootProductionIsFlexible() throws ParseException {
        assertRoot(ConstantExpression.class, "1");
        assertRoot(StatementExpression.class, "1 | echo");
        assertRoot(StatementExpression.class, "{ 1 | echo }");
        assertRoot(StatementExpression.class, "{ 1 | echo; }");
        assertRoot(ScriptExpression.class, "{ 1 | echo; 2 | echo }");
    }

    @Test
    public void requireThatNewlineIsAllowedAfterStatement() throws ParseException {
        assertScript("{ 1 }");
        assertScript("{\n 1 }");
        assertScript("{ 1\n }");
        assertScript("{\n 1\n }");

        assertScript("{ 1; }");
        assertScript("{\n 1; }");
        assertScript("{ 1;\n }");
        assertScript("{\n 1;\n }");

        assertScript("{ 1; 2 }");
        assertScript("{\n 1; 2 }");
        assertScript("{\n 1;\n 2 }");
        assertScript("{\n 1; 2\n }");
        assertScript("{ 1;\n 2\n }");
        assertScript("{\n 1;\n 2\n }");
    }

    @Test
    public void requireThatNewlineIsAllowedWithinStatement() throws ParseException {
        assertStatement("1 |\n 2");
    }

    private void assertRoot(Class expectedClass, String input) throws ParseException {
        assertEquals(expectedClass, new IndexingParser(input).root().getClass());
    }

    private static void assertScript(String input) throws ParseException {
        assertNotNull(new IndexingParser(input).script());
    }

    private static void assertStatement(String input) throws ParseException {
        assertNotNull(new IndexingParser(input).statement());
    }
}