Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit 73bf34d

Browse files
committed
Improves iterators tests: table-driven and coverage
Signed-off-by: ncordon <[email protected]>
1 parent 0c39f34 commit 73bf34d

File tree

2 files changed

+76
-22
lines changed

2 files changed

+76
-22
lines changed

src/main/scala/org/bblfsh/client/v2/NodeExt.scala

+8-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,14 @@ case class JBool(value: Boolean) extends JNode
158158

159159
case class JObject(obj: mutable.Buffer[JField]) extends JNode {
160160
def this() = this(mutable.Buffer[JField]())
161-
def filter(p: ((String, JNode)) => Boolean) = this.obj.filter(p)
161+
def filter(p: JField => Boolean) = obj.filter(p)
162+
def keys(): mutable.Buffer[String] = {
163+
obj.map{ case (key, value) => key }
164+
}
165+
// Gets only the first ocurrence
166+
def get(key: String): Option[JNode] = {
167+
obj.collectFirst { case (k, v) if k == key => v }
168+
}
162169
def add(k: String, v: JNode) = {
163170
obj += ((k, v))
164171
}

src/test/scala/org/bblfsh/client/v2/libuast/IteratorManagedTest.scala

+68-21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ package org.bblfsh.client.v2.libuast
33
import org.bblfsh.client.v2.{BblfshClient, JArray, JInt, JNode, JObject, JString}
44
// TODO import org.bblfsh.client.v2.nodes._
55
import org.scalatest.{BeforeAndAfter, BeforeAndAfterAll, FlatSpec, Matchers}
6+
import org.scalatest.prop.TableDrivenPropertyChecks._
7+
import BblfshClient.{
8+
TreeOrder,
9+
PreOrder,
10+
PostOrder,
11+
ChildrenOrder,
12+
PositionOrder,
13+
AnyOrder,
14+
LevelOrder
15+
}
616

717
class IteratorManagedTest extends FlatSpec
818
with Matchers
@@ -23,7 +33,7 @@ class IteratorManagedTest extends FlatSpec
2333
}
2434

2535
"Managed UAST iterator" should "return non-empty results on JVM objects" in {
26-
iter = BblfshClient.iterator(mangedRootNode, BblfshClient.PreOrder)
36+
iter = BblfshClient.iterator(mangedRootNode, PreOrder)
2737
iter.hasNext() should be(true)
2838

2939
val nodes = iter.toList
@@ -36,7 +46,7 @@ class IteratorManagedTest extends FlatSpec
3646
}
3747

3848
"Managed UAST iterator" should "go though all nodes of small object" in {
39-
iter = BblfshClient.iterator(mangedRootNode, BblfshClient.PreOrder)
49+
iter = BblfshClient.iterator(mangedRootNode, PreOrder)
4050
val nodes = iter.toList
4151

4252
nodes.size should be(3) // number of composite nodes
@@ -107,30 +117,67 @@ class IteratorManagedTest extends FlatSpec
107117
.map(_ ("@type").asInstanceOf[JString].str)
108118
.toList
109119

110-
// Equivalent of the test.py#testIteratorPreOrder
111-
// https://github.com/bblfsh/python-client/blob/15ffb98bfa09e6aae4d1580f0e4f02eb2a530205/bblfsh/test.py#L270
112-
"Managed UAST iterator" should "return nodes in PreOrder" in {
113-
val preIter = BblfshClient.iterator(testTree, BblfshClient.PreOrder)
114-
val nodes = getNodeTypes(preIter)
115-
116-
val poActual = Seq("root", "son1", "son1_1", "son1_2", "son2", "son2_1", "son2_2")
117-
nodes should have size (poActual.size)
118-
nodes shouldEqual poActual
119-
120-
preIter.close()
120+
def getNodePositions(iterator: Libuast.UastIter): List[(Long, Long, Long)] = {
121+
def positionToTuple(pos: JObject): Option[(Long, Long, Long)] = {
122+
val offset = pos.get("offset")
123+
val line = pos.get("line")
124+
val col = pos.get("col")
125+
val maybePos = (offset, line, col)
126+
127+
maybePos match {
128+
case (Some(o: JInt), Some(l: JInt), Some(c: JInt)) =>
129+
Some(o.num, l.num, c.num)
130+
case _ => None
131+
}
132+
}
133+
134+
// Filter only the nodes which correspond to
135+
// positions and have a start field
136+
iterator
137+
.collect { case node: JObject =>
138+
node.get("@pos").collect { case pos: JObject =>
139+
pos.get("start").collect { case start: JObject =>
140+
positionToTuple(start)
141+
// The result of this is an Option[Option]. Convert it to a single option
142+
}.flatten
143+
// Likewise
144+
}.flatten
145+
}
146+
.collect { case Some(pos) => pos }
147+
.toList
121148
}
122149

123-
"Managed UAST iterator" should "return nodes in PostOrder" in {
124-
val postIter = BblfshClient.iterator(testTree, BblfshClient.PostOrder)
125-
val nodes = getNodeTypes(postIter)
150+
val iterators =
151+
Table(
152+
("order", "expected"),
153+
(PreOrder, Seq("root", "son1", "son1_1", "son1_2", "son2", "son2_1", "son2_2")),
154+
(PostOrder, Seq("son1_1", "son1_2", "son1", "son2_1", "son2_2", "son2", "root")),
155+
(LevelOrder, Seq("root", "son1", "son2", "son1_1", "son1_2", "son2_1", "son2_2")),
156+
(PositionOrder, Seq("root", "son1", "son2_1", "son1_1", "son1_2", "son2_2", "son2")),
157+
(AnyOrder, Seq("root", "son1", "son2_1", "son1_1", "son1_2", "son2_2", "son2")),
158+
(ChildrenOrder, Seq("son1", "son2"))
159+
)
160+
161+
forAll (iterators) { (order: TreeOrder, expected: Seq[String]) =>
162+
val iter = BblfshClient.iterator(testTree, order)
163+
val nodes = getNodeTypes(iter)
126164

127-
val poActual = Seq("son1_1", "son1_2", "son1", "son2_1", "son2_2", "son2", "root")
128-
nodes should have size (poActual.size)
129-
nodes shouldEqual poActual
165+
order match {
166+
case AnyOrder =>
167+
nodes.toSet shouldEqual expected.toSet
168+
case _ =>
169+
nodes shouldEqual expected
170+
}
130171

131-
postIter.close()
172+
iter.close()
132173
}
133174

134-
// TODO(#108) more tests coverage for other iteration orders, refactor to a table-driven test
175+
"Positions in PositionOrder" should "actually be ordered" in {
176+
val posIter = BblfshClient.iterator(testTree, PositionOrder)
177+
val positions = getNodePositions(posIter)
178+
val expected = Seq((0,1,1), (2,2,2), (5,5,1), (10,10,1), (10,10,1), (15,15,1), (100,100,1))
179+
positions shouldEqual expected
135180

181+
posIter.close()
182+
}
136183
}

0 commit comments

Comments
 (0)