i18next_plurals_spec.cr 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. require "spectator"
  2. require "../src/invidious/helpers/i18next.cr"
  3. Spectator.configure do |config|
  4. config.fail_blank
  5. config.randomize
  6. end
  7. def resolver
  8. I18next::Plurals::RESOLVER
  9. end
  10. FORM_TESTS = {
  11. "ach" => I18next::Plurals::PluralForms::Single_gt_one,
  12. "ar" => I18next::Plurals::PluralForms::Special_Arabic,
  13. "be" => I18next::Plurals::PluralForms::Dual_Slavic,
  14. "cy" => I18next::Plurals::PluralForms::Special_Welsh,
  15. "fr" => I18next::Plurals::PluralForms::Special_French_Portuguese,
  16. "en" => I18next::Plurals::PluralForms::Single_not_one,
  17. "es" => I18next::Plurals::PluralForms::Special_Spanish_Italian,
  18. "ga" => I18next::Plurals::PluralForms::Special_Irish,
  19. "gd" => I18next::Plurals::PluralForms::Special_Scottish_Gaelic,
  20. "he" => I18next::Plurals::PluralForms::Special_Hebrew,
  21. "hr" => I18next::Plurals::PluralForms::Special_Hungarian_Serbian,
  22. "is" => I18next::Plurals::PluralForms::Special_Icelandic,
  23. "it" => I18next::Plurals::PluralForms::Special_Spanish_Italian,
  24. "jv" => I18next::Plurals::PluralForms::Special_Javanese,
  25. "kw" => I18next::Plurals::PluralForms::Special_Cornish,
  26. "lt" => I18next::Plurals::PluralForms::Special_Lithuanian,
  27. "lv" => I18next::Plurals::PluralForms::Special_Latvian,
  28. "mk" => I18next::Plurals::PluralForms::Special_Macedonian,
  29. "mnk" => I18next::Plurals::PluralForms::Special_Mandinka,
  30. "mt" => I18next::Plurals::PluralForms::Special_Maltese,
  31. "or" => I18next::Plurals::PluralForms::Special_Odia,
  32. "pl" => I18next::Plurals::PluralForms::Special_Polish_Kashubian,
  33. "pt" => I18next::Plurals::PluralForms::Special_French_Portuguese,
  34. "pt-PT" => I18next::Plurals::PluralForms::Single_gt_one,
  35. "pt-BR" => I18next::Plurals::PluralForms::Special_French_Portuguese,
  36. "ro" => I18next::Plurals::PluralForms::Special_Romanian,
  37. "sk" => I18next::Plurals::PluralForms::Special_Czech_Slovak,
  38. "sl" => I18next::Plurals::PluralForms::Special_Slovenian,
  39. "su" => I18next::Plurals::PluralForms::None,
  40. "sr" => I18next::Plurals::PluralForms::Special_Hungarian_Serbian,
  41. }
  42. SUFFIX_TESTS = {
  43. "ach" => [
  44. {num: 0, suffix: ""},
  45. {num: 1, suffix: ""},
  46. {num: 10, suffix: "_plural"},
  47. ],
  48. "ar" => [
  49. {num: 0, suffix: "_0"},
  50. {num: 1, suffix: "_1"},
  51. {num: 2, suffix: "_2"},
  52. {num: 3, suffix: "_3"},
  53. {num: 4, suffix: "_3"},
  54. {num: 104, suffix: "_3"},
  55. {num: 11, suffix: "_4"},
  56. {num: 99, suffix: "_4"},
  57. {num: 199, suffix: "_4"},
  58. {num: 100, suffix: "_5"},
  59. ],
  60. "be" => [
  61. {num: 0, suffix: "_2"},
  62. {num: 1, suffix: "_0"},
  63. {num: 5, suffix: "_2"},
  64. ],
  65. "cy" => [
  66. {num: 0, suffix: "_2"},
  67. {num: 1, suffix: "_0"},
  68. {num: 3, suffix: "_2"},
  69. {num: 8, suffix: "_3"},
  70. ],
  71. "en" => [
  72. {num: 0, suffix: "_plural"},
  73. {num: 1, suffix: ""},
  74. {num: 10, suffix: "_plural"},
  75. ],
  76. "es" => [
  77. {num: 0, suffix: "_2"},
  78. {num: 1, suffix: "_0"},
  79. {num: 10, suffix: "_2"},
  80. {num: 6_000_000, suffix: "_1"},
  81. ],
  82. "fr" => [
  83. {num: 0, suffix: "_0"},
  84. {num: 1, suffix: "_0"},
  85. {num: 10, suffix: "_2"},
  86. {num: 4_000_000, suffix: "_1"},
  87. {num: 6_260_000, suffix: "_2"},
  88. ],
  89. "ga" => [
  90. {num: 1, suffix: "_0"},
  91. {num: 2, suffix: "_1"},
  92. {num: 3, suffix: "_2"},
  93. {num: 7, suffix: "_3"},
  94. {num: 11, suffix: "_4"},
  95. ],
  96. "gd" => [
  97. {num: 1, suffix: "_0"},
  98. {num: 2, suffix: "_1"},
  99. {num: 3, suffix: "_2"},
  100. {num: 20, suffix: "_3"},
  101. ],
  102. "he" => [
  103. {num: 0, suffix: "_3"},
  104. {num: 1, suffix: "_0"},
  105. {num: 2, suffix: "_1"},
  106. {num: 3, suffix: "_3"},
  107. {num: 20, suffix: "_2"},
  108. {num: 21, suffix: "_3"},
  109. {num: 30, suffix: "_2"},
  110. {num: 100, suffix: "_2"},
  111. {num: 101, suffix: "_3"},
  112. ],
  113. "is" => [
  114. {num: 1, suffix: ""},
  115. {num: 2, suffix: "_plural"},
  116. ],
  117. "jv" => [
  118. {num: 0, suffix: "_0"},
  119. {num: 1, suffix: "_1"},
  120. ],
  121. "kw" => [
  122. {num: 1, suffix: "_0"},
  123. {num: 2, suffix: "_1"},
  124. {num: 3, suffix: "_2"},
  125. {num: 4, suffix: "_3"},
  126. ],
  127. "lt" => [
  128. {num: 1, suffix: "_0"},
  129. {num: 2, suffix: "_1"},
  130. {num: 10, suffix: "_2"},
  131. ],
  132. "lv" => [
  133. {num: 1, suffix: "_0"},
  134. {num: 2, suffix: "_1"},
  135. {num: 0, suffix: "_2"},
  136. ],
  137. "mk" => [
  138. {num: 1, suffix: ""},
  139. {num: 2, suffix: "_plural"},
  140. {num: 0, suffix: "_plural"},
  141. {num: 11, suffix: "_plural"},
  142. {num: 21, suffix: ""},
  143. {num: 31, suffix: ""},
  144. {num: 311, suffix: "_plural"},
  145. ],
  146. "mnk" => [
  147. {num: 0, suffix: "_0"},
  148. {num: 1, suffix: "_1"},
  149. {num: 2, suffix: "_2"},
  150. ],
  151. "mt" => [
  152. {num: 1, suffix: "_0"},
  153. {num: 2, suffix: "_1"},
  154. {num: 11, suffix: "_2"},
  155. {num: 20, suffix: "_3"},
  156. ],
  157. "or" => [
  158. {num: 2, suffix: "_1"},
  159. {num: 1, suffix: "_0"},
  160. ],
  161. "pl" => [
  162. {num: 0, suffix: "_2"},
  163. {num: 1, suffix: "_0"},
  164. {num: 5, suffix: "_2"},
  165. ],
  166. "pt-BR" => [
  167. {num: 0, suffix: "_0"},
  168. {num: 1, suffix: "_0"},
  169. {num: 10, suffix: "_2"},
  170. {num: 42, suffix: "_2"},
  171. {num: 9_000_000, suffix: "_1"},
  172. ],
  173. "pt-PT" => [
  174. {num: 0, suffix: ""},
  175. {num: 1, suffix: ""},
  176. {num: 10, suffix: "_plural"},
  177. {num: 9_000_000, suffix: "_plural"},
  178. ],
  179. "ro" => [
  180. {num: 0, suffix: "_1"},
  181. {num: 1, suffix: "_0"},
  182. {num: 20, suffix: "_2"},
  183. ],
  184. "sk" => [
  185. {num: 0, suffix: "_2"},
  186. {num: 1, suffix: "_0"},
  187. {num: 5, suffix: "_2"},
  188. ],
  189. "sl" => [
  190. {num: 5, suffix: "_0"},
  191. {num: 1, suffix: "_1"},
  192. {num: 2, suffix: "_2"},
  193. {num: 3, suffix: "_3"},
  194. ],
  195. "su" => [
  196. {num: 0, suffix: "_0"},
  197. {num: 1, suffix: "_0"},
  198. {num: 10, suffix: "_0"},
  199. ],
  200. "sr" => [
  201. {num: 1, suffix: "_0"},
  202. {num: 51, suffix: "_0"},
  203. {num: 32, suffix: "_1"},
  204. {num: 100, suffix: "_2"},
  205. {num: 100_000, suffix: "_2"},
  206. ],
  207. }
  208. Spectator.describe "i18next_Plural_Resolver" do
  209. describe "get_plural_form" do
  210. sample FORM_TESTS do |locale, form|
  211. it "returns the right plural form for locale '#{locale}'" do
  212. expect(resolver.get_plural_form(locale)).to eq(form)
  213. end
  214. end
  215. end
  216. describe "get_suffix" do
  217. sample SUFFIX_TESTS do |locale, tests|
  218. it "returns the right suffix for locale '#{locale}'" do
  219. tests.each do |d|
  220. expect(resolver.get_suffix(locale, d[:num])).to eq(d[:suffix])
  221. end
  222. end
  223. end
  224. end
  225. end