aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/language/src/test/java/tools/refinery/language/tests/validation/ModuleValidationTest.java
blob: 00ad051bf8311f3b3432e3243047d284357aaf91 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * SPDX-FileCopyrightText: 2024 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.language.tests.validation;

import com.google.inject.Inject;
import org.eclipse.xtext.testing.InjectWith;
import org.eclipse.xtext.testing.extensions.InjectionExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import tools.refinery.language.model.tests.utils.ProblemParseHelper;
import tools.refinery.language.tests.ProblemInjectorProvider;
import tools.refinery.language.validation.ProblemValidator;

import java.util.stream.Stream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

@ExtendWith(InjectionExtension.class)
@InjectWith(ProblemInjectorProvider.class)
class ModuleValidationTest {
	@Inject
	private ProblemParseHelper parseHelper;

	@ParameterizedTest
	@MethodSource
	void invalidMultiObjectExistsTest(String invalidDeclaration) {
		var problem = parseHelper.parse("""
				module.

				%s
				""".formatted(invalidDeclaration));
		var issues = problem.validate();
		assertThat(issues, hasItem(hasProperty("issueCode",
				is(ProblemValidator.UNSUPPORTED_ASSERTION_ISSUE))));
	}

	static Stream<Arguments> invalidMultiObjectExistsTest() {
		return Stream.of(
				Arguments.of("""
						class Foo.
						exists(Foo::new).
						"""),
				Arguments.of("""
						multi m.
						exists(m).
						"""));
	}

	@Test
	void invalidScopeTest() {
		var problem = parseHelper.parse("""
				module.

				class Foo.
				scope Foo += 1.
				""");
		var issues = problem.validate();
		assertThat(issues, hasItem(hasProperty("issueCode",
				is(ProblemValidator.INVALID_MULTIPLICITY_ISSUE))));
	}

	@Test
	void invalidAssertionArgumentTest() {
		var problem = parseHelper.parse("""
				module.

				class Foo.
				Foo(foo1).
				""");
		var issues = problem.validate();
		assertThat(issues, hasItem(allOf(
				hasProperty("issueCode", is(ProblemValidator.UNSUPPORTED_ASSERTION_ISSUE)),
				hasProperty("message", containsString("foo1")))));
	}

	@ParameterizedTest
	@MethodSource
	void validDeclarationTest(String validDeclaration) {
		var problem = parseHelper.parse(validDeclaration);
		var issues = problem.validate();
		assertThat(issues, hasSize(0));
	}

	static Stream<Arguments> validDeclarationTest() {
		return Stream.concat(
				invalidMultiObjectExistsTest(),
				Stream.of(
						Arguments.of("""
								class Foo.
								scope Foo += 1.
								"""),
						Arguments.of("""
								module.

								class Foo.
								scope Foo = 1.
								"""),
						Arguments.of("""
								class Foo.
								Foo(foo1).
								"""),
						Arguments.of("""
								module.

								class Foo.
								multi foo1.
								Foo(foo1).
								"""),
						Arguments.of("""
								module.

								class Foo.
								atom foo1.
								Foo(foo1).
								"""),
						Arguments.of("""
								module.

								class Foo.
								declare foo1.
								Foo(foo1).
								"""),
						Arguments.of("""
								module.

								enum Foo { foo1 }
								Foo(foo1).
								"""),
						Arguments.of("""
								module.

								class Foo.
								Foo(Foo::new).
								""")
				)
		);
	}
}