Tests that pin the reserved-prefix guards and per-field error order

This commit is contained in:
Mikael Göransson
2026-08-28 00:08:43 +02:00
committed by lilleman-tw
parent e14f3a4145
commit 095a7f2de5
+30 -19
View File
@@ -105,7 +105,8 @@ func TestReferenceErrors(t *testing.T) {
// ".." is reserved for bound references, so an authored key using it would // ".." is reserved for bound references, so an authored key using it would
// name a node nothing can reach and nothing would validate. // name a node nothing can reach and nothing would validate.
"field key using the reference prefix": {"cat": `{"format":"hi","..x":{"format":"{..nope}"}}`}, "field key using the reference prefix": {"cat": `{"format":"hi","..x":{"format":"{..nope}"}}`},
"category name using the reference prefix": {"..bad": `{"format":"{..nope}"}`}, "category name using the reference prefix": {"sv_SE/..bad": `{"format":"{..nope}"}`},
"folder name using the reference prefix": {"sv_SE/..y/cat": `{"format":"{..nope}"}`},
} }
for name, files := range cases { for name, files := range cases {
if _, err := New([]string{writeData(t, files)}); err == nil { if _, err := New([]string{writeData(t, files)}); err == nil {
@@ -129,26 +130,36 @@ func TestReferenceFromUnrenderedFieldTerminates(t *testing.T) {
} }
// TestNewErrorIsDeterministic pins one message per broken data set: map iteration // TestNewErrorIsDeterministic pins one message per broken data set: map iteration
// order must not decide which of several problems the user is told about. // order must not decide which of several problems the user is told about, whether
// they sit in separate categories or in one template's fields.
func TestNewErrorIsDeterministic(t *testing.T) { func TestNewErrorIsDeterministic(t *testing.T) {
dir := writeData(t, map[string]string{ cases := map[string]map[string]string{
"a": `{"format":"{..nope.one}"}`, "three bad references": {
"b": `{"format":"{..nope.two}"}`, "a": `{"format":"{..nope.one}"}`,
"c": `{"format":"{..nope.three}"}`, "b": `{"format":"{..nope.two}"}`,
}) "c": `{"format":"{..nope.three}"}`,
var first string },
for i := 0; i < 50; i++ { "two bad fields in one template": {
_, err := New([]string{dir}) "cat": `{"format":"hi","aaa":{"no":1},"zzz":{"no":2}}`,
if err == nil { },
t.Fatal("New = nil error, want a reference error") }
} for name, files := range cases {
if i == 0 { dir := writeData(t, files)
first = err.Error() var first string
continue for i := 0; i < 50; i++ {
} _, err := New([]string{dir})
if err.Error() != first { if err == nil {
t.Fatalf("New error varies between runs:\n %s\n %s", first, err.Error()) t.Fatalf("%s: New = nil error, want a load error", name)
}
if i == 0 {
first = err.Error()
continue
}
if err.Error() != first {
t.Fatalf("%s: New error varies between runs:\n %s\n %s", name, first, err.Error())
}
} }
t.Logf("%s -> %s", name, first)
} }
} }