-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoquote_test.go
125 lines (122 loc) · 1.88 KB
/
goquote_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
package main
import (
"fmt"
"testing"
)
func Test_realignTabs(t *testing.T) {
for _, tt := range []struct {
name, in, out string
}{
{
"empty",
``,
``,
},
{
"with comment excess indent",
`// hi
func main() {
// cool
}`,
`// hi
func main() {
// cool
}`,
},
{
"with comment no excess indent",
`// hi
func main() {
// cool
}`,
`// hi
func main() {
// cool
}`,
},
{
"no comment no excess indent",
`func main() {
// cool
}`,
`func main() {
// cool
}`,
},
{
"indented inner",
`bar := func() {
// cool
}`,
`bar := func() {
// cool
}`,
},
} {
t.Run(tt.name, func(t *testing.T) {
if got := string(realignTabs([]byte(tt.in))); got != tt.out {
t.Errorf("realignTabs() = %q, want %q", got, tt.out)
}
})
}
}
func Test_parseExampleTest(t *testing.T) {
for _, c := range []struct {
name string
in string
out []string
wantErr bool
}{
{
"splits",
`// Some stuff before the func
func ExampleFooBar() {
FooBar()
FooBaz()
// Output:
// FooBarRan
// FooBazRan
}`,
[]string{
"FooBar()\nFooBaz()",
"FooBarRan\nFooBazRan",
},
false,
},
{
"Indent savvy",
`// Some stuff before the func
func ExampleFooBar() {
for i := 0; i < 5; i++ {
FooBar()
}
// Output:
// FooBarRan
// FooBazRan
}`,
[]string{
"for i := 0; i < 5; i++ {\n\tFooBar()\n}",
"FooBarRan\n\tFooBazRan",
},
false,
},
} {
t.Run(c.name, func(t *testing.T) {
res, err := parseExampleTest([]byte(c.in))
if (err != nil) != c.wantErr {
t.Fatalf("wantErr %v but %v", c.wantErr, err)
}
if err != nil {
return
}
if len(res) != len(c.out) {
t.Fatalf("want %d but got %d", len(c.out), len(res))
}
for i, b := range res {
if s := string(b); c.out[i] != s {
t.Errorf("Wanted:\n%v\nGot:\n%v", fmt.Sprintf("%q", c.out[i]), fmt.Sprintf("%q", s))
}
}
})
}
}