forked from objectionary/eo
/
EoSyntaxTest.java
311 lines (298 loc) 路 10.3 KB
/
EoSyntaxTest.java
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.parser;
import com.jcabi.matchers.XhtmlMatchers;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.cactoos.io.InputOf;
import org.cactoos.io.ResourceOf;
import org.cactoos.text.TextOf;
import org.eolang.jucs.ClasspathSource;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.yaml.snakeyaml.Yaml;
/**
* Test case for {@link EoSyntax}.
*
* @since 0.1
*/
@SuppressWarnings("PMD.TooManyMethods")
final class EoSyntaxTest {
/**
* Empty message for JUnit Assertions.
*/
private static final String EMPTY_MSG = "EMPTY MESSAGE";
@Test
void parsesSimpleCode() throws Exception {
MatcherAssert.assertThat(
EoSyntaxTest.EMPTY_MSG,
XhtmlMatchers.xhtml(
new String(
new EoSyntax(
"test-1",
new ResourceOf("org/eolang/parser/fibonacci.eo")
).parsed().toString().getBytes(),
StandardCharsets.UTF_8
)
),
XhtmlMatchers.hasXPaths(
"/program[@name='test-1']",
"/program[@ms and @time and @version]",
"/program/listing",
"/program/metas/meta[head='meta2']",
"/program/objects/o[@name='fibo']"
)
);
}
@ParameterizedTest
@CsvSource({
" , comment-length-check, Comment must be at least 64 characters long",
"Hello world., comment-length-check, Comment must be at least 64 characters long",
"袩褉懈胁械褌 屑懈褉., comment-content-check, Comment must contain only ASCII printable characters: 0x20-0x7f",
"lowcase., comment-start-character-check, Comment must start with capital letter",
"without dot, comment-ending-check, Comment must end with dot"
})
void containsCommentCheckErrors(
final String comment,
final String check,
final String message
) throws IOException {
MatcherAssert.assertThat(
EoSyntaxTest.EMPTY_MSG,
XhtmlMatchers.xhtml(
new String(
new EoSyntax(
"test-1",
new InputOf(
String.join(
"\n",
String.format("# %s", comment),
"[] > app\n"
)
)
).parsed().toString().getBytes(),
StandardCharsets.UTF_8
)
),
XhtmlMatchers.hasXPath(
String.format(
"//errors/error[@line and @check='%s' and @severity='%s' and text()='%s']",
check, "warning", message
)
)
);
}
@Test
void printsProperListingEvenWhenSyntaxIsBroken() throws Exception {
final String src = String.join(
"\n",
"# This is the default 64+ symbols comment in front of abstract object.",
"[] > x-薪, 1\n"
);
MatcherAssert.assertThat(
EoSyntaxTest.EMPTY_MSG,
XhtmlMatchers.xhtml(
new String(
new EoSyntax(
"test-44",
new InputOf(src)
).parsed().toString().getBytes(StandardCharsets.UTF_8),
StandardCharsets.UTF_8
)
),
XhtmlMatchers.hasXPaths(
"/program/errors[count(error)=2]",
String.format("/program[listing='%s']", src)
)
);
}
@Test
void copiesListingCorrectly() throws Exception {
final String src = new TextOf(
new ResourceOf("org/eolang/parser/factorial.eo")
).asString();
final XML xml = new XMLDocument(
new String(
new EoSyntax(
"test-22",
new InputOf(src)
).parsed().toString().getBytes(),
StandardCharsets.UTF_8
)
);
MatcherAssert.assertThat(
EoSyntaxTest.EMPTY_MSG,
xml.xpath("/program/listing/text()"),
Matchers.contains(src)
);
}
@ParameterizedTest
@ValueSource(strings = {
"1 > x\n2 > y",
"1 > x\r\n2 > y",
"1 > x\r\n\r\n2 > y",
"1 > x\n2 > y\n",
"1 > x\n\n2 > y",
"# This is the default 64+ symbols comment in front of abstract object.\n[] > x",
"a b c > x\n x ^ > @",
"# This is the default 64+ symbols comment in front of abstract object.\n[] > x\n x ^ > @"
})
void parsesSuccessfully(final String code) {
final EoSyntax syntax = new EoSyntax(
"test-2",
new InputOf(code)
);
Assertions.assertDoesNotThrow(
syntax::parsed
);
}
@Test
void parsesArrow() throws IOException {
MatcherAssert.assertThat(
EoSyntaxTest.EMPTY_MSG,
new EoSyntax(
"test-it-3",
new InputOf("1 > x")
).parsed(),
XhtmlMatchers.hasXPaths(
"/program/objects/o[@base='int' and @name='x' and ends-with(text(), '1')]"
)
);
}
@Test
void prasesNested() throws IOException {
final String src = String.join(
"\n",
"# This is the default 64+ symbols comment in front of abstract object.",
"[] > base",
" memory 0 > x",
" # This is the default 64+ symbols comment in front of abstract object.",
" [self] > f",
" v > @",
" v\n"
);
MatcherAssert.assertThat(
EoSyntaxTest.EMPTY_MSG,
new EoSyntax(
"test-it-4",
new InputOf(src)
).parsed(),
XhtmlMatchers.hasXPaths(
"/program/objects[count(o)=1]",
"/program/objects/o[count(o)=2]"
)
);
}
@Test
void parsesDefinition() throws IOException {
MatcherAssert.assertThat(
EoSyntaxTest.EMPTY_MSG,
new EoSyntax(
"test-it-5",
new InputOf(
String.join(
"\n",
"# This is the default 64+ symbols comment in front of named abstract object.",
"[v] > p",
" f.write > @\n"
)
)
).parsed(),
XhtmlMatchers.hasXPaths(
"/program/objects[count(o)=1]",
"/program/objects/o[count(o)=3]"
)
);
}
@Test
void parsesMethodCalls() throws IOException {
MatcherAssert.assertThat(
EoSyntaxTest.EMPTY_MSG,
new EoSyntax(
"test-it-1",
new InputOf("add.\n 0\n TRUE")
).parsed(),
XhtmlMatchers.hasXPaths(
"/program[@name='test-it-1']",
"/program/objects/o[@base='.add']",
"/program/objects/o/o[@base='int']",
"/program/objects/o/o[@base='bool']"
)
);
}
@ParameterizedTest
@ValueSource(strings = {
"FALSE > false",
"TRUE > true"
})
void storesAsBytes(final String code) throws IOException {
final XML xml = new XMLDocument(
new String(
new EoSyntax(
"test-3",
new InputOf(code)
).parsed().toString().getBytes(),
StandardCharsets.UTF_8
)
);
MatcherAssert.assertThat(
EoSyntaxTest.EMPTY_MSG,
xml,
XhtmlMatchers.hasXPaths(
"/program/objects[count(o)=1]",
"/program/objects/o[@data='bytes']"
)
);
}
@ParameterizedTest
@ClasspathSource(value = "org/eolang/parser/typos/", glob = "**.yaml")
void checksTypoPacks(final String yml) throws IOException {
final Yaml yaml = new Yaml();
final Map<String, Object> map = yaml.load(yml);
Assumptions.assumeTrue(map.get("skip") == null);
final XML xml = new EoSyntax(
"typo",
new InputOf(String.format("%s\n", map.get("eo")))
).parsed();
MatcherAssert.assertThat(
EoSyntaxTest.EMPTY_MSG,
XhtmlMatchers.xhtml(xml.toString()),
XhtmlMatchers.hasXPaths("/program/errors/error/@line")
);
MatcherAssert.assertThat(
xml.toString(),
Integer.parseInt(xml.xpath("/program/errors/error[1]/@line").get(0)),
Matchers.equalTo(Integer.parseInt(map.get("line").toString()))
);
}
}