-
Notifications
You must be signed in to change notification settings - Fork 3
/
const_test.go
128 lines (102 loc) · 2.57 KB
/
const_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
120
121
122
123
124
125
126
127
128
package humanize
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
var cr = `
package test
const (
i = iota
j
)
const s float64 = 10
const i1 = 10
const i2 = 10.6
const i3 = "test"
const i4 = 'C'
const i6 = 10i
`
var otherC = `
package test2
const i7 = 10i
const i6 = 1000
`
func TestConstant(t *testing.T) {
Convey("constant test", t, func() {
f, err := ParseFile(cr, &Package{})
So(err, ShouldBeNil)
var p = &Package{
Name: "test",
}
p.Files = append(p.Files, f)
So(p.Bind(), ShouldBeNil)
Convey("Normal define", func() {
i, err := p.FindConstant("i")
So(err, ShouldBeNil)
So(i.Name, ShouldEqual, "i")
So(i.Type.(*IdentType).Ident, ShouldEqual, "iota")
i, err = p.FindConstant("j")
So(err, ShouldBeNil)
So(i.Name, ShouldEqual, "j")
So(i.Type.(*IdentType).Ident, ShouldEqual, "iota")
})
Convey("by value s", func() {
i, err := p.FindConstant("s")
So(err, ShouldBeNil)
So(i.Name, ShouldEqual, "s")
So(i.Type.(*IdentType).Ident, ShouldEqual, "float64")
So(i.String(), ShouldEqual, "s float64 = 10")
})
Convey("by value i1", func() {
i, err := p.FindConstant("i1")
So(err, ShouldBeNil)
So(i.Name, ShouldEqual, "i1")
So(i.Type.(*IdentType).Ident, ShouldEqual, "int")
})
Convey("by value i2", func() {
i, err := p.FindConstant("i2")
So(err, ShouldBeNil)
So(i.Name, ShouldEqual, "i2")
So(i.Type.(*IdentType).Ident, ShouldEqual, "float64")
})
Convey("by value i3", func() {
i, err := p.FindConstant("i3")
So(err, ShouldBeNil)
So(i.Name, ShouldEqual, "i3")
So(i.Type.(*IdentType).Ident, ShouldEqual, "string")
})
Convey("by value i4", func() {
i, err := p.FindConstant("i4")
So(err, ShouldBeNil)
So(i.Name, ShouldEqual, "i4")
So(i.Type.(*IdentType).Ident, ShouldEqual, "char")
})
Convey("by value i6", func() {
i, err := p.FindConstant("i6")
So(err, ShouldBeNil)
So(i.Name, ShouldEqual, "i6")
So(i.Type.(*IdentType).Ident, ShouldEqual, "complex64")
Convey("equality", func() {
So(i.Equal(i), ShouldBeTrue)
i2, err := p.FindConstant("i4")
So(err, ShouldBeNil)
So(i.Equal(i2), ShouldBeFalse)
})
Convey("other package", func() {
f, err := ParseFile(otherC, &Package{})
So(err, ShouldBeNil)
var p2 = &Package{
Name: "test2",
}
p2.Files = append(p2.Files, f)
So(p2.Bind(), ShouldBeNil)
i3, err := p2.FindConstant("i6")
So(err, ShouldBeNil)
So(i3.Equal(i), ShouldBeFalse)
i4, err := p2.FindConstant("i7")
So(err, ShouldBeNil)
So(i4.Equal(i), ShouldBeTrue)
})
})
})
}