You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

210 lines
4.6 KiB

  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package collate
  5. import (
  6. "reflect"
  7. "strings"
  8. "testing"
  9. "golang.org/x/text/internal/colltab"
  10. "golang.org/x/text/language"
  11. )
  12. var (
  13. defaultIgnore = ignore(colltab.Tertiary)
  14. defaultTable = getTable(locales[0])
  15. )
  16. func TestOptions(t *testing.T) {
  17. for i, tt := range []struct {
  18. in []Option
  19. out options
  20. }{
  21. 0: {
  22. out: options{
  23. ignore: defaultIgnore,
  24. },
  25. },
  26. 1: {
  27. in: []Option{IgnoreDiacritics},
  28. out: options{
  29. ignore: [colltab.NumLevels]bool{false, true, false, true, true},
  30. },
  31. },
  32. 2: {
  33. in: []Option{IgnoreCase, IgnoreDiacritics},
  34. out: options{
  35. ignore: ignore(colltab.Primary),
  36. },
  37. },
  38. 3: {
  39. in: []Option{ignoreDiacritics, IgnoreWidth},
  40. out: options{
  41. ignore: ignore(colltab.Primary),
  42. caseLevel: true,
  43. },
  44. },
  45. 4: {
  46. in: []Option{IgnoreWidth, ignoreDiacritics},
  47. out: options{
  48. ignore: ignore(colltab.Primary),
  49. caseLevel: true,
  50. },
  51. },
  52. 5: {
  53. in: []Option{IgnoreCase, IgnoreWidth},
  54. out: options{
  55. ignore: ignore(colltab.Secondary),
  56. },
  57. },
  58. 6: {
  59. in: []Option{IgnoreCase, IgnoreWidth, Loose},
  60. out: options{
  61. ignore: ignore(colltab.Primary),
  62. },
  63. },
  64. 7: {
  65. in: []Option{Force, IgnoreCase, IgnoreWidth, Loose},
  66. out: options{
  67. ignore: [colltab.NumLevels]bool{false, true, true, true, false},
  68. },
  69. },
  70. 8: {
  71. in: []Option{IgnoreDiacritics, IgnoreCase},
  72. out: options{
  73. ignore: ignore(colltab.Primary),
  74. },
  75. },
  76. 9: {
  77. in: []Option{Numeric},
  78. out: options{
  79. ignore: defaultIgnore,
  80. numeric: true,
  81. },
  82. },
  83. 10: {
  84. in: []Option{OptionsFromTag(language.MustParse("und-u-ks-level1"))},
  85. out: options{
  86. ignore: ignore(colltab.Primary),
  87. },
  88. },
  89. 11: {
  90. in: []Option{OptionsFromTag(language.MustParse("und-u-ks-level4"))},
  91. out: options{
  92. ignore: ignore(colltab.Quaternary),
  93. },
  94. },
  95. 12: {
  96. in: []Option{OptionsFromTag(language.MustParse("und-u-ks-identic"))},
  97. out: options{},
  98. },
  99. 13: {
  100. in: []Option{
  101. OptionsFromTag(language.MustParse("und-u-kn-true-kb-true-kc-true")),
  102. },
  103. out: options{
  104. ignore: defaultIgnore,
  105. caseLevel: true,
  106. backwards: true,
  107. numeric: true,
  108. },
  109. },
  110. 14: {
  111. in: []Option{
  112. OptionsFromTag(language.MustParse("und-u-kn-true-kb-true-kc-true")),
  113. OptionsFromTag(language.MustParse("und-u-kn-false-kb-false-kc-false")),
  114. },
  115. out: options{
  116. ignore: defaultIgnore,
  117. },
  118. },
  119. 15: {
  120. in: []Option{
  121. OptionsFromTag(language.MustParse("und-u-kn-true-kb-true-kc-true")),
  122. OptionsFromTag(language.MustParse("und-u-kn-foo-kb-foo-kc-foo")),
  123. },
  124. out: options{
  125. ignore: defaultIgnore,
  126. caseLevel: true,
  127. backwards: true,
  128. numeric: true,
  129. },
  130. },
  131. 16: { // Normal options take precedence over tag options.
  132. in: []Option{
  133. Numeric, IgnoreCase,
  134. OptionsFromTag(language.MustParse("und-u-kn-false-kc-true")),
  135. },
  136. out: options{
  137. ignore: ignore(colltab.Secondary),
  138. caseLevel: false,
  139. numeric: true,
  140. },
  141. },
  142. 17: {
  143. in: []Option{
  144. OptionsFromTag(language.MustParse("und-u-ka-shifted")),
  145. },
  146. out: options{
  147. ignore: defaultIgnore,
  148. alternate: altShifted,
  149. },
  150. },
  151. 18: {
  152. in: []Option{
  153. OptionsFromTag(language.MustParse("und-u-ka-blanked")),
  154. },
  155. out: options{
  156. ignore: defaultIgnore,
  157. alternate: altBlanked,
  158. },
  159. },
  160. 19: {
  161. in: []Option{
  162. OptionsFromTag(language.MustParse("und-u-ka-posix")),
  163. },
  164. out: options{
  165. ignore: defaultIgnore,
  166. alternate: altShiftTrimmed,
  167. },
  168. },
  169. } {
  170. c := newCollator(defaultTable)
  171. c.t = nil
  172. c.variableTop = 0
  173. c.f = 0
  174. c.setOptions(tt.in)
  175. if !reflect.DeepEqual(c.options, tt.out) {
  176. t.Errorf("%d: got %v; want %v", i, c.options, tt.out)
  177. }
  178. }
  179. }
  180. func TestAlternateSortTypes(t *testing.T) {
  181. testCases := []struct {
  182. lang string
  183. in []string
  184. want []string
  185. }{{
  186. lang: "zh,cmn,zh-Hant-u-co-pinyin,zh-HK-u-co-pinyin,zh-pinyin",
  187. in: []string{"爸爸", "妈妈", "儿子", "女儿"},
  188. want: []string{"爸爸", "儿子", "妈妈", "女儿"},
  189. }, {
  190. lang: "zh-Hant,zh-u-co-stroke,zh-Hant-u-co-stroke",
  191. in: []string{"爸爸", "妈妈", "儿子", "女儿"},
  192. want: []string{"儿子", "女儿", "妈妈", "爸爸"},
  193. }}
  194. for _, tc := range testCases {
  195. for _, tag := range strings.Split(tc.lang, ",") {
  196. got := append([]string{}, tc.in...)
  197. New(language.MustParse(tag)).SortStrings(got)
  198. if !reflect.DeepEqual(got, tc.want) {
  199. t.Errorf("New(%s).SortStrings(%v) = %v; want %v", tag, tc.in, got, tc.want)
  200. }
  201. }
  202. }
  203. }