colltab_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package colltab
  2. import (
  3. "testing"
  4. "golang.org/x/text/language"
  5. )
  6. func TestMatchLang(t *testing.T) {
  7. tags := []language.Tag{
  8. 0: language.Und,
  9. 1: language.MustParse("bs"),
  10. 2: language.German,
  11. 3: language.English,
  12. 4: language.AmericanEnglish,
  13. 5: language.MustParse("en-US-u-va-posix"),
  14. 6: language.Portuguese,
  15. 7: language.Serbian,
  16. 8: language.MustParse("sr-Latn"),
  17. 9: language.Chinese,
  18. 10: language.MustParse("zh-u-co-stroke"),
  19. 11: language.MustParse("zh-Hant-u-co-pinyin"),
  20. 12: language.TraditionalChinese,
  21. }
  22. for i, tc := range []struct {
  23. x int
  24. t language.Tag
  25. }{
  26. {0, language.Und},
  27. {0, language.Persian}, // Default to first element when no match.
  28. {3, language.English},
  29. {4, language.AmericanEnglish},
  30. {5, language.MustParse("en-US-u-va-posix")}, // Ext. variant match.
  31. {4, language.MustParse("en-US-u-va-noposix")}, // Ext. variant mismatch.
  32. {3, language.MustParse("en-UK-u-va-noposix")}, // Ext. variant mismatch.
  33. {7, language.Serbian},
  34. {0, language.Croatian}, // Don't match to close language!
  35. {0, language.MustParse("gsw")}, // Don't match to close language!
  36. {1, language.MustParse("bs-Cyrl")}, // Odd, but correct.
  37. {1, language.MustParse("bs-Latn")}, // Estimated script drops.
  38. {8, language.MustParse("sr-Latn")},
  39. {9, language.Chinese},
  40. {9, language.SimplifiedChinese},
  41. {12, language.TraditionalChinese},
  42. {11, language.MustParse("zh-Hant-u-co-pinyin")},
  43. // TODO: should this be 12? Either inherited value (10) or default is
  44. // fine in this case, though. Other locales are not affected.
  45. {10, language.MustParse("zh-Hant-u-co-stroke")},
  46. // There is no "phonebk" sorting order for zh-Hant, so use default.
  47. {12, language.MustParse("zh-Hant-u-co-phonebk")},
  48. {10, language.MustParse("zh-u-co-stroke")},
  49. {12, language.MustParse("und-TW")}, // Infer script and language.
  50. {12, language.MustParse("und-HK")}, // Infer script and language.
  51. {6, language.MustParse("und-BR")}, // Infer script and language.
  52. {6, language.MustParse("und-PT")}, // Infer script and language.
  53. {2, language.MustParse("und-Latn-DE")}, // Infer language.
  54. {0, language.MustParse("und-Jpan-BR")}, // Infers "ja", so no match.
  55. {0, language.MustParse("zu")}, // No match past index.
  56. } {
  57. t.Run(tc.t.String(), func(t *testing.T) {
  58. if x := MatchLang(tc.t, tags); x != tc.x {
  59. t.Errorf("%d: MatchLang(%q, tags) = %d; want %d", i, tc.t, x, tc.x)
  60. }
  61. })
  62. }
  63. }