summaryrefslogtreecommitdiff
path: root/src/test/java/no/eliashaugsbakk/kompilator/semanticAnalysis/AnalyzerTest.java
blob: 531673dd40aafc3fb36a15d937c36ecfcd67fae1 (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
43
44
45
46
47
48
package no.eliashaugsbakk.kompilator.semanticAnalysis;

import static org.junit.jupiter.api.Assertions.*;

import java.util.List;
import no.eliashaugsbakk.kompilator.parsing.AST;
import no.eliashaugsbakk.kompilator.parsing.node.Program;
import no.eliashaugsbakk.kompilator.parsing.node.expression.FunctionCall;
import no.eliashaugsbakk.kompilator.parsing.node.expression.StringLiteral;
import no.eliashaugsbakk.kompilator.parsing.node.statement.ExpressionStatement;
import org.junit.jupiter.api.Test;

class AnalyzerTest {

  @Test
  void validPrintStatementDoesNotThrow() {
    AST ast = buildAST("print", "hello");
    assertDoesNotThrow(() -> new Analyzer(ast).analyze());
  }

  @Test
  void unknownFunctionThrows() {
    AST ast = buildAST("unknown", "hello");
    assertThrows(SemanticException.class, () -> new Analyzer(ast).analyze());
  }

  @Test
  void missingArgumentThrows() {
    AST ast = buildASTNoArgs("print");
    assertThrows(SemanticException.class, () -> new Analyzer(ast).analyze());
  }

  private AST buildAST(String functionName, String argument) {
    FunctionCall call = new FunctionCall(functionName, List.of(new StringLiteral(argument)));
    ExpressionStatement stmt = new ExpressionStatement(call);
    Program program = new Program();
    program.addStatement(stmt);
    return new AST(program);
  }

  private AST buildASTNoArgs(String functionName) {
    FunctionCall call = new FunctionCall(functionName, List.of());
    ExpressionStatement stmt = new ExpressionStatement(call);
    Program program = new Program();
    program.addStatement(stmt);
    return new AST(program);
  }
}