MustachePragmaTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. require_once '../Mustache.php';
  3. /**
  4. * @group pragmas
  5. */
  6. class MustachePragmaTest extends PHPUnit_Framework_TestCase {
  7. public function testUnknownPragmaException() {
  8. $m = new Mustache();
  9. try {
  10. $m->render('{{%I-HAVE-THE-GREATEST-MUSTACHE}}');
  11. } catch (MustacheException $e) {
  12. $this->assertEquals(MustacheException::UNKNOWN_PRAGMA, $e->getCode(), 'Caught exception code was not MustacheException::UNKNOWN_PRAGMA');
  13. return;
  14. }
  15. $this->fail('Mustache should have thrown an unknown pragma exception');
  16. }
  17. public function testSuppressUnknownPragmaException() {
  18. $m = new LessWhinyMustache();
  19. try {
  20. $this->assertEquals('', $m->render('{{%I-HAVE-THE-GREATEST-MUSTACHE}}'));
  21. } catch (MustacheException $e) {
  22. if ($e->getCode() == MustacheException::UNKNOWN_PRAGMA) {
  23. $this->fail('Mustache should have thrown an unknown pragma exception');
  24. } else {
  25. throw $e;
  26. }
  27. }
  28. }
  29. public function testPragmaReplace() {
  30. $m = new Mustache();
  31. $this->assertEquals('', $m->render('{{%UNESCAPED}}'), 'Pragma tag not removed');
  32. }
  33. public function testPragmaReplaceMultiple() {
  34. $m = new Mustache();
  35. $this->assertEquals('', $m->render('{{% UNESCAPED }}'), 'Pragmas should allow whitespace');
  36. $this->assertEquals('', $m->render('{{% UNESCAPED foo=bar }}'), 'Pragmas should allow whitespace');
  37. $this->assertEquals('', $m->render("{{%UNESCAPED}}\n{{%UNESCAPED}}"), 'Multiple pragma tags not removed');
  38. $this->assertEquals(' ', $m->render('{{%UNESCAPED}} {{%UNESCAPED}}'), 'Multiple pragma tags not removed');
  39. }
  40. public function testPragmaReplaceNewline() {
  41. $m = new Mustache();
  42. $this->assertEquals('', $m->render("{{%UNESCAPED}}\n"), 'Trailing newline after pragma tag not removed');
  43. $this->assertEquals("\n", $m->render("\n{{%UNESCAPED}}\n"), 'Too many newlines removed with pragma tag');
  44. $this->assertEquals("1\n23", $m->render("1\n2{{%UNESCAPED}}\n3"), 'Wrong newline removed with pragma tag');
  45. }
  46. public function testPragmaReset() {
  47. $m = new Mustache('', array('symbol' => '>>>'));
  48. $this->assertEquals('>>>', $m->render('{{{symbol}}}'));
  49. $this->assertEquals('>>>', $m->render('{{%UNESCAPED}}{{symbol}}'));
  50. $this->assertEquals('>>>', $m->render('{{{symbol}}}'));
  51. }
  52. }
  53. class LessWhinyMustache extends Mustache {
  54. protected $_throwsExceptions = array(
  55. MustacheException::UNKNOWN_VARIABLE => false,
  56. MustacheException::UNCLOSED_SECTION => true,
  57. MustacheException::UNEXPECTED_CLOSE_SECTION => true,
  58. MustacheException::UNKNOWN_PARTIAL => false,
  59. MustacheException::UNKNOWN_PRAGMA => false,
  60. );
  61. }