Repository navigation
Expand file tree
/
Copy pathexample_api_test.go
More file actions
224 lines (197 loc) · 6.82 KB
/
Copy pathexample_api_test.go
File metadata and controls
224 lines (197 loc) · 6.82 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package jenny_test
import (
"context"
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"testing/fstest"
"github.com/sdboyer/jenny"
)
func printFiles(fl jenny.Files, err error) {
if err != nil {
fmt.Println("error:", err)
return
}
for _, f := range fl {
if data := contents(jenny.Files{f})[f.Path()]; data == "" {
fmt.Printf("%s (%s)\n", f.Path(), f.FromString())
} else {
fmt.Printf("%s (%s) %s\n", f.Path(), f.FromString(), data)
}
}
}
// New accepts a generator func of any of the four shapes. Single-input funcs
// are called once per input; many-input funcs once with all of them.
func ExampleBuilder_New() {
str := jenny.For[string]()
perInput := str.New("PerInput", func(s string) (jenny.File, error) {
return jenny.NewFile(s+".txt", []byte(s)), nil
})
index := str.New("Index", func(ss ...string) (jenny.File, error) {
return jenny.NewFile("index.txt", []byte(strings.Join(ss, ","))), nil
})
printFiles(perInput.Generate("a", "b"))
printFiles(index.Generate("a", "b"))
// Output:
// a.txt (PerInput) a
// b.txt (PerInput) b
// index.txt (Index) a,b
}
// InputName lets errors from single-input jennies say which input failed.
func ExampleInputName() {
type Kind struct{ Name string }
kind := jenny.For[Kind](jenny.InputName(func(k Kind) string { return k.Name }))
j := kind.New("GoTypes", func(k Kind) (jenny.File, error) {
if k.Name == "playlist" {
return nil, errors.New("unsupported field type")
}
return jenny.NewFile(k.Name+".go", nil), nil
})
_, err := j.Generate(Kind{"dashboard"}, Kind{"playlist"})
fmt.Println(err)
// Output:
// GoTypes: unsupported field type for input "playlist"
}
// The adapter func's shape decides how the adapted pipeline runs. Here, a
// single-input adapter runs the Type pipeline once per Group, so the many-input
// "Index" jenny makes one index per group.
func ExampleBuilder_Adapt() {
type Group struct {
Name string
Types []string
}
group := jenny.For[Group]()
typ := jenny.For[string]()
index := typ.New("Index", func(ts ...string) (jenny.File, error) {
return jenny.NewFile("index-"+ts[0], []byte(strings.Join(ts, ","))), nil
})
perGroup := group.Adapt(func(g Group) ([]string, error) { return g.Types, nil }, index)
allAtOnce := group.Adapt(func(gs ...Group) ([]string, error) {
var ts []string
for _, g := range gs {
ts = append(ts, g.Types...)
}
return ts, nil
}, index)
groups := []Group{{"core", []string{"user", "team"}}, {"billing", []string{"invoice"}}}
printFiles(perGroup.Generate(groups...))
printFiles(allAtOnce.Generate(groups...))
// Output:
// index-user (Index) user,team
// index-invoice (Index) invoice
// index-user (Index) user,team,invoice
}
// Pipeline runs jennies of the same input type over the same inputs. Their
// files share one path namespace, so colliding paths are an error.
func ExamplePipeline() {
str := jenny.For[string]()
goFile := str.New("Go", func(s string) (jenny.File, error) { return jenny.NewFile(s+".go", nil), nil })
tsFile := str.New("TS", func(s string) (jenny.File, error) { return jenny.NewFile(s+".ts", nil), nil })
printFiles(jenny.Pipeline(goFile, tsFile).Generate("user"))
_, err := jenny.Pipeline(goFile, goFile).Generate("user")
fmt.Println(err != nil)
// Output:
// user.go (Go)
// user.ts (TS)
// true
}
// Decorating methods apply left to right, in the order they read.
func ExampleJenny_Prefix() {
j := jenny.For[string]().New("J", func(s string) (jenny.File, error) {
return jenny.NewFile(s+".go", nil), nil
})
printFiles(j.Prefix("inner").Prefix("outer").Generate("user"))
// Output:
// outer/inner/user.go (J)
}
func ExampleJenny_Map() {
header := jenny.BytesMapper(func(f jenny.File, data []byte) ([]byte, error) {
return append([]byte("// Code generated by "+f.FromString()+". DO NOT EDIT.\n"), data...), nil
})
j := jenny.For[string]().New("GoTypes", func(s string) (jenny.File, error) {
return jenny.NewFile(s+".go", []byte("package "+s)), nil
})
fl, _ := j.Map(header).Generate("user")
data, _ := jenny.BytesOf(fl[0])
fmt.Printf("%s", data)
// Output:
// // Code generated by GoTypes. DO NOT EDIT.
// package user
}
// With applies wrappers defined outside this package, so they chain like the
// built-in methods.
func ExampleJenny_With() {
// A reusable wrapper: route test files into a separate tree.
testsApart := func(j jenny.Jenny[string]) jenny.Jenny[string] {
return j.Map(func(f jenny.File) (jenny.File, error) {
if strings.HasSuffix(f.Path(), "_test.go") {
return jenny.Move(f, path.Join("tests", f.Path())), nil
}
return f, nil
})
}
j := jenny.For[string]().New("Go", func(s string) (jenny.Files, error) {
return jenny.Files{jenny.NewFile(s+".go", nil), jenny.NewFile(s+"_test.go", nil)}, nil
})
printFiles(j.With(testsApart).Prefix("gen").Generate("user"))
// Output:
// gen/user.go (Go)
// gen/tests/user_test.go (Go)
}
// The Prefix function applies one prefix to a group of jennies.
func ExamplePrefix() {
str := jenny.For[string]()
printFiles(jenny.Prefix("pkg/registry",
str.New("Registry", func(ss ...string) (jenny.File, error) { return jenny.NewFile("registry.go", nil), nil }),
str.New("RegistryTest", func(ss ...string) (jenny.File, error) { return jenny.NewFile("registry_test.go", nil), nil }),
).Generate("a", "b"))
// Output:
// pkg/registry/registry.go (Registry)
// pkg/registry/registry_test.go (RegistryTest)
}
// The usual generator main: write files, or in CI, verify that the files on
// disk are up to date.
func ExampleFS_Verify() {
j := jenny.For[string]().New("Go", func(s string) (jenny.File, error) {
return jenny.NewFile(s+".go", []byte("package "+s+"\n")), nil
})
fs, err := j.GenerateFS("user", "team")
if err != nil {
panic(err)
}
root, _ := os.MkdirTemp("", "jenny-example")
defer os.RemoveAll(root)
ctx := context.Background()
_ = fs.Write(ctx, root)
fmt.Println("after Write:", fs.Verify(ctx, root))
_ = os.WriteFile(filepath.Join(root, "team.go"), []byte("package edited\n"), 0o600)
err = fs.Verify(ctx, root)
// The error goes on to include a diff of the file's contents.
firstLine, _, _ := strings.Cut(err.Error(), "\n")
fmt.Println("after edit:", firstLine)
// Output:
// after Write: <nil>
// after edit: team.go would have changed:
}
// VerifyFS checks against any io/fs.FS, here a map standing in for a goldens
// archive. Each file that drifted is a *DriftError.
func ExampleFS_VerifyFS() {
j := jenny.For[string]().New("Go", func(s string) (jenny.File, error) {
return jenny.NewFile("gen/"+s+".go", []byte("package "+s+"\n")), nil
})
fs, err := j.GenerateFS("user", "team")
if err != nil {
panic(err)
}
goldens := fstest.MapFS{"gen/user.go": {Data: []byte("package user\n")}}
err = fs.VerifyFS(context.Background(), goldens)
var drift *jenny.DriftError
if errors.As(err, &drift) {
fmt.Println(drift.Path, "missing:", drift.Missing)
}
// Output:
// gen/team.go missing: true
}