coverage_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 language
  5. import (
  6. "fmt"
  7. "reflect"
  8. "testing"
  9. "golang.org/x/text/internal/language"
  10. )
  11. func TestSupported(t *testing.T) {
  12. // To prove the results are correct for a type, we test that the number of
  13. // results is identical to the number of results on record, that all results
  14. // are distinct and that all results are valid.
  15. tests := map[string]int{
  16. "BaseLanguages": language.NumLanguages,
  17. "Scripts": language.NumScripts,
  18. "Regions": language.NumRegions,
  19. "Tags": 0,
  20. }
  21. sup := reflect.ValueOf(Supported)
  22. for name, num := range tests {
  23. v := sup.MethodByName(name).Call(nil)[0]
  24. if n := v.Len(); n != num {
  25. t.Errorf("len(%s()) was %d; want %d", name, n, num)
  26. }
  27. dup := make(map[string]bool)
  28. for i := 0; i < v.Len(); i++ {
  29. x := v.Index(i).Interface()
  30. // An invalid value will either cause a crash or result in a
  31. // duplicate when passed to Sprint.
  32. s := fmt.Sprint(x)
  33. if dup[s] {
  34. t.Errorf("%s: duplicate entry %q", name, s)
  35. }
  36. dup[s] = true
  37. }
  38. if len(dup) != v.Len() {
  39. t.Errorf("%s: # unique entries was %d; want %d", name, len(dup), v.Len())
  40. }
  41. }
  42. }
  43. func TestNewCoverage(t *testing.T) {
  44. bases := []Base{Base{0}, Base{3}, Base{7}}
  45. scripts := []Script{Script{11}, Script{17}, Script{23}}
  46. regions := []Region{Region{101}, Region{103}, Region{107}}
  47. tags := []Tag{Make("pt"), Make("en"), Make("en-GB"), Make("en-US"), Make("pt-PT")}
  48. fbases := func() []Base { return bases }
  49. fscripts := func() []Script { return scripts }
  50. fregions := func() []Region { return regions }
  51. ftags := func() []Tag { return tags }
  52. tests := []struct {
  53. desc string
  54. list []interface{}
  55. bases []Base
  56. scripts []Script
  57. regions []Region
  58. tags []Tag
  59. }{
  60. {
  61. desc: "empty",
  62. },
  63. {
  64. desc: "bases",
  65. list: []interface{}{bases},
  66. bases: bases,
  67. },
  68. {
  69. desc: "scripts",
  70. list: []interface{}{scripts},
  71. scripts: scripts,
  72. },
  73. {
  74. desc: "regions",
  75. list: []interface{}{regions},
  76. regions: regions,
  77. },
  78. {
  79. desc: "bases derives from tags",
  80. list: []interface{}{tags},
  81. bases: []Base{Base{_en}, Base{_pt}},
  82. tags: tags,
  83. },
  84. {
  85. desc: "tags and bases",
  86. list: []interface{}{tags, bases},
  87. bases: bases,
  88. tags: tags,
  89. },
  90. {
  91. desc: "fully specified",
  92. list: []interface{}{tags, bases, scripts, regions},
  93. bases: bases,
  94. scripts: scripts,
  95. regions: regions,
  96. tags: tags,
  97. },
  98. {
  99. desc: "bases func",
  100. list: []interface{}{fbases},
  101. bases: bases,
  102. },
  103. {
  104. desc: "scripts func",
  105. list: []interface{}{fscripts},
  106. scripts: scripts,
  107. },
  108. {
  109. desc: "regions func",
  110. list: []interface{}{fregions},
  111. regions: regions,
  112. },
  113. {
  114. desc: "tags func",
  115. list: []interface{}{ftags},
  116. bases: []Base{Base{_en}, Base{_pt}},
  117. tags: tags,
  118. },
  119. {
  120. desc: "tags and bases",
  121. list: []interface{}{ftags, fbases},
  122. bases: bases,
  123. tags: tags,
  124. },
  125. {
  126. desc: "fully specified",
  127. list: []interface{}{ftags, fbases, fscripts, fregions},
  128. bases: bases,
  129. scripts: scripts,
  130. regions: regions,
  131. tags: tags,
  132. },
  133. }
  134. for i, tt := range tests {
  135. l := NewCoverage(tt.list...)
  136. if a := l.BaseLanguages(); !reflect.DeepEqual(a, tt.bases) {
  137. t.Errorf("%d:%s: BaseLanguages was %v; want %v", i, tt.desc, a, tt.bases)
  138. }
  139. if a := l.Scripts(); !reflect.DeepEqual(a, tt.scripts) {
  140. t.Errorf("%d:%s: Scripts was %v; want %v", i, tt.desc, a, tt.scripts)
  141. }
  142. if a := l.Regions(); !reflect.DeepEqual(a, tt.regions) {
  143. t.Errorf("%d:%s: Regions was %v; want %v", i, tt.desc, a, tt.regions)
  144. }
  145. if a := l.Tags(); !reflect.DeepEqual(a, tt.tags) {
  146. t.Errorf("%d:%s: Tags was %v; want %v", i, tt.desc, a, tt.tags)
  147. }
  148. }
  149. }