-
Notifications
You must be signed in to change notification settings - Fork 5
/
CodeTree_test.go
122 lines (98 loc) · 2.91 KB
/
CodeTree_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package codetree_test
import (
"io"
"io/ioutil"
"os"
"strings"
"testing"
"testing/iotest"
"github.com/aerogo/codetree"
"github.com/akyoto/assert"
)
func TestCodeTree(t *testing.T) {
// Repeat this test multiple times to detect memory corruption errors.
// Incorrect usage of the "unsafe" package can lead to those errors.
for i := 0; i < 1000; i++ {
file, err := os.Open("testdata/example.txt")
assert.Nil(t, err)
defer file.Close()
tree, err := codetree.New(file)
assert.Nil(t, err)
defer tree.Close()
assert.Equal(t, -1, tree.Indent)
assert.Equal(t, 6, len(tree.Children))
assert.Equal(t, 1, len(tree.Children[5].Children))
assert.Equal(t, "parent1", tree.Children[0].Line)
assert.Equal(t, "parent2", tree.Children[1].Line)
assert.Equal(t, "parent3", tree.Children[2].Line)
assert.Equal(t, "child1", tree.Children[0].Children[0].Line)
assert.Equal(t, "child1", tree.Children[1].Children[0].Line)
assert.Equal(t, "child1", tree.Children[2].Children[0].Line)
assert.Equal(t, "child1", tree.Children[5].Children[0].Line)
}
}
func TestWindowsLineEndings(t *testing.T) {
data, err := ioutil.ReadFile("testdata/example.txt")
assert.Nil(t, err)
code := string(data)
code = strings.ReplaceAll(code, "\n", "\r\n")
tree, err := codetree.New(strings.NewReader(code))
assert.Nil(t, err)
defer tree.Close()
assert.Equal(t, -1, tree.Indent)
assert.Equal(t, 6, len(tree.Children))
assert.Equal(t, "child1", tree.Children[5].Children[0].Line)
}
func TestBadIndentation(t *testing.T) {
file, err := os.Open("testdata/bad-indentation.txt")
assert.Nil(t, err)
defer file.Close()
tree, err := codetree.New(file)
assert.Nil(t, tree)
assert.NotNil(t, err)
}
func TestLongExample(t *testing.T) {
file, err := os.Open("testdata/long-example.txt")
assert.Nil(t, err)
defer file.Close()
tree, err := codetree.New(file)
assert.Nil(t, err)
defer tree.Close()
assert.Equal(t, -1, tree.Indent)
assert.Equal(t, "parent1", tree.Children[0].Line)
assert.Equal(t, "parent2", tree.Children[1].Line)
assert.Equal(t, "parent3", tree.Children[2].Line)
assert.Equal(t, "child1", tree.Children[0].Children[0].Line)
assert.Equal(t, "child1", tree.Children[1].Children[0].Line)
assert.Equal(t, "child1", tree.Children[2].Children[0].Line)
assert.Equal(t, "child1", tree.Children[5].Children[0].Line)
}
func TestTimeoutReader(t *testing.T) {
file, err := os.Open("testdata/example.txt")
assert.Nil(t, err)
defer file.Close()
timeoutReader := iotest.TimeoutReader(file)
tree, err := codetree.New(timeoutReader)
assert.NotNil(t, err)
assert.Nil(t, tree)
}
func BenchmarkCodeTree(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
file, err := os.Open("testdata/example.txt")
if err != nil {
panic(err)
}
defer file.Close()
for i := 0; i < b.N; i++ {
_, err = file.Seek(0, io.SeekStart)
if err != nil {
panic(err)
}
tree, err := codetree.New(file)
if err != nil {
panic(err)
}
tree.Close()
}
}