-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_list_test.go
180 lines (150 loc) · 4.31 KB
/
node_list_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright (c) 2017 Opsidian Ltd.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package ast_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/conflowio/parsley/ast"
"github.com/conflowio/parsley/ast/astfakes"
"github.com/conflowio/parsley/parsley"
"github.com/conflowio/parsley/parsley/parsleyfakes"
)
var _ = Describe("NodeList", func() {
var (
nl, nl2 ast.NodeList
n1, n2, n3, n4 *parsleyfakes.FakeNode
)
BeforeEach(func() {
n1 = &parsleyfakes.FakeNode{}
n1.TokenReturns("n1")
n2 = &parsleyfakes.FakeNode{}
n2.TokenReturns("n2")
n3 = &parsleyfakes.FakeNode{}
n3.TokenReturns("n3")
n4 = &parsleyfakes.FakeNode{}
n4.TokenReturns("n4")
nl2 = ast.NodeList([]parsley.Node{n3, n4})
})
Describe("Token", func() {
Context("when empty", func() {
BeforeEach(func() {
nl = ast.NodeList([]parsley.Node{})
})
It("should panic", func() {
Expect(func() { nl.Token() }).To(Panic())
})
})
Context("when not empty", func() {
BeforeEach(func() {
nl = ast.NodeList([]parsley.Node{n1, n2})
})
It("should return the reader pos of the first item", func() {
n1.TokenReturns("testtoken")
Expect(nl.Token()).To(Equal("testtoken"))
Expect(n1.TokenCallCount()).To(Equal(1))
Expect(n2.TokenCallCount()).To(Equal(0))
})
})
})
Describe("Pos", func() {
Context("when empty", func() {
BeforeEach(func() {
nl = ast.NodeList([]parsley.Node{})
})
It("should panic", func() {
Expect(func() { nl.Pos() }).To(Panic())
})
})
Context("when not empty", func() {
BeforeEach(func() {
nl = ast.NodeList([]parsley.Node{n1, n2})
n1.PosReturns(parsley.Pos(1))
n2.PosReturns(parsley.Pos(2))
})
It("should return the position of the first item", func() {
Expect(nl.Pos()).To(Equal(n1.Pos()))
})
})
})
Describe("Append", func() {
Context("when empty", func() {
BeforeEach(func() {
nl = ast.NodeList([]parsley.Node{})
})
It("should append a new item", func() {
nl.Append(n1)
Expect(nl).To(Equal(ast.NodeList([]parsley.Node{n1})))
})
It("should append a new item list", func() {
nl.Append(nl2)
Expect(nl).To(Equal(ast.NodeList([]parsley.Node{n3, n4})))
})
})
Context("when already has items", func() {
BeforeEach(func() {
nl = ast.NodeList([]parsley.Node{n1})
})
It("should append a new item", func() {
nl.Append(n2)
Expect(nl).To(Equal(ast.NodeList([]parsley.Node{n1, n2})))
})
It("should append a new item list", func() {
nl.Append(nl2)
Expect(nl).To(Equal(ast.NodeList([]parsley.Node{n1, n3, n4})))
})
It("should only append an empty node once", func() {
nl.Append(ast.EmptyNode(1))
nl.Append(ast.EmptyNode(1))
Expect(nl).To(Equal(ast.NodeList([]parsley.Node{n1, ast.EmptyNode(1)})))
})
})
})
Describe("ReaderPos", func() {
Context("when empty", func() {
BeforeEach(func() {
nl = ast.NodeList([]parsley.Node{})
})
It("should panic", func() {
Expect(func() { nl.ReaderPos() }).To(Panic())
})
})
Context("when not empty", func() {
BeforeEach(func() {
nl = ast.NodeList([]parsley.Node{n1, n2})
})
It("should return the reader pos of the first item", func() {
n1.ReaderPosReturns(parsley.Pos(2))
Expect(nl.ReaderPos()).To(Equal(parsley.Pos(2)))
Expect(n1.ReaderPosCallCount()).To(Equal(1))
Expect(n2.ReaderPosCallCount()).To(Equal(0))
})
})
})
Describe("Walk", func() {
It("should call the function on the first node", func() {
nl = ast.NodeList([]parsley.Node{n1, n2})
called := []parsley.Node{}
f := func(n parsley.Node) bool {
called = append(called, n)
return false
}
res := nl.Walk(f)
Expect(res).To(BeFalse())
Expect(called).To(Equal([]parsley.Node{n1}))
})
})
Describe("SetReaderPos", func() {
It("should call the function on all nodes", func() {
n1 := &astfakes.FakeReaderPosSetterNode{}
n2 := &astfakes.FakeReaderPosSetterNode{}
nl = ast.NodeList([]parsley.Node{n1, n2})
f := func(pos parsley.Pos) parsley.Pos { return pos }
nl.SetReaderPos(f)
Expect(n1.SetReaderPosCallCount()).To(Equal(1))
Expect(n2.SetReaderPosCallCount()).To(Equal(1))
})
})
})