1 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 ; 40 41 44 public class SExpParserTest extends DrJavaTestCase { 45 46 52 private File _fillTempFile(String fname, String 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 ("IOException thrown while writing to temp file"); 62 } 63 return f; 64 } 65 66 71 public void testDifferentInputs() throws SExpParseException, IOException{ 72 String 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 93 public void testParseMultiple() throws SExpParseException{ 94 String text = "(abcdefg)(hijklmnop)"; 95 List <? extends SExp> exps = SExpParser.parse(text); 96 SExp exp1 = exps.get(0); 97 SExp exp2 = exps.get(1); 98 99 100 final SExpVisitor<String > innerVisitor = new SExpVisitor<String >() { 102 private String _failMe(String who) { 103 fail("The inside was "+ who +" but should have been text"); 104 return ""; 105 } 106 public String forEmpty(Empty e){ return _failMe("an empty list"); } 107 public String forCons(Cons c){ return _failMe("an empty list"); } 108 public String forBoolAtom(BoolAtom b){ return _failMe("a boolean"); } 109 public String forNumberAtom(NumberAtom n) { return _failMe("a number"); } 110 public String forTextAtom(TextAtom t) { return t.getText(); } 111 }; 112 113 final SExpVisitor<String > outerVisitor = new SExpVisitor<String >() { 114 private String _failMe(String who) { 115 fail("The top-level was "+ who +" but should have been a cons"); 116 return ""; 117 } 118 public String forEmpty(Empty e){ return _failMe("an empty list"); } 119 public String forCons(Cons c){ return c.getFirst().accept(innerVisitor); } 120 public String forBoolAtom(BoolAtom b){ return _failMe("a boolean"); } 121 public String forNumberAtom(NumberAtom n) { return _failMe("a number"); } 122 public String 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 String 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]"; 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 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 = "(\")"; 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 = "(;)"; 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 n = "\n"; 220 String 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 <SEList> res = SExpParser.parse(text); 240 assertEquals("Should have four trees in forest", 4, res.size()); 241 } 242 } 243 | Popular Tags |