From 51b9385aee08698cde2c5a3e39b91e49ab690965 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Thu, 17 Sep 2026 12:28:49 +0200 Subject: [PATCH] Adjust the fixtures the table fence refuses, and pair README table examples with their TSV and parent --- cmd/fejkdata/main_test.go | 4 ++-- readme_test.go | 27 ++++++++++++++++++++++----- table_test.go | 8 ++++---- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/cmd/fejkdata/main_test.go b/cmd/fejkdata/main_test.go index a195472..d591992 100644 --- a/cmd/fejkdata/main_test.go +++ b/cmd/fejkdata/main_test.go @@ -387,11 +387,11 @@ func TestRunInlineTemplate(t *testing.T) { func TestRunTemplateMisuse(t *testing.T) { for arg, want := range map[string]string{ "{bad": "unterminated", - "[red,green]": "names no template either", + "[red,green]": `starts with "["`, `{"format":"x"}`: "is a string", "name: {/no.such.path}": "no entry", "{/sv_SE.person}": "write sv_SE.person", - "x[1]": "names no template either", + "x[1]y": `"]"`, ` ["a","b"] `: "may not be padded", } { code, out, errb := runOut("--seed", "1", arg) diff --git a/readme_test.go b/readme_test.go index 6283ab9..c253170 100644 --- a/readme_test.go +++ b/readme_test.go @@ -9,13 +9,15 @@ import ( ) var ( - jsonBlock = regexp.MustCompile("(?s)```json\n(.*?)```") - tsvBlock = regexp.MustCompile("(?s)```tsv\n(.*?)```") - rowsFile = regexp.MustCompile(`"rows":\s*"([^"]+)"`) + jsonBlock = regexp.MustCompile("(?s)```json\n(.*?)```") + tsvBlock = regexp.MustCompile("(?s)```tsv\n(.*?)```") + rowsFile = regexp.MustCompile(`"rows":\s*"([^"]+)"`) + parentName = regexp.MustCompile(`"parent":\s*"([^"]+)"`) ) -// exampleFiles is a README json block as a data directory's files: the category, and -// the rows TSV it names, taken from the nearest tsv block above it. +// exampleFiles is a README json block as a data directory's files: the category, the +// rows TSV it names, taken from the nearest tsv block above it, and the parent table it +// names, taken from the nearest json block above it whose rows file is the parent's. func exampleFiles(t *testing.T, src string, at int, body string) map[string]string { t.Helper() files := map[string]string{"example.json": body} @@ -26,6 +28,21 @@ func exampleFiles(t *testing.T, src string, at int, body string) map[string]stri } files[m[1]] = tsv[len(tsv)-1][1] } + if m := parentName.FindStringSubmatch(body); m != nil { + blocks := jsonBlock.FindAllStringSubmatchIndex(src[:at], -1) + for i := len(blocks) - 1; i >= 0; i-- { + parent := src[blocks[i][2]:blocks[i][3]] + if strings.Contains(parent, `"rows": "`+m[1]+`.tsv"`) { + for name, content := range exampleFiles(t, src, blocks[i][0], parent) { + if name == "example.json" { + name = m[1] + ".json" + } + files[name] = content + } + break + } + } + } return files } diff --git a/table_test.go b/table_test.go index 54392fa..9d77e26 100644 --- a/table_test.go +++ b/table_test.go @@ -131,7 +131,7 @@ func TestTableCellsAreStringNodes(t *testing.T) { } bad := writeFiles(t, map[string]string{ "place.json": `{"format":"{name}","rows":"place.tsv"}`, - "place.tsv": "name\tzip\nA\t{name}\n", + "place.tsv": "name\tzip\nA\t{name}\nB\t2\n", }) if _, err := New(WithoutShippedData(), WithDataPath(bad)); err == nil || !strings.Contains(err.Error(), "place.tsv") || !strings.Contains(err.Error(), `no field "name"`) { t.Fatalf("New = %v, want a cell reading a column refused, naming the file", err) @@ -413,7 +413,7 @@ func TestTableFences(t *testing.T) { func TestSameShapedChoiceIsATable(t *testing.T) { rows := `[{"format":"{name}","name":"Sweden","alpha2":"SE"},{"format":"{name}","name":"Norway","alpha2":"NO"},{"format":"{name}","name":"Denmark","alpha2":"DK"}]` _, err := New(WithoutShippedData(), WithDataPath(writeData(t, map[string]string{"country": rows}))) - for _, want := range []string{"country.tsv", `"rows"`, "alpha2\tname", "3 "} { + for _, want := range []string{"country.tsv", `"rows"`, `alpha2\tname`, "3 "} { if err == nil || !strings.Contains(err.Error(), want) { t.Fatalf("New(same-shaped choice) = %v, want it refused mentioning %q", err, want) } @@ -446,12 +446,12 @@ func TestTableInAStructTag(t *testing.T) { var v struct { Region string `fake:"region[12].name"` Locality string `fake:"region[12].locality.code"` - Any string `fake:"{/municipality[Lund].code}"` + Any string `fake:"{/region[Skåne län].timezone}-x"` // the row region[12] names, by name } if err := f.FakeStruct(&v); err != nil { t.Fatal(err) } - if v.Region != "Skåne län" || regionOf[municipalityOf[v.Locality]] != "12" || v.Any != "1281" { + if v.Region != "Skåne län" || regionOf[municipalityOf[v.Locality]] != "12" || v.Any != "Europe/Stockholm-x" { t.Fatalf("FakeStruct = %+v, want the selected rows", v) } }