11package test .processing .parsing ;
22
3- import static org .junit .Assert .*;
3+ import static org .junit .Assert .assertEquals ;
4+ import static org .junit .Assert .fail ;
45import java .io .File ;
5- import java .io .FileInputStream ;
66import java .io .FileWriter ;
7- import java .io .IOException ;
8- import java .io .InputStreamReader ;
9- import java .io .StringWriter ;
107import java .util .regex .Matcher ;
118import java .util .regex .Pattern ;
129import org .junit .BeforeClass ;
1310import org .junit .Test ;
14- import processing .app .Base ;
15- import processing .app .Preferences ;
1611import processing .app .debug .RunnerException ;
17- import processing .app .preproc .PdePreprocessor ;
1812import processing .util .exec .ProcessResult ;
19- import antlr .ANTLRException ;
2013import antlr .RecognitionException ;
14+ import static test .processing .parsing .ProcessingTestUtil .res ;
15+ import static test .processing .parsing .ProcessingTestUtil .COMPILER ;
16+ import static test .processing .parsing .ProcessingTestUtil .preprocess ;
2117
2218public class ParserTests {
23-
24- private static final String RESOURCES = "test/resources/" ;
25- private static final UTCompiler COMPILER ;
26- static {
27- try {
28- Base .initPlatform ();
29- COMPILER = new UTCompiler (new File ("bin" ), new File ("../core/bin" ));
30- } catch (IOException e ) {
31- throw new RuntimeException (e );
32- }
33- }
34-
35- private static File res (final String resourceName ) {
36- return new File (RESOURCES , resourceName );
37- }
38-
19+
3920 @ BeforeClass
40- static public void initPrefs () throws Exception {
41- Preferences .load (new FileInputStream (res ("preferences.txt" )));
42- }
43-
44- static String read (final File f ) {
45- try {
46- final FileInputStream fin = new FileInputStream (f );
47- final InputStreamReader in = new InputStreamReader (fin , "UTF-8" );
48- try {
49- final StringBuilder sb = new StringBuilder ();
50- final char [] buf = new char [1 << 12 ];
51- int len ;
52- while ((len = in .read (buf )) != -1 )
53- sb .append (buf , 0 , len );
54- return sb .toString ().replace ("\r " , "" );
55- } finally {
56- in .close ();
57- }
58- } catch (Exception e ) {
59- throw new RuntimeException ("Unexpected" , e );
60- }
61- }
62-
63- static String preprocess (final String name , final File resource )
64- throws RunnerException , ANTLRException {
65- final String program = read (resource );
66- final StringWriter out = new StringWriter ();
67- new PdePreprocessor (name , 4 ).write (out , program );
68- return out .toString ().replace ("\r " , "" );
21+ public static void init () {
22+ ProcessingTestUtil .init ();
6923 }
7024
7125 static void expectRecognitionException (final String id ,
@@ -108,7 +62,8 @@ static void expectCompilerException(final String id,
10862 final String expectedMessage ,
10963 final int expectedLine ) {
11064 try {
111- final String program = preprocess (id , res (id + ".pde" ));
65+ final String program = ProcessingTestUtil
66+ .preprocess (id , res (id + ".pde" ));
11267 final ProcessResult compilerResult = COMPILER .compile (id , program );
11368 if (compilerResult .succeeded ()) {
11469 fail ("Expected to fail with \" " + expectedMessage + "\" on line "
@@ -129,8 +84,8 @@ static void expectCompilerException(final String id,
12984
13085 static void expectGood (final String id ) {
13186 try {
132- final String program = preprocess ( id , res ( id + ".pde" ));
133-
87+ final String program = ProcessingTestUtil
88+ . preprocess ( id , res ( id + ".pde" ));
13489 final ProcessResult compilerResult = COMPILER .compile (id , program );
13590 if (!compilerResult .succeeded ()) {
13691 System .err .println (program );
@@ -141,13 +96,13 @@ static void expectGood(final String id) {
14196
14297 final File expectedFile = res (id + ".expected" );
14398 if (expectedFile .exists ()) {
144- final String expected = read (expectedFile );
99+ final String expected = ProcessingTestUtil . read (expectedFile );
145100 assertEquals (expected , program );
146101 } else {
147102 System .err .println ("WARN: " + id
148103 + " does not have an expected output file. Generating." );
149104 final FileWriter sug = new FileWriter (res (id + ".expected" ));
150- sug .write (program . replace ( " \r " , "" ));
105+ sug .write (ProcessingTestUtil . normalize ( program ));
151106 sug .close ();
152107 }
153108
0 commit comments