Skip to content

Commit 0547853

Browse files
authored
[list-ops] Use IntList directly in cases_test.go; format IntList output as an IntList{} type (#3124)
[no important files changed]
1 parent 9b74aa9 commit 0547853

3 files changed

Lines changed: 151 additions & 106 deletions

File tree

exercises/practice/list-ops/.meta/gen.go

Lines changed: 62 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,37 @@ import (
55
"fmt"
66
"log"
77
"slices"
8+
"strconv"
89
"strings"
910
"text/template"
1011
)
1112

13+
func sliceIntToStr(ints []int) string {
14+
parts := make([]string, len(ints))
15+
for i, v := range ints {
16+
parts[i] = strconv.Itoa(v)
17+
}
18+
return fmt.Sprintf("%s", strings.Join(parts, ", "))
19+
}
20+
21+
func asIntList(list []int) string {
22+
return fmt.Sprintf("IntList{%s}", sliceIntToStr(list))
23+
}
24+
25+
func asIntListSlice(list [][]int) string {
26+
parts := make([]string, len(list))
27+
for i, p := range list {
28+
parts[i] = fmt.Sprintf("{%s}", sliceIntToStr(p))
29+
}
30+
return fmt.Sprintf("[]IntList{%s}", strings.Join(parts, ", "))
31+
}
32+
1233
func main() {
13-
t, err := template.New("").Parse(tmpl)
34+
funcMap := template.FuncMap{
35+
"IntList": asIntList,
36+
"IntListSlice": asIntListSlice,
37+
}
38+
t, err := template.New("").Funcs(funcMap).Parse(tmpl)
1439
if err != nil {
1540
log.Fatal(err)
1641
}
@@ -145,133 +170,133 @@ var tmpl = `// This file contains tests from the shared problem specifications r
145170
146171
type testCaseAppend struct {
147172
description string
148-
list1, list2 []int
149-
expected []int
173+
list1, list2 IntList
174+
expected IntList
150175
}
151176
152177
var testCasesAppend = []testCaseAppend { {{range .J.append}}
153178
{
154179
description: {{printf "%q" .Description}},
155-
list1: {{printf "%#v" .Input.List1}},
156-
list2: {{printf "%#v" .Input.List2}},
157-
expected: {{printf "%#v" .Expected}},
180+
list1: {{IntList .Input.List1}},
181+
list2: {{IntList .Input.List2}},
182+
expected: {{IntList .Expected}},
158183
},{{end}}
159184
}
160185
161186
type testCaseConcat struct {
162187
description string
163-
lists [][]int
164-
expected []int
188+
lists []IntList
189+
expected IntList
165190
}
166191
167192
var testCasesConcat = []testCaseConcat { {{range .J.concat}}
168193
{
169194
description: {{printf "%q" .Description}},
170-
lists: {{printf "%#v" .Input.Lists}},
171-
expected: {{printf "%#v" .Expected}},
195+
lists: {{IntListSlice .Input.Lists}},
196+
expected: {{IntList .Expected}},
172197
},{{end}}
173198
}
174199
175200
type testCaseFilter struct {
176201
description string
177-
list []int
202+
list IntList
178203
function func(int) bool
179204
functionStr string
180-
expected []int
205+
expected IntList
181206
}
182207
183208
var testCasesFilter = []testCaseFilter { {{range .J.filter}}
184209
{
185210
description: {{printf "%q" .Description}},
186-
list: {{printf "%#v" .Input.List}},
211+
list: {{IntList .Input.List}},
187212
function: {{.Func}},
188213
functionStr: {{printf "%q" .Input.Function}},
189-
expected: {{printf "%#v" .Expected}},
214+
expected: {{IntList .Expected}},
190215
},{{end}}
191216
}
192217
193218
type testCaseFoldl struct {
194219
description string
195-
list []int
196-
initial int
197-
function func(int, int) int
220+
list IntList
221+
initial int
222+
function func(int, int) int
198223
functionStr string
199-
expected int
224+
expected int
200225
}
201226
202227
var testCasesFoldl = []testCaseFoldl { {{range .J.foldl}}
203228
{
204229
description: {{printf "%q" .Description}},
205-
list: {{printf "%#v" .Input.List}},
230+
list: {{IntList .Input.List}},
206231
initial: {{.Input.Initial}},
207232
function: {{.Func}},
208233
functionStr: {{printf "%q" .Input.Function}},
209-
expected: {{printf "%#v" .Expected}},
234+
expected: {{.Expected}},
210235
},{{end}}
211236
}
212237
213238
type testCaseFoldr struct {
214239
description string
215-
list []int
216-
initial int
217-
function func(int, int) int
240+
list IntList
241+
initial int
242+
function func(int, int) int
218243
functionStr string
219-
expected int
244+
expected int
220245
}
221246
222247
var testCasesFoldr = []testCaseFoldr { {{range .J.foldr}}
223248
{
224249
description: {{printf "%q" .Description}},
225-
list: {{printf "%#v" .Input.List}},
250+
list: {{IntList .Input.List}},
226251
initial: {{.Input.Initial}},
227252
function: {{.Func}},
228253
functionStr: {{printf "%q" .Input.Function}},
229-
expected: {{printf "%#v" .Expected}},
254+
expected: {{.Expected}},
230255
},{{end}}
231256
}
232257
233258
type testCaseLength struct {
234259
description string
235-
list []int
260+
list IntList
236261
expected int
237262
}
238263
239264
var testCasesLength = []testCaseLength { {{range .J.length}}
240265
{
241266
description: {{printf "%q" .Description}},
242-
list: {{printf "%#v" .Input.List}},
267+
list: {{IntList .Input.List}},
243268
expected: {{.Expected}},
244269
},{{end}}
245270
}
246271
247272
type testCaseMap struct {
248273
description string
249-
list []int
250-
function func(int) int
274+
list IntList
275+
function func(int) int
251276
functionStr string
252-
expected []int
277+
expected IntList
253278
}
254279
255280
var testCasesMap = []testCaseMap { {{range .J.map}}
256281
{
257282
description: {{printf "%q" .Description}},
258-
list: {{printf "%#v" .Input.List}},
283+
list: {{IntList .Input.List}},
259284
function: {{.Func}},
260285
functionStr: {{printf "%q" .Input.Function}},
261-
expected: {{printf "%#v" .Expected}},
286+
expected: {{IntList .Expected}},
262287
},{{end}}
263288
}
264289
265290
type testCaseReverse struct {
266291
description string
267-
list []int
268-
expected []int
292+
list IntList
293+
expected IntList
269294
}
270295
271296
var testCasesReverse = []testCaseReverse { {{range .J.reverse}}
272297
{
273298
description: {{printf "%q" .Description}},
274-
list: {{printf "%#v" .Input.List}},
275-
expected: {{printf "%#v" .Expected}},
299+
list: {{IntList .Input.List}},
300+
expected: {{IntList .Expected}},
276301
},{{end}}
277302
}`

0 commit comments

Comments
 (0)