clean up some redundant test code

selection
Jordan Orelli 4 years ago
parent 500b02db34
commit 31fa2eb711

@ -0,0 +1,48 @@
package tea
import "testing"
type labelChecker struct {
name string
wanted map[string]bool // all the strings we want
found map[string]bool // all the strings we've found
}
func wantStrings(name string, want ...string) labelChecker {
l := newLabelChecker(name)
l.want(want...)
return l
}
func newLabelChecker(name string) labelChecker {
return labelChecker{
name: name,
wanted: make(map[string]bool),
found: make(map[string]bool),
}
}
func (l *labelChecker) want(names ...string) {
for _, name := range names {
l.wanted[name] = true
}
}
func (l *labelChecker) add(name string) {
l.found[name] = true
}
func (l *labelChecker) report(t *testing.T) {
for name, _ := range l.found {
if l.wanted[name] {
t.Logf("%s saw expected value %s", l.name, name)
} else {
t.Errorf("%s saw unexpected value %s", l.name, name)
}
}
for name, _ := range l.wanted {
if !l.found[name] {
t.Errorf("%s missing expected value %s", l.name, name)
}
}
}

@ -37,6 +37,7 @@ func newLNode(test Test, sel Selection) *lnode {
test: test, test: test,
parents: make([]*lnode, len(sel.nodes)), parents: make([]*lnode, len(sel.nodes)),
} }
// not sure if this copy is necessary
copy(node.parents, sel.nodes) copy(node.parents, sel.nodes)
xID := 0 xID := 0
@ -86,6 +87,9 @@ func (x *xnode) isOnlyTestInLNode() bool {
return len(x.lnode.xnodes) == 1 return len(x.lnode.xnodes) == 1
} }
// label must be unique or some other shit will break, I'm using this as a way
// to globally identify xnodes, which may be very flawed and maybe I should
// have an actual global ID system.
func (x *xnode) label() string { func (x *xnode) label() string {
if x.parent == nil { if x.parent == nil {
switch { switch {
@ -113,7 +117,8 @@ func (x *xnode) label() string {
} }
// ancestry gives a slice of xnodes beginning at the root of the x graph and // ancestry gives a slice of xnodes beginning at the root of the x graph and
// terminating at the receiver xnode. // terminating at the receiver xnode. The ancestry list of a leaf node in the x
// graph is a single chain of tests.
func (x *xnode) ancestry() []*xnode { func (x *xnode) ancestry() []*xnode {
if x.parent == nil { if x.parent == nil {
return []*xnode{x} return []*xnode{x}
@ -135,3 +140,19 @@ func (x *xnode) descendents() []*xnode {
} }
return descendents return descendents
} }
// leaves descends the x graph from the receiver xnode, returning a slice
// containing all of the leaves of the x graph having the receiver x as an
// ancestor.
func (x *xnode) leaves() []*xnode {
if len(x.children) == 0 {
return []*xnode{x}
}
var leaves []*xnode
for _, child := range x.children {
leaves = append(leaves, child.leaves()...)
}
return leaves
}

@ -47,3 +47,27 @@ func (s Selection) countXNodes() int {
} }
return total return total
} }
// xleaves looks at all of the selected xnodes, and for every selected xnode,
// traverses the x graph until we arrive at the set of all leaf nodes that have
// a selected ancestor. If the selection consists of the root node, the xleaves
// are all of the leaves of the x graph.
func (s *Selection) xleaves() []*xnode {
// honestly think that by definition every xnode in the selection has a
// non-overlapping set of leaves but thinking about this shit is extremely
// starting to hurt my brain so I'm going to write this in a way that's
// maybe very redundant.
seen := make(map[string]bool)
var leaves []*xnode
for _, x := range s.xnodes() {
for _, leaf := range x.leaves() {
if seen[leaf.label()] {
panic("double-counting leaves somehow")
}
seen[leaf.label()] = true
leaves = append(leaves, leaf)
}
}
return leaves
}

@ -9,56 +9,31 @@ type selectionTest struct {
selection Selection selection Selection
lnodes []string lnodes []string
xnodes []string xnodes []string
xleaves []string
} }
func (test *selectionTest) Run(t *testing.T) { func (test *selectionTest) Run(t *testing.T) {
lfound := make(map[string]bool) LWanted := wantStrings("selected lnode names", test.lnodes...)
for _, L := range test.selection.nodes { for _, L := range test.selection.nodes {
if len(L.parents) > 0 { LWanted.add(L.name)
pnames := make([]string, 0, len(L.parents))
for _, p := range L.parents {
pnames = append(pnames, p.name)
}
t.Logf("found lnode with label %s having %d parents: %s", L.name, len(L.parents), pnames)
} else {
t.Logf("found root lnode with label %s", L.name)
}
lfound[L.name] = true
} }
LWanted.report(t)
for _, expected := range test.lnodes { XWanted := wantStrings("selected xnode labels", test.xnodes...)
if lfound[expected] { for _, X := range test.selection.xnodes() {
delete(lfound, expected) XWanted.add(X.label())
} else {
t.Errorf("missing expected lnode with label %s", expected)
}
} }
XWanted.report(t)
for label, _ := range lfound { // XLeavesWanted := wantStrings("leaf xnode labels", test.xleaves...)
t.Errorf("found unexpected lnode with label %s", label) // for _, X := range test.selection.xnodes() {
} // leaves := X.leaves()
// for _, leaf := range leaves {
xfound := make(map[string]bool) // XLeavesWanted.
for _, x := range test.selection.xnodes() {
if x.parent != nil {
t.Logf("found xnode with label %s having parent %s", x.label(), x.parent.label())
} else {
t.Logf("found root xnode with label %s", x.label())
}
xfound[x.label()] = true
}
for _, expected := range test.xnodes { // }
if xfound[expected] {
delete(xfound, expected)
} else {
t.Errorf("missing expected xnode with label %s", expected)
}
}
for label, _ := range xfound { // }
t.Errorf("found unexpected xnode with label %s", label)
}
} }
func TestSelections(t *testing.T) { func TestSelections(t *testing.T) {
@ -145,6 +120,20 @@ func TestSelections(t *testing.T) {
} }
}) })
add(func() selectionTest {
root := NewSelection(A)
b := root.Child(B)
c := root.Child(C)
d := b.And(c).Child(D)
d.Child(E)
return selectionTest{
label: "the root of a complex graph",
selection: root,
lnodes: []string{"A"},
xnodes: []string{"A.0"},
}
})
for _, test := range tests { for _, test := range tests {
t.Run(test.label, test.Run) t.Run(test.label, test.Run)
} }

Loading…
Cancel
Save