From 21a4ed17957f31bfdd1117e39169b22c996f0544 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Mon, 5 Oct 2026 21:03:29 +0200 Subject: [PATCH] Test that a table's errors name its path, and a cell's its line --- table_test.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/table_test.go b/table_test.go index 6f850bc..c25f2f7 100644 --- a/table_test.go +++ b/table_test.go @@ -1152,3 +1152,36 @@ func TestDatatypeRefusesARowRead(t *testing.T) { } } } + +func TestATableErrorNamesItsPathAndACellItsLine(t *testing.T) { + files := map[string]string{ + "g/t.json": `{"format":"{a}","rows":"t.tsv","key":"a"}`, + "g/t.tsv": "a\tb\nx\t-\ny\t{/nope}\n", + } + want := "g.t.b, line 3: reference {/nope}" + if _, err := New(WithoutShippedData(), WithDataPath(writeFiles(t, files))); err == nil || !strings.Contains(err.Error(), want) { + t.Errorf("a cell's bad reference: New = %v, want it to contain %q", err, want) + } + files["g/t.json"], files["g/t.tsv"] = `{"format":"{a} {/nope}","rows":"t.tsv","key":"a"}`, "a\tb\nx\t-\ny\t-\n" + want = "g.t: reference {/nope}" + if _, err := New(WithoutShippedData(), WithDataPath(writeFiles(t, files))); err == nil || !strings.Contains(err.Error(), want) { + t.Errorf("a format's bad reference: New = %v, want it to contain %q", err, want) + } + f := newGenerator(t, writeFiles(t, geo()), WithSeed(1)) + for path, want := range map[string]string{ + "region[99]": `no row of region has key or name "99"`, + "locality[Sandby]": `"Sandby" names 2 rows of locality; select one by key, one of L7, L8, or select it inside its municipality`, + } { + if _, err := f.Fake(path); err == nil || !strings.Contains(err.Error(), want) { + t.Errorf("Fake(%s) = %v, want it to contain %q", path, err, want) + } + } + shipped, err := New(WithSeed(1)) + if err != nil { + t.Fatal(err) + } + want = `no row of misc.territory has key or name "ZZZ"` + if _, err := shipped.Fake("misc.territory[ZZZ]"); err == nil || !strings.Contains(err.Error(), want) { + t.Errorf("Fake(misc.territory[ZZZ]) = %v, want it to contain %q", err, want) + } +}