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
+17 -6
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,27 +130,37 @@ 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{
"three bad references": {
"a": `{"format":"{..nope.one}"}`, "a": `{"format":"{..nope.one}"}`,
"b": `{"format":"{..nope.two}"}`, "b": `{"format":"{..nope.two}"}`,
"c": `{"format":"{..nope.three}"}`, "c": `{"format":"{..nope.three}"}`,
}) },
"two bad fields in one template": {
"cat": `{"format":"hi","aaa":{"no":1},"zzz":{"no":2}}`,
},
}
for name, files := range cases {
dir := writeData(t, files)
var first string var first string
for i := 0; i < 50; i++ { for i := 0; i < 50; i++ {
_, err := New([]string{dir}) _, err := New([]string{dir})
if err == nil { if err == nil {
t.Fatal("New = nil error, want a reference error") t.Fatalf("%s: New = nil error, want a load error", name)
} }
if i == 0 { if i == 0 {
first = err.Error() first = err.Error()
continue continue
} }
if err.Error() != first { if err.Error() != first {
t.Fatalf("New error varies between runs:\n %s\n %s", first, err.Error()) t.Fatalf("%s: New error varies between runs:\n %s\n %s", name, first, err.Error())
} }
} }
t.Logf("%s -> %s", name, first)
}
} }
// TestNewErrorPathIsCanonical pins the node path a load error names: a choice arm // TestNewErrorPathIsCanonical pins the node path a load error names: a choice arm