|
| 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