-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
96 lines (79 loc) · 2.24 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"bufio"
"io/ioutil"
"os"
"path"
"strings"
"testing"
)
func assertNotNil(t *testing.T, n *node) {
if n == nil {
t.Error("exptected to find a node, instead got nil")
}
}
func assertIs(t *testing.T, n1, n2 *node) {
assertNotNil(t, n1)
if n1 != n2 {
t.Errorf("exptected to find a node with name \"%s\", instead found node with name \"%s\"", n2.name, n1.name)
}
}
func TestFindChildWithName(t *testing.T) {
root := newNode("root", nil, []*node{})
n1 := newNode("node1", root, []*node{})
n2 := newNode("node2", root, []*node{})
root.children = append(root.children, n1, n2)
n := root.findChildWithName("node1")
assertIs(t, n, n1)
n = root.findChildWithName("node2")
assertIs(t, n, n2)
}
func TestInsertDoesNotDuplicateChildren(t *testing.T) {
root := newNode("root", nil, []*node{})
root.insert("src")
root.insert("src")
if len(root.children) != 1 {
t.Errorf("expected root to have 1 child, got %d children", len(root.children))
}
root = newNode("root", nil, []*node{})
root.insert("src/dir1")
root.insert("src/dir1")
srcNode := root.findChildWithName("src")
assertNotNil(t, srcNode)
if len(srcNode.children) != 1 {
t.Errorf("expected srcNode to have 1 child, got %d children", len(root.children))
}
}
func TestInsertAddsChildrenWithCommonParent(t *testing.T) {
root := newNode("root", nil, []*node{})
root.insert("src/foo")
root.insert("src/bar")
srcNode := root.findChildWithName("src")
assertNotNil(t, srcNode)
assertNotNil(t, srcNode.findChildWithName("foo"))
assertNotNil(t, srcNode.findChildWithName("bar"))
}
func TestFixtures(t *testing.T) {
files, err := ioutil.ReadDir("fixtures")
if err != nil {
t.Fatal(err)
}
for _, f := range files {
if !f.IsDir() {
t.Error("expected everything in fixtures/ to be a directory")
}
inFile, err := os.Open(path.Join("fixtures", f.Name(), "input"))
if err != nil {
t.Fatal(err)
}
wantBytes, err := ioutil.ReadFile(path.Join("fixtures", f.Name(), "want"))
if err != nil {
t.Fatal(err)
}
want := strings.TrimSpace(string(wantBytes))
got := strings.TrimSpace(printScannerAsTree(bufio.NewScanner(inFile)))
if want != got {
t.Errorf("input for fixture %s did not match output.\nwanted:\n%s\ngot:\n%s", f.Name(), want, got)
}
}
}