aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/language/src/test/java/tools/refinery/language/tests/utils/SymbolCollectorTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/language/src/test/java/tools/refinery/language/tests/utils/SymbolCollectorTest.java')
-rw-r--r--subprojects/language/src/test/java/tools/refinery/language/tests/utils/SymbolCollectorTest.java243
1 files changed, 0 insertions, 243 deletions
diff --git a/subprojects/language/src/test/java/tools/refinery/language/tests/utils/SymbolCollectorTest.java b/subprojects/language/src/test/java/tools/refinery/language/tests/utils/SymbolCollectorTest.java
deleted file mode 100644
index d200eeff..00000000
--- a/subprojects/language/src/test/java/tools/refinery/language/tests/utils/SymbolCollectorTest.java
+++ /dev/null
@@ -1,243 +0,0 @@
1/*
2 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
3 *
4 * SPDX-License-Identifier: EPL-2.0
5 */
6package tools.refinery.language.tests.utils;
7
8import com.google.inject.Inject;
9import org.eclipse.xtext.testing.InjectWith;
10import org.eclipse.xtext.testing.extensions.InjectionExtension;
11import org.hamcrest.Matcher;
12import org.junit.jupiter.api.Disabled;
13import org.junit.jupiter.api.Test;
14import org.junit.jupiter.api.extension.ExtendWith;
15import org.junit.jupiter.params.ParameterizedTest;
16import org.junit.jupiter.params.provider.Arguments;
17import org.junit.jupiter.params.provider.MethodSource;
18import tools.refinery.language.model.problem.*;
19import tools.refinery.language.model.tests.utils.ProblemParseHelper;
20import tools.refinery.language.tests.ProblemInjectorProvider;
21import tools.refinery.language.utils.ContainmentRole;
22import tools.refinery.language.utils.ProblemDesugarer;
23
24import java.util.stream.Stream;
25
26import static org.hamcrest.MatcherAssert.assertThat;
27import static org.hamcrest.Matchers.*;
28import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
29
30@ExtendWith(InjectionExtension.class)
31@InjectWith(ProblemInjectorProvider.class)
32class SymbolCollectorTest {
33 @Inject
34 private ProblemParseHelper parseHelper;
35
36 @Inject
37 private ProblemDesugarer desugarer;
38
39 @Test
40 void implicitNodeTest() {
41 var problem = parseHelper.parse("""
42 exists(a).
43 """);
44 var collectedSymbols = desugarer.collectSymbols(problem.get());
45 var node = problem.node("a");
46 assertThat(collectedSymbols.nodes(), hasKey(node));
47 assertThat(collectedSymbols.nodes().get(node).individual(), is(false));
48 }
49
50 @Test
51 void individualNodeTest() {
52 var problem = parseHelper.parse("""
53 indiv a.
54 """);
55 var collectedSymbols = desugarer.collectSymbols(problem.get());
56 var node = problem.individualNode("a");
57 assertThat(collectedSymbols.nodes(), hasKey(node));
58 assertThat(collectedSymbols.nodes().get(node).individual(), is(true));
59 }
60
61 @Test
62 void classTest() {
63 var problem = parseHelper.parse("""
64 class Foo.
65 """);
66 var collectedSymbols = desugarer.collectSymbols(problem.get());
67 var classDeclaration = problem.findClass("Foo").get();
68 assertThat(collectedSymbols.relations(), hasKey(classDeclaration));
69 var classInfo = collectedSymbols.relations().get(classDeclaration);
70 assertThat(classInfo.parameters(), hasSize(1));
71 assertThat(classInfo.containmentRole(), is(ContainmentRole.CONTAINED));
72 assertThat(classInfo.hasDefinition(), is(false));
73 var newNode = classDeclaration.getNewNode();
74 assertThat(collectedSymbols.nodes(), hasKey(newNode));
75 assertThat(collectedSymbols.nodes().get(newNode).individual(), is(false));
76 assertThat(classInfo.assertions(), assertsNode(newNode, LogicValue.TRUE));
77 assertThat(collectedSymbols.relations().get(problem.builtinSymbols().exists()).assertions(),
78 assertsNode(newNode, LogicValue.UNKNOWN));
79 assertThat(collectedSymbols.relations().get(problem.builtinSymbols().equals()).assertions(),
80 assertsNode(newNode, LogicValue.UNKNOWN));
81 }
82
83 @Test
84 void abstractClassTest() {
85 var problem = parseHelper.parse("""
86 abstract class Foo.
87 """);
88 var collectedSymbols = desugarer.collectSymbols(problem.get());
89 assertThat(collectedSymbols.relations().get(problem.findClass("Foo").get()).assertions(), hasSize(0));
90 }
91
92 @Test
93 void referenceTest() {
94 var problem = parseHelper.parse("""
95 class Foo {
96 refers Foo[] bar opposite quux
97 refers Foo quux opposite bar
98 }
99 """);
100 var collectedSymbols = desugarer.collectSymbols(problem.get());
101 var fooClass = problem.findClass("Foo");
102 var barReference = fooClass.feature("bar");
103 var barInfo = collectedSymbols.relations().get(barReference);
104 var quuxReference = fooClass.feature("quux");
105 var quuxInfo = collectedSymbols.relations().get(quuxReference);
106 assertThat(barInfo.containmentRole(), is(ContainmentRole.NONE));
107 assertThat(barInfo.opposite(), is(quuxReference));
108 assertThat(barInfo.multiplicity(), is(instanceOf(UnboundedMultiplicity.class)));
109 assertThat(barInfo.hasDefinition(), is(false));
110 assertThat(quuxInfo.containmentRole(), is(ContainmentRole.NONE));
111 assertThat(quuxInfo.opposite(), is(barReference));
112 assertThat(quuxInfo.multiplicity(), is(instanceOf(ExactMultiplicity.class)));
113 assertThat(quuxInfo.multiplicity(), hasProperty("exactValue", is(1)));
114 assertThat(quuxInfo.hasDefinition(), is(false));
115 }
116
117 @Test
118 void containmentReferenceTest() {
119 var problem = parseHelper.parse("""
120 class Foo {
121 contains Foo[] bar
122 }
123 """);
124 var collectedSymbols = desugarer.collectSymbols(problem.get());
125 assertThat(collectedSymbols.relations().get(problem.findClass("Foo").feature("bar")).containmentRole(),
126 is(ContainmentRole.CONTAINMENT));
127 }
128
129 @Disabled("TODO: Rework numerical references")
130 @Test
131 void dataReferenceTest() {
132 var problem = parseHelper.parse("""
133 class Foo {
134 int bar
135 }
136 """);
137 var collectedSymbols = desugarer.collectSymbols(problem.get());
138 assertThat(collectedSymbols.relations().get(problem.findClass("Foo").feature("bar")).containmentRole(),
139 is(ContainmentRole.CONTAINMENT));
140 }
141
142 @Test
143 void enumTest() {
144 var problem = parseHelper.parse("""
145 enum Foo {
146 bar, quux
147 }
148 """);
149 var collectedSymbols = desugarer.collectSymbols(problem.get());
150 var enumDeclaration = problem.findEnum("Foo");
151 var enumInfo = collectedSymbols.relations().get(enumDeclaration.get());
152 assertThat(enumInfo.containmentRole(), is(ContainmentRole.NONE));
153 assertThat(enumInfo.assertions(), assertsNode(enumDeclaration.literal("bar"), LogicValue.TRUE));
154 assertThat(enumInfo.assertions(), assertsNode(enumDeclaration.literal("quux"), LogicValue.TRUE));
155 }
156
157 @ParameterizedTest
158 @MethodSource
159 void predicateTest(String keyword, ContainmentRole containmentRole) {
160 var problem = parseHelper.parse(keyword + " foo(node x) <-> domain(x); data(x).");
161 var collectedSymbols = desugarer.collectSymbols(problem.get());
162 var predicateInfo = collectedSymbols.relations().get(problem.pred("foo").get());
163 assertThat(predicateInfo.containmentRole(), is(containmentRole));
164 assertThat(predicateInfo.parameters(), hasSize(1));
165 assertThat(predicateInfo.bodies(), hasSize(2));
166 assertThat(predicateInfo.hasDefinition(), is(true));
167 }
168
169 static Stream<Arguments> predicateTest() {
170 return Stream.of(Arguments.of("pred", ContainmentRole.NONE), Arguments.of("error", ContainmentRole.NONE),
171 Arguments.of("contained pred", ContainmentRole.CONTAINED), Arguments.of("containment pred",
172 ContainmentRole.CONTAINMENT));
173 }
174
175 @ParameterizedTest
176 @MethodSource("logicValues")
177 void assertionTest(String keyword, LogicValue value) {
178 var problem = parseHelper.parse("""
179 pred foo(node x).
180 foo(a): %s.
181 """.formatted(keyword));
182 var collectedSymbols = desugarer.collectSymbols(problem.get());
183 assertThat(collectedSymbols.relations().get(problem.pred("foo").get()).assertions(),
184 assertsNode(problem.node("a"), value));
185 }
186
187 @ParameterizedTest
188 @MethodSource("logicValues")
189 void defaultAssertionTest(String keyword, LogicValue value) {
190 var problem = parseHelper.parse("""
191 pred foo(node x).
192 default foo(a): %s.
193 """.formatted(keyword));
194 var collectedSymbols = desugarer.collectSymbols(problem.get());
195 assertThat(collectedSymbols.relations().get(problem.pred("foo").get()).assertions(),
196 assertsNode(problem.node("a"), value));
197 }
198
199 static Stream<Arguments> logicValues() {
200 return Stream.of(Arguments.of("true", LogicValue.TRUE), Arguments.of("false", LogicValue.FALSE),
201 Arguments.of("unknown", LogicValue.UNKNOWN), Arguments.of("error", LogicValue.ERROR));
202 }
203
204 @Test
205 void invalidAssertionArityTest() {
206 var problem = parseHelper.parse("""
207 pred foo(node x).
208 foo(a, b).
209 """);
210 var collectedSymbols = desugarer.collectSymbols(problem.get());
211 assertThat(collectedSymbols.relations().get(problem.pred("foo").get()).assertions(), hasSize(0));
212 }
213
214 @Test
215 void invalidProblemTest() {
216 var problem = parseHelper.parse("""
217 class Foo {
218 bar[] opposite quux
219 Foo quux opposite bar
220 }
221 """).get();
222 assertDoesNotThrow(() -> desugarer.collectSymbols(problem));
223 }
224
225 @Test
226 void errorAssertionTest() {
227 var problem = parseHelper.parse("""
228 error foo(node a, node b) <-> equals(a, b).
229 """);
230 var collectedSymbols = desugarer.collectSymbols(problem.get());
231 var fooInfo = collectedSymbols.relations().get(problem.pred("foo").get());
232 assertThat(fooInfo.assertions(), hasSize(1));
233 var assertion = fooInfo.assertions().stream().findFirst().orElseThrow();
234 assertThat(assertion.getValue(), hasProperty("logicValue", is(LogicValue.FALSE)));
235 assertThat(assertion.getArguments(), hasSize(2));
236 assertThat(assertion.getArguments(), everyItem(instanceOf(WildcardAssertionArgument.class)));
237 }
238
239 private static Matcher<Iterable<? super Assertion>> assertsNode(Node node, LogicValue value) {
240 return hasItem(allOf(hasProperty("arguments", hasItem(hasProperty("node", is(node)))), hasProperty("value",
241 hasProperty("logicValue", is(value)))));
242 }
243}