lookup_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. "testing"
  7. )
  8. func TestRegionID(t *testing.T) {
  9. tests := []struct {
  10. in, out string
  11. }{
  12. {"_ ", ""},
  13. {"_000", ""},
  14. {"419", "419"},
  15. {"AA", "AA"},
  16. {"ATF", "TF"},
  17. {"HV", "HV"},
  18. {"CT", "CT"},
  19. {"DY", "DY"},
  20. {"IC", "IC"},
  21. {"FQ", "FQ"},
  22. {"JT", "JT"},
  23. {"ZZ", "ZZ"},
  24. {"EU", "EU"},
  25. {"QO", "QO"},
  26. {"FX", "FX"},
  27. }
  28. for i, tt := range tests {
  29. if tt.in[0] == '_' {
  30. id := tt.in[1:]
  31. if _, err := ParseRegion(id); err == nil {
  32. t.Errorf("%d:err(%s): found nil; want error", i, id)
  33. }
  34. continue
  35. }
  36. want, _ := ParseRegion(tt.in)
  37. if s := want.String(); s != tt.out {
  38. t.Errorf("%d:%s: found %q; want %q", i, tt.in, s, tt.out)
  39. }
  40. if len(tt.in) == 2 {
  41. want, _ := ParseRegion(tt.in)
  42. if s := want.String(); s != tt.out {
  43. t.Errorf("%d:getISO2(%s): found %q; want %q", i, tt.in, s, tt.out)
  44. }
  45. }
  46. }
  47. }
  48. func TestRegionISO3(t *testing.T) {
  49. tests := []struct {
  50. from, iso3, to string
  51. }{
  52. {" ", "ZZZ", "ZZ"},
  53. {"000", "ZZZ", "ZZ"},
  54. {"AA", "AAA", ""},
  55. {"CT", "CTE", ""},
  56. {"DY", "DHY", ""},
  57. {"EU", "QUU", ""},
  58. {"HV", "HVO", ""},
  59. {"IC", "ZZZ", "ZZ"},
  60. {"JT", "JTN", ""},
  61. {"PZ", "PCZ", ""},
  62. {"QU", "QUU", "EU"},
  63. {"QO", "QOO", ""},
  64. {"YD", "YMD", ""},
  65. {"FQ", "ATF", "TF"},
  66. {"TF", "ATF", ""},
  67. {"FX", "FXX", ""},
  68. {"ZZ", "ZZZ", ""},
  69. {"419", "ZZZ", "ZZ"},
  70. }
  71. for _, tt := range tests {
  72. r, _ := ParseRegion(tt.from)
  73. if s := r.ISO3(); s != tt.iso3 {
  74. t.Errorf("iso3(%q): found %q; want %q", tt.from, s, tt.iso3)
  75. }
  76. if tt.iso3 == "" {
  77. continue
  78. }
  79. want := tt.to
  80. if tt.to == "" {
  81. want = tt.from
  82. }
  83. r, _ = ParseRegion(want)
  84. if id, _ := ParseRegion(tt.iso3); id != r {
  85. t.Errorf("%s: found %q; want %q", tt.iso3, id, want)
  86. }
  87. }
  88. }
  89. func TestRegionM49(t *testing.T) {
  90. fromTests := []struct {
  91. m49 int
  92. id string
  93. }{
  94. {0, ""},
  95. {-1, ""},
  96. {1000, ""},
  97. {10000, ""},
  98. {001, "001"},
  99. {104, "MM"},
  100. {180, "CD"},
  101. {230, "ET"},
  102. {231, "ET"},
  103. {249, "FX"},
  104. {250, "FR"},
  105. {276, "DE"},
  106. {278, "DD"},
  107. {280, "DE"},
  108. {419, "419"},
  109. {626, "TL"},
  110. {736, "SD"},
  111. {840, "US"},
  112. {854, "BF"},
  113. {891, "CS"},
  114. {899, ""},
  115. {958, "AA"},
  116. {966, "QT"},
  117. {967, "EU"},
  118. {999, "ZZ"},
  119. }
  120. for _, tt := range fromTests {
  121. id, err := EncodeM49(tt.m49)
  122. if want, have := err != nil, tt.id == ""; want != have {
  123. t.Errorf("error(%d): have %v; want %v", tt.m49, have, want)
  124. continue
  125. }
  126. r, _ := ParseRegion(tt.id)
  127. if r != id {
  128. t.Errorf("region(%d): have %s; want %s", tt.m49, id, r)
  129. }
  130. }
  131. toTests := []struct {
  132. m49 int
  133. id string
  134. }{
  135. {0, "000"},
  136. {0, "IC"}, // Some codes don't have an ID
  137. {001, "001"},
  138. {104, "MM"},
  139. {104, "BU"},
  140. {180, "CD"},
  141. {180, "ZR"},
  142. {231, "ET"},
  143. {250, "FR"},
  144. {249, "FX"},
  145. {276, "DE"},
  146. {278, "DD"},
  147. {419, "419"},
  148. {626, "TL"},
  149. {626, "TP"},
  150. {729, "SD"},
  151. {826, "GB"},
  152. {840, "US"},
  153. {854, "BF"},
  154. {891, "YU"},
  155. {891, "CS"},
  156. {958, "AA"},
  157. {966, "QT"},
  158. {967, "EU"},
  159. {967, "QU"},
  160. {999, "ZZ"},
  161. // For codes that don't have an M49 code use the replacement value,
  162. // if available.
  163. {854, "HV"}, // maps to Burkino Faso
  164. }
  165. for _, tt := range toTests {
  166. r, _ := ParseRegion(tt.id)
  167. if r.M49() != tt.m49 {
  168. t.Errorf("m49(%q): have %d; want %d", tt.id, r.M49(), tt.m49)
  169. }
  170. }
  171. }
  172. func TestRegionDeprecation(t *testing.T) {
  173. tests := []struct{ in, out string }{
  174. {"BU", "MM"},
  175. {"BUR", "MM"},
  176. {"CT", "KI"},
  177. {"DD", "DE"},
  178. {"DDR", "DE"},
  179. {"DY", "BJ"},
  180. {"FX", "FR"},
  181. {"HV", "BF"},
  182. {"JT", "UM"},
  183. {"MI", "UM"},
  184. {"NH", "VU"},
  185. {"NQ", "AQ"},
  186. {"PU", "UM"},
  187. {"PZ", "PA"},
  188. {"QU", "EU"},
  189. {"RH", "ZW"},
  190. {"TP", "TL"},
  191. {"UK", "GB"},
  192. {"VD", "VN"},
  193. {"WK", "UM"},
  194. {"YD", "YE"},
  195. {"NL", "NL"},
  196. }
  197. for _, tt := range tests {
  198. rIn, _ := ParseRegion(tt.in)
  199. rOut, _ := ParseRegion(tt.out)
  200. r := rIn.Canonicalize()
  201. if rOut == rIn && r.String() == "ZZ" {
  202. t.Errorf("%s: was %q; want %q", tt.in, r, tt.in)
  203. }
  204. if rOut != rIn && r != rOut {
  205. t.Errorf("%s: was %q; want %q", tt.in, r, tt.out)
  206. }
  207. }
  208. }
  209. func TestIsPrivateUse(t *testing.T) {
  210. type test struct {
  211. s string
  212. private bool
  213. }
  214. tests := []test{
  215. {"en", false},
  216. {"und", false},
  217. {"pzn", false},
  218. {"qaa", true},
  219. {"qtz", true},
  220. {"qua", false},
  221. }
  222. for i, tt := range tests {
  223. x, _ := ParseBase(tt.s)
  224. if b := x.IsPrivateUse(); b != tt.private {
  225. t.Errorf("%d: langID.IsPrivateUse(%s) was %v; want %v", i, tt.s, b, tt.private)
  226. }
  227. }
  228. tests = []test{
  229. {"001", false},
  230. {"419", false},
  231. {"899", false},
  232. {"900", false},
  233. {"957", false},
  234. {"958", true},
  235. {"AA", true},
  236. {"AC", false},
  237. {"EU", false}, // CLDR grouping, exceptionally reserved in ISO.
  238. {"QU", true}, // Canonicalizes to EU, User-assigned in ISO.
  239. {"QO", true}, // CLDR grouping, User-assigned in ISO.
  240. {"QA", false},
  241. {"QM", true},
  242. {"QZ", true},
  243. {"XA", true},
  244. {"XK", true}, // Assigned to Kosovo in CLDR, User-assigned in ISO.
  245. {"XZ", true},
  246. {"ZW", false},
  247. {"ZZ", true},
  248. }
  249. for i, tt := range tests {
  250. x, _ := ParseRegion(tt.s)
  251. if b := x.IsPrivateUse(); b != tt.private {
  252. t.Errorf("%d: regionID.IsPrivateUse(%s) was %v; want %v", i, tt.s, b, tt.private)
  253. }
  254. }
  255. tests = []test{
  256. {"Latn", false},
  257. {"Laaa", false}, // invalid
  258. {"Qaaa", true},
  259. {"Qabx", true},
  260. {"Qaby", false},
  261. {"Zyyy", false},
  262. {"Zzzz", false},
  263. }
  264. for i, tt := range tests {
  265. x, _ := ParseScript(tt.s)
  266. if b := x.IsPrivateUse(); b != tt.private {
  267. t.Errorf("%d: scriptID.IsPrivateUse(%s) was %v; want %v", i, tt.s, b, tt.private)
  268. }
  269. }
  270. }