Skip to content

Commit f3b1895

Browse files
committed
Fix generation of puzzle object
1 parent 4520e58 commit f3b1895

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

cmd/next/main.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func Run(config *Config) error {
4040
all := re.FindAll(contents, -1)
4141
next := len(all) + 1
4242

43-
text := fmt.Sprintf("\tpuzzles = append(puzzles, puzzle.Puzzle%03d)\n\t// next puzzle", next)
43+
text := fmt.Sprintf("\tpuzzles = append(puzzles, Puzzle%03d())\n\t// next puzzle", next)
4444

4545
re = regexp.MustCompile("\t// next puzzle")
4646
update := re.ReplaceAllString(string(contents), text)
@@ -50,10 +50,30 @@ func Run(config *Config) error {
5050
}
5151

5252
puzzle := fmt.Sprintf(`package puzzle
53-
54-
func Puzzle%03d() int {
55-
return 0
56-
}`, next)
53+
54+
// Puzzle%03d is the solutions for puzzle %d
55+
func Puzzle%03d() Puzzle {
56+
// Here's where you put some extra data you might need to process
57+
data := ""
58+
59+
// You can create helpers like this that can be called later
60+
processData := func() {
61+
data += "a"
62+
}
63+
64+
return Puzzle {
65+
// Init will be called before any of the solutions. Do all of your
66+
// expsensive pre-processing here.
67+
Init: func() {
68+
processData()
69+
},
70+
// Parts contains all of the different sub-solutions that you need
71+
// to implement (looking at you Advent of Code).
72+
Parts: []Solution{
73+
func() int { return 0 },
74+
},
75+
}
76+
}`, next, next, next)
5777

5878
err = afero.WriteFile(
5979
config.fs,

cmd/next/main_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestRunDetectsMissingMainFile(t *testing.T) {
2121

2222
func TestRunFailsWhenWritingInvalidFile(t *testing.T) {
2323
memFs := afero.NewMemMapFs()
24-
afero.WriteFile(memFs, "puzzle.go", []byte(`func RegisterPuzzles() []Puzzle {
24+
afero.WriteFile(memFs, "puzzles.go", []byte(`func RegisterPuzzles() []Puzzle {
2525
puzzles := Puzzle{}
2626
2727
// next puzzle
@@ -39,7 +39,7 @@ func TestRunFailsWhenWritingInvalidFile(t *testing.T) {
3939

4040
func TestRunRegistersNewTest(t *testing.T) {
4141
memFs := afero.NewMemMapFs()
42-
afero.WriteFile(memFs, "puzzle.go", []byte(`func RegisterPuzzles() []Puzzle {
42+
afero.WriteFile(memFs, "puzzles.go", []byte(`func RegisterPuzzles() []Puzzle {
4343
puzzles := Puzzle{}
4444
4545
// next puzzle
@@ -52,15 +52,15 @@ func TestRunRegistersNewTest(t *testing.T) {
5252
})
5353
assert.NoError(t, err)
5454

55-
contents, err := afero.ReadFile(memFs, "puzzle.go")
55+
contents, err := afero.ReadFile(memFs, "puzzles.go")
5656
assert.NoError(t, err)
57-
assert.Contains(t, string(contents), "puzzles = append(puzzles, puzzle.Puzzle001)")
57+
assert.Contains(t, string(contents), "puzzles = append(puzzles, Puzzle001())")
5858
assert.Contains(t, string(contents), "// next puzzle")
5959
}
6060

6161
func TestRunCreatesPuzzleFile(t *testing.T) {
6262
memFs := afero.NewMemMapFs()
63-
afero.WriteFile(memFs, "puzzle.go", []byte(`func RegisterPuzzles() []Puzzle {
63+
afero.WriteFile(memFs, "puzzles.go", []byte(`func RegisterPuzzles() []Puzzle {
6464
puzzles := Puzzle{}
6565
6666
// next puzzle
@@ -76,5 +76,5 @@ func TestRunCreatesPuzzleFile(t *testing.T) {
7676
contents, err := afero.ReadFile(memFs, "puzzle001.go")
7777
assert.NoError(t, err)
7878
assert.Contains(t, string(contents), "package puzzle")
79-
assert.Contains(t, string(contents), "func Puzzle001() int")
79+
assert.Contains(t, string(contents), "func Puzzle001() Puzzle")
8080
}

0 commit comments

Comments
 (0)