UnitTestCase(); } function testNoPatterns() { $regex = &new Doku_LexerParallelRegex(false); $this->assertFalse($regex->match("Hello", $match)); $this->assertEqual($match, ""); } function testNoSubject() { $regex = &new Doku_LexerParallelRegex(false); $regex->addPattern(".*"); $this->assertTrue($regex->match("", $match)); $this->assertEqual($match, ""); } function testMatchAll() { $regex = &new Doku_LexerParallelRegex(false); $regex->addPattern(".*"); $this->assertTrue($regex->match("Hello", $match)); $this->assertEqual($match, "Hello"); } function testCaseSensitive() { $regex = &new Doku_LexerParallelRegex(true); $regex->addPattern("abc"); $this->assertTrue($regex->match("abcdef", $match)); $this->assertEqual($match, "abc"); $this->assertTrue($regex->match("AAABCabcdef", $match)); $this->assertEqual($match, "abc"); } function testCaseInsensitive() { $regex = &new Doku_LexerParallelRegex(false); $regex->addPattern("abc"); $this->assertTrue($regex->match("abcdef", $match)); $this->assertEqual($match, "abc"); $this->assertTrue($regex->match("AAABCabcdef", $match)); $this->assertEqual($match, "ABC"); } function testMatchMultiple() { $regex = &new Doku_LexerParallelRegex(true); $regex->addPattern("abc"); $regex->addPattern("ABC"); $this->assertTrue($regex->match("abcdef", $match)); $this->assertEqual($match, "abc"); $this->assertTrue($regex->match("AAABCabcdef", $match)); $this->assertEqual($match, "ABC"); $this->assertFalse($regex->match("Hello", $match)); } function testPatternLabels() { $regex = &new Doku_LexerParallelRegex(false); $regex->addPattern("abc", "letter"); $regex->addPattern("123", "number"); $this->assertIdentical($regex->match("abcdef", $match), "letter"); $this->assertEqual($match, "abc"); $this->assertIdentical($regex->match("0123456789", $match), "number"); $this->assertEqual($match, "123"); } function testMatchMultipleWithLookaheadNot() { $regex = &new Doku_LexerParallelRegex(true); $regex->addPattern("abc"); $regex->addPattern("ABC"); $regex->addPattern("a(?!\n).{1}"); $this->assertTrue($regex->match("abcdef", $match)); $this->assertEqual($match, "abc"); $this->assertTrue($regex->match("AAABCabcdef", $match)); $this->assertEqual($match, "ABC"); $this->assertTrue($regex->match("a\nab", $match)); $this->assertEqual($match, "ab"); $this->assertFalse($regex->match("Hello", $match)); } function testMatchSetOptionCaseless() { $regex = &new Doku_LexerParallelRegex(true); $regex->addPattern("a(?i)b(?i)c"); $this->assertTrue($regex->match("aBc", $match)); $this->assertEqual($match, "aBc"); } function testMatchSetOptionUngreedy() { $regex = &new Doku_LexerParallelRegex(true); $regex->addPattern("(?U)\w+"); $this->assertTrue($regex->match("aaaaaa", $match)); $this->assertEqual($match, "a"); } function testMatchLookaheadEqual() { $regex = &new Doku_LexerParallelRegex(true); $regex->addPattern("\w(?=c)"); $this->assertTrue($regex->match("xbyczd", $match)); $this->assertEqual($match, "y"); } function testMatchLookaheadNot() { $regex = &new Doku_LexerParallelRegex(true); $regex->addPattern("\w(?!b|c)"); $this->assertTrue($regex->match("xbyczd", $match)); $this->assertEqual($match, "b"); } function testMatchLookbehindEqual() { $regex = &new Doku_LexerParallelRegex(true); $regex->addPattern("(?<=c)\w"); $this->assertTrue($regex->match("xbyczd", $match)); $this->assertEqual($match, "z"); } function testMatchLookbehindNot() { $regex = &new Doku_LexerParallelRegex(true); $regex->addPattern("(?assertTrue($regex->match("xbyczd", $match)); $this->assertEqual($match, "c"); } } class TestOfLexerStateStack extends UnitTestCase { function TestOfLexerStateStack() { $this->UnitTestCase(); } function testStartState() { $stack = &new Doku_LexerStateStack("one"); $this->assertEqual($stack->getCurrent(), "one"); } function testExhaustion() { $stack = &new Doku_LexerStateStack("one"); $this->assertFalse($stack->leave()); } function testStateMoves() { $stack = &new Doku_LexerStateStack("one"); $stack->enter("two"); $this->assertEqual($stack->getCurrent(), "two"); $stack->enter("three"); $this->assertEqual($stack->getCurrent(), "three"); $this->assertTrue($stack->leave()); $this->assertEqual($stack->getCurrent(), "two"); $stack->enter("third"); $this->assertEqual($stack->getCurrent(), "third"); $this->assertTrue($stack->leave()); $this->assertTrue($stack->leave()); $this->assertEqual($stack->getCurrent(), "one"); } } class TestParser { function TestParser() { } function accept() { } function a() { } function b() { } } Mock::generate('TestParser'); class TestOfLexer extends UnitTestCase { function TestOfLexer() { $this->UnitTestCase(); } function testNoPatterns() { $handler = &new MockTestParser($this); $handler->expectNever("accept"); $handler->setReturnValue("accept", true); $lexer = &new Doku_Lexer($handler); $this->assertFalse($lexer->parse("abcdef")); } function testEmptyPage() { $handler = &new MockTestParser($this); $handler->expectNever("accept"); $handler->setReturnValue("accept", true); $handler->expectNever("accept"); $handler->setReturnValue("accept", true); $lexer = &new Doku_Lexer($handler); $lexer->addPattern("a+"); $this->assertTrue($lexer->parse("")); } function testSinglePattern() { $handler = &new MockTestParser($this); $handler->expectArgumentsAt(0, "accept", array("aaa", DOKU_LEXER_MATCHED, 0)); $handler->expectArgumentsAt(1, "accept", array("x", DOKU_LEXER_UNMATCHED, 3)); $handler->expectArgumentsAt(2, "accept", array("a", DOKU_LEXER_MATCHED, 4)); $handler->expectArgumentsAt(3, "accept", array("yyy", DOKU_LEXER_UNMATCHED, 5)); $handler->expectArgumentsAt(4, "accept", array("a", DOKU_LEXER_MATCHED, 8)); $handler->expectArgumentsAt(5, "accept", array("x", DOKU_LEXER_UNMATCHED, 9)); $handler->expectArgumentsAt(6, "accept", array("aaa", DOKU_LEXER_MATCHED, 10)); $handler->expectArgumentsAt(7, "accept", array("z", DOKU_LEXER_UNMATCHED, 13)); $handler->expectCallCount("accept", 8); $handler->setReturnValue("accept", true); $lexer = &new Doku_Lexer($handler); $lexer->addPattern("a+"); $this->assertTrue($lexer->parse("aaaxayyyaxaaaz")); $handler->tally(); } function testMultiplePattern() { $handler = &new MockTestParser($this); $target = array("a", "b", "a", "bb", "x", "b", "a", "xxxxxx", "a", "x"); $positions = array(0,1,2,3,5,6,7,8,14,15); for ($i = 0; $i < count($target); $i++) { $handler->expectArgumentsAt($i, "accept", array($target[$i], '*', $positions[$i])); } $handler->expectCallCount("accept", count($target)); $handler->setReturnValue("accept", true); $lexer = &new Doku_Lexer($handler); $lexer->addPattern("a+"); $lexer->addPattern("b+"); $this->assertTrue($lexer->parse("ababbxbaxxxxxxax")); $handler->tally(); } } class TestOfLexerModes extends UnitTestCase { function TestOfLexerModes() { $this->UnitTestCase(); } function testIsolatedPattern() { $handler = &new MockTestParser($this); $handler->expectArgumentsAt(0, "a", array("a", DOKU_LEXER_MATCHED,0)); $handler->expectArgumentsAt(1, "a", array("b", DOKU_LEXER_UNMATCHED,1)); $handler->expectArgumentsAt(2, "a", array("aa", DOKU_LEXER_MATCHED,2)); $handler->expectArgumentsAt(3, "a", array("bxb", DOKU_LEXER_UNMATCHED,4)); $handler->expectArgumentsAt(4, "a", array("aaa", DOKU_LEXER_MATCHED,7)); $handler->expectArgumentsAt(5, "a", array("x", DOKU_LEXER_UNMATCHED,10)); $handler->expectArgumentsAt(6, "a", array("aaaa", DOKU_LEXER_MATCHED,11)); $handler->expectArgumentsAt(7, "a", array("x", DOKU_LEXER_UNMATCHED,15)); $handler->expectCallCount("a", 8); $handler->setReturnValue("a", true); $lexer = &new Doku_Lexer($handler, "a"); $lexer->addPattern("a+", "a"); $lexer->addPattern("b+", "b"); $this->assertTrue($lexer->parse("abaabxbaaaxaaaax")); $handler->tally(); } function testModeChange() { $handler = &new MockTestParser($this); $handler->expectArgumentsAt(0, "a", array("a", DOKU_LEXER_MATCHED,0)); $handler->expectArgumentsAt(1, "a", array("b", DOKU_LEXER_UNMATCHED,1)); $handler->expectArgumentsAt(2, "a", array("aa", DOKU_LEXER_MATCHED,2)); $handler->expectArgumentsAt(3, "a", array("b", DOKU_LEXER_UNMATCHED,4)); $handler->expectArgumentsAt(4, "a", array("aaa", DOKU_LEXER_MATCHED,5)); $handler->expectArgumentsAt(0, "b", array(":", DOKU_LEXER_ENTER,8)); $handler->expectArgumentsAt(1, "b", array("a", DOKU_LEXER_UNMATCHED,9)); $handler->expectArgumentsAt(2, "b", array("b", DOKU_LEXER_MATCHED, 10)); $handler->expectArgumentsAt(3, "b", array("a", DOKU_LEXER_UNMATCHED,11)); $handler->expectArgumentsAt(4, "b", array("bb", DOKU_LEXER_MATCHED,12)); $handler->expectArgumentsAt(5, "b", array("a", DOKU_LEXER_UNMATCHED,14)); $handler->expectArgumentsAt(6, "b", array("bbb", DOKU_LEXER_MATCHED,15)); $handler->expectArgumentsAt(7, "b", array("a", DOKU_LEXER_UNMATCHED,18)); $handler->expectCallCount("a", 5); $handler->expectCallCount("b", 8); $handler->setReturnValue("a", true); $handler->setReturnValue("b", true); $lexer = &new Doku_Lexer($handler, "a"); $lexer->addPattern("a+", "a"); $lexer->addEntryPattern(":", "a", "b"); $lexer->addPattern("b+", "b"); $this->assertTrue($lexer->parse("abaabaaa:ababbabbba")); $handler->tally(); } function testNesting() { $handler = &new MockTestParser($this); $handler->setReturnValue("a", true); $handler->setReturnValue("b", true); $handler->expectArgumentsAt(0, "a", array("aa", DOKU_LEXER_MATCHED,0)); $handler->expectArgumentsAt(1, "a", array("b", DOKU_LEXER_UNMATCHED,2)); $handler->expectArgumentsAt(2, "a", array("aa", DOKU_LEXER_MATCHED,3)); $handler->expectArgumentsAt(3, "a", array("b", DOKU_LEXER_UNMATCHED,5)); $handler->expectArgumentsAt(0, "b", array("(", DOKU_LEXER_ENTER,6)); $handler->expectArgumentsAt(1, "b", array("bb", DOKU_LEXER_MATCHED,7)); $handler->expectArgumentsAt(2, "b", array("a", DOKU_LEXER_UNMATCHED,9)); $handler->expectArgumentsAt(3, "b", array("bb", DOKU_LEXER_MATCHED,10)); $handler->expectArgumentsAt(4, "b", array(")", DOKU_LEXER_EXIT,12)); $handler->expectArgumentsAt(4, "a", array("aa", DOKU_LEXER_MATCHED,13)); $handler->expectArgumentsAt(5, "a", array("b", DOKU_LEXER_UNMATCHED,15)); $handler->expectCallCount("a", 6); $handler->expectCallCount("b", 5); $lexer = &new Doku_Lexer($handler, "a"); $lexer->addPattern("a+", "a"); $lexer->addEntryPattern("(", "a", "b"); $lexer->addPattern("b+", "b"); $lexer->addExitPattern(")", "b"); $this->assertTrue($lexer->parse("aabaab(bbabb)aab")); $handler->tally(); } function testSingular() { $handler = &new MockTestParser($this); $handler->setReturnValue("a", true); $handler->setReturnValue("b", true); $handler->expectArgumentsAt(0, "a", array("aa", DOKU_LEXER_MATCHED,0)); $handler->expectArgumentsAt(1, "a", array("aa", DOKU_LEXER_MATCHED,3)); $handler->expectArgumentsAt(2, "a", array("xx", DOKU_LEXER_UNMATCHED,5)); $handler->expectArgumentsAt(3, "a", array("xx", DOKU_LEXER_UNMATCHED,10)); $handler->expectArgumentsAt(0, "b", array("b", DOKU_LEXER_SPECIAL,2)); $handler->expectArgumentsAt(1, "b", array("bbb", DOKU_LEXER_SPECIAL,7)); $handler->expectCallCount("a", 4); $handler->expectCallCount("b", 2); $lexer = &new Doku_Lexer($handler, "a"); $lexer->addPattern("a+", "a"); $lexer->addSpecialPattern("b+", "a", "b"); $this->assertTrue($lexer->parse("aabaaxxbbbxx")); $handler->tally(); } function testUnwindTooFar() { $handler = &new MockTestParser($this); $handler->setReturnValue("a", true); $handler->expectArgumentsAt(0, "a", array("aa", DOKU_LEXER_MATCHED,0)); $handler->expectArgumentsAt(1, "a", array(")", DOKU_LEXER_EXIT,2)); $handler->expectCallCount("a", 2); $lexer = &new Doku_Lexer($handler, "a"); $lexer->addPattern("a+", "a"); $lexer->addExitPattern(")", "a"); $this->assertFalse($lexer->parse("aa)aa")); $handler->tally(); } } class TestOfLexerHandlers extends UnitTestCase { function TestOfLexerHandlers() { $this->UnitTestCase(); } function testModeMapping() { $handler = &new MockTestParser($this); $handler->setReturnValue("a", true); $handler->expectArgumentsAt(0, "a", array("aa", DOKU_LEXER_MATCHED,0)); $handler->expectArgumentsAt(1, "a", array("(", DOKU_LEXER_ENTER,2)); $handler->expectArgumentsAt(2, "a", array("bb", DOKU_LEXER_MATCHED,3)); $handler->expectArgumentsAt(3, "a", array("a", DOKU_LEXER_UNMATCHED,5)); $handler->expectArgumentsAt(4, "a", array("bb", DOKU_LEXER_MATCHED,6)); $handler->expectArgumentsAt(5, "a", array(")", DOKU_LEXER_EXIT,8)); $handler->expectArgumentsAt(6, "a", array("b", DOKU_LEXER_UNMATCHED,9)); $handler->expectCallCount("a", 7); $lexer = &new Doku_Lexer($handler, "mode_a"); $lexer->addPattern("a+", "mode_a"); $lexer->addEntryPattern("(", "mode_a", "mode_b"); $lexer->addPattern("b+", "mode_b"); $lexer->addExitPattern(")", "mode_b"); $lexer->mapHandler("mode_a", "a"); $lexer->mapHandler("mode_b", "a"); $this->assertTrue($lexer->parse("aa(bbabb)b")); $handler->tally(); } } class TestParserByteIndex { function TestParserByteIndex() {} function ignore() {} function catch() {} } Mock::generate('TestParserByteIndex'); class TestOfLexerByteIndices extends UnitTestCase { function TestOfLexerByteIndices() { $this->UnitTestCase(); } function testIndex() { $doc = "aaabcdeee"; $handler = &new MockTestParserByteIndex($this); $handler->setReturnValue("ignore", true); $handler->setReturnValue("catch", true); $handler->expectArgumentsAt( 0, "catch", array("", DOKU_LEXER_ENTER, strpos($doc,'')) ); $handler->expectArgumentsAt( 1, "catch", array("b", DOKU_LEXER_SPECIAL, strpos($doc,'b')) ); $handler->expectArgumentsAt( 2, "catch", array("c", DOKU_LEXER_MATCHED, strpos($doc,'c')) ); $handler->expectArgumentsAt( 3, "catch", array("d", DOKU_LEXER_UNMATCHED, strpos($doc,'d')) ); $handler->expectArgumentsAt( 4, "catch", array("", DOKU_LEXER_EXIT, strpos($doc,'')) ); $handler->expectCallCount("catch", 5); $lexer = &new Doku_Lexer($handler, "ignore"); $lexer->addEntryPattern("", "ignore", "catch"); $lexer->addExitPattern("", "catch"); $lexer->addSpecialPattern('b','catch','special'); $lexer->mapHandler('special','catch'); $lexer->addPattern('c','catch'); $this->assertTrue($lexer->parse($doc)); $handler->tally(); } function testIndexLookaheadEqual() { $doc = "aaabcdeee"; $handler = &new MockTestParserByteIndex($this); $handler->setReturnValue("ignore", true); $handler->setReturnValue("catch", true); $handler->expectArgumentsAt( 0, "catch", array("", DOKU_LEXER_ENTER, strpos($doc,'')) ); $handler->expectArgumentsAt( 1, "catch", array("b", DOKU_LEXER_SPECIAL, strpos($doc,'b')) ); $handler->expectArgumentsAt( 2, "catch", array("c", DOKU_LEXER_MATCHED, strpos($doc,'c')) ); $handler->expectArgumentsAt( 3, "catch", array("d", DOKU_LEXER_UNMATCHED, strpos($doc,'d')) ); $handler->expectArgumentsAt( 4, "catch", array("", DOKU_LEXER_EXIT, strpos($doc,'')) ); $handler->expectCallCount("catch", 5); $lexer = &new Doku_Lexer($handler, "ignore"); $lexer->addEntryPattern('(?=.*\x3C/file\x3E)', "ignore", "catch"); $lexer->addExitPattern("", "catch"); $lexer->addSpecialPattern('b','catch','special'); $lexer->mapHandler('special','catch'); $lexer->addPattern('c','catch'); $this->assertTrue($lexer->parse($doc)); $handler->tally(); } function testIndexLookaheadNotEqual() { $doc = "aaabcdeee"; $handler = &new MockTestParserByteIndex($this); $handler->setReturnValue("ignore", true); $handler->setReturnValue("catch", true); $handler->expectArgumentsAt( 0, "catch", array("", DOKU_LEXER_ENTER, strpos($doc,'')) ); $handler->expectArgumentsAt( 1, "catch", array("b", DOKU_LEXER_SPECIAL, strpos($doc,'b')) ); $handler->expectArgumentsAt( 2, "catch", array("c", DOKU_LEXER_MATCHED, strpos($doc,'c')) ); $handler->expectArgumentsAt( 3, "catch", array("d", DOKU_LEXER_UNMATCHED, strpos($doc,'d')) ); $handler->expectArgumentsAt( 4, "catch", array("", DOKU_LEXER_EXIT, strpos($doc,'')) ); $handler->expectCallCount("catch", 5); $lexer = &new Doku_Lexer($handler, "ignore"); $lexer->addEntryPattern('(?!foo)', "ignore", "catch"); $lexer->addExitPattern("", "catch"); $lexer->addSpecialPattern('b','catch','special'); $lexer->mapHandler('special','catch'); $lexer->addPattern('c','catch'); $this->assertTrue($lexer->parse($doc)); $handler->tally(); } function testIndexLookbehindEqual() { $doc = "aaabcdeee"; $handler = &new MockTestParserByteIndex($this); $handler->setReturnValue("ignore", true); $handler->setReturnValue("catch", true); $handler->expectArgumentsAt( 0, "catch", array("", DOKU_LEXER_ENTER, strpos($doc,'')) ); $handler->expectArgumentsAt( 1, "catch", array("b", DOKU_LEXER_SPECIAL, strpos($doc,'b')) ); $handler->expectArgumentsAt( 2, "catch", array("c", DOKU_LEXER_MATCHED, strpos($doc,'c')) ); $handler->expectArgumentsAt( 3, "catch", array("d", DOKU_LEXER_UNMATCHED, strpos($doc,'d')) ); $handler->expectArgumentsAt( 4, "catch", array("", DOKU_LEXER_EXIT, strpos($doc,'')) ); $handler->expectCallCount("catch", 5); $lexer = &new Doku_Lexer($handler, "ignore"); $lexer->addEntryPattern('', "ignore", "catch"); $lexer->addExitPattern("(?<=d)", "catch"); $lexer->addSpecialPattern('b','catch','special'); $lexer->mapHandler('special','catch'); $lexer->addPattern('c','catch'); $this->assertTrue($lexer->parse($doc)); $handler->tally(); } function testIndexLookbehindNotEqual() { $doc = "aaabcdeee"; $handler = &new MockTestParserByteIndex($this); $handler->setReturnValue("ignore", true); $handler->setReturnValue("catch", true); $handler->expectArgumentsAt( 0, "catch", array("", DOKU_LEXER_ENTER, strpos($doc,'')) ); $handler->expectArgumentsAt( 1, "catch", array("b", DOKU_LEXER_SPECIAL, strpos($doc,'b')) ); $handler->expectArgumentsAt( 2, "catch", array("c", DOKU_LEXER_MATCHED, strpos($doc,'c')) ); $handler->expectArgumentsAt( 3, "catch", array("d", DOKU_LEXER_UNMATCHED, strpos($doc,'d')) ); $handler->expectArgumentsAt( 4, "catch", array("", DOKU_LEXER_EXIT, strpos($doc,'')) ); $handler->expectCallCount("catch", 5); $lexer = &new Doku_Lexer($handler, "ignore"); $lexer->addEntryPattern('', "ignore", "catch"); $lexer->addExitPattern("(?", "catch"); $lexer->addSpecialPattern('b','catch','special'); $lexer->mapHandler('special','catch'); $lexer->addPattern('c','catch'); $this->assertTrue($lexer->parse($doc)); $handler->tally(); } } /** * Conditional test runner */ if (!defined('TEST_RUNNING')) { define('TEST_RUNNING', true); $test = & new GroupTest('LexerTests'); $test->addTestCase(new TestOfLexerParallelRegex()); $test->addTestCase(new TestOfLexerStateStack()); $test->addTestCase(new TestOfLexer()); $test->addTestCase(new TestOfLexerModes()); $test->addTestCase(new TestOfLexerHandlers()); $test->addTestCase(new TestOfLexerByteIndices()); $test->run(new HtmlReporter()); } ?>