/
pretty_test.go
151 lines (132 loc) · 2.49 KB
/
pretty_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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package pretty
import(
"testing"
"reflect"
"fmt"
)
func testBody(s interface{}, expected string, t *testing.T) {
result := Pretty(s, " ")
if result != expected {
t.Errorf("Bad string:\n%s %v\nproduced\n%s\nrather than\n%s\n",
reflect.TypeOf(s), s, result, expected)
}
}
func TestString(t *testing.T) {
testBody("This is a test", "This is a test", t)
}
func TestString2(t *testing.T) {
testBody("This is\na test", "This is\na test", t)
}
func TestInt(t *testing.T) {
testBody(42, "42", t)
}
func TestArray(t *testing.T) {
a := [3]int{4, 5, 6}
expected := `[3]int[
4
5
6
]`
testBody(a, expected, t)
}
func TestSlice(t *testing.T) {
a := [3]int{4, 5, 6}
expected := `[3]int[
4
5
6
]`
testBody(a[:], expected, t)
}
type StringerStruct struct {
AField int
}
func (s StringerStruct)String() string {
return fmt.Sprintf("StringerStruct: %s", s.AField)
}
func TestStringer(t *testing.T) {
s := StringerStruct{42}
testBody(s, s.String(), t)
}
func TestPtr(t *testing.T) {
s := 42
testBody(&s, "&42", t)
}
type Nested struct {
Something int
}
type TStruct struct {
AField int
aPrivateField int
NestedStr Nested
NestedPtr *Nested
Intf interface{}
privateNested Nested
}
func TestStruct(t *testing.T) {
n := Nested{4}
s := TStruct{42, 43, Nested{3}, &n, Nested{5}, Nested{6}}
expected := `pretty.TStruct{
AField: 42
aPrivateField: 43
NestedStr: pretty.Nested{
Something: 3
}
NestedPtr: &pretty.Nested{
Something: 4
}
Intf: pretty.Nested{
Something: 5
}
privateNested: pretty.Nested{
Something: 6
}
}`
testBody(s, expected, t)
}
func TestMap(t *testing.T) {
m := map[string]string{ "foo" : "bar", "baz": "wox" }
expected :=
`map[string]string[
foo: bar
baz: wox
]`
testBody(m, expected, t)
}
type PStruct struct {
intField int
int8Field int8
uint8Field uint8
complex128Field complex128
mapField map[int]int
ssField StringerStruct
stringerField fmt.Stringer
intPtrField *int
arrayField [3]int
}
func TestStructUnexp(t *testing.T) {
var ps PStruct
ps.stringerField = StringerStruct{3}
expected :=
`pretty.PStruct{
intField: 0
int8Field: 0
uint8Field: 0
complex128Field: (0+0i)
mapField: map[int]int[
]
ssField: pretty.StringerStruct{
AField: 0
}
stringerField: pretty.StringerStruct{
AField: 3
}
intPtrField: nil
arrayField: [3]int[
0
0
0
]
}`
testBody(ps, expected, t)
}