Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

176 rindas
4.0 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 cldr
  5. import (
  6. "reflect"
  7. "testing"
  8. )
  9. type testSlice []*Common
  10. func mkElem(alt, typ, ref string) *Common {
  11. return &Common{
  12. Type: typ,
  13. Reference: ref,
  14. Alt: alt,
  15. }
  16. }
  17. var (
  18. testSlice1 = testSlice{
  19. mkElem("1", "a", "i.a"),
  20. mkElem("1", "b", "i.b"),
  21. mkElem("1", "c", "i.c"),
  22. mkElem("2", "b", "ii"),
  23. mkElem("3", "c", "iii"),
  24. mkElem("4", "a", "iv.a"),
  25. mkElem("4", "d", "iv.d"),
  26. }
  27. testSliceE = testSlice{}
  28. )
  29. func panics(f func()) (panics bool) {
  30. defer func() {
  31. if err := recover(); err != nil {
  32. panics = true
  33. }
  34. }()
  35. f()
  36. return panics
  37. }
  38. func TestMakeSlice(t *testing.T) {
  39. foo := 1
  40. bar := []int{}
  41. tests := []struct {
  42. i interface{}
  43. panics bool
  44. err string
  45. }{
  46. {&foo, true, "should panic when passed a pointer to the wrong type"},
  47. {&bar, true, "should panic when slice element of the wrong type"},
  48. {testSlice1, true, "should panic when passed a slice"},
  49. {&testSlice1, false, "should not panic"},
  50. }
  51. for i, tt := range tests {
  52. if panics(func() { MakeSlice(tt.i) }) != tt.panics {
  53. t.Errorf("%d: %s", i, tt.err)
  54. }
  55. }
  56. }
  57. var anyOfTests = []struct {
  58. sl testSlice
  59. values []string
  60. n int
  61. }{
  62. {testSliceE, []string{}, 0},
  63. {testSliceE, []string{"1", "2", "3"}, 0},
  64. {testSlice1, []string{}, 0},
  65. {testSlice1, []string{"1"}, 3},
  66. {testSlice1, []string{"2"}, 1},
  67. {testSlice1, []string{"5"}, 0},
  68. {testSlice1, []string{"1", "2", "3"}, 5},
  69. }
  70. func TestSelectAnyOf(t *testing.T) {
  71. for i, tt := range anyOfTests {
  72. sl := tt.sl
  73. s := MakeSlice(&sl)
  74. s.SelectAnyOf("alt", tt.values...)
  75. if len(sl) != tt.n {
  76. t.Errorf("%d: found len == %d; want %d", i, len(sl), tt.n)
  77. }
  78. }
  79. sl := testSlice1
  80. s := MakeSlice(&sl)
  81. if !panics(func() { s.SelectAnyOf("foo") }) {
  82. t.Errorf("should panic on non-existing attribute")
  83. }
  84. }
  85. func TestFilter(t *testing.T) {
  86. for i, tt := range anyOfTests {
  87. sl := tt.sl
  88. s := MakeSlice(&sl)
  89. s.Filter(func(e Elem) bool {
  90. v, _ := findField(reflect.ValueOf(e), "alt")
  91. return in(tt.values, v.String())
  92. })
  93. if len(sl) != tt.n {
  94. t.Errorf("%d: found len == %d; want %d", i, len(sl), tt.n)
  95. }
  96. }
  97. }
  98. func TestGroup(t *testing.T) {
  99. f := func(excl ...string) func(Elem) string {
  100. return func(e Elem) string {
  101. return Key(e, excl...)
  102. }
  103. }
  104. tests := []struct {
  105. sl testSlice
  106. f func(Elem) string
  107. lens []int
  108. }{
  109. {testSliceE, f(), []int{}},
  110. {testSlice1, f(), []int{1, 1, 1, 1, 1, 1, 1}},
  111. {testSlice1, f("type"), []int{3, 1, 1, 2}},
  112. {testSlice1, f("alt"), []int{2, 2, 2, 1}},
  113. {testSlice1, f("alt", "type"), []int{7}},
  114. {testSlice1, f("alt", "type"), []int{7}},
  115. }
  116. for i, tt := range tests {
  117. sl := tt.sl
  118. s := MakeSlice(&sl)
  119. g := s.Group(tt.f)
  120. if len(tt.lens) != len(g) {
  121. t.Errorf("%d: found %d; want %d", i, len(g), len(tt.lens))
  122. continue
  123. }
  124. for j, v := range tt.lens {
  125. if n := g[j].Value().Len(); n != v {
  126. t.Errorf("%d: found %d for length of group %d; want %d", i, n, j, v)
  127. }
  128. }
  129. }
  130. }
  131. func TestSelectOnePerGroup(t *testing.T) {
  132. tests := []struct {
  133. sl testSlice
  134. attr string
  135. values []string
  136. refs []string
  137. }{
  138. {testSliceE, "alt", []string{"1"}, []string{}},
  139. {testSliceE, "type", []string{"a"}, []string{}},
  140. {testSlice1, "alt", []string{"2", "3", "1"}, []string{"i.a", "ii", "iii"}},
  141. {testSlice1, "alt", []string{"1", "4"}, []string{"i.a", "i.b", "i.c", "iv.d"}},
  142. {testSlice1, "type", []string{"c", "d"}, []string{"i.c", "iii", "iv.d"}},
  143. }
  144. for i, tt := range tests {
  145. sl := tt.sl
  146. s := MakeSlice(&sl)
  147. s.SelectOnePerGroup(tt.attr, tt.values)
  148. if len(sl) != len(tt.refs) {
  149. t.Errorf("%d: found result length %d; want %d", i, len(sl), len(tt.refs))
  150. continue
  151. }
  152. for j, e := range sl {
  153. if tt.refs[j] != e.Reference {
  154. t.Errorf("%d:%d found %s; want %s", i, j, e.Reference, tt.refs[i])
  155. }
  156. }
  157. }
  158. sl := testSlice1
  159. s := MakeSlice(&sl)
  160. if !panics(func() { s.SelectOnePerGroup("foo", nil) }) {
  161. t.Errorf("should panic on non-existing attribute")
  162. }
  163. }