Merge branch '19-add-junit-testsuite' into 'master'
Resolve "Add JUnit Testsuite" Closes #19 See merge request mkais001/klang!19
This commit is contained in:
@@ -9,7 +9,18 @@ build:
|
||||
script:
|
||||
- mvn package
|
||||
|
||||
test:
|
||||
test_parsing:
|
||||
image: ubuntu:rolling
|
||||
stage: test
|
||||
tags:
|
||||
- docker
|
||||
before_script:
|
||||
- apt update
|
||||
- apt install -y build-essential openjdk-13-jre-headless maven
|
||||
script:
|
||||
- make testJava
|
||||
|
||||
test_compilation:
|
||||
image: ubuntu:rolling
|
||||
stage: test
|
||||
tags:
|
||||
|
||||
1
.mvn/jvm.config
Normal file
1
.mvn/jvm.config
Normal file
@@ -0,0 +1 @@
|
||||
--add-opens java.base/java.lang=ALL-UNNAMED
|
||||
@@ -1,4 +1,5 @@
|
||||
eclipse.preferences.version=1
|
||||
encoding//src/main/java=UTF-8
|
||||
encoding//src/test/java=UTF-8
|
||||
encoding//target/generated-sources/antlr4=UTF-8
|
||||
encoding/<project>=UTF-8
|
||||
|
||||
5
makefile
5
makefile
@@ -13,10 +13,13 @@ eval: code.k target/klang-1.0-jar-with-dependencies.jar
|
||||
build: clean target/klang-1.0-jar-with-dependencies.jar
|
||||
|
||||
target/klang-1.0-jar-with-dependencies.jar:
|
||||
mvn package
|
||||
mvn -Dmaven.test.skip=true package
|
||||
|
||||
test: ./src/test/test
|
||||
./src/test/test
|
||||
|
||||
testJava:
|
||||
mvn test
|
||||
|
||||
./src/test/test: ./src/test/test.s
|
||||
gcc -o ./src/test/test ./src/test/test.s ./src/test/**/*.c ./src/test/test.c
|
||||
|
||||
21
pom.xml
21
pom.xml
@@ -1,5 +1,4 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>de.hsrm.compiler</groupId>
|
||||
@@ -21,6 +20,16 @@
|
||||
<artifactId>antlr4-runtime</artifactId>
|
||||
<version>4.7.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>5.6.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>5.6.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -74,6 +83,14 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-failsafe-plugin</artifactId>
|
||||
<version>2.22.2</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
20
src/test/java/AndTest.java
Normal file
20
src/test/java/AndTest.java
Normal file
@@ -0,0 +1,20 @@
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import de.hsrm.compiler.Klang.ContextAnalysis;
|
||||
|
||||
public class AndTest {
|
||||
@Test
|
||||
void onlyForBool() {
|
||||
ParseTree tree = Helper.prepareParser("function foo(): bool { return 1 && 2; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:30 && is only defined for bool.", e.getMessage());
|
||||
}
|
||||
}
|
||||
43
src/test/java/ConstructorCallTest.java
Normal file
43
src/test/java/ConstructorCallTest.java
Normal file
@@ -0,0 +1,43 @@
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import de.hsrm.compiler.Klang.ContextAnalysis;
|
||||
|
||||
public class ConstructorCallTest {
|
||||
|
||||
@Test
|
||||
void structNotDefined() {
|
||||
ParseTree tree = Helper.prepareParser("struct bar { a: int; } function foo(): bar { return create schwurbel(1); } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:52 Struct with name \"schwurbel\" not defined.", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void numConstructorParameterMissmatch() {
|
||||
ParseTree tree = Helper.prepareParser("struct bar { a: int; } function foo(): bar { return create bar(1, false); } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:52 Struct \"bar\" defined 1 fields, but got 2 constructor parameters.", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructorParameterTypeMismatch() {
|
||||
ParseTree tree = Helper.prepareParser("struct bar { a: int; } function foo(): bar { return create bar(false); } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:63 argument 0 Type missmatch: cannot combine bool and int", e.getMessage());
|
||||
}
|
||||
}
|
||||
21
src/test/java/DestroyStatementTest.java
Normal file
21
src/test/java/DestroyStatementTest.java
Normal file
@@ -0,0 +1,21 @@
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import de.hsrm.compiler.Klang.ContextAnalysis;
|
||||
|
||||
|
||||
public class DestroyStatementTest {
|
||||
@Test
|
||||
void variableNotDefined() {
|
||||
ParseTree tree = Helper.prepareParser("struct bar { a: int; } function foo(): int { destroy x; return 1; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:45 Variable with name \"x\" not defined.", e.getMessage());
|
||||
}
|
||||
}
|
||||
32
src/test/java/FieldAssignmentTest.java
Normal file
32
src/test/java/FieldAssignmentTest.java
Normal file
@@ -0,0 +1,32 @@
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import de.hsrm.compiler.Klang.ContextAnalysis;
|
||||
|
||||
public class FieldAssignmentTest {
|
||||
|
||||
@Test
|
||||
void variableNotDefined() {
|
||||
ParseTree tree = Helper.prepareParser("struct test { a: int; } function foo(): int { str.a = 1; return 1; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:46 Variable with name str not defined.", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void fieldAssignmentOnNonStruct() {
|
||||
ParseTree tree = Helper.prepareParser("struct test { a: int; } function foo(): int { let x: int = 0; x.a = 0; return 1; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:62 Variable must reference a struct but references int.", e.getMessage());
|
||||
}
|
||||
}
|
||||
43
src/test/java/FunctionCallTest.java
Normal file
43
src/test/java/FunctionCallTest.java
Normal file
@@ -0,0 +1,43 @@
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import de.hsrm.compiler.Klang.ContextAnalysis;
|
||||
|
||||
public class FunctionCallTest {
|
||||
|
||||
@Test
|
||||
void funcNotDefined() {
|
||||
ParseTree tree = Helper.prepareParser("function foo(): int { return 1; } bar();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:34 Function with name \"bar\" not defined.", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void numParameterMismatch() {
|
||||
ParseTree tree = Helper.prepareParser("function foo(): int { return 1; } foo(5);");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:34 Function \"foo\" expects 0 parameters, but got 1.", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parameterTypeMissmatch() {
|
||||
ParseTree tree = Helper.prepareParser("function foo(x: int): int { return x; } foo(false);");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:40 argument 0 Expected int but got: bool", e.getMessage());
|
||||
}
|
||||
}
|
||||
32
src/test/java/FunctionDefinitionTest.java
Normal file
32
src/test/java/FunctionDefinitionTest.java
Normal file
@@ -0,0 +1,32 @@
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import de.hsrm.compiler.Klang.ContextAnalysis;
|
||||
|
||||
public class FunctionDefinitionTest {
|
||||
|
||||
@Test
|
||||
void typeNotDefined() {
|
||||
ParseTree tree = Helper.prepareParser("function foo(): schwurbel { return 1; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:0 Type schwurbel not defined.", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void noReturnExpression() {
|
||||
ParseTree tree = Helper.prepareParser("function foo(): int { let x: int; x = 0; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:0 Function foo has to return something of type int.", e.getMessage());
|
||||
}
|
||||
}
|
||||
36
src/test/java/Helper.java
Normal file
36
src/test/java/Helper.java
Normal file
@@ -0,0 +1,36 @@
|
||||
import java.util.*;
|
||||
|
||||
import org.antlr.v4.runtime.*;
|
||||
import org.antlr.v4.runtime.tree.*;
|
||||
|
||||
import de.hsrm.compiler.Klang.*;
|
||||
import de.hsrm.compiler.Klang.helper.*;
|
||||
import de.hsrm.compiler.Klang.nodes.*;
|
||||
|
||||
public class Helper {
|
||||
public static ParseTree prepareParser(String input) {
|
||||
CharStream inStream = CharStreams.fromString(input);
|
||||
KlangLexer lexer = new KlangLexer(inStream);
|
||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||
KlangParser parser = new KlangParser(tokens);
|
||||
return parser.parse();
|
||||
}
|
||||
|
||||
public static Map<String, FunctionInformation> getFuncs(ParseTree tree) {
|
||||
var functionDefinitions = new HashMap<String, FunctionInformation>();
|
||||
new GetFunctions(functionDefinitions).visit(tree);
|
||||
return functionDefinitions;
|
||||
}
|
||||
|
||||
public static Set<String> getStructNames(ParseTree tree) {
|
||||
var structNames = new HashSet<String>();
|
||||
new GetStructNames(structNames).visit(tree);
|
||||
return structNames;
|
||||
}
|
||||
|
||||
public static Map<String, StructDefinition> getStructs(ParseTree tree) {
|
||||
var structs = new HashMap<String, StructDefinition>();
|
||||
new GetStructs(getStructNames(tree), structs).visit(tree);
|
||||
return structs;
|
||||
}
|
||||
}
|
||||
20
src/test/java/ModuloTest.java
Normal file
20
src/test/java/ModuloTest.java
Normal file
@@ -0,0 +1,20 @@
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import de.hsrm.compiler.Klang.ContextAnalysis;
|
||||
|
||||
public class ModuloTest {
|
||||
@Test
|
||||
void onlyForInt() {
|
||||
ParseTree tree = Helper.prepareParser("function foo(): float { return 1.0 % 2.3; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:31 Only integers are allowed for modulo.", e.getMessage());
|
||||
}
|
||||
}
|
||||
21
src/test/java/OrTest.java
Normal file
21
src/test/java/OrTest.java
Normal file
@@ -0,0 +1,21 @@
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import de.hsrm.compiler.Klang.ContextAnalysis;
|
||||
|
||||
public class OrTest {
|
||||
|
||||
@Test
|
||||
void onlyForBool() {
|
||||
ParseTree tree = Helper.prepareParser("function foo(): bool { return 1 || 2; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:30 || is only defined for bool.", e.getMessage());
|
||||
}
|
||||
}
|
||||
14
src/test/java/ParameterTest.java
Normal file
14
src/test/java/ParameterTest.java
Normal file
@@ -0,0 +1,14 @@
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ParameterTest {
|
||||
@Test
|
||||
void typeNotDefined() {
|
||||
ParseTree tree = Helper.prepareParser("struct test { a: schwurbel; } function foo(): int { return 1; } foo();");
|
||||
Exception e = assertThrows(RuntimeException.class, () -> Helper.getStructs(tree));
|
||||
assertEquals("Error in line 1:14 Type schwurbel not defined.", e.getMessage());
|
||||
}
|
||||
}
|
||||
31
src/test/java/StructFieldAccessTest.java
Normal file
31
src/test/java/StructFieldAccessTest.java
Normal file
@@ -0,0 +1,31 @@
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import de.hsrm.compiler.Klang.ContextAnalysis;
|
||||
|
||||
public class StructFieldAccessTest {
|
||||
@Test
|
||||
void variableNotDefined() {
|
||||
ParseTree tree = Helper.prepareParser("struct test { a: int; } function foo(): int { return str.a; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:53 Variable with name str not defined.", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void fieldAssignmentOnNonStruct() {
|
||||
ParseTree tree = Helper.prepareParser("struct test { a: int; } function foo(): int { let x: int = 0; return x.a; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:69 Variable must reference a struct but references int.", e.getMessage());
|
||||
}
|
||||
}
|
||||
21
src/test/java/VariableAssignmentTest.java
Normal file
21
src/test/java/VariableAssignmentTest.java
Normal file
@@ -0,0 +1,21 @@
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import de.hsrm.compiler.Klang.ContextAnalysis;
|
||||
|
||||
public class VariableAssignmentTest {
|
||||
|
||||
@Test
|
||||
void variableNotDefined() {
|
||||
ParseTree tree = Helper.prepareParser("function foo(): int { x = 1; return 1; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:22 Variable with name \"x\" not defined.", e.getMessage());
|
||||
}
|
||||
}
|
||||
32
src/test/java/VariableDeclarationTest.java
Normal file
32
src/test/java/VariableDeclarationTest.java
Normal file
@@ -0,0 +1,32 @@
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import de.hsrm.compiler.Klang.ContextAnalysis;
|
||||
|
||||
public class VariableDeclarationTest {
|
||||
@Test
|
||||
void typeNotDefined() {
|
||||
ParseTree tree = Helper.prepareParser("function foo(): int { let X: unk; return 1; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:22 Type unk not defined.", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void variableRedeclaration()
|
||||
{
|
||||
ParseTree tree = Helper.prepareParser("function foo(): int { let x: int; let x: bool; return 1; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:34 Redeclaration of variable with name \"x\".", e.getMessage());
|
||||
}
|
||||
}
|
||||
32
src/test/java/VariableTest.java
Normal file
32
src/test/java/VariableTest.java
Normal file
@@ -0,0 +1,32 @@
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import de.hsrm.compiler.Klang.ContextAnalysis;
|
||||
|
||||
public class VariableTest {
|
||||
|
||||
@Test
|
||||
void variableNotDefined() {
|
||||
ParseTree tree = Helper.prepareParser("function foo(): int { return x; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:29 Variable with name \"x\" not defined.", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void variableNotInitialized() {
|
||||
ParseTree tree = Helper.prepareParser("function foo(): int { let x: int; return x; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:41 Variable with name \"x\" has not been initialized.", e.getMessage());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user