[ Index ]

PHP Cross Reference of Web Application Component Toolkit

title

Body

[close]

/tests/cases/template/compiler/ -> expressionlexer.test.php (source)

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


Generated: Sun Nov 28 19:36:09 2004 Cross-referenced by PHPXref 0.5