PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/test/UtilityTest.php

http://github.com/brianhaveri/Underscore.php
PHP | 212 lines | 155 code | 42 blank | 15 comment | 1 complexity | 85125c0f53869cae5f03c1ed87cc9e45 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. class UnderscoreUtilityTest extends PHPUnit_Framework_TestCase {
  3. public function testIdentity() {
  4. // from js
  5. $moe = array('name'=>'moe');
  6. $moe_obj = (object) $moe;
  7. $this->assertEquals($moe, __::identity($moe));
  8. $this->assertEquals($moe_obj, __::identity($moe_obj));
  9. // extra
  10. $this->assertEquals($moe, __($moe)->identity());
  11. $this->assertEquals($moe_obj, __($moe_obj)->identity());
  12. // docs
  13. $moe = array('name'=>'moe');
  14. $this->assertTrue($moe === __::identity($moe));
  15. }
  16. public function testUniqueId() {
  17. // docs
  18. $this->assertEquals(0, __::uniqueId());
  19. $this->assertEquals('stooge_1', __::uniqueId('stooge_'));
  20. $this->assertEquals(2, __::uniqueId());
  21. // from js
  22. $ids = array();
  23. $i = 0;
  24. while($i++ < 100) array_push($ids, __::uniqueId());
  25. $this->assertEquals(count($ids), count(__::uniq($ids)));
  26. // extra
  27. $this->assertEquals('stooges', join('', (__::first(__::uniqueId('stooges'), 7))), 'prefix assignment works');
  28. $this->assertEquals('stooges', join('', __(__('stooges')->uniqueId())->first(7)), 'prefix assignment works in OO-style call');
  29. while($i++ < 100) array_push($ids, __()->uniqueId());
  30. $this->assertEquals(count($ids), count(__()->uniq($ids)));
  31. }
  32. public function testTimes() {
  33. // from js
  34. $vals = array();
  35. __::times(3, function($i) use (&$vals) { $vals[] = $i; });
  36. $this->assertEquals(array(0,1,2), $vals, 'is 0 indexed');
  37. $vals = array();
  38. __(3)->times(function($i) use (&$vals) { $vals[] = $i; });
  39. $this->assertEquals(array(0,1,2), $vals, 'works as a wrapper in OO-style call');
  40. // docs
  41. $result = '';
  42. __::times(3, function() use (&$result) { $result .= 'a'; });
  43. $this->assertEquals('aaa', $result);
  44. }
  45. public function testMixin() {
  46. // from js
  47. __::mixin(array(
  48. 'myReverse' => function($string) {
  49. $chars = str_split($string);
  50. krsort($chars);
  51. return join('', $chars);
  52. }
  53. ));
  54. $this->assertEquals('aecanap', __::myReverse('panacea'), 'mixed in a function to _');
  55. $this->assertEquals('pmahc', __('champ')->myReverse(), 'mixed in a function to _ with OO-style call');
  56. // docs
  57. __::mixin(array(
  58. 'capitalize'=> function($string) { return ucwords($string); },
  59. 'yell' => function($string) { return strtoupper($string); }
  60. ));
  61. $this->assertEquals('Moe', __::capitalize('moe'));
  62. $this->assertEquals('MOE', __::yell('moe'));
  63. }
  64. public function testTemplate() {
  65. // from js
  66. $basicTemplate = __::template('<%= $thing %> is gettin on my noives!');
  67. $this->assertEquals("This is gettin on my noives!", $basicTemplate(array('thing'=>'This')), 'can do basic attribute interpolation');
  68. $this->assertEquals("This is gettin on my noives!", $basicTemplate((object) array('thing'=>'This')), 'can do basic attribute interpolation');
  69. $backslashTemplate = __::template('<%= $thing %> is \\ridanculous');
  70. $this->assertEquals('This is \\ridanculous', $backslashTemplate(array('thing'=>'This')));
  71. $escapeTemplate = __::template('<%= $a ? "checked=\\"checked\\"" : "" %>');
  72. $this->assertEquals('checked="checked"', $escapeTemplate(array('a'=>true)), 'can handle slash escapes in interpolations');
  73. $fancyTemplate = __::template('<ul><% foreach($people as $key=>$name) { %><li><%= $name %></li><% } %></ul>');
  74. $result = $fancyTemplate(array('people'=>array('moe'=>'Moe', 'larry'=>'Larry', 'curly'=>'Curly')));
  75. $this->assertEquals('<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>', $result, 'can run arbitrary php in templates');
  76. $namespaceCollisionTemplate = __::template('<%= $pageCount %> <%= $thumbnails[$pageCount] %> <% __::each($thumbnails, function($p) { %><div class=\"thumbnail\" rel=\"<%= $p %>\"></div><% }); %>');
  77. $result = $namespaceCollisionTemplate((object) array(
  78. 'pageCount' => 3,
  79. 'thumbnails'=> array(
  80. 1 => 'p1-thumbnail.gif',
  81. 2 => 'p2-thumbnail.gif',
  82. 3 => 'p3-thumbnail.gif'
  83. )
  84. ));
  85. $expected = '3 p3-thumbnail.gif <div class=\"thumbnail\" rel=\"p1-thumbnail.gif\"></div><div class=\"thumbnail\" rel=\"p2-thumbnail.gif\"></div><div class=\"thumbnail\" rel=\"p3-thumbnail.gif\"></div>';
  86. $this->assertEquals($expected, $result);
  87. $noInterpolateTemplate = __::template("<div><p>Just some text. Hey, I know this is silly but it aids consistency.</p></div>");
  88. $result = $noInterpolateTemplate();
  89. $expected = "<div><p>Just some text. Hey, I know this is silly but it aids consistency.</p></div>";
  90. $this->assertEquals($expected, $result);
  91. $quoteTemplate = __::template("It's its, not it's");
  92. $this->assertEquals("It's its, not it's", $quoteTemplate(new StdClass));
  93. $quoteInStatementAndBody = __::template('<%
  94. if($foo == "bar"){
  95. %>Statement quotes and \'quotes\'.<% } %>');
  96. $this->assertEquals("Statement quotes and 'quotes'.", $quoteInStatementAndBody((object) array('foo'=>'bar')));
  97. $withNewlinesAndTabs = __::template('This\n\t\tis: <%= $x %>.\n\tok.\nend.');
  98. $this->assertEquals('This\n\t\tis: that.\n\tok.\nend.', $withNewlinesAndTabs((object) array('x'=>'that')));
  99. $template = __::template('<i><%- $value %></i>');
  100. $result = $template((object) array('value'=>'<script>'));
  101. $this->assertEquals('<i>&lt;script&gt;</i>', $result);
  102. __::templateSettings(array(
  103. 'evaluate' => '/\{\{([\s\S]+?)\}\}/',
  104. 'interpolate' => '/\{\{=([\s\S]+?)\}\}/'
  105. ));
  106. $custom = __::template('<ul>{{ foreach($people as $key=>$name) { }}<li>{{= $people[$key] }}</li>{{ } }}</ul>');
  107. $result = $custom(array('people'=>array('moe'=>'Moe', 'larry'=>'Larry', 'curly'=>'Curly')));
  108. $this->assertEquals("<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", $result, 'can run arbitrary php in templates using custom tags');
  109. $customQuote = __::template("It's its, not it's");
  110. $this->assertEquals("It's its, not it's", $customQuote(new StdClass));
  111. $quoteInStatementAndBody = __::template('{{ if($foo == "bar"){ }}Statement quotes and \'quotes\'.{{ } }}');
  112. $this->assertEquals("Statement quotes and 'quotes'.", $quoteInStatementAndBody(array('foo'=>'bar')));
  113. __::templateSettings(array(
  114. 'evaluate' => '/<\?([\s\S]+?)\?>/',
  115. 'interpolate' => '/<\?=([\s\S]+?)\?>/'
  116. ));
  117. $customWithSpecialChars = __::template('<ul><? foreach($people as $key=>$name) { ?><li><?= $people[$key] ?></li><? } ?></ul>');
  118. $result = $customWithSpecialChars(array('people'=>array('moe'=>'Moe', 'larry'=>'Larry', 'curly'=>'Curly')));
  119. $this->assertEquals("<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", $result, 'can run arbitrary php in templates');
  120. $customWithSpecialCharsQuote = __::template("It's its, not it's");
  121. $this->assertEquals("It's its, not it's", $customWithSpecialCharsQuote(new StdClass));
  122. $quoteInStatementAndBody = __::template('<? if($foo == "bar"){ ?>Statement quotes and \'quotes\'.<? } ?>');
  123. $this->assertEquals("Statement quotes and 'quotes'.", $quoteInStatementAndBody(array('foo'=>'bar')));
  124. __::templateSettings(array(
  125. 'interpolate' => '/\{\{(.+?)\}\}/'
  126. ));
  127. $mustache = __::template('Hello {{$planet}}!');
  128. $this->assertEquals("Hello World!", $mustache(array('planet'=>'World')), 'can mimic mustache.js');
  129. // extra
  130. __::templateSettings(); // reset to default
  131. $basicTemplate = __::template('<%= $thing %> is gettin\' on my <%= $nerves %>!');
  132. $this->assertEquals("This is gettin' on my noives!", $basicTemplate(array('thing'=>'This', 'nerves'=>'noives')), 'can do basic attribute interpolation for multiple variables');
  133. $result = __('hello: <%= $name %>')->template(array('name'=>'moe'));
  134. $this->assertEquals('hello: moe', $result, 'works with OO-style call');
  135. $result = __('<%= $thing %> is gettin\' on my <%= $nerves %>!')->template(array('thing'=>'This', 'nerves'=>'noives'));
  136. $this->assertEquals("This is gettin' on my noives!", $result, 'can do basic attribute interpolation for multiple variables with OO-style call');
  137. $result = __('<%
  138. if($foo == "bar"){
  139. %>Statement quotes and \'quotes\'.<% } %>')->template((object) array('foo'=>'bar'));
  140. $this->assertEquals("Statement quotes and 'quotes'.", $result);
  141. // docs
  142. $compiled = __::template('hello: <%= $name %>');
  143. $result = $compiled(array('name'=>'moe'));
  144. $this->assertEquals('hello: moe', $result);
  145. $list = '<% __::each($people, function($name) { %><li><%= $name %></li><% }); %>';
  146. $result = __::template($list, array('people'=>array('moe', 'curly', 'larry')));
  147. $this->assertEquals('<li>moe</li><li>curly</li><li>larry</li>', $result);
  148. __::templateSettings(array(
  149. 'interpolate' => '/\{\{(.+?)\}\}/'
  150. ));
  151. $mustache = __::template('Hello {{$planet}}!');
  152. $result = $mustache(array('planet'=>'World'));
  153. $this->assertEquals('Hello World!', $result);
  154. $template = __::template('<i><%- $value %></i>');
  155. $result = $template(array('value'=>'<script>'));
  156. $this->assertEquals('<i>&lt;script&gt;</i>', $result);
  157. $sans = __::template('A <% $this %> B');
  158. $this->assertEquals('A B', $sans());
  159. }
  160. public function testEscape() {
  161. // from js
  162. $this->assertEquals('Curly &amp; Moe', __::escape('Curly & Moe'));
  163. $this->assertEquals('Curly &amp;amp; Moe', __::escape('Curly &amp; Moe'));
  164. // extra
  165. $this->assertEquals('Curly &amp; Moe', __('Curly & Moe')->escape());
  166. $this->assertEquals('Curly &amp;amp; Moe', __('Curly &amp; Moe')->escape());
  167. }
  168. }