Commit bb92be21 authored by Johannes Mey's avatar Johannes Mey
Browse files

move more tests to gradle junit5 tests

parent 83a04e63
......@@ -5,3 +5,7 @@ build
src/gen-res/
src/gen/
*.class
src/test/jastadd/relations/Relations.ast
src/test/jastadd/relations/Relations.jadd
src/test/jastadd/lowerbounds/LowerBounds.ast
src/test/jastadd/lowerbounds/LowerBounds.jadd
\ No newline at end of file
......@@ -21,6 +21,7 @@ buildscript {
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.4.0'
runtime 'org.jastadd:jastadd:2.3.2'
compile group: 'net.sf.beaver', name: 'beaver-rt', version: '0.9.11'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.10.0'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.10.0'
......@@ -30,6 +31,9 @@ sourceSets {
main {
java.srcDir "src/gen/java"
}
test {
java.srcDir "src/test/java-gen"
}
}
jar {
......@@ -98,8 +102,68 @@ jastadd {
jastaddOptions = ["--lineColumnNumbers", "--safeLazy", "--visitCheck=true", "--rewrite=cnta", "--cache=all"]
}
task preprocessRelationTest(type: JavaExec, group: 'verification') {
doFirst {
delete 'src/test/jastadd/relations/Relations.ast', 'src/test/jastadd/relations/Relations.jadd'
}
classpath = sourceSets.main.runtimeClasspath
main = 'org.jastadd.relast.compiler.Compiler'
args 'src/test/jastadd/relations/Relations.relast', '--file', '--grammarName=src/test/jastadd/relations/Relations'
}
task doublePreprocessRelationTest(type: JavaExec, group: 'verification') {
doFirst {
delete 'src/test/jastadd/relations/Relations2.ast', 'src/test/jastadd/relations/Relations2.jadd'
}
classpath = sourceSets.main.runtimeClasspath
main = 'org.jastadd.relast.compiler.Compiler'
args 'src/test/jastadd/relations/Relations.ast', '--file', '--grammarName=src/test/jastadd/relations/Relations2'
}
task compileRelationTest(type: JavaExec, group: 'verification') {
doFirst {
delete 'src/test/java-gen/relations'
}
classpath = sourceSets.main.runtimeClasspath
main = 'org.jastadd.JastAdd'
args '--o=src/test/java-gen/', '--package=relations.ast', 'src/test/jastadd/relations/Relations.ast', 'src/test/jastadd/relations/Relations.jadd', 'src/test/jastadd/Utils.jadd'
}
task preprocessLowerBoundsTest(type: JavaExec, group: 'verification') {
doFirst {
delete 'src/test/jastadd/lowerbounds/LowerBounds.ast', 'src/test/jastadd/lowerbounds/LowerBounds.jadd'
}
classpath = sourceSets.main.runtimeClasspath
main = 'org.jastadd.relast.compiler.Compiler'
args 'src/test/jastadd/lowerbounds/LowerBounds.relast', '--file', '--grammarName=src/test/jastadd/lowerbounds/LowerBounds'
}
task compileLowerBoundsTest(type: JavaExec, group: 'verification') {
doFirst {
delete 'src/test/java-gen/lowerbounds'
}
classpath = sourceSets.main.runtimeClasspath
main = 'org.jastadd.JastAdd'
args '--o=src/test/java-gen/', '--package=lowerbounds.ast', 'src/test/jastadd/lowerbounds/LowerBounds.ast', 'src/test/jastadd/lowerbounds/LowerBounds.jadd', 'src/test/jastadd/Utils.jadd'
}
test {
outputs.upToDateWhen { false }
useJUnitPlatform()
}
\ No newline at end of file
}
test.dependsOn compileRelationTest
test.dependsOn compileLowerBoundsTest
doublePreprocessRelationTest.dependsOn preprocessRelationTest
compileRelationTest.dependsOn doublePreprocessRelationTest
compileLowerBoundsTest.dependsOn preprocessLowerBoundsTest
\ No newline at end of file
Root ::= A* B*;
A ::= <Name:String> <_impl_Di2:B> <_impl_Bi2:B> <_impl_Bi4:B> <_impl_Bi3:B> <_impl_Bi8:ArrayList<B>> <_impl_Bi9:ArrayList<B>> <_impl_Bi1:B> <_impl_Bi5:B> <_impl_Di3:ArrayList<B>> <_impl_Bi7:ArrayList<B>> <_impl_Bi6:B> <_impl_Di1:B>;
B ::= <Name:String> <_impl_Bi7:A> <_impl_Bi3:ArrayList<A>> <_impl_Bi5:A> <_impl_Bi1:A> <_impl_Bi6:ArrayList<A>> <_impl_Bi9:ArrayList<A>> <_impl_Bi2:A> <_impl_Bi4:A> <_impl_Bi8:A>;
import java.util.ArrayList;
import java.util.Collections;
aspect RelAstAPI {
public boolean ASTNode.violatesLowerBounds() {
return !getLowerBoundsViolations().isEmpty();
}
public java.util.List<Pair<ASTNode, String>> ASTNode.getLowerBoundsViolations() {
ArrayList<Pair<ASTNode, String>> list = new ArrayList<>();
computeLowerBoundsViolations(list);
return list;
}
public void ASTNode.computeLowerBoundsViolations(java.util.List<Pair<ASTNode, String>> list) {
for (int i = 0; i < getNumChildNoTransform(); i++) {
getChildNoTransform(i).computeLowerBoundsViolations(list);
}
}
public class Pair<T1, T2> {
public final T1 _1;
public final T2 _2;
public Pair(T1 _1, T2 _2) {
ASTNode.assertNotNull(_1);
ASTNode.assertNotNull(_2);
this._1 = _1;
this._2 = _2;
}
public boolean equals(Object other) {
if (other instanceof Pair) {
Pair<?,?> p = (Pair<?,?>) other;
return _1.equals(p._1) && _2.equals(p._2);
} else {
return false;
}
}
public int hashCode() {
return 31*_1.hashCode() + _2.hashCode();
}
}
public static void ASTNode.assertNotNull(Object obj) {
if (obj == null) {
throw new NullPointerException();
}
}
}
package org.jastadd.relast.tests;
import org.jastadd.relast.tests.testsuite.TestSuite;
public class AnalysisTests extends TestSuite {
}
package org.jastadd.relast.tests.errors;
package org.jastadd.relast.tests;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......@@ -9,42 +9,14 @@ import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import static org.jastadd.relast.tests.TestHelpers.exec;
import static org.jastadd.relast.tests.TestHelpers.readFile;
public class Errors {
private static final Logger logger = LogManager.getLogger(Errors.class);
static String readFile(String path, Charset encoding)
throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
static int exec(Class klass, String[] args, File err) throws IOException,
InterruptedException {
String javaHome = System.getProperty("java.home");
String javaBin = javaHome + File.separator + "bin" + File.separator + "java";
String classpath = System.getProperty("java.class.path");
String className = klass.getName();
String[] newArgs = new String[args.length + 4];
newArgs[0] = javaBin;
newArgs[1] = "-cp";
newArgs[2] = classpath;
newArgs[3] = className;
System.arraycopy(args, 0, newArgs, 4, args.length);
ProcessBuilder builder = new ProcessBuilder(newArgs);
builder.redirectError(err);
Process process = builder.start();
process.waitFor();
return process.exitValue();
}
class Errors {
private static final Logger logger = LogManager.getLogger(Errors.class);
@Test
void test1() throws IOException {
......
package org.jastadd.relast.tests;
import lowerbounds.ast.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class LowerBounds {
/*
* Root ::= A* B*;
* A ::= <Name> [C];
* B ::= <Name>;
* C ::= <Name>;
* rel A.Br -> B;
* rel B.Cr <-> C.Br;
* rel Root.Aa? -> A;
*/
@Test
void test() {
Root r = new Root();
C c1 = new C("c1");
C c2 = new C("c2");
A a1 = new A("a1", new Opt<>(c1));
A a2 = new A("a2", new Opt<>(c2));
B b1 = new B("b1");
B b2 = new B("b2");
r.addA(a1);
r.addA(a2);
r.addB(b1);
r.addB(b2);
assertTrue(r.violatesLowerBounds());
a1.setBr(b1);
a2.setBr(b2);
b1.setCr(c1);
b2.setCr(c2);
assertFalse(r.violatesLowerBounds());
b2.setCr(c1);
assertTrue(r.violatesLowerBounds());
b1.setCr(c2);
assertFalse(r.violatesLowerBounds());
}
}
package org.jastadd.relast.tests;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import relations.ast.A;
import relations.ast.B;
import relations.ast.Root;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
import static org.jastadd.relast.tests.TestHelpers.readFile;
import static org.junit.jupiter.api.Assertions.*;
@SuppressWarnings("ArraysAsListWithZeroOrOneArgument")
class Relations {
private Root r;
private A a1;
private A a2;
private A a3;
private B b1;
private B b2;
private B b3;
@Test
void doubleRelastRun() throws IOException {
String firstRun = "./src/test/jastadd/relations/Relations.ast";
String secondRun = "./src/test/jastadd/relations/Relations2.ast";
String first = readFile(firstRun, Charset.defaultCharset());
String second = readFile(secondRun, Charset.defaultCharset());
Assertions.assertEquals(first, second);
}
/**
* rel A.Di1 -> B;
*/
@Test
void testDi1() {
setup();
a1.setDi1(b2);
a2.setDi1(b1);
assertSame(a1.getDi1(), b2);
assertSame(a2.getDi1(), b1);
a2.setDi1(b2);
assertSame(a1.getDi1(), b2);
assertSame(a2.getDi1(), b2);
try {
a3.setDi1(null);
fail("should throw an exception");
} catch (Exception e) {
// OK
}
}
/**
* rel A.Di2? -> B;
*/
@Test
void testDi2() {
setup();
a1.setDi2(b2);
a2.setDi2(b1);
assertSame(a1.getDi2(), b2);
assertSame(a2.getDi2(), b1);
a2.setDi2(b2);
assertSame(a1.getDi2(), b2);
assertSame(a2.getDi2(), b2);
a2.clearDi2();
assertSame(a1.getDi2(), b2);
assertNull(a2.getDi2());
assertTrue(a1.hasDi2());
assertFalse(a2.hasDi2());
assertFalse(a3.hasDi2());
}
/**
* rel A.Di3* -> B;
*/
@Test
void testDi3() {
setup();
a1.addDi3(b1);
a1.addDi3(b2);
a1.addDi3(b3);
a2.addDi3(b2);
assertEquals(a1.getDi3s(), Arrays.asList(b1, b2, b3));
assertEquals(a1.getDi3List(), Arrays.asList(b1, b2, b3));
assertEquals(a2.getDi3s(), Arrays.asList(b2));
assertEquals(a2.getDi3List(), Arrays.asList(b2));
assertEquals(a3.getDi3s(), Arrays.asList());
assertEquals(a3.getDi3List(), Arrays.asList());
a1.addDi3(b1);
a2.addDi3(b1);
a2.addDi3(b2);
assertEquals(a1.getDi3s(), Arrays.asList(b1, b2, b3, b1));
assertEquals(a1.getDi3List(), Arrays.asList(b1, b2, b3, b1));
assertEquals(a2.getDi3s(), Arrays.asList(b2, b1, b2));
assertEquals(a2.getDi3List(), Arrays.asList(b2, b1, b2));
assertEquals(a3.getDi3s(), Arrays.asList());
assertEquals(a3.getDi3List(), Arrays.asList());
a1.removeDi3(b1);
a2.removeDi3(b2);
assertEquals(a1.getDi3s(), Arrays.asList(b2, b3, b1));
assertEquals(a1.getDi3List(), Arrays.asList(b2, b3, b1));
assertEquals(a2.getDi3s(), Arrays.asList(b1, b2));
assertEquals(a2.getDi3List(), Arrays.asList(b1, b2));
assertEquals(a3.getDi3s(), Arrays.asList());
assertEquals(a3.getDi3List(), Arrays.asList());
}
/**
* rel A.Bi1 <-> B.Bi1;
*/
@Test
void testBi11() {
// Init
setup();
a1.setBi1(b1);
a2.setBi1(b2);
// Change
a2.setBi1(b1);
assertNull(a1.getBi1());
assertSame(a2.getBi1(), b1);
assertSame(b1.getBi1(), a2);
assertNull(b2.getBi1());
}
@Test
void testBi12() {
// Init
setup();
a1.setBi1(b2);
// Change
a2.setBi1(b2);
assertNull(a1.getBi1());
assertSame(a2.getBi1(), b2);
assertNull(b1.getBi1());
assertSame(b2.getBi1(), a2);
}
/**
* rel A.Bi2 <-> B.Bi2?;
*/
@Test
void testBi21() {
// Init
setup();
a1.setBi2(b1);
a2.setBi2(b2);
// Change
a2.setBi2(b1);
assertNull(a1.getBi2());
assertSame(a2.getBi2(), b1);
assertSame(b1.getBi2(), a2);
assertNull(b2.getBi2());
}
@Test
void testBi22() {
// Init
setup();
a1.setBi2(b2);
// Change
a2.setBi2(b2);
assertNull(a1.getBi2());
assertSame(a2.getBi2(), b2);
assertNull(b1.getBi2());
assertSame(b2.getBi2(), a2);
}
/**
* rel A.Bi3 <-> B.Bi3*;
*/
@Test
void testBi3() {
setup();
a2.setBi3(b2);
assertNull(a1.getBi3());
assertSame(a2.getBi3(), b2);
assertEquals(b1.getBi3s(), Arrays.asList());
assertEquals(b1.getBi3List(), Arrays.asList());
assertEquals(b2.getBi3s(), Arrays.asList(a2));
assertEquals(b2.getBi3List(), Arrays.asList(a2));
assertEquals(b3.getBi3s(), Arrays.asList());
assertEquals(b3.getBi3List(), Arrays.asList());
a2.setBi3(b3);
assertNull(a1.getBi3());
assertSame(a2.getBi3(), b3);
assertEquals(b1.getBi3s(), Arrays.asList());
assertEquals(b1.getBi3List(), Arrays.asList());
assertEquals(b2.getBi3s(), Arrays.asList());
assertEquals(b2.getBi3List(), Arrays.asList());
assertEquals(b3.getBi3s(), Arrays.asList(a2));
assertEquals(b3.getBi3List(), Arrays.asList(a2));
a1.setBi3(b3);
a3.setBi3(b3);
assertSame(a1.getBi3(), b3);
assertSame(a2.getBi3(), b3);
assertSame(a3.getBi3(), b3);
assertEquals(b1.getBi3s(), Arrays.asList());
assertEquals(b1.getBi3List(), Arrays.asList());
assertEquals(b2.getBi3s(), Arrays.asList());
assertEquals(b2.getBi3List(), Arrays.asList());
assertEquals(b3.getBi3s(), Arrays.asList(a2, a1, a3));
assertEquals(b3.getBi3List(), Arrays.asList(a2, a1, a3));
a2.setBi3(b1);
assertSame(a1.getBi3(), b3);
assertSame(a2.getBi3(), b1);
assertSame(a3.getBi3(), b3);
assertEquals(b1.getBi3s(), Arrays.asList(a2));
assertEquals(b1.getBi3List(), Arrays.asList(a2));
assertEquals(b2.getBi3s(), Arrays.asList());
assertEquals(b2.getBi3List(), Arrays.asList());
assertEquals(b3.getBi3s(), Arrays.asList(a1, a3));
assertEquals(b3.getBi3List(), Arrays.asList(a1, a3));
try {
a2.setBi3(null);
fail("should throw an exception");
} catch (Exception e) {
// OK
}
}
/**
* rel A.Bi4? <-> B.Bi4;
*/
@Test
void testBi41() {
// Init
setup();
a1.setBi4(b1);
a2.setBi4(b2);
// Change
a2.setBi4(b1);
assertNull(a1.getBi4());
assertSame(a2.getBi4(), b1);
assertSame(b1.getBi4(), a2);
assertNull(b2.getBi4());
}
@Test
void testBi42() {
// Init
setup();
a1.setBi4(b2);
// Change
a2.setBi4(b2);
assertNull(a1.getBi4());
assertSame(a2.getBi4(), b2);
assertNull(b1.getBi4());
assertSame(b2.getBi4(), a2);
}
/**
* rel A.Bi5? <-> B.Bi5?;
*/
@Test
void testBi51() {
// Init
setup();
a1.setBi5(b1);
a2.setBi5(b2);
// Change
a2.setBi5(b1);
assertNull(a1.getBi5());
assertSame(a2.getBi5(), b1);
assertSame(b1.getBi5(), a2);
assertNull(b2.getBi5());
}
@Test
void testBi52() {
// Init
setup();
a1.setBi5(b2);
// Change
a2.setBi5(b2);
assertNull(a1.getBi5());
assertSame(a2.getBi5(), b2);