Elgg_Ecml_TokenizerTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. class Elgg_Ecml_TokenizerTest extends UnitTestCase {
  3. /**
  4. * @var Elgg_Ecml_Tokenizer
  5. */
  6. protected $tk;
  7. function setUp() {
  8. $this->tk = new Elgg_Ecml_Tokenizer();
  9. parent::setUp();
  10. }
  11. function testValidTokens() {
  12. $valids = array(
  13. '[bar/baz foo="234" bool bool2=true f-.2 pow=\'pow\']' => array(
  14. 'keyword' => 'bar/baz',
  15. 'attrs' => array(
  16. 'foo' => '234',
  17. 'bool' => true,
  18. 'bool2' => true,
  19. 'f-.2' => true,
  20. 'pow' => 'pow',
  21. ),
  22. ),
  23. '[bar.123 cat="fig\\"ht"]' => array(
  24. 'keyword' => 'bar.123',
  25. 'attrs' => array(
  26. 'cat' => 'fig"ht',
  27. ),
  28. ),
  29. );
  30. foreach ($valids as $text => $test) {
  31. $tokens = $this->tk->getTokens($text);
  32. $this->assertTrue(isset($tokens[0]));
  33. $token = $tokens[0];
  34. $this->assertIsA($token, 'Elgg_Ecml_Token');
  35. /* @var Elgg_Ecml_Token $token */
  36. $this->assertFalse($token->isText);
  37. $this->assertEqual($token->keyword, $test['keyword']);
  38. $this->assertIdentical($token->attrs, $test['attrs']);
  39. }
  40. }
  41. function testMultibyteToken() {
  42. $tokens = $this->tk->getTokens('[foo 日本語="日本\\"語"]');
  43. $this->assertTrue(
  44. isset($tokens[0])
  45. && $tokens[0]->keyword == 'foo'
  46. && $tokens[0]->attrs == array('日本語' => '日本"語'));
  47. }
  48. function testInvalidTokens() {
  49. $invalids = array(
  50. '[foo a="b]',
  51. '[foo a="b\\"]',
  52. );
  53. foreach ($invalids as $text) {
  54. $tokens = $this->tk->getTokens($text);
  55. $this->assertTrue(isset($tokens[0]));
  56. $token = $tokens[0];
  57. $this->assertIsA($token, 'Elgg_Ecml_Token');
  58. /* @var Elgg_Ecml_Token $token */
  59. $this->assertTrue($token->isText);
  60. }
  61. }
  62. function testMultipleTokens() {
  63. $tokens = $this->tk->getTokens('Hello [foo bar="bar"], this [cat do="g] is [/foo].');
  64. $expected = array(
  65. 'Hello ',
  66. array('foo', array('bar' => 'bar')),
  67. ', this ',
  68. '[cat do="g]',
  69. ' is ',
  70. array('/foo', array()),
  71. '.'
  72. );
  73. if ($this->assertEqual(count($tokens), count($expected))) {
  74. foreach ($expected as $i => $data) {
  75. if (is_array($data)) {
  76. $this->assertFalse($tokens[$i]->isText);
  77. $this->assertEqual($tokens[$i]->keyword, $data[0]);
  78. $this->assertEqual($tokens[$i]->attrs, $data[1]);
  79. }
  80. }
  81. }
  82. }
  83. }