From d8048df16936536776722cdf813c2a5af2d5d79f Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Thu, 3 Sep 2026 20:57:25 +0200 Subject: [PATCH] Tests: padded and stray-brace arguments, the inline held fence, a template under concurrent use, and the fieldless-token hint --- cmd/fejkdata/main_test.go | 19 +++++++++++-------- inline_test.go | 15 ++++++++++++--- shipped_data_test.go | 6 ++++++ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/cmd/fejkdata/main_test.go b/cmd/fejkdata/main_test.go index 5bc7612..5798565 100644 --- a/cmd/fejkdata/main_test.go +++ b/cmd/fejkdata/main_test.go @@ -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) diff --git a/inline_test.go b/inline_test.go index 31be71e..f2e04e4 100644 --- a/inline_test.go +++ b/inline_test.go @@ -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) diff --git a/shipped_data_test.go b/shipped_data_test.go index be776c1..fe1409d 100644 --- a/shipped_data_test.go +++ b/shipped_data_test.go @@ -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() } }() }