BufferTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /*
  3. * This file is part of Mustache.php.
  4. *
  5. * (c) 2012 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Mustache\Test;
  11. use Mustache\Mustache;
  12. use Mustache\Buffer;
  13. /**
  14. * @group unit
  15. */
  16. class BufferTest extends \PHPUnit_Framework_TestCase {
  17. /**
  18. * @dataProvider getConstructorArgs
  19. */
  20. public function testConstructor($indent, $charset) {
  21. $buffer = new Buffer($indent, $charset);
  22. $this->assertEquals($indent, $buffer->getIndent());
  23. $this->assertEquals($charset, $buffer->getCharset());
  24. }
  25. public function getConstructorArgs() {
  26. return array(
  27. array('', 'UTF-8'),
  28. array(' ', 'ISO-8859-1'),
  29. array("\t\t\t", 'Shift_JIS'),
  30. );
  31. }
  32. public function testWrite() {
  33. $buffer = new Buffer;
  34. $this->assertEquals('', $buffer->flush());
  35. $buffer->writeLine();
  36. $buffer->writeLine();
  37. $buffer->writeLine();
  38. $this->assertEquals("\n\n\n", $buffer->flush());
  39. $this->assertEquals('', $buffer->flush());
  40. $buffer->write('foo');
  41. $buffer->write('bar');
  42. $buffer->writeLine();
  43. $buffer->write('baz');
  44. $this->assertEquals("foobar\nbaz", $buffer->flush());
  45. $this->assertEquals('', $buffer->flush());
  46. $indent = "\t\t";
  47. $buffer->setIndent($indent);
  48. $buffer->writeText('foo');
  49. $buffer->writeLine();
  50. $buffer->writeText('bar');
  51. $this->assertEquals("\t\tfoo\n\t\tbar", $buffer->flush());
  52. $this->assertEquals('', $buffer->flush());
  53. }
  54. /**
  55. * @dataProvider getEscapeAndIndent
  56. */
  57. public function testEscapingAndIndenting($text, $escape, $indent, $whitespace, $expected) {
  58. $buffer = new Buffer;
  59. $buffer->setIndent($whitespace);
  60. $buffer->write($text, $indent, $escape);
  61. $this->assertEquals($expected, $buffer->flush());
  62. }
  63. public function getEscapeAndIndent() {
  64. return array(
  65. array('> "fun & games" <', false, false, "\t", '> "fun & games" <'),
  66. array('> "fun & games" <', true, false, "\t", '&gt; &quot;fun &amp; games&quot; &lt;'),
  67. array('> "fun & games" <', true, true, "\t", "\t&gt; &quot;fun &amp; games&quot; &lt;"),
  68. array('> "fun & games" <', false, true, "\t", "\t> \"fun & games\" <"),
  69. );
  70. }
  71. public function testChangeIndent() {
  72. $indent = "\t\t";
  73. $buffer = new Buffer($indent);
  74. $this->assertEquals($indent, $buffer->getIndent());
  75. $indent = "";
  76. $buffer->setIndent($indent);
  77. $this->assertEquals($indent, $buffer->getIndent());
  78. $indent = " ";
  79. $buffer->setIndent($indent);
  80. $this->assertEquals($indent, $buffer->getIndent());
  81. }
  82. }