diff --git a/src/main/java/io/usethesource/vallang/type/Type.java b/src/main/java/io/usethesource/vallang/type/Type.java index 0ac63f3c..404c7d38 100644 --- a/src/main/java/io/usethesource/vallang/type/Type.java +++ b/src/main/java/io/usethesource/vallang/type/Type.java @@ -650,11 +650,16 @@ public Type getTypeParameters() { */ public int compareTo(Type o) { if (isSubtypeOf(o)) { - return -1; - } else if (o.isSubtypeOf(this)) { + return o.isSubtypeOf(this) + ? 0 + : -1; + } + else if (o.isSubtypeOf(this)) { return 1; } - return 0; + else { + return 0; + } } protected boolean isSubtypeOfParameter(Type type) { diff --git a/src/test/java/io/usethesource/vallang/specification/TypeTest.java b/src/test/java/io/usethesource/vallang/specification/TypeTest.java index a0f65f24..4ce4c7ec 100644 --- a/src/test/java/io/usethesource/vallang/specification/TypeTest.java +++ b/src/test/java/io/usethesource/vallang/specification/TypeTest.java @@ -10,7 +10,6 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; -import java.util.Map.Entry; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -61,6 +60,30 @@ public void emptyTupleNeverHasLabels(TypeFactory tf) { assertTrue(tf.tupleEmpty() == tf.tupleType(new Type[0], new String[0])); } + @ParameterizedTest @ArgumentsSource(ValueProvider.class) @TypeConfig(Option.ALL) + public void compareTo(Type t, Type u) { + if (t.isSubtypeOf(u)) { + if (!t.equivalent(u)) { + assertTrue(t.compareTo(u) == -1); + } + else { + assertTrue(t.compareTo(u) == 0); + } + } + else if (u.isSubtypeOf(t)) { + if (!t.equivalent(u)) { + assertTrue(t.compareTo(u) == 1); + } + else { + assertTrue(t.compareTo(u) == 0); + } + } + else { + assertTrue(t.compareTo(u) == 0); + assertTrue(u.compareTo(t) == 0); + } + } + @ParameterizedTest @ArgumentsSource(ValueProvider.class) @TypeConfig(Option.ALL) public void covariance(TypeFactory tf, Type t, Type u) { if (!t.comparable(u)) {