parse_test.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright 2013 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 compact
  5. import (
  6. "strings"
  7. "testing"
  8. "golang.org/x/text/internal/language"
  9. )
  10. var errSyntax = language.ErrSyntax
  11. type parseTest struct {
  12. i int // the index of this test
  13. in string
  14. lang, script, region string
  15. variants, ext string
  16. extList []string // only used when more than one extension is present
  17. invalid bool
  18. rewrite bool // special rewrite not handled by parseTag
  19. changed bool // string needed to be reformatted
  20. }
  21. func parseTests() []parseTest {
  22. tests := []parseTest{
  23. {in: "root", lang: "und"},
  24. {in: "und", lang: "und"},
  25. {in: "en", lang: "en"},
  26. {in: "en-US-u-va-posix", lang: "en", region: "US", ext: "u-va-posix"},
  27. {in: "ca-ES-valencia", lang: "ca", region: "ES", variants: "valencia"},
  28. {in: "en-US-u-rg-gbzzzz", lang: "en", region: "US", ext: "u-rg-gbzzzz"},
  29. {in: "xy", lang: "und", invalid: true},
  30. {in: "en-ZY", lang: "en", invalid: true},
  31. {in: "gsw", lang: "gsw"},
  32. {in: "sr_Latn", lang: "sr", script: "Latn"},
  33. {in: "af-Arab", lang: "af", script: "Arab"},
  34. {in: "nl-BE", lang: "nl", region: "BE"},
  35. {in: "es-419", lang: "es", region: "419"},
  36. {in: "und-001", lang: "und", region: "001"},
  37. {in: "de-latn-be", lang: "de", script: "Latn", region: "BE"},
  38. // Variants
  39. {in: "de-1901", lang: "de", variants: "1901"},
  40. // Accept with unsuppressed script.
  41. {in: "de-Latn-1901", lang: "de", script: "Latn", variants: "1901"},
  42. // Specialized.
  43. {in: "sl-rozaj", lang: "sl", variants: "rozaj"},
  44. {in: "sl-rozaj-lipaw", lang: "sl", variants: "rozaj-lipaw"},
  45. {in: "sl-rozaj-biske", lang: "sl", variants: "rozaj-biske"},
  46. {in: "sl-rozaj-biske-1994", lang: "sl", variants: "rozaj-biske-1994"},
  47. {in: "sl-rozaj-1994", lang: "sl", variants: "rozaj-1994"},
  48. // Maximum number of variants while adhering to prefix rules.
  49. {in: "sl-rozaj-biske-1994-alalc97-fonipa-fonupa-fonxsamp", lang: "sl", variants: "rozaj-biske-1994-alalc97-fonipa-fonupa-fonxsamp"},
  50. // Sorting.
  51. {in: "sl-1994-biske-rozaj", lang: "sl", variants: "rozaj-biske-1994", changed: true},
  52. {in: "sl-rozaj-biske-1994-alalc97-fonupa-fonipa-fonxsamp", lang: "sl", variants: "rozaj-biske-1994-alalc97-fonipa-fonupa-fonxsamp", changed: true},
  53. {in: "nl-fonxsamp-alalc97-fonipa-fonupa", lang: "nl", variants: "alalc97-fonipa-fonupa-fonxsamp", changed: true},
  54. // Duplicates variants are removed, but not an error.
  55. {in: "nl-fonupa-fonupa", lang: "nl", variants: "fonupa"},
  56. // Variants that do not have correct prefixes. We still accept these.
  57. {in: "de-Cyrl-1901", lang: "de", script: "Cyrl", variants: "1901"},
  58. {in: "sl-rozaj-lipaw-1994", lang: "sl", variants: "rozaj-lipaw-1994"},
  59. {in: "sl-1994-biske-rozaj-1994-biske-rozaj", lang: "sl", variants: "rozaj-biske-1994", changed: true},
  60. {in: "de-Cyrl-1901", lang: "de", script: "Cyrl", variants: "1901"},
  61. // Invalid variant.
  62. {in: "de-1902", lang: "de", variants: "", invalid: true},
  63. {in: "EN_CYRL", lang: "en", script: "Cyrl"},
  64. // private use and extensions
  65. {in: "x-a-b-c-d", ext: "x-a-b-c-d"},
  66. {in: "x_A.-B-C_D", ext: "x-b-c-d", invalid: true, changed: true},
  67. {in: "x-aa-bbbb-cccccccc-d", ext: "x-aa-bbbb-cccccccc-d"},
  68. {in: "en-c_cc-b-bbb-a-aaa", lang: "en", changed: true, extList: []string{"a-aaa", "b-bbb", "c-cc"}},
  69. {in: "en-x_cc-b-bbb-a-aaa", lang: "en", ext: "x-cc-b-bbb-a-aaa", changed: true},
  70. {in: "en-c_cc-b-bbb-a-aaa-x-x", lang: "en", changed: true, extList: []string{"a-aaa", "b-bbb", "c-cc", "x-x"}},
  71. {in: "en-v-c", lang: "en", ext: "", invalid: true},
  72. {in: "en-v-abcdefghi", lang: "en", ext: "", invalid: true},
  73. {in: "en-v-abc-x", lang: "en", ext: "v-abc", invalid: true},
  74. {in: "en-v-abc-x-", lang: "en", ext: "v-abc", invalid: true},
  75. {in: "en-v-abc-w-x-xx", lang: "en", extList: []string{"v-abc", "x-xx"}, invalid: true, changed: true},
  76. {in: "en-v-abc-w-y-yx", lang: "en", extList: []string{"v-abc", "y-yx"}, invalid: true, changed: true},
  77. {in: "en-v-c-abc", lang: "en", ext: "c-abc", invalid: true, changed: true},
  78. {in: "en-v-w-abc", lang: "en", ext: "w-abc", invalid: true, changed: true},
  79. {in: "en-v-x-abc", lang: "en", ext: "x-abc", invalid: true, changed: true},
  80. {in: "en-v-x-a", lang: "en", ext: "x-a", invalid: true, changed: true},
  81. {in: "en-9-aa-0-aa-z-bb-x-a", lang: "en", extList: []string{"0-aa", "9-aa", "z-bb", "x-a"}, changed: true},
  82. {in: "en-u-c", lang: "en", ext: "", invalid: true},
  83. {in: "en-u-co-phonebk", lang: "en", ext: "u-co-phonebk"},
  84. {in: "en-u-co-phonebk-ca", lang: "en", ext: "u-co-phonebk", invalid: true},
  85. {in: "en-u-nu-arabic-co-phonebk-ca", lang: "en", ext: "u-co-phonebk-nu-arabic", invalid: true, changed: true},
  86. {in: "en-u-nu-arabic-co-phonebk-ca-x", lang: "en", ext: "u-co-phonebk-nu-arabic", invalid: true, changed: true},
  87. {in: "en-u-nu-arabic-co-phonebk-ca-s", lang: "en", ext: "u-co-phonebk-nu-arabic", invalid: true, changed: true},
  88. {in: "en-u-nu-arabic-co-phonebk-ca-a12345678", lang: "en", ext: "u-co-phonebk-nu-arabic", invalid: true, changed: true},
  89. {in: "en-u-co-phonebook", lang: "en", ext: "", invalid: true},
  90. {in: "en-u-co-phonebook-cu-xau", lang: "en", ext: "u-cu-xau", invalid: true, changed: true},
  91. {in: "en-Cyrl-u-co-phonebk", lang: "en", script: "Cyrl", ext: "u-co-phonebk"},
  92. {in: "en-US-u-co-phonebk", lang: "en", region: "US", ext: "u-co-phonebk"},
  93. {in: "en-US-u-co-phonebk-cu-xau", lang: "en", region: "US", ext: "u-co-phonebk-cu-xau"},
  94. {in: "en-scotland-u-co-phonebk", lang: "en", variants: "scotland", ext: "u-co-phonebk"},
  95. {in: "en-u-cu-xua-co-phonebk", lang: "en", ext: "u-co-phonebk-cu-xua", changed: true},
  96. {in: "en-u-def-abc-cu-xua-co-phonebk", lang: "en", ext: "u-abc-def-co-phonebk-cu-xua", changed: true},
  97. {in: "en-u-def-abc", lang: "en", ext: "u-abc-def", changed: true},
  98. {in: "en-u-cu-xua-co-phonebk-a-cd", lang: "en", extList: []string{"a-cd", "u-co-phonebk-cu-xua"}, changed: true},
  99. // Invalid "u" extension. Drop invalid parts.
  100. {in: "en-u-cu-co-phonebk", lang: "en", extList: []string{"u-co-phonebk"}, invalid: true, changed: true},
  101. {in: "en-u-cu-xau-co", lang: "en", extList: []string{"u-cu-xau"}, invalid: true},
  102. // We allow duplicate keys as the LDML spec does not explicitly prohibit it.
  103. // TODO: Consider eliminating duplicates and returning an error.
  104. {in: "en-u-cu-xau-co-phonebk-cu-xau", lang: "en", ext: "u-co-phonebk-cu-xau", changed: true},
  105. {in: "en-t-en-Cyrl-NL-fonipa", lang: "en", ext: "t-en-cyrl-nl-fonipa", changed: true},
  106. {in: "en-t-en-Cyrl-NL-fonipa-t0-abc-def", lang: "en", ext: "t-en-cyrl-nl-fonipa-t0-abc-def", changed: true},
  107. {in: "en-t-t0-abcd", lang: "en", ext: "t-t0-abcd"},
  108. // Not necessary to have changed here.
  109. {in: "en-t-nl-abcd", lang: "en", ext: "t-nl", invalid: true},
  110. {in: "en-t-nl-latn", lang: "en", ext: "t-nl-latn"},
  111. {in: "en-t-t0-abcd-x-a", lang: "en", extList: []string{"t-t0-abcd", "x-a"}},
  112. // invalid
  113. {in: "", lang: "und", invalid: true},
  114. {in: "-", lang: "und", invalid: true},
  115. {in: "x", lang: "und", invalid: true},
  116. {in: "x-", lang: "und", invalid: true},
  117. {in: "x--", lang: "und", invalid: true},
  118. {in: "a-a-b-c-d", lang: "und", invalid: true},
  119. {in: "en-", lang: "en", invalid: true},
  120. {in: "enne-", lang: "und", invalid: true},
  121. {in: "en.", lang: "und", invalid: true},
  122. {in: "en.-latn", lang: "und", invalid: true},
  123. {in: "en.-en", lang: "en", invalid: true},
  124. {in: "x-a-tooManyChars-c-d", ext: "x-a-c-d", invalid: true, changed: true},
  125. {in: "a-tooManyChars-c-d", lang: "und", invalid: true},
  126. // TODO: check key-value validity
  127. // { in: "en-u-cu-xd", lang: "en", ext: "u-cu-xd", invalid: true },
  128. {in: "en-t-abcd", lang: "en", invalid: true},
  129. {in: "en-Latn-US-en", lang: "en", script: "Latn", region: "US", invalid: true},
  130. // rewrites (more tests in TestGrandfathered)
  131. {in: "zh-min-nan", lang: "nan"},
  132. {in: "zh-yue", lang: "yue"},
  133. {in: "zh-xiang", lang: "hsn", rewrite: true},
  134. {in: "zh-guoyu", lang: "cmn", rewrite: true},
  135. {in: "iw", lang: "iw"},
  136. {in: "sgn-BE-FR", lang: "sfb", rewrite: true},
  137. {in: "i-klingon", lang: "tlh", rewrite: true},
  138. }
  139. for i, tt := range tests {
  140. tests[i].i = i
  141. if tt.extList != nil {
  142. tests[i].ext = strings.Join(tt.extList, "-")
  143. }
  144. if tt.ext != "" && tt.extList == nil {
  145. tests[i].extList = []string{tt.ext}
  146. }
  147. }
  148. return tests
  149. }
  150. // partChecks runs checks for each part by calling the function returned by f.
  151. func partChecks(t *testing.T, f func(*parseTest) (Tag, bool)) {
  152. for i, tt := range parseTests() {
  153. tag, skip := f(&tt)
  154. if skip {
  155. continue
  156. }
  157. if l, _ := language.ParseBase(tt.lang); l != tag.Tag().LangID {
  158. t.Errorf("%d: lang was %q; want %q", i, tag.Tag().LangID, l)
  159. }
  160. if sc, _ := language.ParseScript(tt.script); sc != tag.Tag().ScriptID {
  161. t.Errorf("%d: script was %q; want %q", i, tag.Tag().ScriptID, sc)
  162. }
  163. if r, _ := language.ParseRegion(tt.region); r != tag.Tag().RegionID {
  164. t.Errorf("%d: region was %q; want %q", i, tag.Tag().RegionID, r)
  165. }
  166. v := tag.Tag().Variants()
  167. if v != "" {
  168. v = v[1:]
  169. }
  170. if v != tt.variants {
  171. t.Errorf("%d: variants was %q; want %q", i, v, tt.variants)
  172. }
  173. if e := strings.Join(tag.Tag().Extensions(), "-"); e != tt.ext {
  174. t.Errorf("%d: extensions were %q; want %q", i, e, tt.ext)
  175. }
  176. }
  177. }
  178. func mk(s string) Tag {
  179. tag, _ := language.Parse(s)
  180. return Make(tag)
  181. }