summaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/no/eliashaugsbakk/kompilator/parsing/ParserTest.java6
-rw-r--r--src/test/java/no/eliashaugsbakk/kompilator/semanticAnalysis/AnalyzerTest.java48
2 files changed, 51 insertions, 3 deletions
diff --git a/src/test/java/no/eliashaugsbakk/kompilator/parsing/ParserTest.java b/src/test/java/no/eliashaugsbakk/kompilator/parsing/ParserTest.java
index 133e9a6..ac31dfc 100644
--- a/src/test/java/no/eliashaugsbakk/kompilator/parsing/ParserTest.java
+++ b/src/test/java/no/eliashaugsbakk/kompilator/parsing/ParserTest.java
@@ -30,9 +30,9 @@ class ParserTest {
AST ast = new Parser(tokens).parse();
// Verify tree structure
- assertNotNull(ast.root);
- assertInstanceOf(Program.class, ast.root);
- Program program = (Program) ast.root;
+ assertNotNull(ast.getRoot());
+ assertInstanceOf(Program.class, ast.getRoot());
+ Program program = (Program) ast.getRoot();
assertEquals(1, program.statements.size());
ExpressionStatement stmt = (ExpressionStatement) program.statements.getFirst();
diff --git a/src/test/java/no/eliashaugsbakk/kompilator/semanticAnalysis/AnalyzerTest.java b/src/test/java/no/eliashaugsbakk/kompilator/semanticAnalysis/AnalyzerTest.java
new file mode 100644
index 0000000..531673d
--- /dev/null
+++ b/src/test/java/no/eliashaugsbakk/kompilator/semanticAnalysis/AnalyzerTest.java
@@ -0,0 +1,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);
+ }
+}