Skip to content

Commit d36da3b

Browse files
committed
update test
1 parent 2a3f188 commit d36da3b

File tree

8 files changed

+219
-77
lines changed

8 files changed

+219
-77
lines changed

check_test.go

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
11
package gotype
22

3-
import "testing"
3+
import (
4+
"testing"
5+
)
46

57
func TestImplements(t *testing.T) {
6-
scopeTime := Import(t, "time")
78

8-
scopeEncodin := Import(t, "encoding")
9+
const src = `
10+
package a
11+
12+
import "time"
13+
var now = time.Now()
14+
`
15+
scope := Parse(t, src)
16+
17+
typ, ok := scope.ChildByName("now")
18+
if !ok {
19+
t.Fail()
20+
}
921

22+
scopeTime := Import(t, "time")
1023
val, ok := scopeTime.ChildByName("Time")
1124
if !ok {
1225
t.Fail()
1326
return
1427
}
1528

29+
if !Identical(typ.Declaration().Declaration(), val) {
30+
t.Fail()
31+
}
32+
33+
scopeEncodin := Import(t, "encoding")
1634
inter, ok := scopeEncodin.ChildByName("TextMarshaler")
1735
if !ok {
1836
t.Fail()
@@ -23,3 +41,56 @@ func TestImplements(t *testing.T) {
2341
t.Fail()
2442
}
2543
}
44+
45+
func TestIdentical(t *testing.T) {
46+
var src = `package a
47+
import time "time"
48+
49+
type A struct {
50+
String string
51+
Int int
52+
Array [8]byte
53+
Channel chan int
54+
Interface interface{}
55+
Map map[string]string
56+
Time time.Time
57+
}
58+
type B struct {
59+
String string
60+
Int int
61+
Array [8]byte
62+
Channel chan int
63+
Interface interface{}
64+
Map map[string]string
65+
Time time.Time
66+
}
67+
`
68+
69+
scopeSrc := Parse(t, src)
70+
a, _ := scopeSrc.ChildByName("A")
71+
b, _ := scopeSrc.ChildByName("B")
72+
if !Identical(a, b) {
73+
t.Fail()
74+
return
75+
}
76+
}
77+
78+
func TestEqual(t *testing.T) {
79+
scopeTime := Import(t, "time")
80+
81+
val, ok := scopeTime.ChildByName("Time")
82+
if !ok {
83+
t.Fail()
84+
return
85+
}
86+
87+
now, ok := scopeTime.ChildByName("Now")
88+
if !ok {
89+
t.Fail()
90+
return
91+
}
92+
93+
if !Equal(val, now.Declaration().Out(0).Declaration()) {
94+
t.Fail()
95+
}
96+
}

import_test.go

Lines changed: 0 additions & 44 deletions
This file was deleted.

other_test.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestOhter(t *testing.T) {
1313
var testpath = []string{
1414
"github.com/wzshiming/gotype/testdata/value",
1515
"github.com/wzshiming/gotype/testdata/kind",
16-
"github.com/wzshiming/gotype/testdata/struct",
16+
"github.com/wzshiming/gotype/testdata/type",
1717
}
1818
for _, src := range testpath {
1919
testAll(t, src)
@@ -136,61 +136,67 @@ func testType(t *testing.T, fset *token.FileSet, v Type) {
136136
if data, ok := tag.Lookup("Value"); ok {
137137
val := v.Value()
138138
if data != val {
139-
t.Fatal(pos, "Error value:", val, data)
139+
t.Fatal(pos, "Error value:", val, ":", data)
140140
}
141141
}
142142
if data, ok := tag.Lookup("Name"); ok {
143143
name := v.Name()
144144
if data != name {
145-
t.Fatal(pos, "Error name:", name, data)
145+
t.Fatal(pos, "Error name:", name, ":", data)
146146
}
147147
}
148148
if data, ok := tag.Lookup("String"); ok {
149149
str := v.String()
150150
if data != str {
151-
t.Fatal(pos, "Error string:", str, data)
151+
t.Fatal(pos, "Error string:", str, ":", data)
152152
}
153153
}
154154
if data, ok := tag.Lookup("Kind"); ok {
155155
kind := v.Kind().String()
156156
if data != kind {
157-
t.Fatal(pos, "Error kind:", kind, data)
157+
t.Fatal(pos, "Error kind:", kind, ":", data)
158158
}
159159
}
160160
if data, ok := tag.Lookup("Len"); ok {
161161
l := fmt.Sprint(v.Len())
162162
if data != l {
163-
t.Fatal(pos, "Error len:", l, data)
163+
t.Fatal(pos, "Error len:", l, ":", data)
164+
}
165+
}
166+
if data, ok := tag.Lookup("NumChild"); ok {
167+
num := fmt.Sprint(v.NumChild())
168+
if data != num {
169+
t.Fatal(pos, "Error num child:", num, ":", data)
164170
}
165171
}
166172
if data, ok := tag.Lookup("NumMethod"); ok {
167173
num := fmt.Sprint(v.NumMethod())
168174
if data != num {
169-
t.Fatal(pos, "Error num method:", num, data)
175+
t.Fatal(pos, "Error num method:", num, ":", data)
170176
}
171177
}
172178
if data, ok := tag.Lookup("NumIn"); ok {
173179
num := fmt.Sprint(v.NumIn())
174180
if data != num {
175-
t.Fatal(pos, "Error num in:", num, data)
181+
t.Fatal(pos, "Error num in:", num, ":", data)
176182
}
177183
}
178184
if data, ok := tag.Lookup("NumOut"); ok {
179185
num := fmt.Sprint(v.NumOut())
180186
if data != num {
181-
t.Fatal(pos, "Error num out:", num, data)
187+
t.Fatal(pos, "Error num out:", num, ":", data)
182188
}
183189
}
184190
if data, ok := tag.Lookup("NumField"); ok {
185191
num := fmt.Sprint(v.NumField())
186192
if data != num {
187-
t.Fatal(pos, "Error num field:", num, data)
193+
t.Fatal(pos, "Error num field:", num, ":", data)
188194
}
189195
}
190196
if data, ok := tag.Lookup("Tag"); ok {
191197
num := string(v.Tag())
192198
if data != num {
193-
t.Fatal(pos, "Error tag:", num, data)
199+
t.Fatal(pos, "Error tag:", num, ":", data)
194200
}
195201
}
196202
}

testdata/kind/a.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package a
22

3+
import (
4+
. "time"
5+
)
6+
37
// Kind:"Bool" Name:"TBool" String:"TBool"
48
type TBool bool
59

@@ -80,3 +84,6 @@ type TPtr *int
8084

8185
// Kind:"Slice" Name:"TSlice" String:"TSlice"
8286
type TSlice []struct{}
87+
88+
// Kind:"Struct" Name:"MyTime"
89+
type MyTime Time
File renamed without changes.

testdata/type/b.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package a
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/url"
7+
"path/filepath"
8+
"time"
9+
)
10+
11+
// Name:"MyStringer"
12+
// Kind:"Interface"
13+
type MyStringer fmt.Stringer
14+
15+
// Name:"a" Value:""
16+
// To:"Declaration" Kind:"Interface" NumMethod:"1" NumField:"2" String:"interface{Stringer; MyString}"
17+
// To:"Declaration,MethodByName:String,Declaration" String:"func() (_)" Value:""
18+
var a = (interface {
19+
fmt.Stringer
20+
MyString() string
21+
})(nil)
22+
23+
// Name:"b" Value:""
24+
// To:"Declaration" Kind:"Struct" String:"struct{fmt.Stringer; String string `s:\"\"`}"
25+
// To:"Declaration,MethodByName:String,Declaration" String:"func() (_)" Value:""
26+
var b = struct {
27+
fmt.Stringer
28+
String string `s:""`
29+
}{}
30+
31+
// Name:"c" Value:""
32+
// To:"Declaration" Kind:"Map" Key:"String" Elme:"Int" String:"map[string]int" Value:""
33+
var c = map[string]int{
34+
"S": 1,
35+
}
36+
37+
// Name:"d" Value:""
38+
// To:"Declaration" Kind:"Slice" Elme:"Int" String:"[]int" Value:""
39+
var d = make([]int, 10)
40+
41+
// Name:"e" Value:""
42+
// To:"Declaration" Kind:"Array" Elme:"Byte" Len:"8" String:"[8]byte" Value:""
43+
var e = [8]byte{}
44+
45+
// Name:"s" Value:""
46+
// Kind:"Interface" NumMethod:"1" String:"s" Value:""
47+
type s fmt.Stringer
48+
49+
// Name:"t" Value:""
50+
// Kind:"Struct" NumField:"3" String:"t" Value:""
51+
type t time.Time
52+
53+
// Name:"v" Value:""
54+
// Kind:"Map" String:"v" Key:"string" Elme:"[]string" Value:""
55+
type v url.Values
56+
57+
// Name:"l" Value:""
58+
// String:"l" Elme:"byte" Kind:"Slice" Value:""
59+
type l json.RawMessage
60+
61+
// Name:"w" Value:""
62+
// String:"w" Kind:"Func" Value:""
63+
type w filepath.WalkFunc

testdata/value/a.go

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,43 @@
11
package a
22

33
const (
4-
// Value:"1" Name:"A1"
5-
A1 = 1 << iota
6-
// Value:"2" Name:"B1"
7-
B1
8-
// Value:"4" Name:"C1"
9-
C1
10-
// Value:"8" Name:"D1"
11-
D1
4+
// Value:"1" Name:"U1"
5+
U1 uint = 1 << iota
6+
// Value:"2" Name:"U2"
7+
U2
8+
// Value:"4" Name:"U3"
9+
U3
10+
// Value:"3" Name:"U4"
11+
U4 = U1 | U2
1212
)
1313

1414
const (
15-
// Value:"0" Name:"A2"
16-
A2 = iota
17-
// Value:"1" Name:"B2"
18-
B2
19-
// Value:"2" Name:"C2"
20-
C2
21-
// Value:"3" Name:"D2"
22-
D2
15+
// Value:"0" Name:"I1"
16+
I1 int = iota + 0
17+
// Value:"1" Name:"I2"
18+
I2
19+
// Value:"2" Name:"I3"
20+
I3
21+
// Value:"3" Name:"I4"
22+
I4 = I2 + I3
23+
// Value:"4" Name:"I5"
24+
I5 = -I2 + I3 + I4
2325
)
2426

25-
// Value:"\"A\"" Name:"StrA"
26-
const StrA = "A"
27+
const (
28+
// Value:"0" Name:"F1"
29+
F1 = float64(iota / 10)
30+
// Value:"1/10" Name:"F2"
31+
F2
32+
// Value:"1/5" Name:"F3"
33+
F3
34+
// Value:"3" Name:"F4"
35+
F4 float64 = iota
36+
)
2737

2838
const (
39+
// Value:"\"A\"" Name:"StrA"
40+
StrA = "A"
2941
// Value:"\"CAB\"" Name:"StrB"
3042
StrB = StrC + "B"
3143
// Value:"\"CA\"" Name:"StrC"
@@ -48,5 +60,5 @@ var (
4860

4961
var (
5062
// To:"Declaration" String:"[3]int"
51-
Arr1 [D2]int
63+
Arr1 [I4]int
5264
)

0 commit comments

Comments
 (0)