From 43e8837308a246f8c4c7ebb984d73dbc38b42563 Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Tue, 17 Jan 2023 12:42:20 +0100 Subject: [PATCH] Simplify tests by adding BoundsChain --- .../dhbwstuttgart/target/generate/Bound.java | 14 -- .../target/generate/BoundsList.java | 44 ++++++ .../target/generate/GenericsResult.java | 10 +- src/test/java/targetast/TestGenerics.java | 144 +++++++++--------- 4 files changed, 123 insertions(+), 89 deletions(-) create mode 100644 src/main/java/de/dhbwstuttgart/target/generate/BoundsList.java diff --git a/src/main/java/de/dhbwstuttgart/target/generate/Bound.java b/src/main/java/de/dhbwstuttgart/target/generate/Bound.java index 3b53e864e..1b39df6c4 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/Bound.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/Bound.java @@ -7,18 +7,4 @@ import java.util.List; public record Bound(boolean isOnMethod, RefTypeOrTPHOrWildcardOrGeneric bound) { - public static boolean chainEquals(List left, List right) { - if (left.size() != right.size()) return false; - for (var i = 0; i < left.size(); i++) { - var l = left.get(i); - var r = right.get(i); - if (l.isOnMethod != r.isOnMethod) return false; - if (i == left.size() - 1) { - if (!(l.bound instanceof TypePlaceholder)) { - if (!(l.bound.equals(r.bound))) return false; - } - } - } - return true; - } } diff --git a/src/main/java/de/dhbwstuttgart/target/generate/BoundsList.java b/src/main/java/de/dhbwstuttgart/target/generate/BoundsList.java new file mode 100644 index 000000000..b92394d62 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/generate/BoundsList.java @@ -0,0 +1,44 @@ +package de.dhbwstuttgart.target.generate; + +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; + +import java.util.AbstractList; +import java.util.List; + +public class BoundsList extends AbstractList { + private final List bounds; + + public BoundsList(List bounds) { + this.bounds = bounds; + } + public BoundsList(Bound... bounds) { + this.bounds = List.of(bounds); + } + + @Override + public Bound get(int index) { + return bounds.get(index); + } + + @Override + public int size() { + return bounds.size(); + } + + @Override + public boolean equals(Object other) { + if (!(other instanceof BoundsList right)) return false; + if (size() != right.size()) return false; + for (var i = 0; i < size(); i++) { + var l = get(i); + var r = right.get(i); + if (l.isOnMethod() != r.isOnMethod()) return false; + if (i == size() - 1) { + if (!(l.bound() instanceof TypePlaceholder)) { + if (!(l.bound().equals(r.bound()))) return false; + } + } + } + return true; + } +} diff --git a/src/main/java/de/dhbwstuttgart/target/generate/GenericsResult.java b/src/main/java/de/dhbwstuttgart/target/generate/GenericsResult.java index 29922d852..df7a555fb 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/GenericsResult.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/GenericsResult.java @@ -11,7 +11,7 @@ import java.util.*; public class GenericsResult { final ASTToTargetAST.Sigma sigma; - public GenericsResult(ASTToTargetAST.Sigma sigma) { + GenericsResult(ASTToTargetAST.Sigma sigma) { this.sigma = sigma; } @@ -23,11 +23,11 @@ public class GenericsResult { return this.sigma.computedGenericsOfMethods.getOrDefault(method, Set.of()); } - public List getBounds(RefTypeOrTPHOrWildcardOrGeneric type, ClassOrInterface clazz) { + public BoundsList getBounds(RefTypeOrTPHOrWildcardOrGeneric type, ClassOrInterface clazz) { return getBounds(type, clazz, null); } - public List getBounds(RefTypeOrTPHOrWildcardOrGeneric type, ClassOrInterface clazz, Method method) { + public BoundsList getBounds(RefTypeOrTPHOrWildcardOrGeneric type, ClassOrInterface clazz, Method method) { type = resolve(type); if (type instanceof TypePlaceholder) { var methodGenerics = get(method); @@ -55,9 +55,9 @@ public class GenericsResult { } bound.ifPresent(result::add); } while (bound.isPresent()); - return result; + return new BoundsList(result); } - return List.of(); + return new BoundsList(List.of()); } public RefTypeOrTPHOrWildcardOrGeneric resolve(RefTypeOrTPHOrWildcardOrGeneric type) { diff --git a/src/test/java/targetast/TestGenerics.java b/src/test/java/targetast/TestGenerics.java index d3b45ca30..8dea939b7 100644 --- a/src/test/java/targetast/TestGenerics.java +++ b/src/test/java/targetast/TestGenerics.java @@ -8,6 +8,7 @@ import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.target.generate.ASTToTargetAST; import de.dhbwstuttgart.target.generate.Bound; +import de.dhbwstuttgart.target.generate.BoundsList; import de.dhbwstuttgart.target.generate.GenericsResult; import org.junit.Ignore; import org.junit.Test; @@ -57,12 +58,13 @@ public class TestGenerics { var ECK1 = generics.getBounds(otherMethod.getParameterList().getParameterAt(0).getType(), result.clazz, anyMethod); var ECK2 = generics.getBounds(otherMethod.getReturnType(), result.clazz, anyMethod); - var ECKChain = List.of(new Bound(false, ASTToTargetAST.OBJECT)); - assertTrue(Bound.chainEquals(ECK1, ECK2) && Bound.chainEquals(ECK2, ECKChain)); + var ECKChain = new BoundsList(new Bound(false, ASTToTargetAST.OBJECT)); + assertEquals(ECK1, ECK2); + assertEquals(ECK2, ECKChain); var M = generics.getBounds(a.getType(), result.clazz); - var MChain = List.of(new Bound(false, TypePlaceholder.of("ECK")), new Bound(false, ASTToTargetAST.OBJECT)); - assertTrue(Bound.chainEquals(M, MChain)); + var MChain = new BoundsList(new Bound(false, TypePlaceholder.of("ECK")), new Bound(false, ASTToTargetAST.OBJECT)); + assertEquals(M, MChain); } @Test @@ -75,8 +77,8 @@ public class TestGenerics { assertEquals(0, generics.get(fReturn).size()); var N = generics.getBounds(fReturn.getReturnType(), result.clazz); - var NChain = List.of(new Bound(false, ASTToTargetAST.OBJECT)); - assertTrue(Bound.chainEquals(N, NChain)); + var NChain = new BoundsList(new Bound(false, ASTToTargetAST.OBJECT)); + assertEquals(N, NChain); } @Test @@ -91,18 +93,18 @@ public class TestGenerics { assertEquals(2, generics.get(main).size()); var N = generics.getBounds(m.getParameterList().getParameterAt(0).getType(), result.clazz, m); - var NChain = List.of(new Bound(true, ASTToTargetAST.OBJECT)); - assertTrue(Bound.chainEquals(N, NChain)); + var NChain = new BoundsList(new Bound(true, ASTToTargetAST.OBJECT)); + assertEquals(N, NChain); var Q = generics.getBounds(m.getReturnType(), result.clazz, m); - var QChain = List.of(new Bound(true, TypePlaceholder.of("N")), new Bound(true, ASTToTargetAST.OBJECT)); - assertTrue(Bound.chainEquals(Q, QChain)); + var QChain = new BoundsList(new Bound(true, TypePlaceholder.of("N")), new Bound(true, ASTToTargetAST.OBJECT)); + assertEquals(Q, QChain); var R = generics.getBounds(main.getParameterList().getParameterAt(0).getType(), result.clazz, main); - assertTrue(Bound.chainEquals(R, NChain)); + assertEquals(R, NChain); var Q2 = generics.getBounds(main.getReturnType(), result.clazz, main); - assertTrue(Bound.chainEquals(Q2, NChain)); + assertEquals(Q2, NChain); } @Test @@ -120,26 +122,26 @@ public class TestGenerics { assertEquals(2, generics.get(m).size()); var R = generics.getBounds(a.getType(), result.clazz); - var RChain = List.of(new Bound(false, ASTToTargetAST.OBJECT)); - assertTrue(Bound.chainEquals(R, RChain)); + var RChain = new BoundsList(new Bound(false, ASTToTargetAST.OBJECT)); + assertEquals(R, RChain); var O = generics.getBounds(id.getParameterList().getParameterAt(0).getType(), result.clazz, id); - var OChain = List.of(new Bound(true, TypePlaceholder.of("AB")), new Bound(true, ASTToTargetAST.OBJECT)); - assertTrue(Bound.chainEquals(O, OChain)); + var OChain = new BoundsList(new Bound(true, TypePlaceholder.of("AB")), new Bound(true, ASTToTargetAST.OBJECT)); + assertEquals(O, OChain); var AB = generics.getBounds(id.getReturnType(), result.clazz, id); - assertTrue(Bound.chainEquals(AB, List.of(new Bound(true, ASTToTargetAST.OBJECT)))); + assertEquals(AB, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); var S = generics.getBounds(setA.getParameterList().getParameterAt(0).getType(), result.clazz, setA); - var SChain = List.of(new Bound(true, TypePlaceholder.of("R")), new Bound(false, ASTToTargetAST.OBJECT)); - assertTrue(Bound.chainEquals(S, SChain)); - assertTrue(Bound.chainEquals(generics.getBounds(setA.getReturnType(), result.clazz, setA), RChain)); + var SChain = new BoundsList(new Bound(true, TypePlaceholder.of("R")), new Bound(false, ASTToTargetAST.OBJECT)); + assertEquals(S, SChain); + assertEquals(generics.getBounds(setA.getReturnType(), result.clazz, setA), RChain); var X = generics.getBounds(m.getParameterList().getParameterAt(0).getType(), result.clazz, m); var Y = generics.getBounds(m.getParameterList().getParameterAt(1).getType(), result.clazz, m); - var XChain = List.of(new Bound(true, ASTToTargetAST.OBJECT)); - var YChain = List.of(new Bound(true, TypePlaceholder.of("Y")), new Bound(true, ASTToTargetAST.OBJECT)); - assertTrue(Bound.chainEquals(X, XChain)); - assertTrue(Bound.chainEquals(Y, YChain)); + var XChain = new BoundsList(new Bound(true, ASTToTargetAST.OBJECT)); + var YChain = new BoundsList(new Bound(true, TypePlaceholder.of("Y")), new Bound(true, ASTToTargetAST.OBJECT)); + assertEquals(X, XChain); + assertEquals(Y, YChain); } @Test @@ -164,28 +166,28 @@ public class TestGenerics { assertEquals(3, generics.get(main).size()); var M = generics.getBounds(a.getType(), result.clazz); - assertTrue(Bound.chainEquals(M, List.of(new Bound(false, ASTToTargetAST.OBJECT)))); + assertEquals(M, new BoundsList(new Bound(false, ASTToTargetAST.OBJECT))); var O = generics.getBounds(id.getParameterList().getParameterAt(0).getType(), result.clazz, id); - var OChain = List.of(new Bound(true, TypePlaceholder.of("P")), new Bound(true, ASTToTargetAST.OBJECT)); - assertTrue(Bound.chainEquals(O, OChain)); + var OChain = new BoundsList(new Bound(true, TypePlaceholder.of("P")), new Bound(true, ASTToTargetAST.OBJECT)); + assertEquals(O, OChain); var AK = generics.getBounds(id.getReturnType(), result.clazz, id); - var AKChain = List.of(new Bound(true, ASTToTargetAST.OBJECT)); - assertTrue(Bound.chainEquals(AK, AKChain)); + var AKChain = new BoundsList(new Bound(true, ASTToTargetAST.OBJECT)); + assertEquals(AK, AKChain); // TODO Shouldn't AK and AK2 be the same and on the class? var AK2 = generics.getBounds(m.getParameterList().getParameterAt(0).getType(), result.clazz, m); var Y = generics.getBounds(m.getParameterList().getParameterAt(0).getType(), result.clazz, m); - assertTrue(Bound.chainEquals(AK2, AKChain)); - assertTrue(Bound.chainEquals(Y, AKChain)); + assertEquals(AK2, AKChain); + assertEquals(Y, AKChain); var AF = generics.getBounds(main.getParameterList().getParameterAt(0).getType(), result.clazz, main); var AG= generics.getBounds(main.getParameterList().getParameterAt(1).getType(), result.clazz, main); var AK3 = generics.getBounds(main.getReturnType(), result.clazz, main); - var AFChain = List.of(new Bound(true, TypePlaceholder.of("AG")), new Bound(true, ASTToTargetAST.OBJECT)); - assertTrue(Bound.chainEquals(AF, AFChain)); - assertTrue(Bound.chainEquals(AG, AKChain)); - assertTrue(Bound.chainEquals(AK3, AKChain)); + var AFChain = new BoundsList(new Bound(true, TypePlaceholder.of("AG")), new Bound(true, ASTToTargetAST.OBJECT)); + assertEquals(AF, AFChain); + assertEquals(AG, AKChain); + assertEquals(AK3, AKChain); } @Test @@ -197,7 +199,7 @@ public class TestGenerics { assertEquals(1, generics.get(anyMethod).size()); var M = generics.getBounds(anyMethod.getReturnType(), result.clazz, anyMethod); - assertTrue(Bound.chainEquals(M, List.of(new Bound(true, ASTToTargetAST.OBJECT)))); + assertEquals(M, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); } @Test @@ -211,15 +213,16 @@ public class TestGenerics { var generics = result.genericsResults.get(0); var M = generics.getBounds(a.getType(), result.clazz); var DYX = generics.getBounds(b.getType(), result.clazz); - var MChain = List.of(new Bound(false, TypePlaceholder.of("DYX")), new Bound(false, ASTToTargetAST.OBJECT)); - var DYXChain = List.of(new Bound(false, ASTToTargetAST.OBJECT)); - assertTrue(Bound.chainEquals(M, MChain)); + var MChain = new BoundsList(new Bound(false, TypePlaceholder.of("DYX")), new Bound(false, ASTToTargetAST.OBJECT)); + var DYXChain = new BoundsList(new Bound(false, ASTToTargetAST.OBJECT)); + assertEquals(M, MChain); var Q = generics.getBounds(anyMethod.getReturnType(), result.clazz, anyMethod); - assertTrue(Bound.chainEquals(Q, List.of(new Bound(true, ASTToTargetAST.OBJECT)))); + assertEquals(Q, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); var DYX2 = generics.getBounds(otherMethod.getReturnType(), result.clazz, otherMethod); - assertTrue(Bound.chainEquals(DYX, DYX2) && Bound.chainEquals(DYX2, DYXChain)); + assertEquals(DYX, DYX2); + assertEquals(DYX2, DYXChain); } @Test @@ -238,18 +241,18 @@ public class TestGenerics { var generics = result.genericsResults.get(0); var U = generics.getBounds(id2.getParameterList().getParameterAt(0).getType(), result.clazz, id2); var FPT = generics.getBounds(id2.getReturnType(), result.clazz, id2); - assertTrue(Bound.chainEquals(U, List.of(new Bound(true, TypePlaceholder.of("FPT")), new Bound(false, ASTToTargetAST.OBJECT)))); - assertTrue(Bound.chainEquals(FPT, List.of(new Bound(false, ASTToTargetAST.OBJECT)))); + assertEquals(U, new BoundsList(new Bound(true, TypePlaceholder.of("FPT")), new Bound(false, ASTToTargetAST.OBJECT))); + assertEquals(FPT, new BoundsList(new Bound(false, ASTToTargetAST.OBJECT))); var AA = generics.getBounds(m.getReturnType(), result.clazz, m); var AC = generics.getBounds(m.getParameterList().getParameterAt(1).getType(), result.clazz, m); - assertTrue(Bound.chainEquals(AA, List.of(new Bound(true, ASTToTargetAST.OBJECT)))); - assertTrue(Bound.chainEquals(AC, List.of(new Bound(true, TypePlaceholder.of("AD")), new Bound(true, ASTToTargetAST.OBJECT)))); + assertEquals(AA, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(AC, new BoundsList(new Bound(true, TypePlaceholder.of("AD")), new Bound(true, ASTToTargetAST.OBJECT))); var AH = generics.getBounds(m2.getReturnType(), result.clazz, m2); var AL = generics.getBounds(m2.getParameterList().getParameterAt(0).getType(), result.clazz, m2); - assertTrue(Bound.chainEquals(AH, List.of(new Bound(true, ASTToTargetAST.OBJECT)))); - assertTrue(Bound.chainEquals(AH, AL)); + assertEquals(AH, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(AH, AL); } @Test @@ -264,33 +267,34 @@ public class TestGenerics { var generics = result.genericsResults.get(0); var AO = generics.getBounds(a.getType(), result.clazz); - var AOBound = List.of( + var AOBound = new BoundsList( new Bound(false, TypePlaceholder.of("Y")), new Bound(false, TypePlaceholder.of("AK")), new Bound(false, TypePlaceholder.of("AE")), new Bound(false, ASTToTargetAST.OBJECT) ); - assertTrue(Bound.chainEquals(AO, AOBound)); + assertEquals(AO, AOBound); var S = generics.getBounds(setA.getParameterList().getParameterAt(0).getType(), result.clazz, setA); - var SChain = new ArrayList(); - SChain.add(new Bound(true, TypePlaceholder.of("AO"))); - SChain.addAll(AOBound); - assertTrue(Bound.chainEquals(S, SChain)); + var c = new ArrayList(); + c.add(new Bound(true, TypePlaceholder.of("AO"))); + c.addAll(AOBound); + var SChain = new BoundsList(c); + assertEquals(S, SChain); var Y = generics.getBounds(m.getParameterList().getParameterAt(1).getType(), result.clazz, m); - var YChain = List.of(new Bound(true, TypePlaceholder.of("AE")), new Bound(false, ASTToTargetAST.OBJECT)); + var YChain = new BoundsList(new Bound(true, TypePlaceholder.of("AE")), new Bound(false, ASTToTargetAST.OBJECT)); var AE = generics.getBounds(m.getReturnType(), result.clazz, m); - assertTrue(Bound.chainEquals(Y, YChain)); - assertTrue(Bound.chainEquals(AE, List.of(new Bound(false, ASTToTargetAST.OBJECT)))); + assertEquals(Y, YChain); + assertEquals(AE, new BoundsList(new Bound(false, ASTToTargetAST.OBJECT))); // TODO main seems to change between runs /*var AE2 = generics.getBounds(main.getReturnType(), result.clazz, main); var AF = generics.getBounds(main.getParameterList().getParameterAt(0).getType(), result.clazz, main); var AG = generics.getBounds(main.getParameterList().getParameterAt(1).getType(), result.clazz, main); - assertTrue(Bound.chainEquals(AE, AE2)); - assertTrue(Bound.chainEquals(AF, List.of(new Bound(true, TypePlaceholder.of("AK")), new Bound(true, TypePlaceholder.of("AE")), new Bound(false, ASTToTargetAST.OBJECT)))); - assertTrue(Bound.chainEquals(AG, SChain));*/ + assertEquals(AE, AE2)); + assertEquals(AF, new BoundsList(new Bound(true, TypePlaceholder.of("AK")), new Bound(true, TypePlaceholder.of("AE")), new Bound(false, ASTToTargetAST.OBJECT)))); + assertEquals(AG, SChain));*/ } @Test @@ -308,15 +312,15 @@ public class TestGenerics { var generics = result.genericsResults.get(0); var Q = generics.getBounds(id.getReturnType(), result.clazz, id); var N = generics.getBounds(id.getParameterList().getParameterAt(0).getType(), result.clazz, id); - assertTrue(Bound.chainEquals(Q, List.of(new Bound(true, ASTToTargetAST.OBJECT)))); - assertTrue(Bound.chainEquals(N, List.of(new Bound(true, TypePlaceholder.of("Q")), new Bound(true, ASTToTargetAST.OBJECT)))); + assertEquals(Q, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(N, new BoundsList(new Bound(true, TypePlaceholder.of("Q")), new Bound(true, ASTToTargetAST.OBJECT))); var Q2 = generics.getBounds(main.getReturnType(), result.clazz, main); var R = generics.getBounds(main.getParameterList().getParameterAt(0).getType(), result.clazz, main); var S = generics.getBounds(main.getParameterList().getParameterAt(1).getType(), result.clazz, main); - assertTrue(Bound.chainEquals(Q2, List.of(new Bound(true, ASTToTargetAST.OBJECT)))); - assertTrue(Bound.chainEquals(R, List.of(new Bound(true, TypePlaceholder.of("Q")), new Bound(true, ASTToTargetAST.OBJECT)))); - assertTrue(Bound.chainEquals(S, List.of(new Bound(true, TypePlaceholder.of("Q")), new Bound(true, ASTToTargetAST.OBJECT)))); + assertEquals(Q2, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(R, new BoundsList(new Bound(true, TypePlaceholder.of("Q")), new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(S, new BoundsList(new Bound(true, TypePlaceholder.of("Q")), new Bound(true, ASTToTargetAST.OBJECT))); } @Test @@ -330,8 +334,8 @@ public class TestGenerics { var S = generics.getBounds(((RefType) par1).getParaList().get(0), result.clazz, m); var ACM = generics.getBounds(((RefType) par2).getParaList().get(0), result.clazz, m); - assertTrue(Bound.chainEquals(S, List.of(new Bound(true, TypePlaceholder.of("V")), new Bound(true, TypePlaceholder.of("ACM")), new Bound(true, ASTToTargetAST.OBJECT)))); - assertTrue(Bound.chainEquals(ACM, List.of(new Bound(true, ASTToTargetAST.OBJECT)))); + assertEquals(S, new BoundsList(new Bound(true, TypePlaceholder.of("V")), new Bound(true, TypePlaceholder.of("ACM")), new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(ACM, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); } @Test @@ -344,14 +348,14 @@ public class TestGenerics { var par1 = generics.resolve(add.getParameterList().getParameterAt(0).getType()); var ACK = generics.getBounds(((RefType) par1).getParaList().get(0), result.clazz, add); var O = generics.getBounds(add.getParameterList().getParameterAt(1).getType(), result.clazz, add); - assertTrue(Bound.chainEquals(ACK, List.of(new Bound(true, ASTToTargetAST.OBJECT)))); - assertTrue(Bound.chainEquals(O, List.of(new Bound(true, TypePlaceholder.of("ACK")), new Bound(true, ASTToTargetAST.OBJECT)))); + assertEquals(ACK, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(O, new BoundsList(new Bound(true, TypePlaceholder.of("ACK")), new Bound(true, ASTToTargetAST.OBJECT))); var par2 = generics.resolve(main.getParameterList().getParameterAt(0).getType()); var ACK2 = generics.getBounds(((RefType) par2).getParaList().get(0), result.clazz, add); var V = generics.getBounds(add.getParameterList().getParameterAt(1).getType(), result.clazz, add); - assertTrue(Bound.chainEquals(ACK2, ACK)); - assertTrue(Bound.chainEquals(V, O)); + assertEquals(ACK2, ACK); + assertEquals(V, O); } @Test