Skip to content
This repository was archived by the owner on Dec 16, 2025. It is now read-only.

Commit 267186e

Browse files
authored
Merge pull request #597 from cassierecher/survey
Add tests for SurveyNode.
2 parents 05da18d + 73746ee commit 267186e

3 files changed

Lines changed: 256 additions & 33 deletions

File tree

claat/nodes/node.go

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -445,36 +445,3 @@ type ButtonNode struct {
445445
func (bn *ButtonNode) Empty() bool {
446446
return bn.Content.Empty()
447447
}
448-
449-
// NewSurveyNode creates a new survey node with optional questions.
450-
// If survey is nil, a new empty map will be created.
451-
func NewSurveyNode(id string, groups ...*SurveyGroup) *SurveyNode {
452-
return &SurveyNode{
453-
node: node{typ: NodeSurvey},
454-
ID: id,
455-
Groups: groups,
456-
}
457-
}
458-
459-
// SurveyNode contains groups of questions. Each group name is the Survey key.
460-
type SurveyNode struct {
461-
node
462-
ID string
463-
Groups []*SurveyGroup
464-
}
465-
466-
// SurveyGroup contains group name/question and possible answers.
467-
type SurveyGroup struct {
468-
Name string
469-
Options []string
470-
}
471-
472-
// Empty returns true if each group has 0 options.
473-
func (sn *SurveyNode) Empty() bool {
474-
for _, g := range sn.Groups {
475-
if len(g.Options) > 0 {
476-
return false
477-
}
478-
}
479-
return true
480-
}

claat/nodes/survey.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package nodes
2+
3+
// TODO general refactor?
4+
5+
// SurveyNode contains groups of questions. Each group name is the Survey key.
6+
type SurveyNode struct {
7+
node
8+
ID string
9+
Groups []*SurveyGroup
10+
}
11+
12+
// SurveyGroup contains group name/question and possible answers.
13+
type SurveyGroup struct {
14+
Name string
15+
Options []string
16+
}
17+
18+
// NewSurveyNode creates a new survey node with optional questions.
19+
// If survey is nil, a new empty map will be created.
20+
// TODO is "map" above a mistake, or should the code below contain a map?
21+
func NewSurveyNode(id string, groups ...*SurveyGroup) *SurveyNode {
22+
return &SurveyNode{
23+
node: node{typ: NodeSurvey},
24+
ID: id,
25+
Groups: groups,
26+
}
27+
}
28+
29+
// Empty returns true if each group has 0 options.
30+
func (sn *SurveyNode) Empty() bool {
31+
for _, g := range sn.Groups {
32+
if len(g.Options) > 0 {
33+
return false
34+
}
35+
}
36+
return true
37+
}

claat/nodes/survey_test.go

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
package nodes
2+
3+
import (
4+
"testing"
5+
6+
"github.com/google/go-cmp/cmp"
7+
)
8+
9+
func TestNewSurveyNode(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
inID string
13+
inGroups []*SurveyGroup
14+
out *SurveyNode
15+
}{
16+
{
17+
name: "Empty",
18+
out: &SurveyNode{
19+
node: node{typ: NodeSurvey},
20+
},
21+
},
22+
{
23+
// TODO: should the absence of an ID be an error?
24+
name: "GroupsNoID",
25+
inGroups: []*SurveyGroup{
26+
&SurveyGroup{
27+
Name: "pick a number",
28+
Options: []string{"1", "2", "3"},
29+
},
30+
&SurveyGroup{
31+
Name: "choose an answer",
32+
Options: []string{"yes", "no", "probably"},
33+
},
34+
},
35+
out: &SurveyNode{
36+
node: node{typ: NodeSurvey},
37+
Groups: []*SurveyGroup{
38+
&SurveyGroup{
39+
Name: "pick a number",
40+
Options: []string{"1", "2", "3"},
41+
},
42+
&SurveyGroup{
43+
Name: "choose an answer",
44+
Options: []string{"yes", "no", "probably"},
45+
},
46+
},
47+
},
48+
},
49+
{
50+
name: "IDNoGroups",
51+
inID: "identifier",
52+
out: &SurveyNode{
53+
node: node{typ: NodeSurvey},
54+
ID: "identifier",
55+
},
56+
},
57+
{
58+
name: "Simple",
59+
inID: "a simple example",
60+
inGroups: []*SurveyGroup{
61+
&SurveyGroup{
62+
Name: "pick a color",
63+
Options: []string{"red", "blue", "yellow"},
64+
},
65+
},
66+
out: &SurveyNode{
67+
node: node{typ: NodeSurvey},
68+
ID: "a simple example",
69+
Groups: []*SurveyGroup{
70+
&SurveyGroup{
71+
Name: "pick a color",
72+
Options: []string{"red", "blue", "yellow"},
73+
},
74+
},
75+
},
76+
},
77+
{
78+
name: "Multiple",
79+
inID: "an example with multiple survey groups",
80+
inGroups: []*SurveyGroup{
81+
&SurveyGroup{
82+
Name: "a",
83+
Options: []string{"a", "aa", "aaa"},
84+
},
85+
&SurveyGroup{
86+
Name: "b",
87+
Options: []string{"b", "bb", "bbb"},
88+
},
89+
&SurveyGroup{
90+
Name: "c",
91+
Options: []string{"c", "cc", "ccc"},
92+
},
93+
},
94+
out: &SurveyNode{
95+
node: node{typ: NodeSurvey},
96+
ID: "an example with multiple survey groups",
97+
Groups: []*SurveyGroup{
98+
&SurveyGroup{
99+
Name: "a",
100+
Options: []string{"a", "aa", "aaa"},
101+
},
102+
&SurveyGroup{
103+
Name: "b",
104+
Options: []string{"b", "bb", "bbb"},
105+
},
106+
&SurveyGroup{
107+
Name: "c",
108+
Options: []string{"c", "cc", "ccc"},
109+
},
110+
},
111+
},
112+
},
113+
}
114+
for _, tc := range tests {
115+
t.Run(tc.name, func(t *testing.T) {
116+
out := NewSurveyNode(tc.inID, tc.inGroups...)
117+
if diff := cmp.Diff(tc.out, out, cmp.AllowUnexported(SurveyNode{}, node{})); diff != "" {
118+
t.Errorf("NewSurveyNode(%q, %v) got diff (-want +got): %s", tc.inID, tc.inGroups, diff)
119+
return
120+
}
121+
})
122+
}
123+
}
124+
125+
func TestSurveyNodeEmpty(t *testing.T) {
126+
tests := []struct {
127+
name string
128+
inID string
129+
inGroups []*SurveyGroup
130+
out bool
131+
}{
132+
{
133+
name: "NoGroups",
134+
inID: "id",
135+
out: true,
136+
},
137+
{
138+
name: "OneGroupEmpty",
139+
inID: "id",
140+
inGroups: []*SurveyGroup{
141+
&SurveyGroup{
142+
Name: "one",
143+
},
144+
},
145+
out: true,
146+
},
147+
{
148+
name: "MultiGroupsEmpty",
149+
inID: "id",
150+
inGroups: []*SurveyGroup{
151+
&SurveyGroup{
152+
Name: "one",
153+
},
154+
&SurveyGroup{
155+
Name: "two",
156+
},
157+
&SurveyGroup{
158+
Name: "three",
159+
},
160+
},
161+
out: true,
162+
},
163+
{
164+
name: "OneGroupNonEmpty",
165+
inID: "id",
166+
inGroups: []*SurveyGroup{
167+
&SurveyGroup{
168+
Name: "one",
169+
Options: []string{"two", "three"},
170+
},
171+
},
172+
},
173+
{
174+
name: "MultiGroupsNonEmpty",
175+
inID: "id",
176+
inGroups: []*SurveyGroup{
177+
&SurveyGroup{
178+
Name: "one",
179+
Options: []string{"two", "three"},
180+
},
181+
&SurveyGroup{
182+
Name: "four",
183+
Options: []string{"five", "six"},
184+
},
185+
&SurveyGroup{
186+
Name: "seven",
187+
Options: []string{"eight", "nine"},
188+
},
189+
},
190+
},
191+
{
192+
name: "MultiGroupsNonEmptySomeNoOptions",
193+
inID: "id",
194+
inGroups: []*SurveyGroup{
195+
&SurveyGroup{
196+
Name: "one",
197+
Options: []string{"two", "three"},
198+
},
199+
&SurveyGroup{
200+
Name: "four",
201+
Options: []string{"five", "six"},
202+
},
203+
&SurveyGroup{
204+
Name: "seven",
205+
},
206+
},
207+
},
208+
}
209+
for _, tc := range tests {
210+
t.Run(tc.name, func(t *testing.T) {
211+
n := NewSurveyNode(tc.inID, tc.inGroups...)
212+
out := n.Empty()
213+
if out != tc.out {
214+
t.Errorf("SurveyNode.Empty() = %t, want %t", out, tc.out)
215+
return
216+
}
217+
})
218+
}
219+
}

0 commit comments

Comments
 (0)