cvs: pear /PHP_Parser package.xml /PHP_Parser/Parser/DocBlock DefaultInlineTagLexer.php /PHP_Parser/tests
PHP_Parser_DocBlock_DefaultInlineTagLexer_test.php testsuite.php
| From: | Greg Beaver | Date: | Sat, 13 Mar 2004 20:30:21 +0000 |
| Subject: | cvs: pear /PHP_Parser package.xml /PHP_Parser/Parser/DocBlock DefaultInlineTagLexer.php /PHP_Parser/tests PHP_Parser_DocBlock_DefaultInlineTagLexer_test.php testsuite.php |
||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-17372@lists.php.net to get a copy of this message | ||
cellog Sat Mar 13 15:30:21 2004 EDT
Added files:
/pear/PHP_Parser/tests
PHP_Parser_DocBlock_DefaultInlineTagLexer_test.php
Modified files:
/pear/PHP_Parser/Parser/DocBlock DefaultInlineTagLexer.php
/pear/PHP_Parser package.xml
/pear/PHP_Parser/tests testsuite.php
Log:
fix up all bugs in the default inline tag lexer, add a test case for every single method
http://cvs.php.net/diff.php/pear/PHP_Parser/Parser/DocBlock/DefaultInlineTagLexer.php?r1=1.1&r2=1.2&ty=u Index: pear/PHP_Parser/Parser/DocBlock/DefaultInlineTagLexer.php diff -u pear/PHP_Parser/Parser/DocBlock/DefaultInlineTagLexer.php:1.1 pear/PHP_Parser/Parser/DocBlock/DefaultInlineTagLexer.php:1.2 --- pear/PHP_Parser/Parser/DocBlock/DefaultInlineTagLexer.php:1.1 Sat Mar 13 01:35:49 2004 +++ pear/PHP_Parser/Parser/DocBlock/DefaultInlineTagLexer.php Sat Mar 13 15:30:14 2004 @@ -11,11 +11,28 @@ */ var $_options = array(); /** + * Stores the tokens for the inline tag + * + * The inline tag buffer has three components: + * + * 1 tokens with trailing whitespace + * 2 tokens without whitespace + * 3 trailing whitespace only + * + * $_buf is a zero-indexed array (index 0 is tokens with whitespace). + * + * If the inline tag allows separators (like ","), then it is an array + * of buffers as described above (see {@link $_multipleBuffers}) * @var string * @access private */ var $_buf; /** + * @var Error_Stack + * @access private + */ + var $_stack; + /** * @var integer * @access private */ @@ -44,19 +61,7 @@ 'multi-word' => 'getWords', 'allwords' => 'getAllWords', ); - /** - * return something useful, when a parse error occurs. - * - * used to build error messages if the parser fails, and needs to know the line number.. - * - * @return string - * @access public - */ - function parseError() - { - return "Error at line {$this->yyline}"; - } - + /** * Set Lexer options * @@ -85,26 +90,80 @@ function getLink() { // must have at least 1 word to be a valid link - $ret = $this->getWord(); - if (!$ret) { + $word = $this->getWord(); + if (!$word) { return false; } - $save = $ret; - if (strpos($ret, '#')) { - $ret = substr($ret, strpos($ret, '#') + 1); // chop off package selector - } - // if the word is function or global - if (in_array($ret, array('function', 'global'))) { - $ret = $save; - if ($this->_readBuf(false)) { - $ret .= $ret . ' ' . $this->_readBuf(); + $details = array(); + $details['link'] = $details['text'] = $word; + $details['valid'] = true; + $details['type'] = 'unknown'; + $save = $word; + if (strpos($word, '#')) { + $details['link'] = $word = substr($word, strpos($word, '#') + 1); // chop off package selector + $details['package'] = substr($save, 0, strlen($save) - strlen($word) - 1); + } + if (in_array($word, array('function', 'global', 'object', 'constant'))) { + $details['link'] = false; + $details['type'] = $word; + if ($word = $this->_readBuf()) { + $details['text'] .= ' ' . $word; + $details['link'] = $word; + $is_func = false; + if (strpos($word, '::')) { + $details['valid'] = false; + $details['link'] = false; + } + if (strpos($word, '()') && $details['type'] != 'function') { + $details['valid'] = false; + $details['link'] = false; + } elseif ($details['type'] == 'function') { + if (strpos($word, '::')) { + $details['valid'] = true; + $details['link'] = substr($word, strpos($word, '::') + 2); + $details['class'] = substr($word, 0, + strlen($word) - strlen($details['link']) - 2); + $details['type'] = 'method'; + } + $details['link'] = str_replace('()', '', $details['link']); + } + if (strpos($word, '$') === 0 && $details['type'] != 'global') { + $details['valid'] = false; + $details['link'] = false; + } } else { - return false; + // invalid link + $details['valid'] = false; + $details['link'] = false; } } else { - return $save; + $is_func = false; + $is_var = false; + $is_member = false; + if (strpos($word, '()')) { + $details['link'] = str_replace('()', '', $details['link']); + $is_func = true; + } + if (strpos($word, '$') === 0) { + $is_var = true; + } + if (strpos($word, '::')) { + $is_member = true; + } + if ($is_member) { + $details['link'] = substr($word, strpos($word, '::') + 2); + $details['class'] = substr($word, 0, + strlen($word) - strlen($details['link']) - 2); + if (strpos($details['link'], '$') === 0) { + $is_var = true; + } + $details['type'] = $is_func ? 'method' : ($is_var ? 'var' : 'class constant'); + $details['link'] = str_replace('()', '', $details['link']); + } else { + $details['type'] = $is_func ? 'function' : ($is_var ? 'global' : 'unknown'); + } } - return $ret; + return $details; } /** @@ -117,7 +176,7 @@ } /** - * Retrieve multiple words from the tag contents + * Retrieve multiple words from the tag contents with trailing whitespace * @param integer the number of words to return * @return false|string */ @@ -157,6 +216,7 @@ * Read a word from the buffer * @param boolean whether to increment the index into the buffer * @param boolean whether to include whitespace + * @return false|string * @access private */ function _readBuf($inc = true, $whitespace = false) @@ -167,7 +227,7 @@ $this->_index++; } if (!isset($buf[1][$index])) { - return null; + return false; } if ($whitespace) { return $buf[0][$index]; @@ -189,7 +249,7 @@ !isset($this->_buf[$this->_bufIndex + 1])) { return false; } - return $this->_bufIndex < count($this->_buf); + return $this->_bufIndex < count($this->_buf) - 1; } /** @@ -198,12 +258,14 @@ */ function moveNextBuf() { - if (isset($this->_buf[++$this->_bufIndex][0])) { - $this->_index = 0; - return $this->_buf[$this->_bufIndex]; - } else { + if (!$this->hasNextBuf()) { + if ($this->_multipleBuffers) { + ++$this->_bufIndex; + } return false; } + $this->_index = 0; + return $this->_buf[++$this->_bufIndex]; } /** @@ -231,25 +293,47 @@ * Prepare inline tag contents for lexing. * * This function replaces the current buffer with the data after - * splitting it along whitespace, and along any separators + * splitting it along whitespace, and along any separators. + * + * Note that separators may be escaped using a backslash. If the separator is + * a comma ",", this string "my \, is escaped, tried, and true" will split into: + * - "my , is escaped" + * - "tried" + * - "and true" + * + * Note that all leading whitespace is removed in all contexts ("tried" instead of " tried") * passed to {@link setOptions()} * @param string */ function newTag($data) { + $this->_buf = array(); $data = ltrim($data); if ($this->_options['separator']) { $data2 = explode($this->_options['separator'], $data); + $newdata = array(); + $save = ''; + // post-process + foreach ($data2 as $data) { + // allow escaping of the separator using "\" + if (substr($data, -1) == '\\') { + $save = substr($data, 0, strlen($data) - 1) . $this->_options['separator']; + } else { + $newdata[] = $save . $data; + $save = ''; + } + } + $data2 = $newdata; $this->_bufIndex = 0; foreach($data2 as $data) { - // strip open and close {@ } and collapse all whitespace + // collapse all whitespace preg_match_all('/([^\s]+)(\s+)?/', ltrim($data), $this->_buf[$this->_bufIndex++]); // $this->_buf[$this->_bufIndex++] = preg_split("/\s+/", ltrim($data)); } $this->_bufIndex = 0; $this->_multipleBuffers = true; } else { - // strip open and close {@ } and collapse all whitespace + // collapse all whitespace preg_match_all('/([^\s]+)(\s+)?/', ltrim($data), $this->_buf); } $this->_index = 0; @@ -264,5 +348,7 @@ $this->debug = false; $this->setOptions(); $this->newTag($data); +// $this->_stack = &PHP_Parser_Stack::singleton('PHP_Parser_DocBlock_DefaultInlineTagLexer'); } } +?> \ No newline at end of file http://cvs.php.net/diff.php/pear/PHP_Parser/package.xml?r1=1.7&r2=1.8&ty=u Index: pear/PHP_Parser/package.xml diff -u pear/PHP_Parser/package.xml:1.7 pear/PHP_Parser/package.xml:1.8 --- pear/PHP_Parser/package.xml:1.7 Tue Mar 9 11:50:00 2004 +++ pear/PHP_Parser/package.xml Sat Mar 13 15:30:18 2004 @@ -74,12 +74,16 @@ <file role="php" name="Stack.php"/> <file role="php" name="Structure.php"/> <file role="php" name="Tokenizer.php"/> + <dir name="DocBlock"> + <file role="php" name="DefaultInlineTagLexer.php"/> + </dir> <!-- Parser/DocBlock --> <dir name="Tokenizer"> <file role="php" name="Structure.php"/> </dir> <!-- Parser/Tokenizer --> </dir> <!-- Parser --> <dir name="tests"> <file role="test" name="HTML_TestListener.php"/> + <file role="test" name="PHP_Parser_DocBlock_DefaultInlineTagLexer_test.php"/> <file role="test" name="PHP_Parser_MsgServer_test.php"/> <file role="test" name="PHP_Parser_test_class.php"/> <file role="test" name="PHP_Parser_test_class_php5.php"/> http://cvs.php.net/diff.php/pear/PHP_Parser/tests/testsuite.php?r1=1.2&r2=1.3&ty=u Index: pear/PHP_Parser/tests/testsuite.php diff -u pear/PHP_Parser/tests/testsuite.php:1.2 pear/PHP_Parser/tests/testsuite.php:1.3 --- pear/PHP_Parser/tests/testsuite.php:1.2 Tue Mar 9 11:48:04 2004 +++ pear/PHP_Parser/tests/testsuite.php Sat Mar 13 15:30:19 2004 @@ -3,7 +3,7 @@ * HTML output for PHPUnit suite tests. * * Copied for PEAR_PackageFileManager from HTML_CSS - * @version $Id: testsuite.php,v 1.2 2004/03/09 16:48:04 cellog Exp $ + * @version $Id: testsuite.php,v 1.3 2004/03/13 20:30:19 cellog Exp $ * @author Laurent Laville <pear@laurent-laville.org> * @package HTML_CSS */ @@ -11,6 +11,7 @@ require_once 'TestUnit.php'; require_once 'HTML_TestListener.php'; require_once 'PHP/Parser.php'; +require_once 'PHP/Parser/DocBlock/DefaultInlineTagLexer.php'; $title = 'PhpUnit test run, PHP_Parser package'; ?> @@ -60,6 +61,7 @@ } $testcases[] = 'PHP_Parser_MsgServer_test'; + $testcases[] = 'PHP_Parser_DocBlock_DefaultInlineTagLexer_test'; $suite = new PHPUnit_TestSuite(); http://cvs.php.net/co.php/pear/PHP_Parser/tests/PHP_Parser_DocBlock_DefaultInlineTagLexer_test.php?r=1.1&p=1 Index: pear/PHP_Parser/tests/PHP_Parser_DocBlock_DefaultInlineTagLexer_test.php +++ pear/PHP_Parser/tests/PHP_Parser_DocBlock_DefaultInlineTagLexer_test.php <?php /** * API Unit tests for PHP_Parser package. * * @version $Id: PHP_Parser_DocBlock_DefaultInlineTagLexer_test.php,v 1.1 2004/03/13 20:30:19 cellog Exp $ * @author Laurent Laville <pear@laurent-laville.org> portions from HTML_CSS * @author Greg Beaver * @package PHP_Parser */ /** * @package PHP_Parser */ class PHP_Parser_DocBlock_DefaultInlineTagLexer_test extends PHPUnit_TestCase { /** * A PHP_Parser_DocBlock_DefaultInlineTagLexer object (or one of the drivers) * @var object */ var $lexer; /** * This will be Error_Stack if the package is accepted into PEAR */ var $errorStackClass = 'PHP_Parser_Stack'; /**#@+ * Error Stacks */ var $pstack; var $sstack; var $estack; function PHP_Parser_DocBlock_DefaultInlineTagLexer_test($name) { $this->PHPUnit_TestCase($name); } function setUp() { error_reporting(E_ALL); $this->errorOccured = false; set_error_handler(array(&$this, 'errorHandler')); $this->lexer = new PHP_Parser_DocBlock_DefaultInlineTagLexer; $this->_expectedErrors = array(); $this->_testMethod = 'unknown'; } function tearDown() { unset($this->lexer); restore_error_handler(); } function errorCodeToString($code) { $codes = array( 'PHP_Parser_DocBlock_DefaultInlineTagLexer' => array( ), ); return $codes[$code]; } function _stripWhitespace($str) { return preg_replace('/\\s+/', '', $str); } function _methodExists($name) { $test = (version_compare(phpversion(), '4.3.5', '>') == 1) ? $name : strtolower($name); if (in_array($test, get_class_methods($this->lexer))) { return true; } $this->assertTrue(false, 'method '. $name . ' not implemented in ' . get_class($this->lexer)); return false; } function errorHandler($errno, $errstr, $errfile, $errline) { if (error_reporting() == 0) { return; } //die("$errstr in $errfile at line $errline: $errstr"); $this->errorOccured = true; $this->assertTrue(false, "$errstr at line $errline, $errfile"); } function test_setOptions1() { if (!$this->_methodExists('setOptions')) { return; } $this->assertEquals(array('separator' => false), $this->lexer->_options); $this->lexer->setOptions(); $this->assertEquals(array('separator' => false), $this->lexer->_options); } function test_setOptions2() { if (!$this->_methodExists('setOptions')) { return; } $this->assertEquals(array('separator' => false), $this->lexer->_options); $this->lexer->setOptions(array('separator' => ',')); $this->assertEquals(array('separator' => ','), $this->lexer->_options); $this->lexer->setOptions(); $this->assertEquals(array('separator' => false), $this->lexer->_options); } function test_newTag_empty() { if (!$this->_methodExists('newTag')) { return; } $this->lexer->newTag(''); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(), array(), array()), $this->lexer->_buf); } function test_newTag_oneword() { if (!$this->_methodExists('newTag')) { return; } $this->lexer->newTag('test'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array('test'), array('test'), array('')), $this->lexer->_buf, 1); $this->lexer->newTag('test '); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array('test '), array('test'), array(' ')), $this->lexer->_buf, 2); $this->lexer->newTag(' test'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array('test'), array('test'), array('')), $this->lexer->_buf, 3); } function test_newTag_twowords() { if (!$this->_methodExists('newTag')) { return; } $this->lexer->newTag('test two'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array('test ','two'), array('test','two'), array(' ','')), $this->lexer->_buf, 1); $this->lexer->newTag('test two'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array('test ','two'), array('test','two'), array(' ','')), $this->lexer->_buf, 2); } function test_newTag_oneword_multibuffers() { if (!$this->_methodExists('setOptions')) { return; } if (!$this->_methodExists('newTag')) { return; } $this->lexer->setOptions(array('separator' => ',')); $this->lexer->newTag('test'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(array('test'), array('test'), array(''))), $this->lexer->_buf, 1); $this->lexer->newTag('test '); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(array('test '), array('test'), array(' '))), $this->lexer->_buf, 2); $this->lexer->newTag(' test'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(array('test'), array('test'), array(''))), $this->lexer->_buf, 3); } function test_newTag_twowords_multibuffers() { if (!$this->_methodExists('setOptions')) { return; } if (!$this->_methodExists('newTag')) { return; } $this->lexer->setOptions(array('separator' => ',')); $this->lexer->newTag('test two'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(array('test ','two'), array('test','two'), array(' ',''))), $this->lexer->_buf, 1); $this->lexer->newTag('test two'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(array('test ','two'), array('test','two'), array(' ',''))), $this->lexer->_buf, 2); } function test_newTag_twowords_separator() { if (!$this->_methodExists('setOptions')) { return; } if (!$this->_methodExists('newTag')) { return; } $this->lexer->setOptions(array('separator' => ',')); $this->lexer->newTag('test,two'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(array('test'), array('test'), array('')), array(array('two'), array('two'), array(''))), $this->lexer->_buf, 1); $this->lexer->newTag('test , two'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(array('test '), array('test'), array(' ')), array(array('two'), array('two'), array(''))), $this->lexer->_buf, 2); $this->lexer->newTag('test, two'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(array('test'), array('test'), array('')), array(array('two'), array('two'), array(''))), $this->lexer->_buf, 3); $this->lexer->newTag('test ,two '); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(array('test '), array('test'), array(' ')), array(array('two '), array('two'), array(' '))), $this->lexer->_buf, 4); } function test_newTag_escapeseparator_multibuffers() { if (!$this->_methodExists('setOptions')) { return; } if (!$this->_methodExists('newTag')) { return; } $this->lexer->setOptions(array('separator' => ',')); $this->lexer->newTag('test\\,'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(array('test,'), array('test,'), array(''))), $this->lexer->_buf, 1); $this->lexer->newTag('test\\, hi'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(array('test, ','hi'), array('test,','hi'), array(' ',''))), $this->lexer->_buf, 2); $this->lexer->newTag('test\\, hi,there '); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(array('test, ','hi'), array('test,','hi'), array(' ','')), array(array('there '), array('there'), array(' '))), $this->lexer->_buf, 3); } function test_getBuf() { if (!$this->_methodExists('setOptions')) { return; } if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('_getBuf')) { return; } $this->lexer->newTag('test'); $buf = $this->lexer->_getBuf(); $this->assertEquals(array(array('test'), array('test'), array('')), $buf, 1); $this->lexer->setOptions(array('separator' => ',')); $this->lexer->newTag('test,two'); $buf = $this->lexer->_getBuf(); $this->assertEquals(array(array('test'), array('test'), array('')), $buf, 2); $this->lexer->_bufIndex++; $buf = $this->lexer->_getBuf(); $this->assertEquals(array(array('two'), array('two'), array('')), $buf, 3); } function test_moveNextBuf() { if (!$this->_methodExists('setOptions')) { return; } if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('_getBuf')) { return; } if (!$this->_methodExists('moveNextBuf')) { return; } $this->lexer->newTag('test'); $buf = $this->lexer->_getBuf(); $this->assertEquals(array(array('test'), array('test'), array('')), $buf, 1); $this->assertFalse($this->lexer->moveNextBuf(), 'moveNextBuf 1'); $buf = $this->lexer->_getBuf(); $this->assertEquals(array(array('test'), array('test'), array('')), $buf, 1); $this->lexer->setOptions(array('separator' => ',')); $this->lexer->newTag('test,two'); $buf = $this->lexer->_getBuf(); $this->assertEquals(array(array('test'), array('test'), array('')), $buf, 2); $this->assertEquals(array(array('two'), array('two'), array('')), $this->lexer->moveNextBuf(), 'moveNextBuf 2'); $buf = $this->lexer->_getBuf(); $this->assertEquals(array(array('two'), array('two'), array('')), $buf, 3); $this->assertFalse($this->lexer->moveNextBuf(), 'moveNextBuf false'); $this->assertFalse($this->lexer->_getBuf(), '_getBuf false'); } function test_hasNextBuf() { if (!$this->_methodExists('setOptions')) { return; } if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('_getBuf')) { return; } if (!$this->_methodExists('moveNextBuf')) { return; } if (!$this->_methodExists('hasNextBuf')) { return; } $this->lexer->newTag('test'); $buf = $this->lexer->_getBuf(); $this->assertEquals(array(array('test'), array('test'), array('')), $buf, 1); $this->assertFalse($this->lexer->moveNextBuf(), 'moveNextBuf 1'); $this->assertFalse($this->lexer->hasNextBuf(), 'hasNextBuf 1'); $buf = $this->lexer->_getBuf(); $this->assertEquals(array(array('test'), array('test'), array('')), $buf, 1); $this->lexer->setOptions(array('separator' => ',')); $this->lexer->newTag('test,two'); $this->assertTrue($this->lexer->hasNextBuf(), 'hasNextBuf 2'); $buf = $this->lexer->_getBuf(); $this->assertEquals(array(array('test'), array('test'), array('')), $buf, 2); $this->assertEquals(array(array('two'), array('two'), array('')), $this->lexer->moveNextBuf(), 'moveNextBuf 2'); $this->assertFalse($this->lexer->hasNextBuf(), 'hasNextBuf 3'); $buf = $this->lexer->_getBuf(); $this->assertEquals(array(array('two'), array('two'), array('')), $buf, 3); } function test_readBuf_empty() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('_readBuf')) { return; } $this->lexer->newTag(''); $this->assertTrue($this->lexer->_readBuf() === false, 1); $this->assertTrue($this->lexer->_readBuf(false, true) === false, 2); $this->assertTrue($this->lexer->_readBuf(true) === false, 3); $this->assertTrue($this->lexer->_readBuf(true, true) === false, 4); } function test_readBuf_singleBuffer() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('_readBuf')) { return; } $this->lexer->newTag('test me '); $this->assertEquals('test', $this->lexer->_readBuf(false), 1); $this->assertEquals('test', $this->lexer->_readBuf(false), 2); $this->assertEquals('test ', $this->lexer->_readBuf(false, true), 3); $this->assertEquals('test', $this->lexer->_readBuf(true), 4); $this->assertEquals('me', $this->lexer->_readBuf(false), 5); $this->assertEquals('me ', $this->lexer->_readBuf(true, true), 6); $this->assertTrue($this->lexer->_readBuf(true, true) === false, 7); } function test_readBuf_multiBuffer() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('_readBuf')) { return; } if (!$this->_methodExists('moveNextBuf')) { return; } if (!$this->_methodExists('setOptions')) { return; } $this->lexer->setOptions(array('separator' => '#')); $this->lexer->newTag('test me # hi '); $this->assertEquals('test', $this->lexer->_readBuf(false), 1); $this->assertEquals('test', $this->lexer->_readBuf(false), 2); $this->assertEquals('test ', $this->lexer->_readBuf(false, true), 3); $this->assertEquals('test', $this->lexer->_readBuf(true), 4); $this->assertEquals('me', $this->lexer->_readBuf(false), 5); $this->assertEquals('me ', $this->lexer->_readBuf(true, true), 6); $this->assertTrue($this->lexer->_readBuf(true, true) === false, 7); $this->assertEquals(array(array('hi '), array('hi'), array(' ')), $this->lexer->moveNextBuf(), 'move'); $this->assertEquals('hi', $this->lexer->_readBuf(false), 8); $this->assertEquals('hi ', $this->lexer->_readBuf(false, true), 9); $this->assertEquals('hi ', $this->lexer->_readBuf(true, true), 10); $this->assertTrue($this->lexer->_readBuf(true, true) === false, 11); } function test_getWord_empty() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getWord')) { return; } $this->lexer->newTag(''); $this->assertTrue($this->lexer->getWord() === false, 1); $this->assertTrue($this->lexer->getWord() === false, 2); } function test_getWord_singleBuffer() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getWord')) { return; } $this->lexer->newTag('test me '); $this->assertEquals('test', $this->lexer->getWord(), 1); $this->assertEquals('me', $this->lexer->getWord(), 2); $this->assertTrue($this->lexer->getWord() === false, 3); } function test_getWord_multiBuffer() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getWord')) { return; } if (!$this->_methodExists('moveNextBuf')) { return; } if (!$this->_methodExists('setOptions')) { return; } $this->lexer->setOptions(array('separator' => '#')); $this->lexer->newTag('test me # hi '); $this->assertEquals('test', $this->lexer->getWord(), 1); $this->assertEquals('me', $this->lexer->getWord(), 2); $this->assertTrue($this->lexer->getWord() === false, 3); $this->assertEquals(array(array('hi '), array('hi'), array(' ')), $this->lexer->moveNextBuf(), 'move'); $this->assertEquals('hi', $this->lexer->getWord(), 4); $this->assertTrue($this->lexer->getWord() === false, 5); } function test_getAllWords_singleBuffer() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getAllWords')) { return; } $this->lexer->newTag('test me baby'); $this->assertEquals('test me baby', $this->lexer->getAllWords(), 1); $this->assertFalse($this->lexer->getAllWords(), 2); } function test_getAllWords_multiBuffer() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getAllWords')) { return; } if (!$this->_methodExists('moveNextBuf')) { return; } if (!$this->_methodExists('setOptions')) { return; } $this->lexer->setOptions(array('separator' => '#')); $this->lexer->newTag('test me baby# hi there sweetness '); $this->assertEquals('test me baby', $this->lexer->getAllWords(), 1); $this->assertFalse($this->lexer->getAllWords(), 1.1); $this->assertEquals(array(array('hi ','there ', 'sweetness '), array('hi','there','sweetness'), array(' ',' ',' ')), $this->lexer->moveNextBuf(), 'move'); $this->assertEquals('hi there sweetness', $this->lexer->getAllWords(), 2); $this->assertFalse($this->lexer->getAllWords(), 2.1); } function test_getWords_singleBuffer() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getWords')) { return; } $this->lexer->newTag('test me baby '); $this->assertEquals('test me', $this->lexer->getWords(array('count' => 2)), 1); $this->assertEquals('baby', $this->lexer->getWords(array('count' => 2)), 2); $this->assertFalse($this->lexer->getWords(array('count' => 2)), 3); $this->lexer->newTag('test me baby'); $this->assertEquals('test me baby', $this->lexer->getWords(array('count' => 4)), 4); $this->assertFalse($this->lexer->getWords(array('count' => 2)), 5); $this->lexer->newTag('test me baby'); $this->assertEquals('test', $this->lexer->getWords(array('count' => 1)), 6); $this->assertEquals('me', $this->lexer->getWords(array('count' => 1)), 7); $this->assertEquals('baby', $this->lexer->getWords(array('count' => 1)), 8); $this->assertFalse($this->lexer->getWords(array('count' => 2)), 9); } function test_getWords_multiBuffer() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getWords')) { return; } if (!$this->_methodExists('moveNextBuf')) { return; } if (!$this->_methodExists('setOptions')) { return; } $this->lexer->setOptions(array('separator' => '#')); $this->lexer->newTag('test me baby# hi there sweetness '); $this->assertEquals('test me', $this->lexer->getWords(array('count' => 2)), 1); $this->assertEquals('baby', $this->lexer->getWords(array('count' => 2)), 2); $this->assertFalse($this->lexer->getWords(array('count' => 2)), 3); $this->assertEquals(array(array('hi ','there ', 'sweetness '), array('hi','there','sweetness'), array(' ',' ',' ')), $this->lexer->moveNextBuf(), 'move'); $this->assertEquals('hi there', $this->lexer->getWords(array('count' => 2)), 1); $this->assertEquals('sweetness', $this->lexer->getWords(array('count' => 2)), 2); $this->assertFalse($this->lexer->getWords(array('count' => 2)), 3); } function test_getLink_empty() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getLink')) { return; } $this->lexer->newTag(''); $this->assertTrue($this->lexer->getLink() === false, 1); $this->assertTrue($this->lexer->getLink() === false, 2); } function test_getLink_unknown() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getLink')) { return; } $this->lexer->newTag('test'); $this->assertEquals( array( 'text' => 'test', 'link' => 'test', 'valid' => true, 'type' => 'unknown', ), $this->lexer->getLink(), 1); $this->assertFalse($this->lexer->getLink(), 2); } function test_invalid_getLink() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getLink')) { return; } $this->lexer->newTag('object test()'); $this->assertEquals( array( 'text' => 'object test()', 'link' => false, 'valid' => false, 'type' => 'object', ), $this->lexer->getLink(), 1); $this->lexer->newTag('global test()'); $this->assertEquals( array( 'text' => 'global test()', 'link' => false, 'valid' => false, 'type' => 'global', ), $this->lexer->getLink(), 2); $this->lexer->newTag('constant test()'); $this->assertEquals( array( 'text' => 'constant test()', 'link' => false, 'valid' => false, 'type' => 'constant', ), $this->lexer->getLink(), 3); $this->lexer->newTag('constant $test'); $this->assertEquals( array( 'text' => 'constant $test', 'link' => false, 'valid' => false, 'type' => 'constant', ), $this->lexer->getLink(), 4); $this->lexer->newTag('object $test'); $this->assertEquals( array( 'text' => 'object $test', 'link' => false, 'valid' => false, 'type' => 'object', ), $this->lexer->getLink(), 5); $this->lexer->newTag('function $test'); $this->assertEquals( array( 'text' => 'function $test', 'link' => false, 'valid' => false, 'type' => 'function', ), $this->lexer->getLink(), 6); } function test_invalid_getLink2() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getLink')) { return; } $this->lexer->newTag('object'); $this->assertEquals( array( 'text' => 'object', 'link' => false, 'valid' => false, 'type' => 'object', ), $this->lexer->getLink(), 1); $this->lexer->newTag('global'); $this->assertEquals( array( 'text' => 'global', 'link' => false, 'valid' => false, 'type' => 'global', ), $this->lexer->getLink(), 2); $this->lexer->newTag('constant'); $this->assertEquals( array( 'text' => 'constant', 'link' => false, 'valid' => false, 'type' => 'constant', ), $this->lexer->getLink(), 3); $this->lexer->newTag('function'); $this->assertEquals( array( 'text' => 'function', 'link' => false, 'valid' => false, 'type' => 'function', ), $this->lexer->getLink(), 4); $this->lexer->newTag('global some::$var'); $this->assertEquals( array( 'text' => 'global some::$var', 'link' => false, 'valid' => false, 'type' => 'global', ), $this->lexer->getLink(), 4); } function test_getLink_disambiguate() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getLink')) { return; } $this->lexer->newTag('function test'); $this->assertEquals( array( 'text' => 'function test', 'link' => 'test', 'valid' => true, 'type' => 'function', ), $this->lexer->getLink(), 1); $this->lexer->newTag('object test'); $this->assertEquals( array( 'text' => 'object test', 'link' => 'test', 'valid' => true, 'type' => 'object', ), $this->lexer->getLink(), 1); $this->lexer->newTag('global $test'); $this->assertEquals( array( 'text' => 'global $test', 'link' => '$test', 'valid' => true, 'type' => 'global', ), $this->lexer->getLink(), 1); $this->lexer->newTag('constant test'); $this->assertEquals( array( 'text' => 'constant test', 'link' => 'test', 'valid' => true, 'type' => 'constant', ), $this->lexer->getLink(), 1); } function test_getLink_guesstype() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getLink')) { return; } $this->lexer->newTag('test()'); $this->assertEquals( array( 'text' => 'test()', 'link' => 'test', 'valid' => true, 'type' => 'function', ), $this->lexer->getLink(), 1); $this->lexer->newTag('some::$test'); $this->assertEquals( array( 'text' => 'some::$test', 'link' => '$test', 'valid' => true, 'type' => 'var', 'class' => 'some', ), $this->lexer->getLink(), 2); $this->lexer->newTag('function some::func()'); $this->assertEquals( array( 'text' => 'function some::func()', 'link' => 'func', 'valid' => true, 'type' => 'method', 'class' => 'some', ), $this->lexer->getLink(), 3); $this->lexer->newTag('some::func()'); $this->assertEquals( array( 'text' => 'some::func()', 'link' => 'func', 'valid' => true, 'type' => 'method', 'class' => 'some', ), $this->lexer->getLink(), 4); $this->lexer->newTag('some::definedconstant'); $this->assertEquals( array( 'text' => 'some::definedconstant', 'link' => 'definedconstant', 'valid' => true, 'type' => 'class constant', 'class' => 'some', ), $this->lexer->getLink(), 5); $this->lexer->newTag('$test'); $this->assertEquals( array( 'text' => '$test', 'link' => '$test', 'valid' => true, 'type' => 'global', ), $this->lexer->getLink(), 6); } function test_getLink_packageselector() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getLink')) { return; } $this->lexer->newTag('test#test'); $this->assertEquals( array( 'text' => 'test#test', 'link' => 'test', 'valid' => true, 'type' => 'unknown', 'package' => 'test', ), $this->lexer->getLink(), 1); $this->lexer->newTag('test#test()'); $this->assertEquals( array( 'text' => 'test#test()', 'link' => 'test', 'valid' => true, 'type' => 'function', 'package' => 'test', ), $this->lexer->getLink(), 2); } } ?>
http://cvs.php.net/diff.php/pear/PHP_Parser/Parser/DocBlock/DefaultInlineTagLexer.php?r1=1.1&r2=1.2&ty=u Index: pear/PHP_Parser/Parser/DocBlock/DefaultInlineTagLexer.php diff -u pear/PHP_Parser/Parser/DocBlock/DefaultInlineTagLexer.php:1.1 pear/PHP_Parser/Parser/DocBlock/DefaultInlineTagLexer.php:1.2 --- pear/PHP_Parser/Parser/DocBlock/DefaultInlineTagLexer.php:1.1 Sat Mar 13 01:35:49 2004 +++ pear/PHP_Parser/Parser/DocBlock/DefaultInlineTagLexer.php Sat Mar 13 15:30:14 2004 @@ -11,11 +11,28 @@ */ var $_options = array(); /** + * Stores the tokens for the inline tag + * + * The inline tag buffer has three components: + * + * 1 tokens with trailing whitespace + * 2 tokens without whitespace + * 3 trailing whitespace only + * + * $_buf is a zero-indexed array (index 0 is tokens with whitespace). + * + * If the inline tag allows separators (like ","), then it is an array + * of buffers as described above (see {@link $_multipleBuffers}) * @var string * @access private */ var $_buf; /** + * @var Error_Stack + * @access private + */ + var $_stack; + /** * @var integer * @access private */ @@ -44,19 +61,7 @@ 'multi-word' => 'getWords', 'allwords' => 'getAllWords', ); - /** - * return something useful, when a parse error occurs. - * - * used to build error messages if the parser fails, and needs to know the line number.. - * - * @return string - * @access public - */ - function parseError() - { - return "Error at line {$this->yyline}"; - } - + /** * Set Lexer options * @@ -85,26 +90,80 @@ function getLink() { // must have at least 1 word to be a valid link - $ret = $this->getWord(); - if (!$ret) { + $word = $this->getWord(); + if (!$word) { return false; } - $save = $ret; - if (strpos($ret, '#')) { - $ret = substr($ret, strpos($ret, '#') + 1); // chop off package selector - } - // if the word is function or global - if (in_array($ret, array('function', 'global'))) { - $ret = $save; - if ($this->_readBuf(false)) { - $ret .= $ret . ' ' . $this->_readBuf(); + $details = array(); + $details['link'] = $details['text'] = $word; + $details['valid'] = true; + $details['type'] = 'unknown'; + $save = $word; + if (strpos($word, '#')) { + $details['link'] = $word = substr($word, strpos($word, '#') + 1); // chop off package selector + $details['package'] = substr($save, 0, strlen($save) - strlen($word) - 1); + } + if (in_array($word, array('function', 'global', 'object', 'constant'))) { + $details['link'] = false; + $details['type'] = $word; + if ($word = $this->_readBuf()) { + $details['text'] .= ' ' . $word; + $details['link'] = $word; + $is_func = false; + if (strpos($word, '::')) { + $details['valid'] = false; + $details['link'] = false; + } + if (strpos($word, '()') && $details['type'] != 'function') { + $details['valid'] = false; + $details['link'] = false; + } elseif ($details['type'] == 'function') { + if (strpos($word, '::')) { + $details['valid'] = true; + $details['link'] = substr($word, strpos($word, '::') + 2); + $details['class'] = substr($word, 0, + strlen($word) - strlen($details['link']) - 2); + $details['type'] = 'method'; + } + $details['link'] = str_replace('()', '', $details['link']); + } + if (strpos($word, '$') === 0 && $details['type'] != 'global') { + $details['valid'] = false; + $details['link'] = false; + } } else { - return false; + // invalid link + $details['valid'] = false; + $details['link'] = false; } } else { - return $save; + $is_func = false; + $is_var = false; + $is_member = false; + if (strpos($word, '()')) { + $details['link'] = str_replace('()', '', $details['link']); + $is_func = true; + } + if (strpos($word, '$') === 0) { + $is_var = true; + } + if (strpos($word, '::')) { + $is_member = true; + } + if ($is_member) { + $details['link'] = substr($word, strpos($word, '::') + 2); + $details['class'] = substr($word, 0, + strlen($word) - strlen($details['link']) - 2); + if (strpos($details['link'], '$') === 0) { + $is_var = true; + } + $details['type'] = $is_func ? 'method' : ($is_var ? 'var' : 'class constant'); + $details['link'] = str_replace('()', '', $details['link']); + } else { + $details['type'] = $is_func ? 'function' : ($is_var ? 'global' : 'unknown'); + } } - return $ret; + return $details; } /** @@ -117,7 +176,7 @@ } /** - * Retrieve multiple words from the tag contents + * Retrieve multiple words from the tag contents with trailing whitespace * @param integer the number of words to return * @return false|string */ @@ -157,6 +216,7 @@ * Read a word from the buffer * @param boolean whether to increment the index into the buffer * @param boolean whether to include whitespace + * @return false|string * @access private */ function _readBuf($inc = true, $whitespace = false) @@ -167,7 +227,7 @@ $this->_index++; } if (!isset($buf[1][$index])) { - return null; + return false; } if ($whitespace) { return $buf[0][$index]; @@ -189,7 +249,7 @@ !isset($this->_buf[$this->_bufIndex + 1])) { return false; } - return $this->_bufIndex < count($this->_buf); + return $this->_bufIndex < count($this->_buf) - 1; } /** @@ -198,12 +258,14 @@ */ function moveNextBuf() { - if (isset($this->_buf[++$this->_bufIndex][0])) { - $this->_index = 0; - return $this->_buf[$this->_bufIndex]; - } else { + if (!$this->hasNextBuf()) { + if ($this->_multipleBuffers) { + ++$this->_bufIndex; + } return false; } + $this->_index = 0; + return $this->_buf[++$this->_bufIndex]; } /** @@ -231,25 +293,47 @@ * Prepare inline tag contents for lexing. * * This function replaces the current buffer with the data after - * splitting it along whitespace, and along any separators + * splitting it along whitespace, and along any separators. + * + * Note that separators may be escaped using a backslash. If the separator is + * a comma ",", this string "my \, is escaped, tried, and true" will split into: + * - "my , is escaped" + * - "tried" + * - "and true" + * + * Note that all leading whitespace is removed in all contexts ("tried" instead of " tried") * passed to {@link setOptions()} * @param string */ function newTag($data) { + $this->_buf = array(); $data = ltrim($data); if ($this->_options['separator']) { $data2 = explode($this->_options['separator'], $data); + $newdata = array(); + $save = ''; + // post-process + foreach ($data2 as $data) { + // allow escaping of the separator using "\" + if (substr($data, -1) == '\\') { + $save = substr($data, 0, strlen($data) - 1) . $this->_options['separator']; + } else { + $newdata[] = $save . $data; + $save = ''; + } + } + $data2 = $newdata; $this->_bufIndex = 0; foreach($data2 as $data) { - // strip open and close {@ } and collapse all whitespace + // collapse all whitespace preg_match_all('/([^\s]+)(\s+)?/', ltrim($data), $this->_buf[$this->_bufIndex++]); // $this->_buf[$this->_bufIndex++] = preg_split("/\s+/", ltrim($data)); } $this->_bufIndex = 0; $this->_multipleBuffers = true; } else { - // strip open and close {@ } and collapse all whitespace + // collapse all whitespace preg_match_all('/([^\s]+)(\s+)?/', ltrim($data), $this->_buf); } $this->_index = 0; @@ -264,5 +348,7 @@ $this->debug = false; $this->setOptions(); $this->newTag($data); +// $this->_stack = &PHP_Parser_Stack::singleton('PHP_Parser_DocBlock_DefaultInlineTagLexer'); } } +?> \ No newline at end of file http://cvs.php.net/diff.php/pear/PHP_Parser/package.xml?r1=1.7&r2=1.8&ty=u Index: pear/PHP_Parser/package.xml diff -u pear/PHP_Parser/package.xml:1.7 pear/PHP_Parser/package.xml:1.8 --- pear/PHP_Parser/package.xml:1.7 Tue Mar 9 11:50:00 2004 +++ pear/PHP_Parser/package.xml Sat Mar 13 15:30:18 2004 @@ -74,12 +74,16 @@ <file role="php" name="Stack.php"/> <file role="php" name="Structure.php"/> <file role="php" name="Tokenizer.php"/> + <dir name="DocBlock"> + <file role="php" name="DefaultInlineTagLexer.php"/> + </dir> <!-- Parser/DocBlock --> <dir name="Tokenizer"> <file role="php" name="Structure.php"/> </dir> <!-- Parser/Tokenizer --> </dir> <!-- Parser --> <dir name="tests"> <file role="test" name="HTML_TestListener.php"/> + <file role="test" name="PHP_Parser_DocBlock_DefaultInlineTagLexer_test.php"/> <file role="test" name="PHP_Parser_MsgServer_test.php"/> <file role="test" name="PHP_Parser_test_class.php"/> <file role="test" name="PHP_Parser_test_class_php5.php"/> http://cvs.php.net/diff.php/pear/PHP_Parser/tests/testsuite.php?r1=1.2&r2=1.3&ty=u Index: pear/PHP_Parser/tests/testsuite.php diff -u pear/PHP_Parser/tests/testsuite.php:1.2 pear/PHP_Parser/tests/testsuite.php:1.3 --- pear/PHP_Parser/tests/testsuite.php:1.2 Tue Mar 9 11:48:04 2004 +++ pear/PHP_Parser/tests/testsuite.php Sat Mar 13 15:30:19 2004 @@ -3,7 +3,7 @@ * HTML output for PHPUnit suite tests. * * Copied for PEAR_PackageFileManager from HTML_CSS - * @version $Id: testsuite.php,v 1.2 2004/03/09 16:48:04 cellog Exp $ + * @version $Id: testsuite.php,v 1.3 2004/03/13 20:30:19 cellog Exp $ * @author Laurent Laville <pear@laurent-laville.org> * @package HTML_CSS */ @@ -11,6 +11,7 @@ require_once 'TestUnit.php'; require_once 'HTML_TestListener.php'; require_once 'PHP/Parser.php'; +require_once 'PHP/Parser/DocBlock/DefaultInlineTagLexer.php'; $title = 'PhpUnit test run, PHP_Parser package'; ?> @@ -60,6 +61,7 @@ } $testcases[] = 'PHP_Parser_MsgServer_test'; + $testcases[] = 'PHP_Parser_DocBlock_DefaultInlineTagLexer_test'; $suite = new PHPUnit_TestSuite(); http://cvs.php.net/co.php/pear/PHP_Parser/tests/PHP_Parser_DocBlock_DefaultInlineTagLexer_test.php?r=1.1&p=1 Index: pear/PHP_Parser/tests/PHP_Parser_DocBlock_DefaultInlineTagLexer_test.php +++ pear/PHP_Parser/tests/PHP_Parser_DocBlock_DefaultInlineTagLexer_test.php <?php /** * API Unit tests for PHP_Parser package. * * @version $Id: PHP_Parser_DocBlock_DefaultInlineTagLexer_test.php,v 1.1 2004/03/13 20:30:19 cellog Exp $ * @author Laurent Laville <pear@laurent-laville.org> portions from HTML_CSS * @author Greg Beaver * @package PHP_Parser */ /** * @package PHP_Parser */ class PHP_Parser_DocBlock_DefaultInlineTagLexer_test extends PHPUnit_TestCase { /** * A PHP_Parser_DocBlock_DefaultInlineTagLexer object (or one of the drivers) * @var object */ var $lexer; /** * This will be Error_Stack if the package is accepted into PEAR */ var $errorStackClass = 'PHP_Parser_Stack'; /**#@+ * Error Stacks */ var $pstack; var $sstack; var $estack; function PHP_Parser_DocBlock_DefaultInlineTagLexer_test($name) { $this->PHPUnit_TestCase($name); } function setUp() { error_reporting(E_ALL); $this->errorOccured = false; set_error_handler(array(&$this, 'errorHandler')); $this->lexer = new PHP_Parser_DocBlock_DefaultInlineTagLexer; $this->_expectedErrors = array(); $this->_testMethod = 'unknown'; } function tearDown() { unset($this->lexer); restore_error_handler(); } function errorCodeToString($code) { $codes = array( 'PHP_Parser_DocBlock_DefaultInlineTagLexer' => array( ), ); return $codes[$code]; } function _stripWhitespace($str) { return preg_replace('/\\s+/', '', $str); } function _methodExists($name) { $test = (version_compare(phpversion(), '4.3.5', '>') == 1) ? $name : strtolower($name); if (in_array($test, get_class_methods($this->lexer))) { return true; } $this->assertTrue(false, 'method '. $name . ' not implemented in ' . get_class($this->lexer)); return false; } function errorHandler($errno, $errstr, $errfile, $errline) { if (error_reporting() == 0) { return; } //die("$errstr in $errfile at line $errline: $errstr"); $this->errorOccured = true; $this->assertTrue(false, "$errstr at line $errline, $errfile"); } function test_setOptions1() { if (!$this->_methodExists('setOptions')) { return; } $this->assertEquals(array('separator' => false), $this->lexer->_options); $this->lexer->setOptions(); $this->assertEquals(array('separator' => false), $this->lexer->_options); } function test_setOptions2() { if (!$this->_methodExists('setOptions')) { return; } $this->assertEquals(array('separator' => false), $this->lexer->_options); $this->lexer->setOptions(array('separator' => ',')); $this->assertEquals(array('separator' => ','), $this->lexer->_options); $this->lexer->setOptions(); $this->assertEquals(array('separator' => false), $this->lexer->_options); } function test_newTag_empty() { if (!$this->_methodExists('newTag')) { return; } $this->lexer->newTag(''); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(), array(), array()), $this->lexer->_buf); } function test_newTag_oneword() { if (!$this->_methodExists('newTag')) { return; } $this->lexer->newTag('test'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array('test'), array('test'), array('')), $this->lexer->_buf, 1); $this->lexer->newTag('test '); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array('test '), array('test'), array(' ')), $this->lexer->_buf, 2); $this->lexer->newTag(' test'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array('test'), array('test'), array('')), $this->lexer->_buf, 3); } function test_newTag_twowords() { if (!$this->_methodExists('newTag')) { return; } $this->lexer->newTag('test two'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array('test ','two'), array('test','two'), array(' ','')), $this->lexer->_buf, 1); $this->lexer->newTag('test two'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array('test ','two'), array('test','two'), array(' ','')), $this->lexer->_buf, 2); } function test_newTag_oneword_multibuffers() { if (!$this->_methodExists('setOptions')) { return; } if (!$this->_methodExists('newTag')) { return; } $this->lexer->setOptions(array('separator' => ',')); $this->lexer->newTag('test'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(array('test'), array('test'), array(''))), $this->lexer->_buf, 1); $this->lexer->newTag('test '); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(array('test '), array('test'), array(' '))), $this->lexer->_buf, 2); $this->lexer->newTag(' test'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(array('test'), array('test'), array(''))), $this->lexer->_buf, 3); } function test_newTag_twowords_multibuffers() { if (!$this->_methodExists('setOptions')) { return; } if (!$this->_methodExists('newTag')) { return; } $this->lexer->setOptions(array('separator' => ',')); $this->lexer->newTag('test two'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(array('test ','two'), array('test','two'), array(' ',''))), $this->lexer->_buf, 1); $this->lexer->newTag('test two'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(array('test ','two'), array('test','two'), array(' ',''))), $this->lexer->_buf, 2); } function test_newTag_twowords_separator() { if (!$this->_methodExists('setOptions')) { return; } if (!$this->_methodExists('newTag')) { return; } $this->lexer->setOptions(array('separator' => ',')); $this->lexer->newTag('test,two'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(array('test'), array('test'), array('')), array(array('two'), array('two'), array(''))), $this->lexer->_buf, 1); $this->lexer->newTag('test , two'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(array('test '), array('test'), array(' ')), array(array('two'), array('two'), array(''))), $this->lexer->_buf, 2); $this->lexer->newTag('test, two'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(array('test'), array('test'), array('')), array(array('two'), array('two'), array(''))), $this->lexer->_buf, 3); $this->lexer->newTag('test ,two '); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(array('test '), array('test'), array(' ')), array(array('two '), array('two'), array(' '))), $this->lexer->_buf, 4); } function test_newTag_escapeseparator_multibuffers() { if (!$this->_methodExists('setOptions')) { return; } if (!$this->_methodExists('newTag')) { return; } $this->lexer->setOptions(array('separator' => ',')); $this->lexer->newTag('test\\,'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(array('test,'), array('test,'), array(''))), $this->lexer->_buf, 1); $this->lexer->newTag('test\\, hi'); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(array('test, ','hi'), array('test,','hi'), array(' ',''))), $this->lexer->_buf, 2); $this->lexer->newTag('test\\, hi,there '); $this->assertEquals(0, $this->lexer->_index); $this->assertEquals(array(array(array('test, ','hi'), array('test,','hi'), array(' ','')), array(array('there '), array('there'), array(' '))), $this->lexer->_buf, 3); } function test_getBuf() { if (!$this->_methodExists('setOptions')) { return; } if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('_getBuf')) { return; } $this->lexer->newTag('test'); $buf = $this->lexer->_getBuf(); $this->assertEquals(array(array('test'), array('test'), array('')), $buf, 1); $this->lexer->setOptions(array('separator' => ',')); $this->lexer->newTag('test,two'); $buf = $this->lexer->_getBuf(); $this->assertEquals(array(array('test'), array('test'), array('')), $buf, 2); $this->lexer->_bufIndex++; $buf = $this->lexer->_getBuf(); $this->assertEquals(array(array('two'), array('two'), array('')), $buf, 3); } function test_moveNextBuf() { if (!$this->_methodExists('setOptions')) { return; } if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('_getBuf')) { return; } if (!$this->_methodExists('moveNextBuf')) { return; } $this->lexer->newTag('test'); $buf = $this->lexer->_getBuf(); $this->assertEquals(array(array('test'), array('test'), array('')), $buf, 1); $this->assertFalse($this->lexer->moveNextBuf(), 'moveNextBuf 1'); $buf = $this->lexer->_getBuf(); $this->assertEquals(array(array('test'), array('test'), array('')), $buf, 1); $this->lexer->setOptions(array('separator' => ',')); $this->lexer->newTag('test,two'); $buf = $this->lexer->_getBuf(); $this->assertEquals(array(array('test'), array('test'), array('')), $buf, 2); $this->assertEquals(array(array('two'), array('two'), array('')), $this->lexer->moveNextBuf(), 'moveNextBuf 2'); $buf = $this->lexer->_getBuf(); $this->assertEquals(array(array('two'), array('two'), array('')), $buf, 3); $this->assertFalse($this->lexer->moveNextBuf(), 'moveNextBuf false'); $this->assertFalse($this->lexer->_getBuf(), '_getBuf false'); } function test_hasNextBuf() { if (!$this->_methodExists('setOptions')) { return; } if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('_getBuf')) { return; } if (!$this->_methodExists('moveNextBuf')) { return; } if (!$this->_methodExists('hasNextBuf')) { return; } $this->lexer->newTag('test'); $buf = $this->lexer->_getBuf(); $this->assertEquals(array(array('test'), array('test'), array('')), $buf, 1); $this->assertFalse($this->lexer->moveNextBuf(), 'moveNextBuf 1'); $this->assertFalse($this->lexer->hasNextBuf(), 'hasNextBuf 1'); $buf = $this->lexer->_getBuf(); $this->assertEquals(array(array('test'), array('test'), array('')), $buf, 1); $this->lexer->setOptions(array('separator' => ',')); $this->lexer->newTag('test,two'); $this->assertTrue($this->lexer->hasNextBuf(), 'hasNextBuf 2'); $buf = $this->lexer->_getBuf(); $this->assertEquals(array(array('test'), array('test'), array('')), $buf, 2); $this->assertEquals(array(array('two'), array('two'), array('')), $this->lexer->moveNextBuf(), 'moveNextBuf 2'); $this->assertFalse($this->lexer->hasNextBuf(), 'hasNextBuf 3'); $buf = $this->lexer->_getBuf(); $this->assertEquals(array(array('two'), array('two'), array('')), $buf, 3); } function test_readBuf_empty() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('_readBuf')) { return; } $this->lexer->newTag(''); $this->assertTrue($this->lexer->_readBuf() === false, 1); $this->assertTrue($this->lexer->_readBuf(false, true) === false, 2); $this->assertTrue($this->lexer->_readBuf(true) === false, 3); $this->assertTrue($this->lexer->_readBuf(true, true) === false, 4); } function test_readBuf_singleBuffer() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('_readBuf')) { return; } $this->lexer->newTag('test me '); $this->assertEquals('test', $this->lexer->_readBuf(false), 1); $this->assertEquals('test', $this->lexer->_readBuf(false), 2); $this->assertEquals('test ', $this->lexer->_readBuf(false, true), 3); $this->assertEquals('test', $this->lexer->_readBuf(true), 4); $this->assertEquals('me', $this->lexer->_readBuf(false), 5); $this->assertEquals('me ', $this->lexer->_readBuf(true, true), 6); $this->assertTrue($this->lexer->_readBuf(true, true) === false, 7); } function test_readBuf_multiBuffer() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('_readBuf')) { return; } if (!$this->_methodExists('moveNextBuf')) { return; } if (!$this->_methodExists('setOptions')) { return; } $this->lexer->setOptions(array('separator' => '#')); $this->lexer->newTag('test me # hi '); $this->assertEquals('test', $this->lexer->_readBuf(false), 1); $this->assertEquals('test', $this->lexer->_readBuf(false), 2); $this->assertEquals('test ', $this->lexer->_readBuf(false, true), 3); $this->assertEquals('test', $this->lexer->_readBuf(true), 4); $this->assertEquals('me', $this->lexer->_readBuf(false), 5); $this->assertEquals('me ', $this->lexer->_readBuf(true, true), 6); $this->assertTrue($this->lexer->_readBuf(true, true) === false, 7); $this->assertEquals(array(array('hi '), array('hi'), array(' ')), $this->lexer->moveNextBuf(), 'move'); $this->assertEquals('hi', $this->lexer->_readBuf(false), 8); $this->assertEquals('hi ', $this->lexer->_readBuf(false, true), 9); $this->assertEquals('hi ', $this->lexer->_readBuf(true, true), 10); $this->assertTrue($this->lexer->_readBuf(true, true) === false, 11); } function test_getWord_empty() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getWord')) { return; } $this->lexer->newTag(''); $this->assertTrue($this->lexer->getWord() === false, 1); $this->assertTrue($this->lexer->getWord() === false, 2); } function test_getWord_singleBuffer() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getWord')) { return; } $this->lexer->newTag('test me '); $this->assertEquals('test', $this->lexer->getWord(), 1); $this->assertEquals('me', $this->lexer->getWord(), 2); $this->assertTrue($this->lexer->getWord() === false, 3); } function test_getWord_multiBuffer() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getWord')) { return; } if (!$this->_methodExists('moveNextBuf')) { return; } if (!$this->_methodExists('setOptions')) { return; } $this->lexer->setOptions(array('separator' => '#')); $this->lexer->newTag('test me # hi '); $this->assertEquals('test', $this->lexer->getWord(), 1); $this->assertEquals('me', $this->lexer->getWord(), 2); $this->assertTrue($this->lexer->getWord() === false, 3); $this->assertEquals(array(array('hi '), array('hi'), array(' ')), $this->lexer->moveNextBuf(), 'move'); $this->assertEquals('hi', $this->lexer->getWord(), 4); $this->assertTrue($this->lexer->getWord() === false, 5); } function test_getAllWords_singleBuffer() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getAllWords')) { return; } $this->lexer->newTag('test me baby'); $this->assertEquals('test me baby', $this->lexer->getAllWords(), 1); $this->assertFalse($this->lexer->getAllWords(), 2); } function test_getAllWords_multiBuffer() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getAllWords')) { return; } if (!$this->_methodExists('moveNextBuf')) { return; } if (!$this->_methodExists('setOptions')) { return; } $this->lexer->setOptions(array('separator' => '#')); $this->lexer->newTag('test me baby# hi there sweetness '); $this->assertEquals('test me baby', $this->lexer->getAllWords(), 1); $this->assertFalse($this->lexer->getAllWords(), 1.1); $this->assertEquals(array(array('hi ','there ', 'sweetness '), array('hi','there','sweetness'), array(' ',' ',' ')), $this->lexer->moveNextBuf(), 'move'); $this->assertEquals('hi there sweetness', $this->lexer->getAllWords(), 2); $this->assertFalse($this->lexer->getAllWords(), 2.1); } function test_getWords_singleBuffer() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getWords')) { return; } $this->lexer->newTag('test me baby '); $this->assertEquals('test me', $this->lexer->getWords(array('count' => 2)), 1); $this->assertEquals('baby', $this->lexer->getWords(array('count' => 2)), 2); $this->assertFalse($this->lexer->getWords(array('count' => 2)), 3); $this->lexer->newTag('test me baby'); $this->assertEquals('test me baby', $this->lexer->getWords(array('count' => 4)), 4); $this->assertFalse($this->lexer->getWords(array('count' => 2)), 5); $this->lexer->newTag('test me baby'); $this->assertEquals('test', $this->lexer->getWords(array('count' => 1)), 6); $this->assertEquals('me', $this->lexer->getWords(array('count' => 1)), 7); $this->assertEquals('baby', $this->lexer->getWords(array('count' => 1)), 8); $this->assertFalse($this->lexer->getWords(array('count' => 2)), 9); } function test_getWords_multiBuffer() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getWords')) { return; } if (!$this->_methodExists('moveNextBuf')) { return; } if (!$this->_methodExists('setOptions')) { return; } $this->lexer->setOptions(array('separator' => '#')); $this->lexer->newTag('test me baby# hi there sweetness '); $this->assertEquals('test me', $this->lexer->getWords(array('count' => 2)), 1); $this->assertEquals('baby', $this->lexer->getWords(array('count' => 2)), 2); $this->assertFalse($this->lexer->getWords(array('count' => 2)), 3); $this->assertEquals(array(array('hi ','there ', 'sweetness '), array('hi','there','sweetness'), array(' ',' ',' ')), $this->lexer->moveNextBuf(), 'move'); $this->assertEquals('hi there', $this->lexer->getWords(array('count' => 2)), 1); $this->assertEquals('sweetness', $this->lexer->getWords(array('count' => 2)), 2); $this->assertFalse($this->lexer->getWords(array('count' => 2)), 3); } function test_getLink_empty() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getLink')) { return; } $this->lexer->newTag(''); $this->assertTrue($this->lexer->getLink() === false, 1); $this->assertTrue($this->lexer->getLink() === false, 2); } function test_getLink_unknown() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getLink')) { return; } $this->lexer->newTag('test'); $this->assertEquals( array( 'text' => 'test', 'link' => 'test', 'valid' => true, 'type' => 'unknown', ), $this->lexer->getLink(), 1); $this->assertFalse($this->lexer->getLink(), 2); } function test_invalid_getLink() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getLink')) { return; } $this->lexer->newTag('object test()'); $this->assertEquals( array( 'text' => 'object test()', 'link' => false, 'valid' => false, 'type' => 'object', ), $this->lexer->getLink(), 1); $this->lexer->newTag('global test()'); $this->assertEquals( array( 'text' => 'global test()', 'link' => false, 'valid' => false, 'type' => 'global', ), $this->lexer->getLink(), 2); $this->lexer->newTag('constant test()'); $this->assertEquals( array( 'text' => 'constant test()', 'link' => false, 'valid' => false, 'type' => 'constant', ), $this->lexer->getLink(), 3); $this->lexer->newTag('constant $test'); $this->assertEquals( array( 'text' => 'constant $test', 'link' => false, 'valid' => false, 'type' => 'constant', ), $this->lexer->getLink(), 4); $this->lexer->newTag('object $test'); $this->assertEquals( array( 'text' => 'object $test', 'link' => false, 'valid' => false, 'type' => 'object', ), $this->lexer->getLink(), 5); $this->lexer->newTag('function $test'); $this->assertEquals( array( 'text' => 'function $test', 'link' => false, 'valid' => false, 'type' => 'function', ), $this->lexer->getLink(), 6); } function test_invalid_getLink2() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getLink')) { return; } $this->lexer->newTag('object'); $this->assertEquals( array( 'text' => 'object', 'link' => false, 'valid' => false, 'type' => 'object', ), $this->lexer->getLink(), 1); $this->lexer->newTag('global'); $this->assertEquals( array( 'text' => 'global', 'link' => false, 'valid' => false, 'type' => 'global', ), $this->lexer->getLink(), 2); $this->lexer->newTag('constant'); $this->assertEquals( array( 'text' => 'constant', 'link' => false, 'valid' => false, 'type' => 'constant', ), $this->lexer->getLink(), 3); $this->lexer->newTag('function'); $this->assertEquals( array( 'text' => 'function', 'link' => false, 'valid' => false, 'type' => 'function', ), $this->lexer->getLink(), 4); $this->lexer->newTag('global some::$var'); $this->assertEquals( array( 'text' => 'global some::$var', 'link' => false, 'valid' => false, 'type' => 'global', ), $this->lexer->getLink(), 4); } function test_getLink_disambiguate() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getLink')) { return; } $this->lexer->newTag('function test'); $this->assertEquals( array( 'text' => 'function test', 'link' => 'test', 'valid' => true, 'type' => 'function', ), $this->lexer->getLink(), 1); $this->lexer->newTag('object test'); $this->assertEquals( array( 'text' => 'object test', 'link' => 'test', 'valid' => true, 'type' => 'object', ), $this->lexer->getLink(), 1); $this->lexer->newTag('global $test'); $this->assertEquals( array( 'text' => 'global $test', 'link' => '$test', 'valid' => true, 'type' => 'global', ), $this->lexer->getLink(), 1); $this->lexer->newTag('constant test'); $this->assertEquals( array( 'text' => 'constant test', 'link' => 'test', 'valid' => true, 'type' => 'constant', ), $this->lexer->getLink(), 1); } function test_getLink_guesstype() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getLink')) { return; } $this->lexer->newTag('test()'); $this->assertEquals( array( 'text' => 'test()', 'link' => 'test', 'valid' => true, 'type' => 'function', ), $this->lexer->getLink(), 1); $this->lexer->newTag('some::$test'); $this->assertEquals( array( 'text' => 'some::$test', 'link' => '$test', 'valid' => true, 'type' => 'var', 'class' => 'some', ), $this->lexer->getLink(), 2); $this->lexer->newTag('function some::func()'); $this->assertEquals( array( 'text' => 'function some::func()', 'link' => 'func', 'valid' => true, 'type' => 'method', 'class' => 'some', ), $this->lexer->getLink(), 3); $this->lexer->newTag('some::func()'); $this->assertEquals( array( 'text' => 'some::func()', 'link' => 'func', 'valid' => true, 'type' => 'method', 'class' => 'some', ), $this->lexer->getLink(), 4); $this->lexer->newTag('some::definedconstant'); $this->assertEquals( array( 'text' => 'some::definedconstant', 'link' => 'definedconstant', 'valid' => true, 'type' => 'class constant', 'class' => 'some', ), $this->lexer->getLink(), 5); $this->lexer->newTag('$test'); $this->assertEquals( array( 'text' => '$test', 'link' => '$test', 'valid' => true, 'type' => 'global', ), $this->lexer->getLink(), 6); } function test_getLink_packageselector() { if (!$this->_methodExists('newTag')) { return; } if (!$this->_methodExists('getLink')) { return; } $this->lexer->newTag('test#test'); $this->assertEquals( array( 'text' => 'test#test', 'link' => 'test', 'valid' => true, 'type' => 'unknown', 'package' => 'test', ), $this->lexer->getLink(), 1); $this->lexer->newTag('test#test()'); $this->assertEquals( array( 'text' => 'test#test()', 'link' => 'test', 'valid' => true, 'type' => 'function', 'package' => 'test', ), $this->lexer->getLink(), 2); } } ?>