From 05cf5313397005b0e28ab02e1cc52bba6bd76ff4 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sat, 3 Oct 2026 12:05:36 +0200 Subject: [PATCH] Test a name read across draw groups or in a repeat, read once through a path, and misspelt --- name_test.go | 25 +++++++++++++------------ perf_test.go | 2 +- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/name_test.go b/name_test.go index 5e176e9..856c42a 100644 --- a/name_test.go +++ b/name_test.go @@ -175,11 +175,11 @@ func TestNameErrors(t *testing.T) { {"bound twice", map[string]string{"word": `["a","b"]`, "card": `{"format":"{x}{y}","x":"{/word as w}{w}","y":"{/word as w}"}`}, `name "w" is bound twice outside any repeat`}, {"bound inside a repeat written first", map[string]string{"word": `["a","b"]`, "card": `{"format":"{a}{z}","a":{"format":"{/word as w}{w}","repeat":2},"z":"{/word as w}{w}"}`}, - `name "w" is bound outside this repeat already`}, + `name "w" is bound outside this repeat too`}, {"bound in a choice's item", map[string]string{"word": `["a","b"]`, "card": `{"format":"{x}{w}","x":["{/word as w}","b"]}`}, `a choice's item binds no name`}, {"bound outside the repeat too", map[string]string{"word": `["a","b"]`, "card": `{"format":"{/word as w}{x}","x":{"format":"{/word as w}{w}","repeat":2}}`}, - `name "w" is bound outside this repeat already`}, + `name "w" is bound outside this repeat too`}, {"a field too", map[string]string{"word": `["a","b"]`, "card": `{"format":"{/word as w}{w}","w":"x"}`}, `name "w" is a field of the root template too`}, {"an option", map[string]string{"word": `["a","b"]`, "card": `"{/word as format}{format}"`}, @@ -188,6 +188,14 @@ func TestNameErrors(t *testing.T) { `"word" is no reference`}, {"read by nothing", map[string]string{"word": `["a","b"]`, "card": `"{/word as w}"`}, `nothing reads name "w"`}, + {"read once through a path", map[string]string{"word": `{"format":"{w}","w":["a","b"]}`, "card": `"{/word as n}{n.w}"`}, + `write {/word.w}`}, + {"read in two draw groups", map[string]string{"nm.json": `{"format":"{first}","rows":"nm.tsv"}`, "nm.tsv": "first\tlast\nAda\tByron\nBo\tEk\n", "person": `{"format":"{first} {last}","first":"{/nm.first}","last":"{/nm.last}"}`, "card": `{"format":"{b}|{a}","a":"{/person as p}{p}","b":{"format":"{p.first}","drawGroup":"g"}}`}, + `name "p" is read in two draw groups`}, + {"read in a repeat", map[string]string{"nm.json": `{"format":"{first}","rows":"nm.tsv"}`, "nm.tsv": "first\tlast\nAda\tByron\nBo\tEk\n", "person": `{"format":"{first} {last}","first":"{/nm.first}","last":"{/nm.last}"}`, "card": `{"format":"{/person as p}{p}{each}","each":{"format":"{p.first}","repeat":2}}`}, + `name "p" is read inside a repeat`}, + {"a misspelt name", map[string]string{"word": `["a","b"]`, "card": `"{/word as p}{p}{p}{pp}"`}, + `no field or name "pp"; the names bound here are [p]`}, {"read once whole", map[string]string{"word": `["a","b"]`, "card": `{"format":"{/word as w}{x}","x":"{w}"}`}, `write {/word}`}, {"rendered twice where a path reads it", map[string]string{"word": `{"format":"{w}-{w}","w":["a","b"]}`, "card": `"{/word as n}{n}|{n.w}"`}, @@ -201,13 +209,11 @@ func TestNameErrors(t *testing.T) { {"in a column's name", map[string]string{"t.json": `{"format":"{k}","rows":"t.tsv","key":"k"}`, "t.tsv": "k\ta as b\nx\t1\ny\t2\n"}, `contains " as "`}, {"read outside the repeat binding it", map[string]string{"word": `["a","b"]`, "card": `{"format":"{x}{w}","x":{"format":"{/word as w}{w}","repeat":2}}`}, - `name "w" is bound inside the repeat at field "x"`}, - {"unbound", map[string]string{"word": `["a","b"]`, "card": `{"format":"{x}{w}","x":{"format":"{/word as w}{w}","repeat":2}}`}, - `no field "w"`}, + `no field "w"; name "w" is bound inside the repeat at field "x"`}, {"padded", map[string]string{"word": `["a","b"]`, "card": `"{/word as w}{w}"`}, `write {/word as w}`}, - {"selector after a name", map[string]string{"region.json": nameTables()["region.json"], "card": `"{/region as r}{r.municipality[0180].name}"`}, - `a path through name "r" selects no row`}, + {"selector after a name", map[string]string{"region.json": nameTables()["region.json"], "region.tsv": nameTables()["region.tsv"], "municipality.json": nameTables()["municipality.json"], "municipality.tsv": nameTables()["municipality.tsv"], "card": `"{/region as r}{r.municipality[0180].name}"`}, + `read it directly, {/region.municipality[0180].name}`}, {"in a table's format", map[string]string{"word": `["a","b"]`, "t.json": `{"format":"{/word as w}{w}","rows":"t.tsv","key":"k"}`, "t.tsv": "k\nx\ny\n"}, "a table binds no name"}, {"in a field's name", map[string]string{"card": `{"format":"{a as b}","a as b":"x"}`}, @@ -222,11 +228,6 @@ func TestNameErrors(t *testing.T) { } files[name] = body } - if c.name == "selector after a name" { - for k, v := range nameTables() { - files[k] = v - } - } _, err := New(WithoutShippedData(), WithDataPath(writeFiles(t, files))) if err == nil || !strings.Contains(err.Error(), c.want) { t.Fatalf("New() = %v, want an error holding %q", err, c.want) diff --git a/perf_test.go b/perf_test.go index 92cc738..178ffc7 100644 --- a/perf_test.go +++ b/perf_test.go @@ -64,7 +64,7 @@ func TestNoReferenceAllocRegression(t *testing.T) { }{ {"a repeat of a reference path", `{"format":"{r}","r":{"format":"{/word.w}","repeat":20,"separator":", "}}`, 66}, {"a named draw group", `{"format":"{a}","a":{"format":"{/word.w}","drawGroup":"g"}}`, 11}, - {"a repeat of a name", `{"format":"{r}","r":{"format":"{/word as n}{n.w}","repeat":20,"separator":", "}}`, 205}, + {"a repeat of a name", `{"format":"{r}","r":{"format":"{/word as n}{n.w}{n.w}","repeat":20,"separator":", "}}`, 205}, } { f, err := New(WithoutShippedData(), WithDataFS(fstest.MapFS{ "word.json": {Data: []byte(word)},