1 6 package com.hp.hpl.jena.graph.query.regexptrees.test; 7 8 import java.util.Arrays ; 9 10 import junit.framework.TestSuite; 11 12 import com.hp.hpl.jena.graph.query.regexptrees.*; 13 import com.hp.hpl.jena.graph.test.GraphTestBase; 14 15 19 public class TestPerlyParser extends GraphTestBase 20 { 21 public TestPerlyParser( String name ) 22 { super( name ); } 23 24 public static TestSuite suite() 25 { return new TestSuite( TestPerlyParser.class ); } 26 27 protected static class FlagException extends RuntimeException 28 {} 29 30 public void testAlternateGenerator() 31 { 32 RegexpTreeGenerator g = new SimpleGenerator() 33 { 34 public RegexpTree getAnySingle() { throw new FlagException(); } 35 }; 36 PerlPatternParser p = new PerlPatternParser( ".", g ); 37 try { p.parseAtom(); fail( "should be using supplied generator" ); } 38 catch (FlagException e) { pass(); } 39 } 40 41 public void testLit() 42 { 43 assertEquals( Text.create( "a" ), Text.create( "a" ) ); 44 assertDiffer( Text.create( "a" ), Text.create( "b" ) ); 45 assertEquals( Text.create( "aga" ).hashCode(), Text.create( "aga" ).hashCode() ); 46 } 47 48 public void testInitialParserState() 49 { 50 assertEquals( 0, new PerlPatternParser( "hello" ).getPointer() ); 51 assertEquals( "hello", new PerlPatternParser( "hello" ).getString() ); 52 } 53 54 public void testLetterAtoms() 55 { 56 for (char ch = 0; ch < 256; ch += 1) 57 if (Character.isLetter( ch )) 58 { 59 PerlPatternParser p = new PerlPatternParser( "" + ch ); 60 assertEquals( Text.create( ch ), p.parseAtom() ); 61 assertEquals( 1, p.getPointer() ); 62 } 63 } 64 65 public void testEmptyExpression() 66 { assertEquals( new Nothing(), element( "" ) ); } 67 68 public void testDotAtom() 69 { testSimpleSpecialAtom( RegexpTree.ANY, "." ); } 70 71 public void testHatAtom() 72 { testSimpleSpecialAtom( RegexpTree.SOL, "^" ); } 73 74 public void testDollarAtom() 75 { testSimpleSpecialAtom( RegexpTree.EOL, "$" ); } 76 77 public void testTerminatorsReturnNull() 78 { 79 assertEquals( new Nothing(), element( "|" ) ); 80 } 81 82 public void testSimpleBackslashEscapes() 83 { 84 for (char ch = 0; ch < 256; ch += 1) 85 { 86 if ("bBAZnrtfdDwWSsxc0123456789".indexOf( ch ) < 0) 87 assertEquals( Text.create( ch ), new PerlPatternParser( "\\" + ch ).parseAtom() ); 88 } 89 } 90 91 public void testSpecialBackslashEscapes() 92 { 93 String specials = "bBAZ"; 94 for (int i = 0; i < specials.length(); i += 1) 95 try {new PerlPatternParser( "\\" + specials.charAt(i) ).parseAtom(); fail( "backslash escape " + specials.charAt(i) ); } 96 catch (PerlPatternParser.SyntaxException e) 97 { pass(); } 98 } 99 100 public void testWordEscapes() 101 { 102 String letters = "abcdefghijklmnopqrstuvwxyz"; 103 String wordChars = "0123456789" + letters + "_" + letters.toUpperCase(); 104 assertEquals( new AnyOf( wordChars ), element( "\\w" ) ); 105 assertEquals( new NoneOf( wordChars ), element( "\\W" ) ); 106 } 107 108 public void testDigitEscapes() 109 { 110 assertEquals( new AnyOf( "0123456789" ), element( "\\d" ) ); 111 assertEquals( new NoneOf( "0123456789" ), element( "\\D" ) ); 112 } 113 114 public void testWhitespaceEscapes() 115 { 116 assertEquals( Text.create( "\n" ), element( "\\n" ) ); 117 assertEquals( Text.create( "\t" ), element( "\\t" ) ); 118 assertEquals( Text.create( "\f" ), element( "\\f" ) ); 119 assertEquals( Text.create( "\r" ), element( "\\r" ) ); 120 assertEquals( new AnyOf( " \r\n\t\f"), element( "\\s" ) ); 121 assertEquals( new NoneOf( " \r\n\t\f" ), element( "\\S" ) ); 122 } 123 124 public void testHexEscapes() 125 { 126 assertParse( Text.create( "\u00ac" ), "\\xac" ); 127 assertParse( Text.create( "\u00ff" ), "\\xff" ); 128 assertParse( Text.create( "\u0012" ), "\\x12" ); 129 assertParse( Text.create( "\u00af" ), "\\xAF" ); 130 } 131 132 public void testControlEscapes() 133 { 134 assertParse( Text.create( "\u0001" ), "\\cA" ); 135 assertParse( Text.create( "\u001a" ), "\\cZ" ); 136 } 137 138 public void testNoQuantifier() 139 { 140 RegexpTree d = RegexpTree.ANY; 141 assertSame( d, quantifier( "", d ) ); 142 assertSame( d, quantifier( "x", d ) ); 143 assertSame( d, quantifier( "[", d ) ); 144 assertSame( d, quantifier( "(", d ) ); 145 assertSame( d, quantifier( ".", d ) ); 146 assertSame( d, quantifier( "\\", d ) ); 147 } 148 149 public void testStarQuantifier() 150 { 151 RegexpTree d = RegexpTree.EOL; 152 assertEquals( new ZeroOrMore( d ), quantifier( "*", d ) ); 153 } 154 155 public void testPlusQuantifier() 156 { 157 RegexpTree d = RegexpTree.SOL; 158 assertEquals( new OneOrMore( d ), quantifier( "+", d ) ); 159 } 160 161 public void testQueryQuantifier() 162 { 163 RegexpTree d = RegexpTree.ANY; 164 assertEquals( new Optional( d ), quantifier( "?", d ) ); 165 } 166 167 public void testUnboundQuantifiers() 168 { testUnboundQuantifier( "*" ); 169 testUnboundQuantifier( "+" ); 170 testUnboundQuantifier( "?" ); 171 testUnboundQuantifier( "{" ); } 172 173 177 private void testUnboundQuantifier( String q ) 178 { PerlPatternParser p = new PerlPatternParser( q ); 179 try { p.parseElement(); fail( "must trap unbound quantifier " + q ); } 180 catch (PerlPatternParser.SyntaxException e) { pass(); } } 181 182 public void testUnitSeq() 183 { 184 PerlPatternParser p = new PerlPatternParser( "x" ); 185 assertEquals( Text.create( "x" ), p.parseSeq() ); 186 } 187 188 public void testSeq() 189 { 190 PerlPatternParser p = new PerlPatternParser( "^.$" ); 191 assertEquals( seq3( new StartOfLine(), new AnySingle(), new EndOfLine() ), p.parseSeq() ); 192 } 193 194 public void testBracketConstruction() 195 { assertParse( new Paren( Text.create( "x" ) ), "(x)" ); } 196 197 public void testBracketClosure() 198 { 199 PerlPatternParser p = new PerlPatternParser( "()y" ); 200 assertEquals( seq2( new Paren( new Nothing() ), Text.create( "y" ) ), p.parseAlts() ); 201 } 202 203 public void testDetectsMissingClosingBracket() 204 { 205 PerlPatternParser p = new PerlPatternParser( "(x" ); 206 try { p.parseAlts(); fail( "should detect missing close bracket" ); } 207 catch (PerlPatternParser.SyntaxException e) { pass(); } 208 } 209 210 public void testAlt() 211 { 212 PerlPatternParser L = new PerlPatternParser( "abc" ); 213 PerlPatternParser R = new PerlPatternParser( "def" ); 214 PerlPatternParser p = new PerlPatternParser( "abc|def" ); 215 assertEquals( alt( L.parseSeq(), R.parseSeq() ), p.parseAlts() ); 216 } 217 218 public void testSimpleClass() 219 { assertParse( new AnyOf( "x1B" ), "[x1B]" ); } 220 221 public void testSimpleClassNegated() 222 { assertParse( new NoneOf( "b0#" ), "[^b0#]" ); } 223 224 public void testClassRangeAlphabet() 225 { assertParse( new AnyOf( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ), "[A-Z]" ); } 226 227 public void testClassRangeSomeLetters() 228 { assertParse( new AnyOf( "abcdef" ), "[a-f]" ); } 229 230 public void testClassRangeDigits() 231 { assertParse( new AnyOf( "abc0123456789rst" ), "[a-c0-9r-t]" ); } 232 233 public void testClassHats() 234 { assertParse( new AnyOf( "ab^cd" ), "[ab^cd]" ); } 235 236 public void testClassRange() 237 { assertParse( new AnyOf( "-R" ), "[-R]" ); } 238 239 public void testClassBackslash() 240 { assertParse( new AnyOf( "]" ), "[\\]]" ); } 241 242 public void testBackReference() 243 { assertParse( seq2( new Paren( Text.create( "x" ) ), new BackReference( 1 ) ), "(x)\\1" ); } 244 245 public void testOctalNonBackReference() 246 { assertParse( seq2( new Paren( Text.create( "x" ) ), Text.create( "\10" ) ), "(x)\\10" ); } 247 248 protected RegexpTree seq2( RegexpTree a, RegexpTree b ) 249 { 250 return Sequence.create( Arrays.asList( new RegexpTree[] {a, b} ) ); 251 } 252 253 protected RegexpTree seq3( RegexpTree a, RegexpTree b, RegexpTree c ) 254 { 255 return Sequence.create( Arrays.asList( new RegexpTree[] {a, b, c} ) ); 256 } 257 258 protected RegexpTree alt( RegexpTree L, RegexpTree R ) 259 { return Alternatives.create( Arrays.asList( new RegexpTree[] {L, R} ) ); } 260 261 public void testPerlParse() 262 { 263 assertTrue( PerlPatternParser.parse( "this is|a pattern" ) instanceof Alternatives ); 264 assertTrue( PerlPatternParser.parse( "this is|a pattern", new SimpleGenerator() ) instanceof Alternatives ); 265 } 266 267 public void testOldSeq() 268 { 269 PerlPatternParser p = new PerlPatternParser( "hello" ); 270 assertEquals( Text.create( "h" ), p.parseAtom() ); 271 assertEquals( 1, p.getPointer() ); 272 assertEquals( Text.create( "e" ), p.parseAtom() ); 273 assertEquals( 2, p.getPointer() ); 274 assertEquals( Text.create( "l" ), p.parseAtom() ); 275 assertEquals( 3, p.getPointer() ); 276 assertEquals( Text.create( "l" ), p.parseAtom() ); 277 assertEquals( 4, p.getPointer() ); 278 assertEquals( Text.create( "o" ), p.parseAtom() ); 279 assertEquals( 5, p.getPointer() ); 280 assertEquals( new Nothing(), p.parseAtom() ); 281 } 282 283 public void assertParse( RegexpTree wanted, String toParse ) 284 { assertEquals( wanted, new PerlPatternParser( toParse ).parseAlts() ); } 285 286 public void testSimpleSpecialAtom( Object wanted, String toParse ) 287 { 288 PerlPatternParser p = new PerlPatternParser( toParse ); 289 assertEquals( wanted, p.parseAtom() ); 290 assertEquals( 1, p.getPointer() ); 291 } 292 293 protected RegexpTree quantifier( String toParse, RegexpTree x ) 294 { return new PerlPatternParser( toParse ).parseQuantifier( x ); } 295 296 protected RegexpTree element( String toParse ) 297 { return new PerlPatternParser( toParse ).parseElement(); } 298 } 299 300 301 | Popular Tags |