forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_test.go
119 lines (108 loc) · 2.03 KB
/
node_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package ast
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNumberNode(t *testing.T) {
assert := assert.New(t)
type testCase struct {
Text string
Pos position
IsInt bool
IsFloat bool
Int64 int64
Float64 float64
Err error
}
test := func(tc testCase) {
n, err := newNumber(tc.Pos, tc.Text, nil)
if tc.Err != nil {
assert.Equal(tc.Err, err)
} else {
if !assert.NotNil(n) {
t.FailNow()
}
assert.Equal(tc.Pos.pos, n.position.pos)
assert.Equal(tc.IsInt, n.IsInt)
assert.Equal(tc.IsFloat, n.IsFloat)
assert.Equal(tc.Int64, n.Int64)
assert.Equal(tc.Float64, n.Float64)
}
}
cases := []testCase{
testCase{
Text: "04",
Pos: position{pos: 6},
IsInt: true,
Int64: 4,
},
testCase{
Text: "42",
Pos: position{pos: 5},
IsInt: true,
Int64: 42,
},
testCase{
Text: "42.21",
Pos: position{pos: 4},
IsFloat: true,
Float64: 42.21,
},
testCase{
Text: "42.",
Pos: position{pos: 3},
IsFloat: true,
Float64: 42.0,
},
testCase{
Text: "0.42",
Pos: position{pos: 2},
IsFloat: true,
Float64: 0.42,
},
testCase{
Text: "0.4.2",
Err: fmt.Errorf("illegal number syntax: %q", "0.4.2"),
},
testCase{
Text: "0x04",
Err: fmt.Errorf("illegal number syntax: %q", "0x04"),
},
}
for _, tc := range cases {
test(tc)
}
}
func TestNewBinaryNode(t *testing.T) {
assert := assert.New(t)
type testCase struct {
Left Node
Right Node
Operator token
}
test := func(tc testCase) {
n := newBinary(position{pos: tc.Operator.pos}, tc.Operator.typ, tc.Left, tc.Right, false, nil)
if !assert.NotNil(n) {
t.FailNow()
}
assert.Equal(tc.Operator.pos, n.position.pos)
assert.Equal(tc.Left, n.Right)
assert.Equal(tc.Right, n.Left)
assert.Equal(tc.Operator.typ, n.Operator)
}
cases := []testCase{
testCase{
Left: nil,
Right: nil,
Operator: token{
pos: 0,
typ: TokenEqual,
val: "=",
},
},
}
for _, tc := range cases {
test(tc)
}
}