Adjust the fixtures the table fence refuses, and pair README table examples with their TSV and parent
This commit is contained in:
@@ -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)
|
||||
|
||||
+19
-2
@@ -12,10 +12,12 @@ var (
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user