naturalsort.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Vincent Petry <PVince81@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class Test_NaturalSort extends \Test\TestCase {
  9. /**
  10. * @dataProvider naturalSortDataProvider
  11. */
  12. public function testNaturalSortCompare($array, $sorted)
  13. {
  14. if(!class_exists('Collator')) {
  15. $this->markTestSkipped('The intl module is not available, natural sorting might not work as expected.');
  16. return;
  17. }
  18. $comparator = \OC\NaturalSort::getInstance();
  19. usort($array, array($comparator, 'compare'));
  20. $this->assertEquals($sorted, $array);
  21. }
  22. /**
  23. * @dataProvider defaultCollatorDataProvider
  24. */
  25. public function testDefaultCollatorCompare($array, $sorted)
  26. {
  27. $comparator = new \OC\NaturalSort(new \OC\NaturalSort_DefaultCollator());
  28. usort($array, array($comparator, 'compare'));
  29. $this->assertEquals($sorted, $array);
  30. }
  31. /**
  32. * Data provider for natural sorting with php5-intl's Collator.
  33. * Must provide the same result as in core/js/tests/specs/coreSpec.js
  34. * @return array test cases
  35. */
  36. public function naturalSortDataProvider()
  37. {
  38. return array(
  39. // different casing
  40. array(
  41. // unsorted
  42. array(
  43. 'aaa',
  44. 'bbb',
  45. 'BBB',
  46. 'AAA'
  47. ),
  48. // sorted
  49. array(
  50. 'aaa',
  51. 'AAA',
  52. 'bbb',
  53. 'BBB'
  54. )
  55. ),
  56. // numbers
  57. array(
  58. // unsorted
  59. array(
  60. '124.txt',
  61. 'abc1',
  62. '123.txt',
  63. 'abc',
  64. 'abc2',
  65. 'def (2).txt',
  66. 'ghi 10.txt',
  67. 'abc12',
  68. 'def.txt',
  69. 'def (1).txt',
  70. 'ghi 2.txt',
  71. 'def (10).txt',
  72. 'abc10',
  73. 'def (12).txt',
  74. 'z',
  75. 'ghi.txt',
  76. 'za',
  77. 'ghi 1.txt',
  78. 'ghi 12.txt',
  79. 'zz',
  80. '15.txt',
  81. '15b.txt',
  82. ),
  83. // sorted
  84. array(
  85. '15.txt',
  86. '15b.txt',
  87. '123.txt',
  88. '124.txt',
  89. 'abc',
  90. 'abc1',
  91. 'abc2',
  92. 'abc10',
  93. 'abc12',
  94. 'def.txt',
  95. 'def (1).txt',
  96. 'def (2).txt',
  97. 'def (10).txt',
  98. 'def (12).txt',
  99. 'ghi.txt',
  100. 'ghi 1.txt',
  101. 'ghi 2.txt',
  102. 'ghi 10.txt',
  103. 'ghi 12.txt',
  104. 'z',
  105. 'za',
  106. 'zz',
  107. )
  108. ),
  109. // chinese characters
  110. array(
  111. // unsorted
  112. array(
  113. '十.txt',
  114. '一.txt',
  115. '二.txt',
  116. '十 2.txt',
  117. '三.txt',
  118. '四.txt',
  119. 'abc.txt',
  120. '五.txt',
  121. '七.txt',
  122. '八.txt',
  123. '九.txt',
  124. '六.txt',
  125. '十一.txt',
  126. '波.txt',
  127. '破.txt',
  128. '莫.txt',
  129. '啊.txt',
  130. '123.txt',
  131. ),
  132. // sorted
  133. array(
  134. '123.txt',
  135. 'abc.txt',
  136. '一.txt',
  137. '七.txt',
  138. '三.txt',
  139. '九.txt',
  140. '二.txt',
  141. '五.txt',
  142. '八.txt',
  143. '六.txt',
  144. '十.txt',
  145. '十 2.txt',
  146. '十一.txt',
  147. '啊.txt',
  148. '四.txt',
  149. '波.txt',
  150. '破.txt',
  151. '莫.txt',
  152. )
  153. ),
  154. // with umlauts
  155. array(
  156. // unsorted
  157. array(
  158. 'öh.txt',
  159. 'Äh.txt',
  160. 'oh.txt',
  161. 'Üh 2.txt',
  162. 'Üh.txt',
  163. 'ah.txt',
  164. 'Öh.txt',
  165. 'uh.txt',
  166. 'üh.txt',
  167. 'äh.txt',
  168. ),
  169. // sorted
  170. array(
  171. 'ah.txt',
  172. 'äh.txt',
  173. 'Äh.txt',
  174. 'oh.txt',
  175. 'öh.txt',
  176. 'Öh.txt',
  177. 'uh.txt',
  178. 'üh.txt',
  179. 'Üh.txt',
  180. 'Üh 2.txt',
  181. )
  182. ),
  183. );
  184. }
  185. /**
  186. * Data provider for natural sorting with \OC\NaturalSort_DefaultCollator.
  187. * Must provide the same result as in core/js/tests/specs/coreSpec.js
  188. * @return array test cases
  189. */
  190. public function defaultCollatorDataProvider()
  191. {
  192. return array(
  193. // different casing
  194. array(
  195. // unsorted
  196. array(
  197. 'aaa',
  198. 'bbb',
  199. 'BBB',
  200. 'AAA'
  201. ),
  202. // sorted
  203. array(
  204. 'aaa',
  205. 'AAA',
  206. 'bbb',
  207. 'BBB'
  208. )
  209. ),
  210. // numbers
  211. array(
  212. // unsorted
  213. array(
  214. '124.txt',
  215. 'abc1',
  216. '123.txt',
  217. 'abc',
  218. 'abc2',
  219. 'def (2).txt',
  220. 'ghi 10.txt',
  221. 'abc12',
  222. 'def.txt',
  223. 'def (1).txt',
  224. 'ghi 2.txt',
  225. 'def (10).txt',
  226. 'abc10',
  227. 'def (12).txt',
  228. 'z',
  229. 'ghi.txt',
  230. 'za',
  231. 'ghi 1.txt',
  232. 'ghi 12.txt',
  233. 'zz',
  234. '15.txt',
  235. '15b.txt',
  236. ),
  237. // sorted
  238. array(
  239. '15.txt',
  240. '15b.txt',
  241. '123.txt',
  242. '124.txt',
  243. 'abc',
  244. 'abc1',
  245. 'abc2',
  246. 'abc10',
  247. 'abc12',
  248. 'def.txt',
  249. 'def (1).txt',
  250. 'def (2).txt',
  251. 'def (10).txt',
  252. 'def (12).txt',
  253. 'ghi.txt',
  254. 'ghi 1.txt',
  255. 'ghi 2.txt',
  256. 'ghi 10.txt',
  257. 'ghi 12.txt',
  258. 'z',
  259. 'za',
  260. 'zz',
  261. )
  262. ),
  263. );
  264. }
  265. }