-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformer_test.go
83 lines (71 loc) · 2.33 KB
/
transformer_test.go
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
package event
import (
"errors"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewTransformer(t *testing.T) {
var (
transformer Transformer
transformed interface{}
useResult bool
err error
)
transformer = NewTransformer(func(i int) float64 {
return float64(i)
})
assert.Equal(t, reflect.TypeOf(int(666)), transformer.SourceEventType())
assert.Equal(t, reflect.TypeOf(float64(666)), transformer.ResultEventType())
assert.False(t, transformer.IsFilter())
transformed, useResult, err = transformer.TransformEvent(int(666))
assert.NoError(t, err)
assert.True(t, useResult)
assert.Exactly(t, float64(666), transformed)
transformer = NewTransformer(func(i int) (float64, bool) {
return float64(i), true
})
assert.Equal(t, reflect.TypeOf(int(666)), transformer.SourceEventType())
assert.Equal(t, reflect.TypeOf(float64(666)), transformer.ResultEventType())
assert.True(t, transformer.IsFilter())
transformed, useResult, err = transformer.TransformEvent(int(666))
assert.NoError(t, err)
assert.True(t, useResult)
assert.Exactly(t, float64(666), transformed)
transformer = NewTransformer(func(i int) (float64, bool, error) {
return 0, false, nil
})
transformed, useResult, err = transformer.TransformEvent(int(666))
assert.NoError(t, err)
assert.False(t, useResult)
invalidTransformerFuncs := []interface{}{
"not a function",
func() {},
func() (i int) { return },
func() (b bool) { return },
func(int, int) (i int) { return },
func(int, int) (b bool) { return },
func(int) {},
func(int) (r0, r1 int) { return },
func(int) (r0, r1 int, b bool) { return },
func(int) (r0 int, r1 error, r2 bool) { return },
}
for _, transformerFunc := range invalidTransformerFuncs {
assert.Panics(t, func() {
NewTransformer(transformerFunc)
})
}
transformer = NewTransformer(func(i int) (float64, bool, error) {
return 0, false, errors.New("resultError")
})
transformed, useResult, err = transformer.TransformEvent(int(666))
assert.EqualError(t, err, "resultError")
assert.False(t, useResult)
transformer = NewTransformer(func(i int) (int, bool, error) {
return -1, true, errors.New("resultError")
})
transformed, useResult, err = transformer.TransformEvent(int(666))
assert.EqualError(t, err, "resultError")
assert.True(t, useResult)
assert.Exactly(t, int(-1), transformed)
}