diff options
author | 2023-12-22 02:19:38 +0100 | |
---|---|---|
committer | 2023-12-24 17:32:54 +0100 | |
commit | c65275be9fce0fbba70094c754690a8bd9228ab4 (patch) | |
tree | 8ba97d3668229482f8e25272ebfe62c2d27cca12 /subprojects/language-semantics/src/test/java/tools | |
parent | refactor(language): use file extension provider (diff) | |
download | refinery-c65275be9fce0fbba70094c754690a8bd9228ab4.tar.gz refinery-c65275be9fce0fbba70094c754690a8bd9228ab4.tar.zst refinery-c65275be9fce0fbba70094c754690a8bd9228ab4.zip |
feat: solution serializer
Diffstat (limited to 'subprojects/language-semantics/src/test/java/tools')
-rw-r--r-- | subprojects/language-semantics/src/test/java/tools/refinery/language/semantics/SolutionSerializerTest.java | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/subprojects/language-semantics/src/test/java/tools/refinery/language/semantics/SolutionSerializerTest.java b/subprojects/language-semantics/src/test/java/tools/refinery/language/semantics/SolutionSerializerTest.java new file mode 100644 index 00000000..2d759c86 --- /dev/null +++ b/subprojects/language-semantics/src/test/java/tools/refinery/language/semantics/SolutionSerializerTest.java | |||
@@ -0,0 +1,164 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.language.semantics; | ||
7 | |||
8 | import com.google.inject.Inject; | ||
9 | import org.eclipse.xtext.testing.InjectWith; | ||
10 | import org.eclipse.xtext.testing.extensions.InjectionExtension; | ||
11 | import org.junit.jupiter.api.extension.ExtendWith; | ||
12 | import org.junit.jupiter.params.ParameterizedTest; | ||
13 | import org.junit.jupiter.params.provider.Arguments; | ||
14 | import org.junit.jupiter.params.provider.MethodSource; | ||
15 | import tools.refinery.generator.ModelGeneratorFactory; | ||
16 | import tools.refinery.generator.ProblemLoader; | ||
17 | import tools.refinery.language.tests.ProblemInjectorProvider; | ||
18 | |||
19 | import java.io.ByteArrayOutputStream; | ||
20 | import java.io.IOException; | ||
21 | import java.util.Map; | ||
22 | import java.util.stream.Stream; | ||
23 | |||
24 | import static org.hamcrest.MatcherAssert.assertThat; | ||
25 | import static org.hamcrest.Matchers.is; | ||
26 | |||
27 | @ExtendWith(InjectionExtension.class) | ||
28 | @InjectWith(ProblemInjectorProvider.class) | ||
29 | class SolutionSerializerTest { | ||
30 | @Inject | ||
31 | ProblemLoader loader; | ||
32 | |||
33 | @Inject | ||
34 | ModelGeneratorFactory generatorFactory; | ||
35 | |||
36 | @Inject | ||
37 | SolutionSerializer serializer; | ||
38 | |||
39 | @ParameterizedTest | ||
40 | @MethodSource | ||
41 | void solutionSerializerTest(String prefix, String input, String expectedOutput) throws IOException { | ||
42 | var problem = loader.loadString(prefix + "\n" + input); | ||
43 | var generator = generatorFactory.createGenerator(problem); | ||
44 | generator.generate(); | ||
45 | var solution = serializer.serializeSolution(generator.getProblemTrace(), generator.getModel()); | ||
46 | String actualOutput; | ||
47 | try (var outputStream = new ByteArrayOutputStream()) { | ||
48 | solution.eResource().save(outputStream, Map.of()); | ||
49 | actualOutput = outputStream.toString(); | ||
50 | } | ||
51 | assertThat(actualOutput, is(prefix + "\n" + expectedOutput)); | ||
52 | } | ||
53 | |||
54 | static Stream<Arguments> solutionSerializerTest() { | ||
55 | return Stream.of(Arguments.of(""" | ||
56 | class Foo. | ||
57 | """, """ | ||
58 | scope Foo = 3. | ||
59 | """, """ | ||
60 | !exists(Foo::new). | ||
61 | Foo(foo1). | ||
62 | Foo(foo2). | ||
63 | Foo(foo3). | ||
64 | """), Arguments.of(""" | ||
65 | class Foo { | ||
66 | contains Bar[2] bars | ||
67 | } | ||
68 | |||
69 | class Bar. | ||
70 | """, """ | ||
71 | scope Foo = 1. | ||
72 | """, """ | ||
73 | !exists(Foo::new). | ||
74 | !exists(Bar::new). | ||
75 | Foo(foo1). | ||
76 | Bar(bar1). | ||
77 | Bar(bar2). | ||
78 | bars(foo1, bar1). | ||
79 | bars(foo1, bar2). | ||
80 | """), Arguments.of(""" | ||
81 | class Foo { | ||
82 | Bar[2] bars opposite foo | ||
83 | } | ||
84 | |||
85 | class Bar { | ||
86 | Foo[1] foo opposite bars | ||
87 | } | ||
88 | """, """ | ||
89 | scope Foo = 1, Bar = 2. | ||
90 | """, """ | ||
91 | !exists(Foo::new). | ||
92 | !exists(Bar::new). | ||
93 | Foo(foo1). | ||
94 | Bar(bar1). | ||
95 | Bar(bar2). | ||
96 | default !bars(*, *). | ||
97 | bars(foo1, bar1). | ||
98 | bars(foo1, bar2). | ||
99 | """), Arguments.of(""" | ||
100 | class Person { | ||
101 | Person[2] friend opposite friend | ||
102 | } | ||
103 | """, """ | ||
104 | friend(a, b). | ||
105 | friend(a, c). | ||
106 | friend(b, c). | ||
107 | |||
108 | scope Person += 0. | ||
109 | """, """ | ||
110 | !exists(Person::new). | ||
111 | Person(a). | ||
112 | Person(b). | ||
113 | Person(c). | ||
114 | default !friend(*, *). | ||
115 | friend(a, b). | ||
116 | friend(a, c). | ||
117 | friend(b, a). | ||
118 | friend(b, c). | ||
119 | friend(c, a). | ||
120 | friend(c, b). | ||
121 | """), Arguments.of(""" | ||
122 | class Foo { | ||
123 | Bar bar | ||
124 | } | ||
125 | |||
126 | enum Bar { | ||
127 | BAR_A, | ||
128 | BAR_B | ||
129 | } | ||
130 | """, """ | ||
131 | bar(foo, BAR_A). | ||
132 | |||
133 | scope Foo += 0. | ||
134 | """, """ | ||
135 | !exists(Foo::new). | ||
136 | Foo(foo). | ||
137 | default !bar(*, *). | ||
138 | bar(foo, BAR_A). | ||
139 | """), Arguments.of(""" | ||
140 | class Foo. | ||
141 | class Bar extends Foo. | ||
142 | """, """ | ||
143 | scope Foo = 1, Bar = 0. | ||
144 | """, """ | ||
145 | !exists(Foo::new). | ||
146 | !exists(Bar::new). | ||
147 | Foo(foo1). | ||
148 | !Bar(foo1). | ||
149 | """), Arguments.of(""" | ||
150 | class Foo { | ||
151 | Foo[] ref | ||
152 | } | ||
153 | """, """ | ||
154 | ref(a, b). | ||
155 | !exists(b). | ||
156 | |||
157 | scope Foo += 0. | ||
158 | """, """ | ||
159 | !exists(Foo::new). | ||
160 | Foo(a). | ||
161 | default !ref(*, *). | ||
162 | """)); | ||
163 | } | ||
164 | } | ||