From 75849b2f36952bf88ebc0ba0c90450a08d8cd6b3 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 1 Aug 2020 16:19:37 +0000 Subject: [PATCH] testing selections a bit --- const_test.go | 33 ++++++++++++++ node.go | 6 ++- node_test.go | 9 ++++ selection.go | 16 +++++++ selection_test.go | 110 ++++++++++++++++++++++++++++++++++++++++++---- 5 files changed, 163 insertions(+), 11 deletions(-) create mode 100644 const_test.go create mode 100644 node_test.go diff --git a/const_test.go b/const_test.go new file mode 100644 index 0000000..bb14689 --- /dev/null +++ b/const_test.go @@ -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") +) diff --git a/node.go b/node.go index 12d5272..d6a1141 100644 --- a/node.go +++ b/node.go @@ -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 diff --git a/node_test.go b/node_test.go new file mode 100644 index 0000000..f2c4657 --- /dev/null +++ b/node_test.go @@ -0,0 +1,9 @@ +package tea + +import ( + "testing" +) + +func TestXNodeDot(t *testing.T) { + +} diff --git a/selection.go b/selection.go index 4a96f12..389e661 100644 --- a/selection.go +++ b/selection.go @@ -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) { + +} diff --git a/selection_test.go b/selection_test.go index ebcc8b7..3041ac6 100644 --- a/selection_test.go +++ b/selection_test.go @@ -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 +} + +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 count := test.selection.countXNodes(); count != test.selXNodes { + t.Errorf("expected %d xnode in lnode, saw %d", test.selXNodes, count) } +} - 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 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, + }, } - if len(l.xnodes) != 1 { - t.Fatalf("expected 1 xnode in lnode, saw %d", len(l.xnodes)) + 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) } }