-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVulnBaseJavaToolTest.java
51 lines (42 loc) · 1.75 KB
/
VulnBaseJavaToolTest.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
/*
* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
package org.antlr.v4.test.tool;
import org.antlr.v4.Tool;
import org.antlr.v4.test.runtime.BaseRuntimeTest;
import org.antlr.v4.test.runtime.ErrorQueue;
import org.antlr.v4.test.runtime.java.BaseJavaTest;
import java.io.File;
import static org.junit.Assert.assertEquals;
public class BaseJavaToolTest extends BaseJavaTest {
public void testErrors(String[] pairs, boolean printTree) {
for (int i = 0; i < pairs.length; i+=2) {
String grammarStr = pairs[i];
String expect = pairs[i+1];
String[] lines = grammarStr.split("\n");
String fileName = getFilenameFromFirstLineOfGrammar(lines[0]);
ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, null, fileName, grammarStr, false); // use default language target in case test overrides
String actual = equeue.toString(true);
actual = actual.replace(tmpdir + File.separator, "");
// System.err.println(actual);
String msg = grammarStr;
msg = msg.replace("\n","\\n");
msg = msg.replace("\r","\\r");
msg = msg.replace("\t","\\t");
assertEquals("error in: "+msg,expect,actual);
}
}
public String getFilenameFromFirstLineOfGrammar(String line) {
String fileName = "A" + Tool.GRAMMAR_EXTENSION;
int grIndex = line.lastIndexOf("grammar");
int semi = line.lastIndexOf(';');
if ( grIndex>=0 && semi>=0 ) {
int space = line.indexOf(' ', grIndex);
fileName = line.substring(space+1, semi)+Tool.GRAMMAR_EXTENSION;
}
if ( fileName.length()==Tool.GRAMMAR_EXTENSION.length() ) fileName = "A" + Tool.GRAMMAR_EXTENSION;
return fileName;
}
}