| [ Index ] |
PHP Cross Reference of JPSpan 0.4 (beta) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @version $Id: lexer.test.php,v 1.1 2004/11/10 16:03:10 harryf Exp $ 4 * @package JPSpan 5 * @subpackage Tests 6 */ 7 8 /** 9 * Includes 10 */ 11 require_once ('../config.php'); 12 require_once JPSPAN . 'Lexer.php'; 13 14 /** 15 * @package JPSpan 16 * @subpackage Tests 17 */ 18 class TestOfLexerParallelRegex extends UnitTestCase { 19 function TestOfLexerParallelRegex() { 20 $this->UnitTestCase(); 21 } 22 function testNoPatterns() { 23 $regex = &new JPSpan_LexerParallelRegex(false); 24 $this->assertFalse($regex->match("Hello", $match)); 25 $this->assertEqual($match, ""); 26 } 27 function testNoSubject() { 28 $regex = &new JPSpan_LexerParallelRegex(false); 29 $regex->addPattern(".*"); 30 $this->assertTrue($regex->match("", $match)); 31 $this->assertEqual($match, ""); 32 } 33 function testMatchAll() { 34 $regex = &new JPSpan_LexerParallelRegex(false); 35 $regex->addPattern(".*"); 36 $this->assertTrue($regex->match("Hello", $match)); 37 $this->assertEqual($match, "Hello"); 38 } 39 function testCaseSensitive() { 40 $regex = &new JPSpan_LexerParallelRegex(true); 41 $regex->addPattern("abc"); 42 $this->assertTrue($regex->match("abcdef", $match)); 43 $this->assertEqual($match, "abc"); 44 $this->assertTrue($regex->match("AAABCabcdef", $match)); 45 $this->assertEqual($match, "abc"); 46 } 47 function testCaseInsensitive() { 48 $regex = &new JPSpan_LexerParallelRegex(false); 49 $regex->addPattern("abc"); 50 $this->assertTrue($regex->match("abcdef", $match)); 51 $this->assertEqual($match, "abc"); 52 $this->assertTrue($regex->match("AAABCabcdef", $match)); 53 $this->assertEqual($match, "ABC"); 54 } 55 function testMatchMultiple() { 56 $regex = &new JPSpan_LexerParallelRegex(true); 57 $regex->addPattern("abc"); 58 $regex->addPattern("ABC"); 59 $this->assertTrue($regex->match("abcdef", $match)); 60 $this->assertEqual($match, "abc"); 61 $this->assertTrue($regex->match("AAABCabcdef", $match)); 62 $this->assertEqual($match, "ABC"); 63 $this->assertFalse($regex->match("Hello", $match)); 64 } 65 function testPatternLabels() { 66 $regex = &new JPSpan_LexerParallelRegex(false); 67 $regex->addPattern("abc", "letter"); 68 $regex->addPattern("123", "number"); 69 $this->assertIdentical($regex->match("abcdef", $match), "letter"); 70 $this->assertEqual($match, "abc"); 71 $this->assertIdentical($regex->match("0123456789", $match), "number"); 72 $this->assertEqual($match, "123"); 73 } 74 } 75 76 /** 77 * @package WACT_TESTS 78 */ 79 class TestOfLexerStateStack extends UnitTestCase { 80 function TestOfLexerStateStack() { 81 $this->UnitTestCase(); 82 } 83 function testStartState() { 84 $stack = &new JPSpan_LexerStateStack("one"); 85 $this->assertEqual($stack->getCurrent(), "one"); 86 } 87 function testExhaustion() { 88 $stack = &new JPSpan_LexerStateStack("one"); 89 $this->assertFalse($stack->leave()); 90 } 91 function testStateMoves() { 92 $stack = &new JPSpan_LexerStateStack("one"); 93 $stack->enter("two"); 94 $this->assertEqual($stack->getCurrent(), "two"); 95 $stack->enter("three"); 96 $this->assertEqual($stack->getCurrent(), "three"); 97 $this->assertTrue($stack->leave()); 98 $this->assertEqual($stack->getCurrent(), "two"); 99 $stack->enter("third"); 100 $this->assertEqual($stack->getCurrent(), "third"); 101 $this->assertTrue($stack->leave()); 102 $this->assertTrue($stack->leave()); 103 $this->assertEqual($stack->getCurrent(), "one"); 104 } 105 } 106 107 class TestParser { 108 function TestParser() { 109 } 110 function accept() { 111 } 112 function a() { 113 } 114 function b() { 115 } 116 } 117 Mock::generate('TestParser'); 118 119 class TestOfLexer extends UnitTestCase { 120 function TestOfLexer() { 121 $this->UnitTestCase(); 122 } 123 function testNoPatterns() { 124 $handler = &new MockTestParser($this); 125 $handler->expectNever("accept"); 126 $handler->setReturnValue("accept", true); 127 $lexer = &new JPSpan_Lexer($handler); 128 $this->assertFalse($lexer->parse("abcdef")); 129 } 130 function testEmptyPage() { 131 $handler = &new MockTestParser($this); 132 $handler->expectNever("accept"); 133 $handler->setReturnValue("accept", true); 134 $handler->expectNever("accept"); 135 $handler->setReturnValue("accept", true); 136 $lexer = &new JPSpan_Lexer($handler); 137 $lexer->addPattern("a+"); 138 $this->assertTrue($lexer->parse("")); 139 } 140 function testSinglePattern() { 141 $handler = &new MockTestParser($this); 142 $handler->expectArgumentsAt(0, "accept", array("aaa", JPSPAN_LEXER_MATCHED)); 143 $handler->expectArgumentsAt(1, "accept", array("x", JPSPAN_LEXER_UNMATCHED)); 144 $handler->expectArgumentsAt(2, "accept", array("a", JPSPAN_LEXER_MATCHED)); 145 $handler->expectArgumentsAt(3, "accept", array("yyy", JPSPAN_LEXER_UNMATCHED)); 146 $handler->expectArgumentsAt(4, "accept", array("a", JPSPAN_LEXER_MATCHED)); 147 $handler->expectArgumentsAt(5, "accept", array("x", JPSPAN_LEXER_UNMATCHED)); 148 $handler->expectArgumentsAt(6, "accept", array("aaa", JPSPAN_LEXER_MATCHED)); 149 $handler->expectArgumentsAt(7, "accept", array("z", JPSPAN_LEXER_UNMATCHED)); 150 $handler->expectCallCount("accept", 8); 151 $handler->setReturnValue("accept", true); 152 $lexer = &new JPSpan_Lexer($handler); 153 $lexer->addPattern("a+"); 154 $this->assertTrue($lexer->parse("aaaxayyyaxaaaz")); 155 $handler->tally(); 156 } 157 function testMultiplePattern() { 158 $handler = &new MockTestParser($this); 159 $target = array("a", "b", "a", "bb", "x", "b", "a", "xxxxxx", "a", "x"); 160 for ($i = 0; $i < count($target); $i++) { 161 $handler->expectArgumentsAt($i, "accept", array($target[$i], '*')); 162 } 163 $handler->expectCallCount("accept", count($target)); 164 $handler->setReturnValue("accept", true); 165 $lexer = &new JPSpan_Lexer($handler); 166 $lexer->addPattern("a+"); 167 $lexer->addPattern("b+"); 168 $this->assertTrue($lexer->parse("ababbxbaxxxxxxax")); 169 $handler->tally(); 170 } 171 } 172 173 class TestOfLexerModes extends UnitTestCase { 174 function TestOfLexerModes() { 175 $this->UnitTestCase(); 176 } 177 function testIsolatedPattern() { 178 $handler = &new MockTestParser($this); 179 $handler->expectArgumentsAt(0, "a", array("a", JPSPAN_LEXER_MATCHED)); 180 $handler->expectArgumentsAt(1, "a", array("b", JPSPAN_LEXER_UNMATCHED)); 181 $handler->expectArgumentsAt(2, "a", array("aa", JPSPAN_LEXER_MATCHED)); 182 $handler->expectArgumentsAt(3, "a", array("bxb", JPSPAN_LEXER_UNMATCHED)); 183 $handler->expectArgumentsAt(4, "a", array("aaa", JPSPAN_LEXER_MATCHED)); 184 $handler->expectArgumentsAt(5, "a", array("x", JPSPAN_LEXER_UNMATCHED)); 185 $handler->expectArgumentsAt(6, "a", array("aaaa", JPSPAN_LEXER_MATCHED)); 186 $handler->expectArgumentsAt(7, "a", array("x", JPSPAN_LEXER_UNMATCHED)); 187 $handler->expectCallCount("a", 8); 188 $handler->setReturnValue("a", true); 189 $lexer = &new JPSpan_Lexer($handler, "a"); 190 $lexer->addPattern("a+", "a"); 191 $lexer->addPattern("b+", "b"); 192 $this->assertTrue($lexer->parse("abaabxbaaaxaaaax")); 193 $handler->tally(); 194 } 195 function testModeChange() { 196 $handler = &new MockTestParser($this); 197 $handler->expectArgumentsAt(0, "a", array("a", JPSPAN_LEXER_MATCHED)); 198 $handler->expectArgumentsAt(1, "a", array("b", JPSPAN_LEXER_UNMATCHED)); 199 $handler->expectArgumentsAt(2, "a", array("aa", JPSPAN_LEXER_MATCHED)); 200 $handler->expectArgumentsAt(3, "a", array("b", JPSPAN_LEXER_UNMATCHED)); 201 $handler->expectArgumentsAt(4, "a", array("aaa", JPSPAN_LEXER_MATCHED)); 202 $handler->expectArgumentsAt(0, "b", array(":", JPSPAN_LEXER_ENTER)); 203 $handler->expectArgumentsAt(1, "b", array("a", JPSPAN_LEXER_UNMATCHED)); 204 $handler->expectArgumentsAt(2, "b", array("b", JPSPAN_LEXER_MATCHED)); 205 $handler->expectArgumentsAt(3, "b", array("a", JPSPAN_LEXER_UNMATCHED)); 206 $handler->expectArgumentsAt(4, "b", array("bb", JPSPAN_LEXER_MATCHED)); 207 $handler->expectArgumentsAt(5, "b", array("a", JPSPAN_LEXER_UNMATCHED)); 208 $handler->expectArgumentsAt(6, "b", array("bbb", JPSPAN_LEXER_MATCHED)); 209 $handler->expectArgumentsAt(7, "b", array("a", JPSPAN_LEXER_UNMATCHED)); 210 $handler->expectCallCount("a", 5); 211 $handler->expectCallCount("b", 8); 212 $handler->setReturnValue("a", true); 213 $handler->setReturnValue("b", true); 214 $lexer = &new JPSpan_Lexer($handler, "a"); 215 $lexer->addPattern("a+", "a"); 216 $lexer->addEntryPattern(":", "a", "b"); 217 $lexer->addPattern("b+", "b"); 218 $this->assertTrue($lexer->parse("abaabaaa:ababbabbba")); 219 $handler->tally(); 220 } 221 function testNesting() { 222 $handler = &new MockTestParser($this); 223 $handler->setReturnValue("a", true); 224 $handler->setReturnValue("b", true); 225 $handler->expectArgumentsAt(0, "a", array("aa", JPSPAN_LEXER_MATCHED)); 226 $handler->expectArgumentsAt(1, "a", array("b", JPSPAN_LEXER_UNMATCHED)); 227 $handler->expectArgumentsAt(2, "a", array("aa", JPSPAN_LEXER_MATCHED)); 228 $handler->expectArgumentsAt(3, "a", array("b", JPSPAN_LEXER_UNMATCHED)); 229 $handler->expectArgumentsAt(0, "b", array("(", JPSPAN_LEXER_ENTER)); 230 $handler->expectArgumentsAt(1, "b", array("bb", JPSPAN_LEXER_MATCHED)); 231 $handler->expectArgumentsAt(2, "b", array("a", JPSPAN_LEXER_UNMATCHED)); 232 $handler->expectArgumentsAt(3, "b", array("bb", JPSPAN_LEXER_MATCHED)); 233 $handler->expectArgumentsAt(4, "b", array(")", JPSPAN_LEXER_EXIT)); 234 $handler->expectArgumentsAt(4, "a", array("aa", JPSPAN_LEXER_MATCHED)); 235 $handler->expectArgumentsAt(5, "a", array("b", JPSPAN_LEXER_UNMATCHED)); 236 $handler->expectCallCount("a", 6); 237 $handler->expectCallCount("b", 5); 238 $lexer = &new JPSpan_Lexer($handler, "a"); 239 $lexer->addPattern("a+", "a"); 240 $lexer->addEntryPattern("(", "a", "b"); 241 $lexer->addPattern("b+", "b"); 242 $lexer->addExitPattern(")", "b"); 243 $this->assertTrue($lexer->parse("aabaab(bbabb)aab")); 244 $handler->tally(); 245 } 246 function testSingular() { 247 $handler = &new MockTestParser($this); 248 $handler->setReturnValue("a", true); 249 $handler->setReturnValue("b", true); 250 $handler->expectArgumentsAt(0, "a", array("aa", JPSPAN_LEXER_MATCHED)); 251 $handler->expectArgumentsAt(1, "a", array("aa", JPSPAN_LEXER_MATCHED)); 252 $handler->expectArgumentsAt(2, "a", array("xx", JPSPAN_LEXER_UNMATCHED)); 253 $handler->expectArgumentsAt(3, "a", array("xx", JPSPAN_LEXER_UNMATCHED)); 254 $handler->expectArgumentsAt(0, "b", array("b", JPSPAN_LEXER_SPECIAL)); 255 $handler->expectArgumentsAt(1, "b", array("bbb", JPSPAN_LEXER_SPECIAL)); 256 $handler->expectCallCount("a", 4); 257 $handler->expectCallCount("b", 2); 258 $lexer = &new JPSpan_Lexer($handler, "a"); 259 $lexer->addPattern("a+", "a"); 260 $lexer->addSpecialPattern("b+", "a", "b"); 261 $this->assertTrue($lexer->parse("aabaaxxbbbxx")); 262 $handler->tally(); 263 } 264 function testUnwindTooFar() { 265 $handler = &new MockTestParser($this); 266 $handler->setReturnValue("a", true); 267 $handler->expectArgumentsAt(0, "a", array("aa", JPSPAN_LEXER_MATCHED)); 268 $handler->expectArgumentsAt(1, "a", array(")", JPSPAN_LEXER_EXIT)); 269 $handler->expectCallCount("a", 2); 270 $lexer = &new JPSpan_Lexer($handler, "a"); 271 $lexer->addPattern("a+", "a"); 272 $lexer->addExitPattern(")", "a"); 273 $this->assertFalse($lexer->parse("aa)aa")); 274 $handler->tally(); 275 } 276 } 277 278 class TestOfLexerHandlers extends UnitTestCase { 279 function TestOfLexerHandlers() { 280 $this->UnitTestCase(); 281 } 282 function testModeMapping() { 283 $handler = &new MockTestParser($this); 284 $handler->setReturnValue("a", true); 285 $handler->expectArgumentsAt(0, "a", array("aa", JPSPAN_LEXER_MATCHED)); 286 $handler->expectArgumentsAt(1, "a", array("(", JPSPAN_LEXER_ENTER)); 287 $handler->expectArgumentsAt(2, "a", array("bb", JPSPAN_LEXER_MATCHED)); 288 $handler->expectArgumentsAt(3, "a", array("a", JPSPAN_LEXER_UNMATCHED)); 289 $handler->expectArgumentsAt(4, "a", array("bb", JPSPAN_LEXER_MATCHED)); 290 $handler->expectArgumentsAt(5, "a", array(")", JPSPAN_LEXER_EXIT)); 291 $handler->expectArgumentsAt(6, "a", array("b", JPSPAN_LEXER_UNMATCHED)); 292 $handler->expectCallCount("a", 7); 293 $lexer = &new JPSpan_Lexer($handler, "mode_a"); 294 $lexer->addPattern("a+", "mode_a"); 295 $lexer->addEntryPattern("(", "mode_a", "mode_b"); 296 $lexer->addPattern("b+", "mode_b"); 297 $lexer->addExitPattern(")", "mode_b"); 298 $lexer->mapHandler("mode_a", "a"); 299 $lexer->mapHandler("mode_b", "a"); 300 $this->assertTrue($lexer->parse("aa(bbabb)b")); 301 $handler->tally(); 302 } 303 } 304 305 /** 306 * Conditional test runner 307 */ 308 if (!defined('TEST_RUNNING')) { 309 define('TEST_RUNNING', true); 310 $test = & new GroupTest('UnserializerGroupTest'); 311 $test->addTestCase(new TestOfLexerParallelRegex()); 312 $test->addTestCase(new TestOfLexerStateStack()); 313 $test->addTestCase(new TestOfLexer()); 314 $test->addTestCase(new TestOfLexerModes()); 315 $test->addTestCase(new TestOfLexerHandlers()); 316 $test->run(new HtmlReporter()); 317 } 318 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Fri Nov 26 11:42:46 2004 | Cross-referenced by PHPXref 0.6 |