Skip to content

Commit 697a484

Browse files
authored
Merge pull request #8 from vcaesar/t-pr
add more Is type function support
2 parents 3b53089 + 525c9bf commit 697a484

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

tt.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,54 @@ func BM(b *testing.B, fn func()) {
7575
}
7676
}
7777

78+
func typeMap(expect string) string {
79+
m := map[string]string{
80+
"str": "string",
81+
"int": "int",
82+
"i8": "int8",
83+
"i16": "int16",
84+
"i32": "int32",
85+
"i64": "int64",
86+
"u": "uint",
87+
"u8": "uint8",
88+
"u16": "uint16",
89+
"ui32": "uint32",
90+
"ui64": "uint64",
91+
"f32": "float32",
92+
"f64": "float64",
93+
"b": "bool",
94+
"m": "map",
95+
"ch": "chan",
96+
"stu": "struct",
97+
"bytes": "[]byte",
98+
"u8s": "[]uint8",
99+
"c64": "complex64",
100+
"c128": "complex128",
101+
}
102+
103+
return m[expect]
104+
}
105+
106+
// IsTypes return bool when actual is expect type
107+
func IsTypes(expect string, actual interface{}) bool {
108+
mt := typeMap(expect)
109+
s := reflect.TypeOf(actual).String()
110+
if s == mt || s == expect {
111+
return true
112+
}
113+
114+
return false
115+
}
116+
117+
func typVal(expect string) string {
118+
ept := typeMap(expect)
119+
if ept == "" {
120+
ept = expect
121+
}
122+
123+
return ept
124+
}
125+
78126
// TypeOf equal two interface{} type
79127
func TypeOf(expect, actual interface{}) bool {
80128
return reflect.TypeOf(expect) == reflect.TypeOf(actual)
@@ -90,6 +138,7 @@ func IsType(t TestingT, expect string, actual interface{}, args ...interface{})
90138
}
91139

92140
s := reflect.TypeOf(actual).String()
141+
expect = typVal(expect)
93142

94143
info, call, cinfo := typeCall(expect, s, args...)
95144
return Equal(t, expect, s, info, call, cinfo)

tt_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ func TestArgs(t *testing.T) {
136136

137137
func TestType(t *testing.T) {
138138
IsType(t, "int", 11)
139+
IsType(t, "f64", 0.1)
140+
IsTypes("f64", 0.1)
139141

140142
Type = true
141143
Equal(t, 1, 1)

0 commit comments

Comments
 (0)