From 2895686a53a433ee6d243315ff6186de326f005f Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Sun, 19 Apr 2009 15:44:50 +0200 Subject: add unit tests for correct pattern selection when patterns contain non-captured elements (e.g. boundaries, lookaheads & lookbehinds) darcs-hash:20090419134450-f07c6-4ff7d226fcba002c840828336e73fb89cf48e3db.gz --- _test/cases/inc/parser/lexer.test.php | 33 ++++++++++++++++++++-- _test/cases/inc/parser/parser.inc.php | 1 + .../cases/inc/parser/parser_replacements.test.php | 17 +++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) (limited to '_test') 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('(?=.*\x3C/file\x3E)', "ignore", "caught"); + $lexer->addEntryPattern('(?=.*)', "ignore", "caught"); $lexer->addExitPattern("", "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'); -- cgit v1.2.3