Files
fejkdata/readsurvey_test.go
T

139 lines
4.0 KiB
Go

package fejkdata
import (
"strings"
"testing"
"testing/fstest"
)
func TestEveryReadARenderMakesIsGathered(t *testing.T) {
f := fenceCorpus(t)
roots, tables := fenceRoots(t, f)
traced := 0
for _, root := range roots {
traced += wantGathered(t, tables, root.label, surveyRender(root.t).reads, func(trace renderTrace) { renderRoot(f.rand, root.t, trace) })
if !root.t.isRecord || len(root.t.fields) == 0 {
continue
}
_, columns, err := recordOf(root.t)
if err != nil {
t.Fatalf("%s: recordOf = %v", root.label, err)
}
traced += wantGathered(t, tables, root.label+" as a record", surveyColumns(root.t, sortedNames(root.t.fields)).reads, func(trace renderTrace) { renderRecordRoot(f.rand, root.t, columns, trace) })
}
if traced == 0 {
t.Error("no render traced a reference read, so the comparison proved nothing")
}
}
func wantGathered(t *testing.T, tables map[string]*table, label string, gathered []pathRead, renderWith func(renderTrace)) int {
t.Helper()
var reads []pathRead
type traceKey struct {
group, path, table string
row int
}
seen := map[traceKey]bool{}
trace := func(group, table string, row int, a arm) {
if !isRef(a.head) {
return
}
r := pathRead{at: surveyAt{group: group}, a: a}
if table != "" {
r.at.pins.add(tables[table], row)
}
if k := (traceKey{group, a.path, table, row}); !seen[k] {
seen[k] = true
reads = append(reads, r)
}
}
for i := 0; i < 5; i++ {
renderWith(trace)
}
for _, r := range reads {
if !gathers(gathered, r) {
t.Errorf("%s: the render read {%s} in draw group %q from %s, and readSurvey gathered no such read", label, r.a.spelling, r.at.group, spellPins(&r.at.pins))
}
}
return len(reads)
}
func gathers(gathered []pathRead, r pathRead) bool {
for _, g := range gathered {
if g.at.group == r.at.group && g.a.path == r.a.path && !g.at.pins.differs(&r.at.pins) && !g.at.wholePins.differs(&r.at.pins) {
return true
}
}
return false
}
func spellPins(p *pinSet) string {
var rows []string
p.each(func(t *table, r int) { rows = append(rows, t.selectorSpelling(r)) })
if len(rows) == 0 {
return "no row"
}
return "row " + strings.Join(rows, ", ")
}
func renderRoot(s *session, t *template, trace renderTrace) {
set := holdSet{trace: trace}
sc := renderScope{set: &set}
switch {
case t.site.row == formatRow:
tbl := t.site.table
render(s, tbl, sc)
for r := 0; r < tbl.rowCount(); r++ {
set := holdSet{trace: trace}
sc := renderScope{set: &set}
sc.groupHold().pins.pin(tbl, r)
render(s, tbl.rowNode, sc.at(tbl.rowNode, &sc.groupHold().pins))
}
case t.repeat > 1:
expand(s, t, sc)
default:
render(s, t, sc)
}
}
func renderRecordRoot(s *session, t *template, columns []Column, trace renderTrace) {
set := holdSet{trace: trace}
sc := renderScope{set: &set}
if t.site.row == formatRow {
t.site.table.drawIn(s, &sc.groupHold().pins)
}
renderRecord(s, t, columns, sc)
}
func TestReadSurveyWalksACellByEveryRoute(t *testing.T) {
fsys := fstest.MapFS{
"g.json": {Data: []byte(`{"format":"{/x.v}","drawGroup":"g2"}`)},
"r.json": {Data: []byte(`"{/g} {/x}"`)},
"x.json": {Data: []byte(`{"format":"{/g}","rows":"x.tsv","key":"code"}`)},
"x.tsv": {Data: []byte("code\tv\n1\t{/z}\n2\t{/z.a}\n")},
"z.json": {Data: []byte(`{"format":"{a}","a":"hi"}`)},
}
g, err := loadDir(dataSource{fsys: fsys}, ".")
if err != nil {
t.Fatalf("loadDir = %v", err)
}
if err := treeBinding(g.children).link(); err != nil {
t.Fatalf("link = %v", err)
}
r, isTemplate := g.children["r"].(*template)
if !isTemplate {
t.Fatalf("r is a %T, want a template", g.children["r"])
}
var pinned, whole bool
for _, read := range surveyRender(r).reads {
if read.a.path != "/z" {
continue
}
pinned = pinned || read.at.pins.used > 0
whole = whole || read.at.wholePins.used > 0
}
if !pinned || !whole {
t.Errorf("{/z} gathered under a pinned row %v and a whole read's row %v, want both: {/g} reaches x's cells from the root and from inside {/x}", pinned, whole)
}
}