InflectorTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
  2. /**
  3. * Tests Kohana inflector class
  4. *
  5. * @group kohana
  6. * @group kohana.core
  7. * @group kohana.core.inflector
  8. *
  9. * @package Kohana
  10. * @category Tests
  11. * @author Kohana Team
  12. * @author Jeremy Bush <[email protected]>
  13. * @copyright (c) 2008-2012 Kohana Team
  14. * @license http://kohanaframework.org/license
  15. */
  16. class Kohana_InflectorTest extends Unittest_TestCase
  17. {
  18. /**
  19. * Provides test data for test_lang()
  20. *
  21. * @return array
  22. */
  23. public function provider_uncountable()
  24. {
  25. return array(
  26. // $value, $result
  27. array('fish', TRUE),
  28. array('cat', FALSE),
  29. array('deer', TRUE),
  30. array('bison', TRUE),
  31. array('friend', FALSE),
  32. );
  33. }
  34. /**
  35. * Tests Inflector::uncountable
  36. *
  37. * @test
  38. * @dataProvider provider_uncountable
  39. * @param boolean $input Input for File::mime
  40. * @param boolean $expected Output for File::mime
  41. */
  42. public function test_uncountable($input, $expected)
  43. {
  44. $this->assertSame($expected, Inflector::uncountable($input));
  45. }
  46. /**
  47. * Provides test data for test_lang()
  48. *
  49. * @return array
  50. */
  51. public function provider_singular()
  52. {
  53. return array(
  54. // $value, $result
  55. array('fish', NULL, 'fish'),
  56. array('cats', NULL, 'cat'),
  57. array('cats', 2, 'cats'),
  58. array('cats', '2', 'cats'),
  59. array('children', NULL, 'child'),
  60. array('meters', 0.6, 'meters'),
  61. array('meters', 1.6, 'meters'),
  62. array('meters', 1.0, 'meter'),
  63. array('status', NULL, 'status'),
  64. array('statuses', NULL, 'status'),
  65. array('heroes', NULL, 'hero'),
  66. );
  67. }
  68. /**
  69. * Tests Inflector::singular
  70. *
  71. * @test
  72. * @dataProvider provider_singular
  73. * @param boolean $input Input for File::mime
  74. * @param boolean $expected Output for File::mime
  75. */
  76. public function test_singular($input, $count, $expected)
  77. {
  78. $this->assertSame($expected, Inflector::singular($input, $count));
  79. }
  80. /**
  81. * Provides test data for test_lang()
  82. *
  83. * @return array
  84. */
  85. public function provider_plural()
  86. {
  87. return array(
  88. // $value, $result
  89. array('fish', NULL, 'fish'),
  90. array('cat', NULL, 'cats'),
  91. array('cats', 1, 'cats'),
  92. array('cats', '1', 'cats'),
  93. array('movie', NULL, 'movies'),
  94. array('meter', 0.6, 'meters'),
  95. array('meter', 1.6, 'meters'),
  96. array('meter', 1.0, 'meter'),
  97. array('hero', NULL, 'heroes'),
  98. array('Dog', NULL, 'Dogs'), // Titlecase
  99. array('DOG', NULL, 'DOGS'), // Uppercase
  100. );
  101. }
  102. /**
  103. * Tests Inflector::plural
  104. *
  105. * @test
  106. * @dataProvider provider_plural
  107. * @param boolean $input Input for File::mime
  108. * @param boolean $expected Output for File::mime
  109. */
  110. public function test_plural($input, $count, $expected)
  111. {
  112. $this->assertSame($expected, Inflector::plural($input, $count));
  113. }
  114. /**
  115. * Provides test data for test_camelize()
  116. *
  117. * @return array
  118. */
  119. public function provider_camelize()
  120. {
  121. return array(
  122. // $value, $result
  123. array('mother cat', 'camelize', 'motherCat'),
  124. array('kittens in bed', 'camelize', 'kittensInBed'),
  125. array('mother cat', 'underscore', 'mother_cat'),
  126. array('kittens in bed', 'underscore', 'kittens_in_bed'),
  127. array('kittens-are-cats', 'humanize', 'kittens are cats'),
  128. array('dogs_as_well', 'humanize', 'dogs as well'),
  129. );
  130. }
  131. /**
  132. * Tests Inflector::camelize
  133. *
  134. * @test
  135. * @dataProvider provider_camelize
  136. * @param boolean $input Input for File::mime
  137. * @param boolean $expected Output for File::mime
  138. */
  139. public function test_camelize($input, $method, $expected)
  140. {
  141. $this->assertSame($expected, Inflector::$method($input));
  142. }
  143. /**
  144. * Provides data for test_decamelize()
  145. *
  146. * @return array
  147. */
  148. public function provider_decamelize()
  149. {
  150. return array(
  151. array('getText', '_', 'get_text'),
  152. array('getJSON', '_', 'get_json'),
  153. array('getLongText', '_', 'get_long_text'),
  154. array('getI18N', '_', 'get_i18n'),
  155. array('getL10n', '_', 'get_l10n'),
  156. array('getTe5t1ng', '_', 'get_te5t1ng'),
  157. array('OpenFile', '_', 'open_file'),
  158. array('CloseIoSocket', '_', 'close_io_socket'),
  159. array('fooBar', ' ', 'foo bar'),
  160. array('camelCase', '+', 'camel+case'),
  161. );
  162. }
  163. /**
  164. * Tests Inflector::decamelize()
  165. *
  166. * @test
  167. * @dataProvider provider_decamelize
  168. * @param string $input Camelized string
  169. * @param string $glue Glue
  170. * @param string $expected Expected string
  171. */
  172. public function test_decamelize($input, $glue, $expected)
  173. {
  174. $this->assertSame($expected, Inflector::decamelize($input, $glue));
  175. }
  176. }