testing selections a bit

selection
Jordan Orelli 4 years ago
parent 8659b230c8
commit 75849b2f36

@ -0,0 +1,33 @@
package tea
// these constant tests are useful for testing things that manipulate the
// graph.
const (
A = Passing("A")
B = Passing("B")
C = Passing("C")
D = Passing("D")
E = Passing("E")
F = Passing("F")
G = Passing("G")
H = Passing("H")
I = Passing("I")
J = Passing("J")
K = Passing("K")
L = Passing("L")
M = Passing("M")
N = Passing("N")
O = Passing("O")
P = Passing("P")
Q = Passing("Q")
R = Passing("R")
S = Passing("S")
T = Passing("T")
U = Passing("U")
V = Passing("V")
W = Passing("W")
X = Passing("X")
Y = Passing("Y")
Z = Passing("Z")
)

@ -20,14 +20,15 @@ type lnode struct {
}
// child adds an lnode as a child of the receiver. For every xnode in this
// lnode, the child lnode has a component xnode whose parent is the
// receiver, the child lnode has a component xnode whose parent is the
// corresponding xnode in this lnode.
func (l *lnode) child(c *lnode, t Test) {
c.parents = append(c.parents, l)
l.children = append(l.children, c)
for _, x := range l.xnodes {
for i, x := range l.xnodes {
xchild := x.child(t)
xchild.lnode = c
xchild.id = [2]int{l.id, i + 1}
c.xnodes = append(c.xnodes, xchild)
}
}
@ -36,6 +37,7 @@ func (l *lnode) child(c *lnode, t Test) {
// to be executed. xnode is the unit test in tea. every xnode is either
// unparented or has one parent.
type xnode struct {
id [2]int
lnode *lnode
test Test
parent *xnode

@ -0,0 +1,9 @@
package tea
import (
"testing"
)
func TestXNodeDot(t *testing.T) {
}

@ -1,5 +1,9 @@
package tea
import (
"io"
)
func NewSelection(test Test) Selection {
x := xnode{
test: clone(test),
@ -42,3 +46,15 @@ func (s Selection) And(other Selection) Selection {
return Selection{nodes: out}
}
func (s Selection) countXNodes() int {
total := 0
for _, child := range s.nodes {
total += len(child.xnodes)
}
return total
}
func (s Selection) writeDOT(w io.Writer) {
}

@ -4,18 +4,110 @@ import (
"testing"
)
func TestNewSelection(t *testing.T) {
s := NewSelection(Passing("A"))
if len(s.nodes) != 1 {
t.Fatalf("expected 1 node in new selection, saw %d", len(s.nodes))
type selectionTest struct {
label string
selection Selection
selLNodes int // number of lnodes in the selection
selXNodes int // number of xnodes in the selection
reachLNodes int // number of lnodes reachable by the selection
reachXnodes int // number of xnodes reachable by the selection
}
l := s.nodes[0]
if len(l.children) != 0 {
t.Fatalf("new selection should not have any children, but has %d", len(l.children))
func (test *selectionTest) Run(t *testing.T) {
if count := len(test.selection.nodes); count != test.selLNodes {
t.Errorf("expected %d node in selection, saw %d", test.selLNodes, count)
}
if len(l.xnodes) != 1 {
t.Fatalf("expected 1 xnode in lnode, saw %d", len(l.xnodes))
if count := test.selection.countXNodes(); count != test.selXNodes {
t.Errorf("expected %d xnode in lnode, saw %d", test.selXNodes, count)
}
}
func TestSelections(t *testing.T) {
tests := []selectionTest{
{
label: "new selection",
selection: NewSelection(A),
selLNodes: 1,
selXNodes: 1,
},
{
label: "root with one child",
selection: NewSelection(A).Child(B),
selLNodes: 1,
selXNodes: 1,
},
{
label: "two selected roots",
selection: NewSelection(A).And(NewSelection(B)),
selLNodes: 2,
selXNodes: 2,
},
}
add := func(fn func() selectionTest) { tests = append(tests, fn()) }
add(func() selectionTest {
root := NewSelection(A)
b := root.Child(B)
return selectionTest{
label: "root and child selected",
selection: root.And(b),
selLNodes: 2,
selXNodes: 2,
}
})
add(func() selectionTest {
root := NewSelection(A)
b := root.Child(B)
return selectionTest{
label: "an optional test",
selection: root.And(b).Child(C),
selLNodes: 1,
selXNodes: 2,
}
})
add(func() selectionTest {
root := NewSelection(A)
b := root.Child(B)
c := root.Child(C)
return selectionTest{
label: "two children selected",
selection: b.And(c),
selLNodes: 2,
selXNodes: 2,
}
})
add(func() selectionTest {
root := NewSelection(A)
b := root.Child(B)
c := root.Child(C)
return selectionTest{
label: "a diamond test",
selection: b.And(c).Child(D),
selLNodes: 1,
selXNodes: 2,
}
})
add(func() selectionTest {
root := NewSelection(A)
b := root.Child(B)
c := root.Child(C)
d := b.And(c).Child(D)
return selectionTest{
label: "child of a node having multiple parents",
selection: d.Child(E),
selLNodes: 1,
selXNodes: 2,
}
})
for _, test := range tests {
t.Run(test.label, test.Run)
}
}

Loading…
Cancel
Save