Tests: padded and stray-brace arguments, the inline held fence, a template under concurrent use, and the fieldless-token hint

This commit is contained in:
2026-09-03 20:57:25 +02:00
parent 71cccaf4c9
commit d8048df169
3 changed files with 29 additions and 11 deletions
+11 -8
View File
@@ -309,6 +309,7 @@ func TestClassify(t *testing.T) {
`{"format":"x"}`: argTemplate,
`["a","b"]`: argTemplate, // a JSON array carries no brace
`[1, 2]`: argTemplate,
` ["a","b"]`: argTemplate, // padding is the template's own error, not a shape verdict
`"hello"`: argTemplate, // a JSON string, the spelling a format-only object names
} {
got, err := classify(arg)
@@ -321,6 +322,7 @@ func TestClassify(t *testing.T) {
"[abc].field": `holds a "["`,
"x[1]": `holds a "["`,
"a]b": `holds a "]"`,
"a}b": `holds a "}"`,
`"abc`: `holds a "\""`,
`"a]b`: `holds a "\""`, // the opener the reader typed, not the bracket behind it
} {
@@ -358,16 +360,17 @@ func TestRunInlineTemplate(t *testing.T) {
}
func TestRunTemplateMisuse(t *testing.T) {
for _, arg := range []string{
"{bad", // unterminated brace
"[red,green]", // a near-miss JSON array (unquoted strings)
`{"format":"x"}`, // an object holding only a format
"{/no.such.path}", // a reference into nothing
"x[1]", // a bracket no path may hold
for arg, want := range map[string]string{
"{bad": "unterminated",
"[red,green]": "names no template either",
`{"format":"x"}`: "is a string",
"{/no.such.path}": "no entry",
"x[1]": "names no template either",
` ["a","b"] `: "may not be padded",
} {
code, out, errb := runOut("--seed", "1", arg)
if code != 2 || out != "" || !strings.Contains(errb, "try 'fejkdata --help'") {
t.Errorf("run(%q) = %d, %q, %q; want misuse naming --help", arg, code, out, errb)
if code != 2 || out != "" || !strings.Contains(errb, "try 'fejkdata --help'") || !strings.Contains(errb, want) {
t.Errorf("run(%q) = %d, %q, %q; want misuse naming %q and --help", arg, code, out, errb, want)
}
if strings.Contains(errb, "fejkdata: fejkdata:") {
t.Errorf("run(%q) doubled the program prefix: %q", arg, errb)
+12 -3
View File
@@ -76,12 +76,20 @@ func TestFakeTemplateDeterministic(t *testing.T) {
}
}
func TestBareStringReferenceHint(t *testing.T) {
func TestFieldlessTokenHint(t *testing.T) {
f := shipped(t)
_, err := f.FakeTemplate(`{sv_SE.person.last}`)
if err == nil || !strings.Contains(err.Error(), "write {/sv_SE.person.last}") {
t.Fatalf("FakeTemplate(bare token) = %v, want a hint naming {/sv_SE.person.last}", err)
}
_, err = f.FakeTemplate(`{"format":"{x}","repeat":2}`)
if err == nil || strings.Contains(err.Error(), "bare string") {
t.Errorf("FakeTemplate(fieldless object) = %v, want an error that does not call it a bare string", err)
}
_, err = f.FakeTemplate(`{ /sv_SE.person.last }`)
if err == nil || strings.Contains(err.Error(), "write {") {
t.Errorf("FakeTemplate(malformed token) = %v, want no hint naming a spelling that fails too", err)
}
}
func TestFakeTemplateErrors(t *testing.T) {
@@ -97,6 +105,7 @@ func TestFakeTemplateErrors(t *testing.T) {
{`name: {..nope}`, "write {/nope}"},
{`{"format":"x"}`, "is a string"},
{`{/misc.country} {/misc.country.alpha2}`, "renders a level"},
{`{"format":"{/misc.country.alpha2} {x}","x":"{/misc.country}"}`, "renders"},
} {
_, err := f.FakeTemplate(c.input)
if err == nil || !strings.Contains(err.Error(), c.want) {
@@ -124,13 +133,13 @@ func TestPaddedJSONIsRejected(t *testing.T) {
func TestNewTemplateReusable(t *testing.T) {
f := shipped(t)
tmpl, err := f.NewTemplate(`{digits(2)}`)
reusable, 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
seen[reusable.Fake()] = true
}
if len(seen) < 2 {
t.Fatalf("Template.Fake() repeated %v, want varied draws from one compile", seen)
+6
View File
@@ -108,6 +108,12 @@ func TestFakeIsSafeForConcurrentUse(t *testing.T) {
return
}
f.List()
tmpl, err := f.NewTemplate("{/sv_SE.person.last}")
if err != nil {
t.Error(err)
return
}
tmpl.Fake()
}
}()
}