Tests: reusable NewTemplate, and bracket names rejected at load

This commit is contained in:
2026-09-03 17:48:20 +02:00
parent 031a3ec7d6
commit 47ec883bb0
2 changed files with 25 additions and 0 deletions
+15
View File
@@ -97,6 +97,21 @@ func TestFakeTemplateErrors(t *testing.T) {
} }
} }
func TestNewTemplateReusable(t *testing.T) {
f := shipped(t)
tmpl, err := f.NewTemplate(`{digits(2)}`)
if err != nil {
t.Fatalf("NewTemplate: %v", err)
}
seen := map[string]bool{}
for i := 0; i < 50; i++ {
seen[tmpl.Fake()] = true
}
if len(seen) < 2 {
t.Fatalf("Template.Fake() repeated %v, want varied draws from one compile", seen)
}
}
func TestFakeTemplateRepeatBound(t *testing.T) { func TestFakeTemplateRepeatBound(t *testing.T) {
f := shipped(t) f := shipped(t)
_, err := f.FakeTemplate(`{"format":"{x}","repeat":200,"x":{"format":"{y}","repeat":200,"y":{"format":"z","repeat":200}}}`) _, err := f.FakeTemplate(`{"format":"{x}","repeat":200,"x":{"format":"{y}","repeat":200,"y":{"format":"z","repeat":200}}}`)
+10
View File
@@ -275,6 +275,16 @@ func TestNewErrors(t *testing.T) {
map[string]string{"a|b": `"1"`}, map[string]string{"a|b": `"1"`},
`category "a|b" contains "|"`, `category "a|b" contains "|"`,
}, },
// A bracket is not a token-grammar character, but the CLI reads an argument
// starting with [ as a JSON array, so a name carrying one would be misread.
"field name with a bracket": {
map[string]string{"a": `{"format":"{x}","x":"1","b[c":"2"}`},
`field "b[c" contains "["`,
},
"category name with a bracket": {
map[string]string{"[abc]": `"1"`},
`category "[abc]" contains "["`,
},
// An empty name is not a path segment, so List never offered it — while a // An empty name is not a path segment, so List never offered it — while a
// bare {}, a trailing dot in Fake("a.") and a {/a.} reference all reached // bare {}, a trailing dot in Fake("a.") and a {/a.} reference all reached
// it. The engine accepted spellings it would never advertise. // it. The engine accepted spellings it would never advertise.