Implement StructField type checking in ContextAnalysis.
This commit is contained in:
@@ -1,14 +0,0 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
62
src/test/java/StructDefinitionTest.java
Normal file
62
src/test/java/StructDefinitionTest.java
Normal file
@@ -0,0 +1,62 @@
|
||||
import de.hsrm.compiler.Klang.ContextAnalysis;
|
||||
import de.hsrm.compiler.Klang.GetDefinitions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class StructDefinitionTest {
|
||||
@Test
|
||||
void shouldNotThrowIfStructIsWellDefined() {
|
||||
// given
|
||||
var tree = Helper.prepareParser("struct test { a: int; } function foo(): int { return 1; } foo();");
|
||||
var ctxAnal = new ContextAnalysis(Helper.getFuncs(tree), Helper.getStructs(tree), Helper.getEnums(tree));
|
||||
|
||||
// when / then
|
||||
assertDoesNotThrow(() -> ctxAnal.visit(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotThrowIfStructFieldTypeIsReferringToEnum() {
|
||||
// given
|
||||
var tree = Helper.prepareParser("struct test { a: bar; } enum bar {A,B,C} function foo(): int { return 1; } foo();");
|
||||
var ctxAnal = new ContextAnalysis(Helper.getFuncs(tree), Helper.getStructs(tree), Helper.getEnums(tree));
|
||||
|
||||
// when / then
|
||||
assertDoesNotThrow(() -> ctxAnal.visit(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldThrowExceptionIfStructFieldTypeIsNotDefined() {
|
||||
// given
|
||||
var tree = Helper.prepareParser("struct test { a: schwurbel; } function foo(): int { return 1; } foo();");
|
||||
var ctxAnal = new ContextAnalysis(Helper.getFuncs(tree), Helper.getStructs(tree), Helper.getEnums(tree));
|
||||
|
||||
// when / then
|
||||
var e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:14 Type schwurbel not defined.", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldThrowExceptionIfStructFieldTypeIsReferringToAFunction() {
|
||||
// given
|
||||
var tree = Helper.prepareParser("struct test { a: foo; } function foo(): int { return 1; } foo();");
|
||||
var ctxAnal = new ContextAnalysis(Helper.getFuncs(tree), Helper.getStructs(tree), Helper.getEnums(tree));
|
||||
|
||||
// when / then
|
||||
var e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:14 Type foo not defined.", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldThrowExceptionIfStructFieldNameIsDuplicated() {
|
||||
// given
|
||||
var tree = Helper.prepareParser("struct test { a: int; a: bool; } function foo(): int { return 1; } foo();");
|
||||
var getDefs = new GetDefinitions(new HashMap<>(), new HashMap<>(), new HashMap<>());
|
||||
|
||||
// when / then
|
||||
var e = assertThrows(RuntimeException.class, () -> getDefs.visit(tree));
|
||||
assertEquals("Error in line 1:22 Duplicate struct field a in struct test.", e.getMessage());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user