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