-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers_test.go
82 lines (74 loc) · 1.87 KB
/
helpers_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
package testhelpers
import (
"fmt"
"testing"
)
func TestTypeName(t *testing.T) {
cases := []struct {
want string
inst interface{}
}{
{"int32", int32(1)},
{"int64", int64(1)},
{"float64", float64(1)},
{"string", ""},
{"*testing.T", t},
{"testing.T", *t},
}
for _, tcase := range cases {
actual := TypeName(tcase.inst)
expected := tcase.want
if actual != expected {
t.Error("TypeName()",
NotEqualMsg(expected, actual))
}
}
}
// mISlice - Make Interface{} Slice is a helper function to aid in constructing test cases
func mISlice(a ...interface{}) []interface{} {
return a
}
func TestExtractFormatAndArgs(t *testing.T) {
cases := []struct {
name string
ok bool
format string
args []interface{}
subj []interface{}
}{
{name: "Should not be OK on nil subj", ok: false, subj: nil},
{"Zero args", true, "fmt", nil, mISlice("fmt")},
{"One arg", true, "fmt", mISlice(1), mISlice("fmt", 1)},
{"Two args", true, "fmt", mISlice(1, 2), mISlice("fmt", 1, 2)},
}
for idx, tcase := range cases {
aOk, aFmt, _ := ExtractFormatAndArgs(tcase.subj...)
msgBase := "Case #%d (%s), Variable %q Mismatch"
if aOk != tcase.ok {
t.Error(fmt.Sprintf(msgBase, idx, tcase.name, "OK"), NotEqualMsg(tcase.ok, aOk))
}
if aOk {
if aFmt != tcase.format {
t.Error(fmt.Sprintf(msgBase, idx, tcase.name, "Format"), NotEqualMsg(tcase.format, aFmt))
}
}
}
}
func TestSprintf(t *testing.T) {
cases := []struct {
name string
args []interface{}
expected string
}{
{"Zero args", mISlice("fmt"), "fmt"},
{"One arg", mISlice("fmt %d", 3), "fmt 3"},
{"Two args", mISlice("fmt %d %d", 3, -45), "fmt 3 -45"},
}
for idx, tcase := range cases {
actual := Sprintf(tcase.args...)
msgBase := fmt.Sprintf("Case #%d (%s)", idx, tcase.name)
if actual != tcase.expected {
t.Error(msgBase, NotEqualMsg(tcase.expected, actual))
}
}
}