From 8cbcac32471c643aa69533dd15b42d4f776bb7d8 Mon Sep 17 00:00:00 2001
From: Niklas Fors <niklas.fors@cs.lth.se>
Date: Thu, 5 Jul 2018 10:38:07 +0200
Subject: [PATCH] Add test cases for directed relations

---
 spec/jastadd/Backend.jadd         |  2 +-
 test/.gitignore                   |  4 +-
 test/{AllBi.relast => All.relast} |  4 ++
 test/Makefile                     |  4 +-
 test/Test.java                    | 91 +++++++++++++++++++++++++++++++
 5 files changed, 100 insertions(+), 5 deletions(-)
 rename test/{AllBi.relast => All.relast} (82%)

diff --git a/spec/jastadd/Backend.jadd b/spec/jastadd/Backend.jadd
index bcc9c37..bd2e290 100644
--- a/spec/jastadd/Backend.jadd
+++ b/spec/jastadd/Backend.jadd
@@ -165,7 +165,7 @@ aspect BackendDirectedAPI {
 		// Get
 		sb.append(ind(1) + "public java.util.List<" + ofTypeDecl() + "> " + toTypeDecl());
 		sb.append("." + name() + "() {\n");
-		sb.append(ind(2) + "ArrayList<List> l = get" + getImplAttributeName() + "();\n");
+		sb.append(ind(2) + "ArrayList<" + ofTypeDecl() + "> l = get" + getImplAttributeName() + "();\n");
 		sb.append(ind(2) + "return l != null ? l : Collections.emptyList();\n");
 		sb.append(ind(1) + "}\n");
 
diff --git a/test/.gitignore b/test/.gitignore
index c9c2125..f09d75c 100644
--- a/test/.gitignore
+++ b/test/.gitignore
@@ -1,3 +1,3 @@
 /AST/*
-/AllBiGen.ast
-/AllBiGen.jadd
+/AllGen.ast
+/AllGen.jadd
diff --git a/test/AllBi.relast b/test/All.relast
similarity index 82%
rename from test/AllBi.relast
rename to test/All.relast
index be67e83..56fe6de 100644
--- a/test/AllBi.relast
+++ b/test/All.relast
@@ -2,6 +2,10 @@ Root ::= A* B*;
 A ::= <Name>;
 B ::= <Name>;
 
+rel A.di1  -> B;
+rel A.di2? -> B;
+rel A.di3* -> B;
+
 rel A.bi1 <-> B.bi1;
 rel A.bi2 <-> B.bi2?;
 rel A.bi3 <-> B.bi3*;
diff --git a/test/Makefile b/test/Makefile
index 1f34553..b85d7a3 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -1,8 +1,8 @@
 all: compile run
 compile:
 	(cd .. && ant jar)
-	java -jar ../compiler.jar AllBi.relast --file
-	java -jar ../tools/jastadd2.jar --package=AST AllBiGen.ast AllBiGen.jadd Utils.jadd
+	java -jar ../compiler.jar All.relast --file
+	java -jar ../tools/jastadd2.jar --package=AST AllGen.ast AllGen.jadd Utils.jadd
 run:
 	javac AST/*.java Test.java
 	java Test
\ No newline at end of file
diff --git a/test/Test.java b/test/Test.java
index 7dda0f3..10651e4 100644
--- a/test/Test.java
+++ b/test/Test.java
@@ -15,6 +15,10 @@ public class Test {
 	}
 
 	public void test() {
+		testDi1();
+		testDi2();
+		testDi3();
+
 		testBi1();
 		testBi2();
 		testBi3();
@@ -26,6 +30,87 @@ public class Test {
 		testBi9();
 	}
 
+	/**
+	 * rel A.di1 -> B;
+	 */
+	private void testDi1() {
+		setup();
+		a1.setDi1(b2);
+		a2.setDi1(b1);
+
+		assertSame(a1.di1(), b2);
+		assertSame(a2.di1(), b1);
+
+		a2.setDi1(b2);
+
+		assertSame(a1.di1(), b2);
+		assertSame(a2.di1(), b2);
+
+		try {
+			a3.setDi1(null);
+			check(false, "should throw exception");
+		} catch (Exception e) {
+			// OK
+		}
+	}
+
+
+	/**
+	 * rel A.di2? -> B;
+	 */
+	private void testDi2() {
+		setup();
+		a1.setDi2(b2);
+		a2.setDi2(b1);
+
+		assertSame(a1.di2(), b2);
+		assertSame(a2.di2(), b1);
+
+		a2.setDi2(b2);
+
+		assertSame(a1.di2(), b2);
+		assertSame(a2.di2(), b2);
+
+		a2.setDi2(null);
+
+		assertSame(a1.di2(), b2);
+		assertNull(a2.di2());
+
+		assertTrue(a1.hasDi2());
+		assertFalse(a2.hasDi2());
+		assertFalse(a3.hasDi2());
+	}
+
+
+	/**
+	 * rel A.di3* -> B;
+	 */
+	private void testDi3() {
+		setup();
+		a1.addToDi3(b1);
+		a1.addToDi3(b2);
+		a1.addToDi3(b3);
+		a2.addToDi3(b2);
+		
+		assertEquals(a1.di3(), Arrays.asList(b1, b2, b3));
+		assertEquals(a2.di3(), Arrays.asList(b2));
+		assertEquals(a3.di3(), Arrays.asList());
+
+		a1.addToDi3(b1);
+		a2.addToDi3(b1);
+		a2.addToDi3(b2);
+
+		assertEquals(a1.di3(), Arrays.asList(b1, b2, b3, b1));
+		assertEquals(a2.di3(), Arrays.asList(b2, b1, b2));
+		assertEquals(a3.di3(), Arrays.asList());
+
+		a1.removeFromDi3(b1);
+		a2.removeFromDi3(b2);
+
+		assertEquals(a1.di3(), Arrays.asList(b2, b3, b1));
+		assertEquals(a2.di3(), Arrays.asList(b1, b2));
+		assertEquals(a3.di3(), Arrays.asList());
+	}
 
 
 	/**
@@ -263,6 +348,12 @@ public class Test {
 		r.addB(b3);
 	}
 
+	private void assertTrue(boolean b) {
+		check(b, "value should be true (is false)");
+	}
+	private void assertFalse(boolean b) {
+		check(!b, "value should be flase (is true)");
+	}
 	private void assertNull(Object obj) {
 		check(obj == null, "Object not null: " + obj);
 	}
-- 
GitLab