|
6 | 6 |
|
7 | 7 | package org.antlr.v4.test.tool; |
8 | 8 |
|
| 9 | +import org.antlr.runtime.CommonToken; |
9 | 10 | import org.antlr.runtime.RecognitionException; |
| 11 | +import org.antlr.v4.test.runtime.ErrorQueue; |
| 12 | +import org.antlr.v4.tool.ANTLRMessage; |
10 | 13 | import org.antlr.v4.tool.ErrorType; |
| 14 | +import org.junit.jupiter.api.Assertions; |
11 | 15 | import org.junit.jupiter.api.Test; |
| 16 | +import org.junit.jupiter.api.condition.EnabledOnOs; |
| 17 | +import org.junit.jupiter.api.condition.OS; |
12 | 18 | import org.junit.jupiter.params.ParameterizedTest; |
13 | 19 | import org.junit.jupiter.params.provider.Arguments; |
14 | 20 | import org.junit.jupiter.params.provider.MethodSource; |
|
18 | 24 |
|
19 | 25 | import java.util.ArrayList; |
20 | 26 | import java.util.List; |
| 27 | +import java.util.function.Consumer; |
21 | 28 | import java.util.stream.Stream; |
22 | 29 |
|
23 | 30 | import static org.antlr.v4.test.tool.ToolTestUtils.testErrors; |
@@ -235,6 +242,28 @@ public void testDynamicInlineActions(String input, String expected) { |
235 | 242 | testAction("inline", attributeTemplate, input, expected); |
236 | 243 | } |
237 | 244 |
|
| 245 | + @Test |
| 246 | + @EnabledOnOs({OS.WINDOWS}) |
| 247 | + public void shouldDetectOffendingTokenAbsolutePositionInWindows(){ |
| 248 | + String expectedError = "error(" + ErrorType.ISOLATED_RULE_REF.code + "): A.g4:7:4: missing attribute access on rule reference lab in $lab\n"; |
| 249 | + testAction("inline", attributeTemplate, "$lab", expectedError, q -> assertOffendingToken(q, 131, 133)); |
| 250 | + } |
| 251 | + |
| 252 | + @Test |
| 253 | + @EnabledOnOs({OS.LINUX, OS.MAC}) |
| 254 | + public void shouldDetectOffendingTokenAbsolutePositionInLinux(){ |
| 255 | + String expectedError = "error(" + ErrorType.ISOLATED_RULE_REF.code + "): A.g4:7:4: missing attribute access on rule reference lab in $lab\n"; |
| 256 | + testAction("inline", attributeTemplate, "$lab", expectedError, q -> assertOffendingToken(q, 125, 127)); |
| 257 | + } |
| 258 | + |
| 259 | + private static void assertOffendingToken(ErrorQueue q, int expected, int expected1) { |
| 260 | + Assertions.assertEquals(1, q.errors.size()); |
| 261 | + ANTLRMessage antlrMessage = q.errors.get(0); |
| 262 | + CommonToken token = (CommonToken) antlrMessage.offendingToken; |
| 263 | + Assertions.assertEquals(expected, token.getStartIndex(), "Offending Token start index assertion"); |
| 264 | + Assertions.assertEquals(expected1, token.getStopIndex(), "Offending Token stop index assertion"); |
| 265 | + } |
| 266 | + |
238 | 267 | @Test public void testBadInlineActions() throws RecognitionException { |
239 | 268 | testActions("inline", bad_inlineChecks, attributeTemplate); |
240 | 269 | } |
@@ -266,11 +295,16 @@ private static void testActions(String location, String[] pairs, String template |
266 | 295 | } |
267 | 296 |
|
268 | 297 | private static void testAction(String location, String template, String action, String expected) { |
| 298 | + testAction(location, template, action, expected, $ -> {}); |
| 299 | + } |
| 300 | + |
| 301 | + private static void testAction(String location, String template, String action, String expected, Consumer<ErrorQueue> errorQueueConsumer) { |
269 | 302 | STGroup g = new STGroup('<', '>'); |
270 | | - g.setListener(new ErrorBuffer()); // hush warnings |
| 303 | + ErrorBuffer listener = new ErrorBuffer(); |
| 304 | + g.setListener(listener); // hush warnings |
271 | 305 | ST st = new ST(g, template); |
272 | 306 | st.add(location, action); |
273 | 307 | String grammar = st.render(); |
274 | | - testErrors(new String[]{grammar, expected}, false); |
| 308 | + testErrors(new String[]{grammar, expected}, errorQueueConsumer); |
275 | 309 | } |
276 | 310 | } |
0 commit comments