InflectorTest.php 4.1 KB

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