Skip to content

Commit

Permalink
Add additional unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Panagiotis Kourouklidis committed Feb 1, 2024
1 parent 49ac46d commit b60938c
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 49 deletions.
Empty file.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,103 @@
import org.junit.Test;

public class EolStaticAnalyserTests {
static int ident = 0;
static int ident = 0;

@Test
public void testModelTypesVariableDeclaration() throws Exception {
EolModule module = new EolModule();
module.parse(EolStaticAnalyserTests.class.getResource("variableDeclaration.eol").toURI());
public void testModelElementTypeVariableDeclaration() throws Exception {
EPackage.Registry.INSTANCE.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);

printTree(module);
StringBuffer st = new StringBuffer();
st.append("model M driver EMF {nsuri=\"http://www.eclipse.org/emf/2002/Ecore\"};");
st.append("var i:M!EClass;");
assertValid(st.toString());
}

@Test
public void testModelElementTypeVariableDeclarationError() throws Exception {
EPackage.Registry.INSTANCE.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
EolStaticAnalyser staticAnalyser = new EolStaticAnalyser(new StaticModelFactory());
List<ModuleMarker> errors = staticAnalyser.validate(module);
System.out.println(errors.get(0).getMessage());
assertEquals(1, errors.size());

StringBuffer st = new StringBuffer();
st.append("model M driver EMF {nsuri=\"http://www.eclipse.org/emf/2002/Ecore\"};");
st.append("var i:M!EClss;");
assertValidMessage(st.toString(), "Unknown type M!EClss");
}

public void printTree(ModuleElement parent) {
System.out.println(" ".repeat(ident) + parent.getClass().getSimpleName());
ident += 1;
for (ModuleElement m : parent.getChildren()) {
printTree(m);
}
ident -= 1;
@Test
public void testModelElementTypeAssignment() throws Exception {
EPackage.Registry.INSTANCE.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);

StringBuffer st = new StringBuffer();
st.append("model M driver EMF {nsuri=\"http://www.eclipse.org/emf/2002/Ecore\"};");
st.append("var i:M!EClass = new M!EClass;");
assertValid(st.toString());
}

@Test
public void testModelElementTypeAssignmentError() throws Exception {
EPackage.Registry.INSTANCE.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);

StringBuffer st = new StringBuffer();
st.append("model M driver EMF {nsuri=\"http://www.eclipse.org/emf/2002/Ecore\"};");
st.append("var i:M!EClass = new M!EPackage;");
assertValidMessage(st.toString(), "M!EPackage cannot be assigned to M!EClass");
}

@Test
public void testModelElementTypePropertyAssignmentError() throws Exception {
EPackage.Registry.INSTANCE.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);

StringBuffer st = new StringBuffer();
st.append("model M driver EMF {nsuri=\"http://www.eclipse.org/emf/2002/Ecore\"};");
st.append("var i:M!EClass = new M!EClass;");
st.append("i.names = \"className\";");
assertValidMessage(st.toString(), "Structural feature names not found in type EClass");
}

@Test
public void testModelAliases() throws Exception {
EPackage.Registry.INSTANCE.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);

StringBuffer st = new StringBuffer();
st.append("model M alias X driver EMF {nsuri=\"http://www.eclipse.org/emf/2002/Ecore\"};");
st.append("var i:X!EClass = new X!EClass;");
st.append("i.name = \"className\";");
assertValid(st.toString());
}

@Test
public void testTuplePropertyError() throws Exception {
StringBuffer st = new StringBuffer();
st.append("var t:Tuple = new Tuple(name = \"bob\");");
st.append("t.names.println();");
assertValidMessage(st.toString(), "Tuple t does not have property names");
}

@Test
public void testUseAfterDelete() throws Exception {
EPackage.Registry.INSTANCE.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);

StringBuffer st = new StringBuffer();
st.append("model M driver EMF {nsuri=\"http://www.eclipse.org/emf/2002/Ecore\"};");
st.append("var i:M!EClass = new M!EClass;");
st.append("delete i;");
st.append("i.name = \"className\";");
assertValidMessage(st.toString(), "Cannot access object i after deletion");
}

@Test
public void testUseAfterStochasticDelete() throws Exception {
EPackage.Registry.INSTANCE.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);

StringBuffer st = new StringBuffer();
st.append("model M driver EMF {nsuri=\"http://www.eclipse.org/emf/2002/Ecore\"};");
st.append("var i:M!EClass = new M!EClass;");
st.append("var r = new Native(\"java.util.Random\");");
st.append("if (r.nextint(99) > 50){delete i;}");
st.append("i.name = \"className\";");
assertValidMessage(st.toString(), "Cannot access object i after deletion");
}

@Test
public void testPrimitiveTypesVariableDeclaration() throws Exception {
assertValid("var i : Integer; (/*Integer*/i).println();");
Expand All @@ -51,7 +124,7 @@ public void testPrimitiveTypesAssignmentExpression() throws Exception {
StringBuffer st = new StringBuffer();
st.append("var i : Integer = 4;");
st.append("var s : String = \"test\";");
st.append("(/*Integer*/i) = (/*String*/s);");
st.append("(/*Integer*/i) = 5;");
st.append("var a : Any = true;");
st.append("(/*Any*/ a) = (/*String*/s);");
assertValid(st.toString());
Expand All @@ -63,7 +136,8 @@ public void testCollectionTypesAssignmentExpression() throws Exception {
st.append("var col : Collection<Integer> = Collection{0..9};\n");
st.append("var seq : Sequence<Integer> = Sequence{0..9};\n");
st.append("var bg : Bag<Integer> = Bag{0..9};\n");
st.append("(/*Bag<Integer>*/bg) = (/*Collection<Integer>*/col);\n");
st.append("(/*Collection<Integer>*/col) = (/*Bag<Integer>*/bg);\n");
st.append("(/*Collection<Integer>*/col) = (/*Sequence<Integer>*/seq);\n");
assertValid(st.toString());
}

Expand All @@ -74,14 +148,15 @@ public void testPrimitiveTypesAssignmentExpressionErrorMessage() throws Exceptio
st.append("var s : String = \"test\";");
String expectedMessage = "String cannot be assigned to Integer";
st.append("i = s;");
assertValidMessage(st.toString(),expectedMessage);
assertValidMessage(st.toString(), expectedMessage);
}

public void assertValid(String eol) throws Exception {
EolModule module = new EolModule();
module.parse(eol);
EolStaticAnalyser staticAnalyser = new EolStaticAnalyser(new StaticModelFactory());
staticAnalyser.validate(module);
List<ModuleMarker> errors = staticAnalyser.validate(module);
assert errors.size() == 0 : "Was expecting 0 errors but found " + errors.size();
visit(module.getChildren());
}

Expand All @@ -90,6 +165,8 @@ public void assertValidMessage(String eol, String message) throws Exception {
module.parse(eol);
EolStaticAnalyser staticAnalyser = new EolStaticAnalyser(new StaticModelFactory());
List<ModuleMarker> errors = staticAnalyser.validate(module);

assert (errors.size() == 1): "unexpected number of errors (" + errors.size() + ") in eol module";
assertEquals(message, errors.get(0).getMessage());
}

Expand All @@ -105,4 +182,13 @@ protected void visit(List<ModuleElement> elements) {
protected EolType getResolvedType(ModuleElement element) {
return (EolType) element.getData().get("resolvedType");
}

public void printTree(ModuleElement parent) {
System.out.println(" ".repeat(ident) + parent.getClass().getSimpleName());
ident += 1;
for (ModuleElement m : parent.getChildren()) {
printTree(m);
}
ident -= 1;
}
}
Empty file.

This file was deleted.

0 comments on commit b60938c

Please sign in to comment.