-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_checker_test.go
95 lines (76 loc) · 1.49 KB
/
list_checker_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
93
94
95
package overloading
import (
"fmt"
"testing"
)
func TestChecker(t *testing.T) {
t.Parallel()
// Expect (int, int)
c := NewListChecker(
NewListRule(
NewArgument("int"),
NewArgument("int")))
f := func(args ...interface{}) int {
out := c.Check(args)
a := out[0].(int)
b := out[1].(int)
return a + b
}
if f(3, 2) != 5 {
t.Error("Wrong checker")
}
}
func TestCheckerViolation(t *testing.T) {
t.Parallel()
defer func() {
if r := recover(); r == nil {
t.Error("The code did not panic")
}
}()
// Expect (int, int)
c := NewListChecker(
NewListRule(
NewArgument("int"),
NewArgument("int")))
f := func(args ...interface{}) int {
out := c.Check(args)
a := out[0].(int)
b := out[1].(int)
return a + b
}
_ = f(2, 3, 4)
}
func TestMultiRuleChecker(t *testing.T) {
t.Parallel()
// Expect (int, int) or (float64, float64)
c := NewListChecker(
NewListRule(
NewArgument("int"),
NewArgument("int")),
NewListRule(
NewArgument("float64"),
NewArgument("float64")))
f := func(args ...interface{}) interface{} {
out := c.Check(args)
switch out[0].(type) {
case int:
na := out[0].(int)
nb := out[1].(int)
return na + nb
case float64:
na := out[0].(float64)
nb := out[1].(float64)
return na + nb
default:
panic("Unknown type")
}
}
if fmt.Sprint(f(3, 2)) != "5" {
t.Log(fmt.Sprint(f(3, 2)))
t.Error("Wrong checker")
}
if fmt.Sprint(f(3.0, 2.0)) != "5" {
t.Log(fmt.Sprint(f(3.0, 2.0)))
t.Error("Wrong checker")
}
}