KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > sexp > SExpParserTest


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2006 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.util.sexp;
35
36 import edu.rice.cs.drjava.DrJavaTestCase;
37
38 import java.io.*;
39 import java.util.List JavaDoc;
40
41 /** A JUnit test case class. Every method starting with the word "test" will be called when running
42  * the test with JUnit.
43  */

44 public class SExpParserTest extends DrJavaTestCase {
45   
46   /**
47    * Creates a temporary file and writes the given string to that file
48    * @param fname the name of the file to create
49    * @param text the text to write to the file
50    * @return the File that was created
51    */

52   private File _fillTempFile(String JavaDoc fname, String JavaDoc text) {
53     File f = null;
54     try {
55       f = File.createTempFile(fname, null).getCanonicalFile();
56       FileWriter fw = new FileWriter(f);
57       fw.write(text, 0, text.length());
58       fw.close();
59     }
60     catch (IOException e) {
61       throw new RuntimeException JavaDoc("IOException thrown while writing to temp file");
62     }
63     return f;
64   }
65   
66   /**
67    * There are three ways to input the data to a parse.
68    * this tests to make sure they all three return the same
69    * thing.
70    */

71   public void testDifferentInputs() throws SExpParseException, IOException{
72     String JavaDoc text = "()";
73     File f = _fillTempFile("temp1",text);
74     char[] ca = new char[text.length()];
75     text.getChars(0, text.length(), ca, 0);
76     Reader r = new CharArrayReader(ca);
77     
78     SExp sa1 = SExpParser.parse(text).get(0);
79     SExp sa2 = SExpParser.parse(f).get(0);
80     SExp sa3 = SExpParser.parse(r).get(0);
81     
82     SExp ans = Empty.ONLY;
83     
84     assertEquals("the 1st parse wasn't right", ans, sa1);
85     assertEquals("the 2nd parse wasn't right", ans, sa2);
86     assertEquals("the 3rd parse wasn't right", ans, sa3);
87   }
88   
89   /**
90    * Tests to make sure that multiple top-level s-exps
91    * are parsed separately and in tact
92    */

93   public void testParseMultiple() throws SExpParseException{
94     String JavaDoc text = "(abcdefg)(hijklmnop)";
95     List JavaDoc<? extends SExp> exps = SExpParser.parse(text);
96     SExp exp1 = exps.get(0);
97     SExp exp2 = exps.get(1);
98     
99     
100     // use a few visitors to test the instances
101
final SExpVisitor<String JavaDoc> innerVisitor = new SExpVisitor<String JavaDoc>() {
102       private String JavaDoc _failMe(String JavaDoc who) {
103         fail("The inside was "+ who +" but should have been text");
104         return "";
105       }
106       public String JavaDoc forEmpty(Empty e){ return _failMe("an empty list"); }
107       public String JavaDoc forCons(Cons c){ return _failMe("an empty list"); }
108       public String JavaDoc forBoolAtom(BoolAtom b){ return _failMe("a boolean"); }
109       public String JavaDoc forNumberAtom(NumberAtom n) { return _failMe("a number"); }
110       public String JavaDoc forTextAtom(TextAtom t) { return t.getText(); }
111     };
112     
113     final SExpVisitor<String JavaDoc> outerVisitor = new SExpVisitor<String JavaDoc>() {
114       private String JavaDoc _failMe(String JavaDoc who) {
115         fail("The top-level was "+ who +" but should have been a cons");
116         return "";
117       }
118       public String JavaDoc forEmpty(Empty e){ return _failMe("an empty list"); }
119       public String JavaDoc forCons(Cons c){ return c.getFirst().accept(innerVisitor); }
120       public String JavaDoc forBoolAtom(BoolAtom b){ return _failMe("a boolean"); }
121       public String JavaDoc forNumberAtom(NumberAtom n) { return _failMe("a number"); }
122       public String JavaDoc forTextAtom(TextAtom t) { return _failMe("text"); }
123     };
124     
125     assertEquals("wrong text in 1st s-expression", "abcdefg", exp1.accept(outerVisitor));
126     assertEquals("wrong text in 2nd s-expression", "hijklmnop",exp2.accept(outerVisitor));
127   }
128   
129   public void testTopLevel() throws SExpParseException {
130     // Test an illegal top-level s-exp
131
String JavaDoc text = "true";
132     try {
133       SExp exp = SExpParser.parse(text).get(0);
134       fail("Didn't throw a parse exception");
135     }catch(SExpParseException e) {
136       assertEquals("Incorrect exception message",
137                    "A top-level s-expression must be a list. "+
138                    "Invalid start of list: true",
139                    e.getMessage());
140     }
141     text = "123 ((help) me)";
142     try {
143       SExp exp = SExpParser.parse(text).get(0);
144       fail("Didn't throw a parse exception");
145     }catch(SExpParseException e) {
146       assertEquals("Incorrect exception message",
147                    "A top-level s-expression must be a list. "+
148                    "Invalid start of list: 123",
149                    e.getMessage());
150     }
151     text = "[help me]"; // right now, I haven't allowed other brace types
152
try {
153       SExp exp = SExpParser.parse(text).get(0);
154       fail("Didn't throw a parse exception");
155     }catch(SExpParseException e) {
156       assertEquals("Incorrect exception message",
157                    "A top-level s-expression must be a list. "+
158                    "Invalid start of list: [help",
159                    e.getMessage());
160     }
161   }
162   
163   public void testInvalidLowerLevel() {
164     
165     String JavaDoc text = "(abcdefg";
166     try {
167       SExp exp = SExpParser.parse(text).get(0);
168       fail("Didn't throw a parse exception");
169     }catch(SExpParseException e) {
170       assertEquals("Incorrect exception message",
171                    "Unexpected <EOF> at line 1",
172                    e.getMessage());
173     }
174     
175     text = "(ab\ncdefg";
176     try {
177       SExp exp = SExpParser.parse(text).get(0);
178       fail("Didn't throw a parse exception");
179     }catch(SExpParseException e) {
180       assertEquals("Incorrect exception message",
181                    "Unexpected <EOF> at line 2",
182                    e.getMessage());
183     }
184     
185     text = "(ab\ncdefg))";
186     try {
187       SExp exp = SExpParser.parse(text).get(0);
188       fail("Didn't throw a parse exception");
189     }catch(SExpParseException e) {
190       assertEquals("Incorrect exception message",
191                    "A top-level s-expression must be a list. "+
192                    "Invalid start of list: )",
193                    e.getMessage());
194     }
195     
196     text = "(\")"; // (") <-- unclosed string
197
try {
198       SExp exp = SExpParser.parse(text).get(0);
199       fail("Didn't throw a parse exception");
200     }catch(SExpParseException e) {
201       assertEquals("Incorrect exception message",
202                    "Unexpected <EOF> at line 1",
203                    e.getMessage());
204     }
205     
206     
207     text = "(;)"; // <-- last ) is commented out
208
try {
209       SExp exp = SExpParser.parse(text).get(0);
210       fail("Didn't throw a parse exception");
211     }catch(SExpParseException e) {
212       assertEquals("Incorrect exception message",
213                    "Unexpected <EOF> at line 1",
214                    e.getMessage());
215     }
216   }
217   
218   public void testCorrectParse() throws SExpParseException {
219     String JavaDoc n = "\n";
220     String JavaDoc text =
221       "; this is a comment line " + n +
222       "; this is another comment line " + n +
223       "(Source " + n +
224       " (/sexp/Atom.java) " + n +
225       " (/sexp/Cons.java) " + n +
226       " (/sexp/Empty.java) " + n +
227       " (/sexp/Lexer.java) " + n +
228       " (/sexp/SExp.java) " + n +
229       " (/sexp/SExpParser.java) " + n +
230       " (/sexp/SExpVisitor.java) " + n +
231       " (/sexp/Tokens.java) " + n +
232       ") " + n +
233       "; This is the build directory. Absolute path " + n +
234       "(BuildDir \"/home/javaplt/drjava/built\") " + n +
235       "(MainFile \"/sexp/SExpParser.java\") " + n +
236       "(Included " + n +
237       ")";
238     
239     List JavaDoc<SEList> res = SExpParser.parse(text);
240     assertEquals("Should have four trees in forest", 4, res.size());
241   }
242 }
243
Popular Tags