aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/shedlang/compiler/parser/ParserPrecedenceTests.java
blob: 471b46fe0477b1746d75258def151626aadbc931 (plain)
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
package org.shedlang.compiler.parser;

import org.junit.jupiter.api.Test;

import static org.shedlang.compiler.ast.untyped.UntypedNodeMatchers.*;
import static org.shedlang.compiler.parser.ParserTesting.parseString;
import static org.zwobble.precisely.AssertThat.assertThat;

public class ParserPrecedenceTests {
    @Test
    public void callHasHigherPrecedenceThanAddition() {
        var source = "1 + f()";

        var node = parseString(source, Parser::parseTopLevelExpression);

        assertThat(
            node,
            isUntypedAddNode(
                isUntypedIntLiteralNode(1),
                isUntypedCallNode()
                    .withReceiver(isUntypedReferenceNode("f"))
            )
        );
    }
    @Test
    public void subtractionIsLeftAssociative() {
        var source = "1 - 2 - 3";

        var node = parseString(source, Parser::parseTopLevelExpression);

        assertThat(
            node,
            isUntypedSubNode(
                isUntypedSubNode(
                    isUntypedIntLiteralNode(1),
                    isUntypedIntLiteralNode(2)
                ),
                isUntypedIntLiteralNode(3)
            )
        );
    }
}