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.
 
 
 

365 lines
14 KiB

  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 language
  5. import (
  6. "bytes"
  7. "strings"
  8. "testing"
  9. "golang.org/x/text/internal/tag"
  10. )
  11. type scanTest struct {
  12. ok bool // true if scanning does not result in an error
  13. in string
  14. tok []string // the expected tokens
  15. }
  16. var tests = []scanTest{
  17. {true, "", []string{}},
  18. {true, "1", []string{"1"}},
  19. {true, "en", []string{"en"}},
  20. {true, "root", []string{"root"}},
  21. {true, "maxchars", []string{"maxchars"}},
  22. {false, "bad/", []string{}},
  23. {false, "morethan8", []string{}},
  24. {false, "-", []string{}},
  25. {false, "----", []string{}},
  26. {false, "_", []string{}},
  27. {true, "en-US", []string{"en", "US"}},
  28. {true, "en_US", []string{"en", "US"}},
  29. {false, "en-US-", []string{"en", "US"}},
  30. {false, "en-US--", []string{"en", "US"}},
  31. {false, "en-US---", []string{"en", "US"}},
  32. {false, "en--US", []string{"en", "US"}},
  33. {false, "-en-US", []string{"en", "US"}},
  34. {false, "-en--US-", []string{"en", "US"}},
  35. {false, "-en--US-", []string{"en", "US"}},
  36. {false, "en-.-US", []string{"en", "US"}},
  37. {false, ".-en--US-.", []string{"en", "US"}},
  38. {false, "en-u.-US", []string{"en", "US"}},
  39. {true, "en-u1-US", []string{"en", "u1", "US"}},
  40. {true, "maxchar1_maxchar2-maxchar3", []string{"maxchar1", "maxchar2", "maxchar3"}},
  41. {false, "moreThan8-moreThan8-e", []string{"e"}},
  42. }
  43. func TestScan(t *testing.T) {
  44. for i, tt := range tests {
  45. scan := makeScannerString(tt.in)
  46. for j := 0; !scan.done; j++ {
  47. if j >= len(tt.tok) {
  48. t.Errorf("%d: extra token %q", i, scan.token)
  49. } else if tag.Compare(tt.tok[j], scan.token) != 0 {
  50. t.Errorf("%d: token %d: found %q; want %q", i, j, scan.token, tt.tok[j])
  51. break
  52. }
  53. scan.scan()
  54. }
  55. if s := strings.Join(tt.tok, "-"); tag.Compare(s, bytes.Replace(scan.b, b("_"), b("-"), -1)) != 0 {
  56. t.Errorf("%d: input: found %q; want %q", i, scan.b, s)
  57. }
  58. if (scan.err == nil) != tt.ok {
  59. t.Errorf("%d: ok: found %v; want %v", i, scan.err == nil, tt.ok)
  60. }
  61. }
  62. }
  63. func TestAcceptMinSize(t *testing.T) {
  64. for i, tt := range tests {
  65. // count number of successive tokens with a minimum size.
  66. for sz := 1; sz <= 8; sz++ {
  67. scan := makeScannerString(tt.in)
  68. scan.end, scan.next = 0, 0
  69. end := scan.acceptMinSize(sz)
  70. n := 0
  71. for i := 0; i < len(tt.tok) && len(tt.tok[i]) >= sz; i++ {
  72. n += len(tt.tok[i])
  73. if i > 0 {
  74. n++
  75. }
  76. }
  77. if end != n {
  78. t.Errorf("%d:%d: found len %d; want %d", i, sz, end, n)
  79. }
  80. }
  81. }
  82. }
  83. type parseTest struct {
  84. i int // the index of this test
  85. in string
  86. lang, script, region string
  87. variants, ext string
  88. extList []string // only used when more than one extension is present
  89. invalid bool
  90. rewrite bool // special rewrite not handled by parseTag
  91. changed bool // string needed to be reformatted
  92. }
  93. func parseTests() []parseTest {
  94. tests := []parseTest{
  95. {in: "root", lang: "und"},
  96. {in: "und", lang: "und"},
  97. {in: "en", lang: "en"},
  98. {in: "xy", lang: "und", invalid: true},
  99. {in: "en-ZY", lang: "en", invalid: true},
  100. {in: "gsw", lang: "gsw"},
  101. {in: "sr_Latn", lang: "sr", script: "Latn"},
  102. {in: "af-Arab", lang: "af", script: "Arab"},
  103. {in: "nl-BE", lang: "nl", region: "BE"},
  104. {in: "es-419", lang: "es", region: "419"},
  105. {in: "und-001", lang: "und", region: "001"},
  106. {in: "de-latn-be", lang: "de", script: "Latn", region: "BE"},
  107. // Variants
  108. {in: "de-1901", lang: "de", variants: "1901"},
  109. // Accept with unsuppressed script.
  110. {in: "de-Latn-1901", lang: "de", script: "Latn", variants: "1901"},
  111. // Specialized.
  112. {in: "sl-rozaj", lang: "sl", variants: "rozaj"},
  113. {in: "sl-rozaj-lipaw", lang: "sl", variants: "rozaj-lipaw"},
  114. {in: "sl-rozaj-biske", lang: "sl", variants: "rozaj-biske"},
  115. {in: "sl-rozaj-biske-1994", lang: "sl", variants: "rozaj-biske-1994"},
  116. {in: "sl-rozaj-1994", lang: "sl", variants: "rozaj-1994"},
  117. // Maximum number of variants while adhering to prefix rules.
  118. {in: "sl-rozaj-biske-1994-alalc97-fonipa-fonupa-fonxsamp", lang: "sl", variants: "rozaj-biske-1994-alalc97-fonipa-fonupa-fonxsamp"},
  119. // Sorting.
  120. {in: "sl-1994-biske-rozaj", lang: "sl", variants: "rozaj-biske-1994", changed: true},
  121. {in: "sl-rozaj-biske-1994-alalc97-fonupa-fonipa-fonxsamp", lang: "sl", variants: "rozaj-biske-1994-alalc97-fonipa-fonupa-fonxsamp", changed: true},
  122. {in: "nl-fonxsamp-alalc97-fonipa-fonupa", lang: "nl", variants: "alalc97-fonipa-fonupa-fonxsamp", changed: true},
  123. // Duplicates variants are removed, but not an error.
  124. {in: "nl-fonupa-fonupa", lang: "nl", variants: "fonupa"},
  125. // Variants that do not have correct prefixes. We still accept these.
  126. {in: "de-Cyrl-1901", lang: "de", script: "Cyrl", variants: "1901"},
  127. {in: "sl-rozaj-lipaw-1994", lang: "sl", variants: "rozaj-lipaw-1994"},
  128. {in: "sl-1994-biske-rozaj-1994-biske-rozaj", lang: "sl", variants: "rozaj-biske-1994", changed: true},
  129. {in: "de-Cyrl-1901", lang: "de", script: "Cyrl", variants: "1901"},
  130. // Invalid variant.
  131. {in: "de-1902", lang: "de", variants: "", invalid: true},
  132. {in: "EN_CYRL", lang: "en", script: "Cyrl"},
  133. // private use and extensions
  134. {in: "x-a-b-c-d", ext: "x-a-b-c-d"},
  135. {in: "x_A.-B-C_D", ext: "x-b-c-d", invalid: true, changed: true},
  136. {in: "x-aa-bbbb-cccccccc-d", ext: "x-aa-bbbb-cccccccc-d"},
  137. {in: "en-c_cc-b-bbb-a-aaa", lang: "en", changed: true, extList: []string{"a-aaa", "b-bbb", "c-cc"}},
  138. {in: "en-x_cc-b-bbb-a-aaa", lang: "en", ext: "x-cc-b-bbb-a-aaa", changed: true},
  139. {in: "en-c_cc-b-bbb-a-aaa-x-x", lang: "en", changed: true, extList: []string{"a-aaa", "b-bbb", "c-cc", "x-x"}},
  140. {in: "en-v-c", lang: "en", ext: "", invalid: true},
  141. {in: "en-v-abcdefghi", lang: "en", ext: "", invalid: true},
  142. {in: "en-v-abc-x", lang: "en", ext: "v-abc", invalid: true},
  143. {in: "en-v-abc-x-", lang: "en", ext: "v-abc", invalid: true},
  144. {in: "en-v-abc-w-x-xx", lang: "en", extList: []string{"v-abc", "x-xx"}, invalid: true, changed: true},
  145. {in: "en-v-abc-w-y-yx", lang: "en", extList: []string{"v-abc", "y-yx"}, invalid: true, changed: true},
  146. {in: "en-v-c-abc", lang: "en", ext: "c-abc", invalid: true, changed: true},
  147. {in: "en-v-w-abc", lang: "en", ext: "w-abc", invalid: true, changed: true},
  148. {in: "en-v-x-abc", lang: "en", ext: "x-abc", invalid: true, changed: true},
  149. {in: "en-v-x-a", lang: "en", ext: "x-a", invalid: true, changed: true},
  150. {in: "en-9-aa-0-aa-z-bb-x-a", lang: "en", extList: []string{"0-aa", "9-aa", "z-bb", "x-a"}, changed: true},
  151. {in: "en-u-c", lang: "en", ext: "", invalid: true},
  152. {in: "en-u-co-phonebk", lang: "en", ext: "u-co-phonebk"},
  153. {in: "en-u-co-phonebk-ca", lang: "en", ext: "u-co-phonebk", invalid: true},
  154. {in: "en-u-nu-arabic-co-phonebk-ca", lang: "en", ext: "u-co-phonebk-nu-arabic", invalid: true, changed: true},
  155. {in: "en-u-nu-arabic-co-phonebk-ca-x", lang: "en", ext: "u-co-phonebk-nu-arabic", invalid: true, changed: true},
  156. {in: "en-u-nu-arabic-co-phonebk-ca-s", lang: "en", ext: "u-co-phonebk-nu-arabic", invalid: true, changed: true},
  157. {in: "en-u-nu-arabic-co-phonebk-ca-a12345678", lang: "en", ext: "u-co-phonebk-nu-arabic", invalid: true, changed: true},
  158. {in: "en-u-co-phonebook", lang: "en", ext: "", invalid: true},
  159. {in: "en-u-co-phonebook-cu-xau", lang: "en", ext: "u-cu-xau", invalid: true, changed: true},
  160. {in: "en-Cyrl-u-co-phonebk", lang: "en", script: "Cyrl", ext: "u-co-phonebk"},
  161. {in: "en-US-u-co-phonebk", lang: "en", region: "US", ext: "u-co-phonebk"},
  162. {in: "en-US-u-co-phonebk-cu-xau", lang: "en", region: "US", ext: "u-co-phonebk-cu-xau"},
  163. {in: "en-scotland-u-co-phonebk", lang: "en", variants: "scotland", ext: "u-co-phonebk"},
  164. {in: "en-u-cu-xua-co-phonebk", lang: "en", ext: "u-co-phonebk-cu-xua", changed: true},
  165. {in: "en-u-def-abc-cu-xua-co-phonebk", lang: "en", ext: "u-abc-def-co-phonebk-cu-xua", changed: true},
  166. {in: "en-u-def-abc", lang: "en", ext: "u-abc-def", changed: true},
  167. {in: "en-u-cu-xua-co-phonebk-a-cd", lang: "en", extList: []string{"a-cd", "u-co-phonebk-cu-xua"}, changed: true},
  168. // Invalid "u" extension. Drop invalid parts.
  169. {in: "en-u-cu-co-phonebk", lang: "en", extList: []string{"u-co-phonebk"}, invalid: true, changed: true},
  170. {in: "en-u-cu-xau-co", lang: "en", extList: []string{"u-cu-xau"}, invalid: true},
  171. // LDML spec is not specific about it, but remove duplicates and return an error if the values differ.
  172. {in: "en-u-cu-xau-co-phonebk-cu-xau", lang: "en", ext: "u-co-phonebk-cu-xau", changed: true},
  173. // No change as the result is a substring of the original!
  174. {in: "en-US-u-cu-xau-cu-eur", lang: "en", region: "US", ext: "u-cu-xau", invalid: true, changed: false},
  175. {in: "en-t-en-Cyrl-NL-fonipa", lang: "en", ext: "t-en-cyrl-nl-fonipa", changed: true},
  176. {in: "en-t-en-Cyrl-NL-fonipa-t0-abc-def", lang: "en", ext: "t-en-cyrl-nl-fonipa-t0-abc-def", changed: true},
  177. {in: "en-t-t0-abcd", lang: "en", ext: "t-t0-abcd"},
  178. // Not necessary to have changed here.
  179. {in: "en-t-nl-abcd", lang: "en", ext: "t-nl", invalid: true},
  180. {in: "en-t-nl-latn", lang: "en", ext: "t-nl-latn"},
  181. {in: "en-t-t0-abcd-x-a", lang: "en", extList: []string{"t-t0-abcd", "x-a"}},
  182. // invalid
  183. {in: "", lang: "und", invalid: true},
  184. {in: "-", lang: "und", invalid: true},
  185. {in: "x", lang: "und", invalid: true},
  186. {in: "x-", lang: "und", invalid: true},
  187. {in: "x--", lang: "und", invalid: true},
  188. {in: "a-a-b-c-d", lang: "und", invalid: true},
  189. {in: "en-", lang: "en", invalid: true},
  190. {in: "enne-", lang: "und", invalid: true},
  191. {in: "en.", lang: "und", invalid: true},
  192. {in: "en.-latn", lang: "und", invalid: true},
  193. {in: "en.-en", lang: "en", invalid: true},
  194. {in: "x-a-tooManyChars-c-d", ext: "x-a-c-d", invalid: true, changed: true},
  195. {in: "a-tooManyChars-c-d", lang: "und", invalid: true},
  196. // TODO: check key-value validity
  197. // { in: "en-u-cu-xd", lang: "en", ext: "u-cu-xd", invalid: true },
  198. {in: "en-t-abcd", lang: "en", invalid: true},
  199. {in: "en-Latn-US-en", lang: "en", script: "Latn", region: "US", invalid: true},
  200. // rewrites (more tests in TestGrandfathered)
  201. {in: "zh-min-nan", lang: "nan"},
  202. {in: "zh-yue", lang: "yue"},
  203. {in: "zh-xiang", lang: "hsn", rewrite: true},
  204. {in: "zh-guoyu", lang: "cmn", rewrite: true},
  205. {in: "iw", lang: "iw"},
  206. {in: "sgn-BE-FR", lang: "sfb", rewrite: true},
  207. {in: "i-klingon", lang: "tlh", rewrite: true},
  208. }
  209. for i, tt := range tests {
  210. tests[i].i = i
  211. if tt.extList != nil {
  212. tests[i].ext = strings.Join(tt.extList, "-")
  213. }
  214. if tt.ext != "" && tt.extList == nil {
  215. tests[i].extList = []string{tt.ext}
  216. }
  217. }
  218. return tests
  219. }
  220. func TestParseExtensions(t *testing.T) {
  221. for i, tt := range parseTests() {
  222. if tt.ext == "" || tt.rewrite {
  223. continue
  224. }
  225. scan := makeScannerString(tt.in)
  226. if len(scan.b) > 1 && scan.b[1] != '-' {
  227. scan.end = nextExtension(string(scan.b), 0)
  228. scan.next = scan.end + 1
  229. scan.scan()
  230. }
  231. start := scan.start
  232. scan.toLower(start, len(scan.b))
  233. parseExtensions(&scan)
  234. ext := string(scan.b[start:])
  235. if ext != tt.ext {
  236. t.Errorf("%d(%s): ext was %v; want %v", i, tt.in, ext, tt.ext)
  237. }
  238. if changed := !strings.HasPrefix(tt.in[start:], ext); changed != tt.changed {
  239. t.Errorf("%d(%s): changed was %v; want %v", i, tt.in, changed, tt.changed)
  240. }
  241. }
  242. }
  243. // partChecks runs checks for each part by calling the function returned by f.
  244. func partChecks(t *testing.T, f func(*testing.T, *parseTest) (Tag, bool)) {
  245. for i, tt := range parseTests() {
  246. t.Run(tt.in, func(t *testing.T) {
  247. tag, skip := f(t, &tt)
  248. if skip {
  249. return
  250. }
  251. if l, _ := getLangID(b(tt.lang)); l != tag.LangID {
  252. t.Errorf("%d: lang was %q; want %q", i, tag.LangID, l)
  253. }
  254. if sc, _ := getScriptID(script, b(tt.script)); sc != tag.ScriptID {
  255. t.Errorf("%d: script was %q; want %q", i, tag.ScriptID, sc)
  256. }
  257. if r, _ := getRegionID(b(tt.region)); r != tag.RegionID {
  258. t.Errorf("%d: region was %q; want %q", i, tag.RegionID, r)
  259. }
  260. if tag.str == "" {
  261. return
  262. }
  263. p := int(tag.pVariant)
  264. if p < int(tag.pExt) {
  265. p++
  266. }
  267. if s, g := tag.str[p:tag.pExt], tt.variants; s != g {
  268. t.Errorf("%d: variants was %q; want %q", i, s, g)
  269. }
  270. p = int(tag.pExt)
  271. if p > 0 && p < len(tag.str) {
  272. p++
  273. }
  274. if s, g := (tag.str)[p:], tt.ext; s != g {
  275. t.Errorf("%d: extensions were %q; want %q", i, s, g)
  276. }
  277. })
  278. }
  279. }
  280. func TestParseTag(t *testing.T) {
  281. partChecks(t, func(t *testing.T, tt *parseTest) (id Tag, skip bool) {
  282. if strings.HasPrefix(tt.in, "x-") || tt.rewrite {
  283. return Tag{}, true
  284. }
  285. scan := makeScannerString(tt.in)
  286. id, end := parseTag(&scan)
  287. id.str = string(scan.b[:end])
  288. tt.ext = ""
  289. tt.extList = []string{}
  290. return id, false
  291. })
  292. }
  293. func TestParse(t *testing.T) {
  294. partChecks(t, func(t *testing.T, tt *parseTest) (id Tag, skip bool) {
  295. id, err := Parse(tt.in)
  296. ext := ""
  297. if id.str != "" {
  298. if strings.HasPrefix(id.str, "x-") {
  299. ext = id.str
  300. } else if int(id.pExt) < len(id.str) && id.pExt > 0 {
  301. ext = id.str[id.pExt+1:]
  302. }
  303. }
  304. if tag, _ := Parse(id.String()); tag.String() != id.String() {
  305. t.Errorf("%d:%s: reparse was %q; want %q", tt.i, tt.in, id.String(), tag.String())
  306. }
  307. if ext != tt.ext {
  308. t.Errorf("%d:%s: ext was %q; want %q", tt.i, tt.in, ext, tt.ext)
  309. }
  310. changed := id.str != "" && !strings.HasPrefix(tt.in, id.str)
  311. if changed != tt.changed {
  312. t.Errorf("%d:%s: changed was %v; want %v", tt.i, tt.in, changed, tt.changed)
  313. }
  314. if (err != nil) != tt.invalid {
  315. t.Errorf("%d:%s: invalid was %v; want %v. Error: %v", tt.i, tt.in, err != nil, tt.invalid, err)
  316. }
  317. return id, false
  318. })
  319. }
  320. func TestErrors(t *testing.T) {
  321. mkInvalid := func(s string) error {
  322. return NewValueError([]byte(s))
  323. }
  324. tests := []struct {
  325. in string
  326. out error
  327. }{
  328. // invalid subtags.
  329. {"ac", mkInvalid("ac")},
  330. {"AC", mkInvalid("ac")},
  331. {"aa-Uuuu", mkInvalid("Uuuu")},
  332. {"aa-AB", mkInvalid("AB")},
  333. // ill-formed wins over invalid.
  334. {"ac-u", ErrSyntax},
  335. {"ac-u-ca", ErrSyntax},
  336. {"ac-u-ca-co-pinyin", ErrSyntax},
  337. {"noob", ErrSyntax},
  338. }
  339. for _, tt := range tests {
  340. _, err := Parse(tt.in)
  341. if err != tt.out {
  342. t.Errorf("%s: was %q; want %q", tt.in, err, tt.out)
  343. }
  344. }
  345. }