1 package arrayiter_test
2
3 import (
4 "context"
5 "fmt"
6 "reflect"
7 "testing"
8 "time"
9
10 "github.com/lestrrat-go/iter/arrayiter"
11 "github.com/stretchr/testify/assert"
12 )
13
14 func TestIterator(t *testing.T) {
15 chSize := 2
16
17 ch := make(chan *arrayiter.Pair, chSize)
18 ch <- &arrayiter.Pair{Index: 1, Value: 2}
19 ch <- &arrayiter.Pair{Index: 2, Value: 4}
20 close(ch)
21
22 i := arrayiter.New(ch)
23
24 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
25 defer cancel()
26
27 var loopCount int
28 for i.Next(ctx) {
29 loopCount++
30 p := i.Pair()
31 if !assert.Equal(t, p.Value, 2*loopCount, "expected values to match") {
32 return
33 }
34 }
35
36 if !assert.Equal(t, chSize, loopCount, "expected to loop for %d times", chSize) {
37 return
38 }
39 }
40
41 type ArrayLike struct {
42 Values []string
43 }
44
45 func (m *ArrayLike) Iterate(ctx context.Context) arrayiter.Iterator {
46 ch := make(chan *arrayiter.Pair)
47 go m.iterate(ctx, ch)
48 return arrayiter.New(ch)
49 }
50
51 func (m *ArrayLike) iterate(ctx context.Context, ch chan *arrayiter.Pair) {
52 defer close(ch)
53 for k, v := range m.Values {
54 ch <- &arrayiter.Pair{Index: k, Value: v}
55 }
56 }
57
58 func TestAsArray(t *testing.T) {
59 t.Run("slice", func(t *testing.T) {
60 inputs := []interface{}{
61 []string{
62 "foo",
63 "bar",
64 "baz",
65 },
66 }
67 for _, x := range inputs {
68 input := x
69 t.Run(fmt.Sprintf("%T", input), func(t *testing.T) {
70 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
71 defer cancel()
72 dst := reflect.New(reflect.TypeOf(input))
73 if !assert.NoError(t, arrayiter.AsArray(ctx, input, dst.Interface()), `arrayiter.AsArray should succeed`) {
74 return
75 }
76 if !assert.Equal(t, input, dst.Elem().Interface(), `slices should be the same`) {
77 return
78 }
79 })
80 }
81 })
82 t.Run("Array-like object", func(t *testing.T) {
83 src := &ArrayLike{
84 Values: []string{
85 "one",
86 "two",
87 "three",
88 "four",
89 "five",
90 },
91 }
92
93 t.Run("dst is nil (slice)", func(t *testing.T) {
94 var m []string
95 if !assert.NoError(t, arrayiter.AsArray(context.Background(), src, &m), `AsArray against uninitialized array should succeed`) {
96 return
97 }
98
99 if !assert.Equal(t, src.Values, m, "slices should match") {
100 return
101 }
102 })
103 t.Run("dst is nil (array)", func(t *testing.T) {
104 var m [5]string
105 if !assert.NoError(t, arrayiter.AsArray(context.Background(), src, &m), `AsArray against uninitialized array should succeed`) {
106 return
107 }
108
109 var expected [5]string
110 for i, v := range src.Values {
111 expected[i] = v
112 }
113
114 if !assert.Equal(t, expected, m, "arrays should match") {
115 return
116 }
117 })
118 t.Run("dst is not nil", func(t *testing.T) {
119 m := make([]string, len(src.Values))
120 if !assert.NoError(t, arrayiter.AsArray(context.Background(), src, &m), `AsArray against nil map should succeed`) {
121 return
122 }
123
124 if !assert.Equal(t, src.Values, m, "maps should match") {
125 return
126 }
127 })
128 })
129 }
130
View as plain text