InflectorTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  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 <contractfrombelow@gmail.com>
  13. * @copyright (c) Kohana Team
  14. * @license https://koseven.ga/LICENSE.md
  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 [
  26. // $value, $result
  27. ['fish', TRUE],
  28. ['cat', FALSE],
  29. ['deer', TRUE],
  30. ['bison', TRUE],
  31. ['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 [
  54. // $value, $result
  55. ['fish', NULL, 'fish'],
  56. ['cats', NULL, 'cat'],
  57. ['cats', 2, 'cats'],
  58. ['cats', '2', 'cats'],
  59. ['children', NULL, 'child'],
  60. ['meters', 0.6, 'meters'],
  61. ['meters', 1.6, 'meters'],
  62. ['meters', 1.0, 'meter'],
  63. ['status', NULL, 'status'],
  64. ['statuses', NULL, 'status'],
  65. ['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 [
  88. // $value, $result
  89. ['fish', NULL, 'fish'],
  90. ['cat', NULL, 'cats'],
  91. ['cats', 1, 'cats'],
  92. ['cats', '1', 'cats'],
  93. ['movie', NULL, 'movies'],
  94. ['meter', 0.6, 'meters'],
  95. ['meter', 1.6, 'meters'],
  96. ['meter', 1.0, 'meter'],
  97. ['hero', NULL, 'heroes'],
  98. ['Dog', NULL, 'Dogs'], // Titlecase
  99. ['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 [
  122. // $value, $result
  123. ['mother cat', 'camelize', 'motherCat'],
  124. ['kittens in bed', 'camelize', 'kittensInBed'],
  125. ['mother cat', 'underscore', 'mother_cat'],
  126. ['kittens in bed', 'underscore', 'kittens_in_bed'],
  127. ['kittens-are-cats', 'humanize', 'kittens are cats'],
  128. ['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 [
  151. ['getText', '_', 'get_text'],
  152. ['getJSON', '_', 'get_json'],
  153. ['getLongText', '_', 'get_long_text'],
  154. ['getI18N', '_', 'get_i18n'],
  155. ['getL10n', '_', 'get_l10n'],
  156. ['getTe5t1ng', '_', 'get_te5t1ng'],
  157. ['OpenFile', '_', 'open_file'],
  158. ['CloseIoSocket', '_', 'close_io_socket'],
  159. ['fooBar', ' ', 'foo bar'],
  160. ['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. }