-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc_test.go
92 lines (86 loc) · 2.39 KB
/
calc_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
84
85
86
87
88
89
90
91
92
package lr0
import (
"io"
"strings"
"testing"
"github.com/vovan-ve/go-lr0-parser/internal/testutils"
)
func TestNewCalcFunc(t *testing.T) {
t.Run("strings.Repeat", func(t *testing.T) {
h1 := newCalcFunc(strings.Repeat, 2)
v, err := h1([]any{"ab", 3})
if err != nil {
t.Errorf("h1 err is %#v", err)
}
if s, ok := v.(string); !ok {
t.Errorf("h1 v is %#v", v)
} else if s != "ababab" {
t.Errorf("h1 v string is %q", s)
}
})
t.Run("someTestFunc", func(t *testing.T) {
type someTestType struct {
i int
s string
b byte
}
someTestFunc := func(i int, s string, b byte) (*someTestType, error) {
if i < 0 {
return nil, io.EOF
}
return &someTestType{i: i, s: s, b: b}, nil
}
h2 := newCalcFunc(someTestFunc, 3)
v, err := h2([]any{-5, "", byte(7)})
if err != io.EOF {
t.Errorf("h2.1 err is %#v", err)
}
v, err = h2([]any{5, "foo", byte(19)})
if err != nil {
t.Errorf("h2.2 err is %#v", err)
}
expect := someTestType{i: 5, s: "foo", b: 19}
if s, ok := v.(*someTestType); !ok {
t.Errorf("h2.2 v is %#v", v)
} else if s == nil || *s != expect {
t.Errorf("h2.2 v data is %#v", s)
}
})
t.Run("panic: not func", func(t *testing.T) {
defer testutils.ExpectPanicError(t, ErrDefine)
newCalcFunc(42, 0)
})
t.Run("panic: null func", func(t *testing.T) {
defer testutils.ExpectPanicError(t, ErrDefine)
var fn func(any)
newCalcFunc(fn, 1)
})
t.Run("panic: args count", func(t *testing.T) {
defer testutils.ExpectPanicError(t, ErrDefine)
newCalcFunc(func(any, any) {}, 4)
})
t.Run("panic: args count+variadic", func(t *testing.T) {
defer testutils.ExpectPanicError(t, ErrDefine)
newCalcFunc(func(any, any, ...any) {}, 4)
})
t.Run("panic: variadic", func(t *testing.T) {
defer testutils.ExpectPanicError(t, ErrDefine)
newCalcFunc(func(any, any, ...any) {}, 3)
})
t.Run("panic: 2nd result not `error`, got any", func(t *testing.T) {
defer testutils.ExpectPanicError(t, ErrDefine)
newCalcFunc(func(any) (any, any) { return nil, nil }, 1)
})
t.Run("panic: 2nd result not `error`, got moreThenError", func(t *testing.T) {
type moreThenError interface {
error
Foo() int
}
defer testutils.ExpectPanicError(t, ErrDefine)
newCalcFunc(func(any) (any, moreThenError) { return nil, nil }, 1)
})
t.Run("panic: results count", func(t *testing.T) {
defer testutils.ExpectPanicError(t, ErrDefine)
newCalcFunc(func(any) {}, 1)
})
}