Add repeat/separator templates; validate at compile time; support Go 1.22+ via GO_VERSION

This commit is contained in:
lilleman
2026-06-08 21:11:58 +02:00
parent 72154502d7
commit 3e1cd3ee2a
8 changed files with 241 additions and 139 deletions
+3 -23
View File
@@ -22,7 +22,7 @@ func TestEscapeEdgeCases(t *testing.T) {
`{"format":"x}y"}`: "x}y", // an unmatched } is literal (x, y aren't classes)
}
for tmpl, want := range cases {
if got := render(t, f, tmpl); got != want {
if got := mustRender(t, f, tmpl); got != want {
t.Errorf("render(%s) = %q, want %q", tmpl, got, want)
}
}
@@ -31,7 +31,7 @@ func TestEscapeEdgeCases(t *testing.T) {
func TestMultibyteFormat(t *testing.T) {
// Scanning is rune-aware: multibyte literals coexist with class chars and
// tokens without corrupting indices.
got := render(t, engine(2), `{"format":"Öster{x}-0å","x":["väg"]}`)
got := mustRender(t, engine(2), `{"format":"Öster{x}-0å","x":["väg"]}`)
if !regexp.MustCompile(`^Österväg-[0-9]å$`).MatchString(got) {
t.Fatalf("multibyte format = %q", got)
}
@@ -41,19 +41,13 @@ func TestAlternationThreeWay(t *testing.T) {
f := engine(4)
seen := map[string]bool{}
for i := 0; i < 200; i++ {
seen[render(t, f, `{"format":"{a|b|c}","a":["A"],"b":["B"],"c":["C"]}`)] = true
seen[mustRender(t, f, `{"format":"{a|b|c}","a":["A"],"b":["B"],"c":["C"]}`)] = true
}
if !seen["A"] || !seen["B"] || !seen["C"] || len(seen) != 3 {
t.Fatalf("3-way alternation produced %v, want A, B and C", seen)
}
}
func TestEmptyChoiceErrors(t *testing.T) {
if _, err := engine(1).render(compiled(t, `[]`)); err == nil {
t.Fatal("render([]) = nil error, want empty-choice error")
}
}
func TestNewErrors(t *testing.T) {
// Pointing New at a file (not a directory) fails.
file := filepath.Join(t.TempDir(), "xx_XX")
@@ -69,20 +63,6 @@ func TestNewErrors(t *testing.T) {
}
}
func TestFakeSurfacesErrors(t *testing.T) {
f := engine(1)
f.categories = map[string]node{
"bad": compiled(t, `[{"format":"{y}","x":["Q"]}]`), // token names a missing field -> render error
"empty": compiled(t, `[]`), // empty choice met on a path -> descend error
}
if _, err := f.Fake("bad"); err == nil {
t.Error("Fake(bad) = nil error, want render error")
}
if _, err := f.Fake("empty.foo"); err == nil {
t.Error("Fake(empty.foo) = nil error, want empty-choice error")
}
}
// --- deep path navigation ---
func TestDeepDottedPath(t *testing.T) {