diff options
author | Chris Smith <chris.eureka@jalakai.co.uk> | 2009-04-19 15:44:50 +0200 |
---|---|---|
committer | Chris Smith <chris.eureka@jalakai.co.uk> | 2009-04-19 15:44:50 +0200 |
commit | 2895686a53a433ee6d243315ff6186de326f005f (patch) | |
tree | 033fe579d61d131f81922be264e9be941e4e8d34 | |
parent | b446308f29808c2df119fd3d79cfb9ac2e49e7ac (diff) | |
download | rpg-2895686a53a433ee6d243315ff6186de326f005f.tar.gz rpg-2895686a53a433ee6d243315ff6186de326f005f.tar.bz2 |
add unit tests for correct pattern selection when patterns contain non-captured elements (e.g. boundaries, lookaheads & lookbehinds)
darcs-hash:20090419134450-f07c6-4ff7d226fcba002c840828336e73fb89cf48e3db.gz
-rw-r--r-- | _test/cases/inc/parser/lexer.test.php | 33 | ||||
-rw-r--r-- | _test/cases/inc/parser/parser.inc.php | 1 | ||||
-rw-r--r-- | _test/cases/inc/parser/parser_replacements.test.php | 17 | ||||
-rw-r--r-- | inc/parser/lexer.php | 3 |
4 files changed, 50 insertions, 4 deletions
diff --git a/_test/cases/inc/parser/lexer.test.php b/_test/cases/inc/parser/lexer.test.php index 75fa32ee0..cbfce9ba9 100644 --- a/_test/cases/inc/parser/lexer.test.php +++ b/_test/cases/inc/parser/lexer.test.php @@ -446,7 +446,7 @@ class TestOfLexerByteIndices extends UnitTestCase { $handler->expectCallCount("caught", 5); $lexer = &new Doku_Lexer($handler, "ignore"); - $lexer->addEntryPattern('<file>(?=.*\x3C/file\x3E)', "ignore", "caught"); + $lexer->addEntryPattern('<file>(?=.*</file>)', "ignore", "caught"); $lexer->addExitPattern("</file>", "caught"); $lexer->addSpecialPattern('b','caught','special'); $lexer->mapHandler('special','caught'); @@ -590,7 +590,36 @@ class TestOfLexerByteIndices extends UnitTestCase { $this->assertTrue($lexer->parse($doc)); $handler->tally(); } - + + /** + * This test is primarily to ensure the correct match is chosen + * when there are non-captured elements in the pattern. + */ + function testIndexSelectCorrectMatch() { + $doc = "ALL FOOLS ARE FOO"; + $pattern = '\bFOO\b'; + + $handler = &new MockTestParserByteIndex($this); + $handler->setReturnValue("ignore", true); + $handler->setReturnValue("caught", true); + + $matches = array(); + preg_match('/'.$pattern.'/',$doc,$matches,PREG_OFFSET_CAPTURE); + + $handler->expectArgumentsAt( + 0, + "caught", + array("FOO", DOKU_LEXER_SPECIAL, $matches[0][1]) + ); + $handler->expectCallCount("caught", 1); + + $lexer = &new Doku_Lexer($handler, "ignore"); + $lexer->addSpecialPattern($pattern,'ignore','caught'); + + $this->assertTrue($lexer->parse($doc)); + $handler->tally(); + } + } ?> diff --git a/_test/cases/inc/parser/parser.inc.php b/_test/cases/inc/parser/parser.inc.php index 45ab67f9f..22574d579 100644 --- a/_test/cases/inc/parser/parser.inc.php +++ b/_test/cases/inc/parser/parser.inc.php @@ -14,6 +14,7 @@ require_once DOKU_INC . 'inc/parser/parser.php'; require_once DOKU_INC . 'inc/parser/handler.php'; require_once DOKU_INC . 'inc/events.php'; require_once DOKU_INC . 'inc/mail.php'; + //require_once DOKU . 'parser/renderer.php'; //Mock::generate('Doku_Renderer'); diff --git a/_test/cases/inc/parser/parser_replacements.test.php b/_test/cases/inc/parser/parser_replacements.test.php index 73d3bf59e..11ebbd80a 100644 --- a/_test/cases/inc/parser/parser_replacements.test.php +++ b/_test/cases/inc/parser/parser_replacements.test.php @@ -40,6 +40,23 @@ class TestOfDoku_Parser_Replacements extends TestOfDoku_Parser { $this->assertEqual(array_map('stripbyteindex',$this->H->calls),$calls); } + function testPickAcronymCorrectly() { + $this->P->addMode('acronym',new Doku_Parser_Mode_Acronym(array('FOO'))); + $this->P->parse('ALL FOOLS ARE FOO'); + + $calls = array ( + array('document_start',array()), + array('p_open',array()), + array('cdata',array("\n".'ALL FOOLS ARE ')), + array('acronym',array('FOO')), + array('cdata',array("\n")), + array('p_close',array()), + array('document_end',array()), + ); + + $this->assertEqual(array_map('stripbyteindex',$this->H->calls),$calls); + } + function testMultipleAcronyms() { $this->P->addMode('acronym',new Doku_Parser_Mode_Acronym(array('FOO','BAR'))); $this->P->parse('abc FOO def BAR xyz'); diff --git a/inc/parser/lexer.php b/inc/parser/lexer.php index 78a8d04f5..86e818d9c 100644 --- a/inc/parser/lexer.php +++ b/inc/parser/lexer.php @@ -136,10 +136,9 @@ class Doku_LexerParallelRegex { } $idx = count($matches)-2; - list($pre, $post) = preg_split($this->_patterns[$idx].$this->_getPerlMatchingFlags(), $subject, 2); - $split = array($pre, $matches[0], $post); + return isset($this->_labels[$idx]) ? $this->_labels[$idx] : true; } |