TransTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. class Twig_Tests_Node_TransTest extends Twig_Test_NodeTestCase
  11. {
  12. /**
  13. * @covers \Twig_Node_Trans::__construct
  14. */
  15. public function testConstructor()
  16. {
  17. $count = new Twig_Node_Expression_Constant(12, 0);
  18. $body = new Twig_Node(array(
  19. new Twig_Node_Text('Hello', 0),
  20. ), array(), 0);
  21. $plural = new Twig_Node(array(
  22. new Twig_Node_Text('Hey ', 0),
  23. new Twig_Node_Print(new Twig_Node_Expression_Name('name', 0), 0),
  24. new Twig_Node_Text(', I have ', 0),
  25. new Twig_Node_Print(new Twig_Node_Expression_Name('count', 0), 0),
  26. new Twig_Node_Text(' apples', 0),
  27. ), array(), 0);
  28. $node = new Twig_Extensions_Node_Trans($body, $plural, $count, null, 0);
  29. $this->assertEquals($body, $node->getNode('body'));
  30. $this->assertEquals($count, $node->getNode('count'));
  31. $this->assertEquals($plural, $node->getNode('plural'));
  32. }
  33. public function getTests()
  34. {
  35. $tests = array();
  36. $body = new Twig_Node_Expression_Name('foo', 0);
  37. $node = new Twig_Extensions_Node_Trans($body, null, null, null, 0);
  38. $tests[] = array($node, sprintf('echo gettext(%s);', $this->getVariableGetter('foo')));
  39. $body = new Twig_Node_Expression_Constant('Hello', 0);
  40. $node = new Twig_Extensions_Node_Trans($body, null, null, null, 0);
  41. $tests[] = array($node, 'echo gettext("Hello");');
  42. $body = new Twig_Node(array(
  43. new Twig_Node_Text('Hello', 0),
  44. ), array(), 0);
  45. $node = new Twig_Extensions_Node_Trans($body, null, null, null, 0);
  46. $tests[] = array($node, 'echo gettext("Hello");');
  47. $body = new Twig_Node(array(
  48. new Twig_Node_Text('J\'ai ', 0),
  49. new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0),
  50. new Twig_Node_Text(' pommes', 0),
  51. ), array(), 0);
  52. $node = new Twig_Extensions_Node_Trans($body, null, null, null, 0);
  53. $tests[] = array($node, sprintf('echo strtr(gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));', $this->getVariableGetter('foo')));
  54. $count = new Twig_Node_Expression_Constant(12, 0);
  55. $body = new Twig_Node(array(
  56. new Twig_Node_Text('Hey ', 0),
  57. new Twig_Node_Print(new Twig_Node_Expression_Name('name', 0), 0),
  58. new Twig_Node_Text(', I have one apple', 0),
  59. ), array(), 0);
  60. $plural = new Twig_Node(array(
  61. new Twig_Node_Text('Hey ', 0),
  62. new Twig_Node_Print(new Twig_Node_Expression_Name('name', 0), 0),
  63. new Twig_Node_Text(', I have ', 0),
  64. new Twig_Node_Print(new Twig_Node_Expression_Name('count', 0), 0),
  65. new Twig_Node_Text(' apples', 0),
  66. ), array(), 0);
  67. $node = new Twig_Extensions_Node_Trans($body, $plural, $count, null, 0);
  68. $tests[] = array($node, sprintf('echo strtr(ngettext("Hey %%name%%, I have one apple", "Hey %%name%%, I have %%count%% apples", abs(12)), array("%%name%%" => %s, "%%name%%" => %s, "%%count%%" => abs(12), ));', $this->getVariableGetter('name'), $this->getVariableGetter('name')));
  69. // with escaper extension set to on
  70. $body = new Twig_Node(array(
  71. new Twig_Node_Text('J\'ai ', 0),
  72. new Twig_Node_Print(new Twig_Node_Expression_Filter(new Twig_Node_Expression_Name('foo', 0), new Twig_Node_Expression_Constant('escape', 0), new Twig_Node(), 0), 0),
  73. new Twig_Node_Text(' pommes', 0),
  74. ), array(), 0);
  75. $node = new Twig_Extensions_Node_Trans($body, null, null, null, 0);
  76. $tests[] = array($node, sprintf('echo strtr(gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));', $this->getVariableGetter('foo')));
  77. // with notes
  78. $body = new Twig_Node_Expression_Constant('Hello', 0);
  79. $notes = new Twig_Node_Text('Notes for translators', 0);
  80. $node = new Twig_Extensions_Node_Trans($body, null, null, $notes, 0);
  81. $tests[] = array($node, "// notes: Notes for translators\necho gettext(\"Hello\");");
  82. $body = new Twig_Node_Expression_Constant('Hello', 0);
  83. $notes = new Twig_Node_Text("Notes for translators\nand line breaks", 0);
  84. $node = new Twig_Extensions_Node_Trans($body, null, null, $notes, 0);
  85. $tests[] = array($node, "// notes: Notes for translators and line breaks\necho gettext(\"Hello\");");
  86. $count = new Twig_Node_Expression_Constant(5, 0);
  87. $body = new Twig_Node_Text('There is 1 pending task', 0);
  88. $plural = new Twig_Node(array(
  89. new Twig_Node_Text('There are ', 0),
  90. new Twig_Node_Print(new Twig_Node_Expression_Name('count', 0), 0),
  91. new Twig_Node_Text(' pending tasks', 0),
  92. ), array(), 0);
  93. $notes = new Twig_Node_Text('Notes for translators', 0);
  94. $node = new Twig_Extensions_Node_Trans($body, $plural, $count, $notes, 0);
  95. $tests[] = array($node, "// notes: Notes for translators\n".'echo strtr(ngettext("There is 1 pending task", "There are %count% pending tasks", abs(5)), array("%count%" => abs(5), ));');
  96. return $tests;
  97. }
  98. }