-
Notifications
You must be signed in to change notification settings - Fork 8
/
node_generator_test.go
152 lines (133 loc) · 5.01 KB
/
node_generator_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
151
152
package gtree
// ref: https://zenn.dev/kimuson13/articles/go_table_driven_test
import (
"testing"
)
type want struct {
name string
hierarchy uint
index uint
err error
}
const fixedIndex uint = 1
func TestGenerateTab(t *testing.T) {
tests := map[string]struct {
row string
want *want
}{
"root/hierarchy=1": {"- aaa bb", &want{name: "aaa bb", hierarchy: 1, index: fixedIndex, err: nil}},
"child/hierarchy=2": {" - aaa bb", &want{name: "aaa bb", hierarchy: 2, index: fixedIndex, err: nil}},
"child/hierarchy=2/tab on the way": {" - aaa bb", &want{name: "aaa bb", hierarchy: 2, index: fixedIndex, err: nil}},
"invalid/hierarchy=0/prefix chars": {"xx- aaa bb", &want{err: &inputFormatError{row: "xx- aaa bb"}}},
"invalid/hierarchy=0/no hyphen": {"xx aaa bb", &want{err: &inputFormatError{row: "xx aaa bb"}}},
"invalid/hierarchy=0/tab only": {" ", nil},
}
nodeGenerator := newNodeGenerator()
_, _ = nodeGenerator.generate(" - xxx", fixedIndex) // Parserのインデントスペース数を決めるために必要
for name, tt := range tests {
tt := tt
t.Run(name, func(t *testing.T) {
t.Parallel()
node, err := nodeGenerator.generate(tt.row, fixedIndex)
if node == nil && err == nil {
return
}
if tt.want.err != nil {
if e, ok := err.(*inputFormatError); !ok {
t.Errorf("\ngot: \n%v\nwant: \n%v", e, tt.want.err)
}
return
}
if node.name != tt.want.name {
t.Errorf("\ngot: \n%s\nwant: \n%s", node.name, tt.want.name)
}
if node.hierarchy != tt.want.hierarchy {
t.Errorf("\ngot: \n%d\nwant: \n%d", node.hierarchy, tt.want.hierarchy)
}
if node.index != tt.want.index {
t.Errorf("\ngot: \n%d\nwant: \n%d", node.index, tt.want.index)
}
})
}
}
func TestGenerateTwoSpaces(t *testing.T) {
tests := map[string]struct {
row string
want *want
}{
"root/hierarchy=1": {"- aaa bb", &want{name: "aaa bb", hierarchy: 1, index: fixedIndex, err: nil}},
"child/hierarchy=2": {" - aaa bb", &want{name: "aaa bb", hierarchy: 2, index: fixedIndex, err: nil}},
"invalid/hierarchy=0/prefix odd space": {" - aaa bb", &want{err: &inputFormatError{row: " - aaa bb"}}},
"invalid/hierarchy=0/prefix chars": {"xx- aaa bb", &want{err: &inputFormatError{row: "xx- aaa bb"}}},
"invalid/hierarchy=0/no hyphen": {"xx aaa bb", &want{err: &inputFormatError{row: "xx aaa bb"}}},
"invalid/hierarchy=0/space only": {" ", nil},
}
nodeGenerator := newNodeGenerator()
_, _ = nodeGenerator.generate(" - xxx", fixedIndex) // Parserのインデントスペース数を決めるために必要
for name, tt := range tests {
tt := tt
t.Run(name, func(t *testing.T) {
t.Parallel()
node, err := nodeGenerator.generate(tt.row, fixedIndex)
if node == nil && err == nil {
return
}
if tt.want.err != nil {
if e, ok := err.(*inputFormatError); !ok {
t.Errorf("\ngot: \n%v\nwant: \n%v", e, tt.want.err)
}
return
}
if node.name != tt.want.name {
t.Errorf("\ngot: \n%s\nwant: \n%s", node.name, tt.want.name)
}
if node.hierarchy != tt.want.hierarchy {
t.Errorf("\ngot: \n%d\nwant: \n%d", node.hierarchy, tt.want.hierarchy)
}
if node.index != tt.want.index {
t.Errorf("\ngot: \n%d\nwant: \n%d", node.index, tt.want.index)
}
})
}
}
func TestGenerateFourSpaces(t *testing.T) {
tests := map[string]struct {
row string
want *want
}{
"root/hierarchy=1": {"- aaa bb", &want{name: "aaa bb", hierarchy: 1, index: fixedIndex, err: nil}},
"child/hierarchy=2": {" - aaa bb", &want{name: "aaa bb", hierarchy: 2, index: fixedIndex, err: nil}},
"child/hierarchy=3": {" - aaa bb", &want{name: "aaa bb", hierarchy: 3, index: fixedIndex, err: nil}},
"invalid/hierarchy=0/prefix odd space": {" - aaa bb", &want{err: &inputFormatError{row: " - aaa bb"}}},
"invalid/hierarchy=0/prefix chars": {"xx- aaa bb", &want{err: &inputFormatError{row: "xx- aaa bb"}}},
"invalid/hierarchy=0/no hyphen": {"xx aaa bb", &want{err: &inputFormatError{row: "xx aaa bb"}}},
"invalid/hierarchy=0/space only": {" ", nil},
}
nodeGenerator := newNodeGenerator()
_, _ = nodeGenerator.generate(" - xxx", fixedIndex) // Parserのインデントスペース数を決めるために必要
for name, tt := range tests {
tt := tt
t.Run(name, func(t *testing.T) {
t.Parallel()
node, err := nodeGenerator.generate(tt.row, fixedIndex)
if node == nil && err == nil {
return
}
if tt.want.err != nil {
if e, ok := err.(*inputFormatError); !ok {
t.Errorf("\ngot: \n%v\nwant: \n%v", e, tt.want.err)
}
return
}
if node.name != tt.want.name {
t.Errorf("\ngot: \n%s\nwant: \n%s", node.name, tt.want.name)
}
if node.hierarchy != tt.want.hierarchy {
t.Errorf("\ngot: \n%d\nwant: \n%d", node.hierarchy, tt.want.hierarchy)
}
if node.index != tt.want.index {
t.Errorf("\ngot: \n%d\nwant: \n%d", node.index, tt.want.index)
}
})
}
}