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

Commit 81afd64

Browse files
committed
Adds tests for value filters
Signed-off-by: ncordon <nacho.cordon.castillo@gmail.com>
1 parent 596cd6f commit 81afd64

File tree

3 files changed

+41
-30
lines changed

3 files changed

+41
-30
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,10 @@ object BblfshClient {
242242
// do not dispose the context, iterator steals it
243243
}
244244

245-
// Method to filter managed nodes based on type
246-
// The aim is to not return a JNode but a specialized subclass of JNode, as JBool,
247-
// for example, when T = JBool
245+
/** Method to filter managed nodes based on type
246+
* The aim is to not return a JNode but a specialized subclass of JNode, as JBool,
247+
* for example, when T = JBool
248+
*/
248249
private def filterOnType[T : ClassTag](node: JNode, query: String): Iterator[T] = {
249250
val it = filter(node, query)
250251
// Filter only the nodes that are of type T

src/test/scala/org/bblfsh/client/v2/BblfshClientParseTest.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class BblfshClientParseTest extends BblfshClientBaseTest {
5454
encodedBytes shouldEqual resp.uast.asReadOnlyByteBuffer
5555
}
5656

57-
5857
"Encoding python UAST to a new Context" should "produce the same bytes" in {
5958
val fileName = "src/test/resources/python_file.py"
6059
val fileContent = Source.fromFile(fileName).getLines.mkString("\n")

src/test/scala/org/bblfsh/client/v2/FilterManagedTest.scala

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,42 @@ package org.bblfsh.client.v2
22

33
import gopkg.in.bblfsh.sdk.v2.protocol.driver.Mode
44
import org.bblfsh.client.v2.libuast.Libuast
5+
import org.scalatest.prop.TableDrivenPropertyChecks._
56

67
import scala.io.Source
78

89
class FilterManagedTest extends BblfshClientBaseTest {
910

1011
import BblfshClient._ // enables uast.* methods
1112

12-
var ctx: Context = _
13+
val ctx: Context = Context()
1314
val managedRoot = JArray(
1415
JObject(
1516
"@type" -> JString("file"),
1617
"k1" -> JString("v1"),
1718
"k2" -> JObject(
18-
"k3" -> JInt(24)
19+
"k3" -> JInt(24),
20+
"k4" -> JFloat(1.0),
21+
"k5" -> JArray(
22+
JObject(
23+
"k6" -> JBool(true),
24+
"k7" -> JString("v2"),
25+
"k8" -> JObject(
26+
"k9" -> JBool(false),
27+
"k10" -> JFloat(2.0)
28+
)
29+
)
30+
)
1931
)
20-
))
21-
22-
override def beforeAll() = {
23-
super.beforeAll()
24-
System.err.println(s"Libuast.loaded: ${Libuast.loaded}")
25-
// to load native JNI lib \wo the full client
26-
}
27-
28-
override def beforeEach() = {
29-
super.beforeEach()
30-
ctx = Context()
31-
}
32-
33-
override def afterAll() = {
34-
super.afterAll()
35-
System.runFinalization()
36-
System.gc()
37-
}
32+
)
33+
)
3834

3935
"XPath filter" should "find all positions under context" in {
4036
val it = ctx.filter("//file", managedRoot)
4137
it.hasNext() should be(true)
4238

4339
val pos = it.toList
44-
pos should have size (1) // Tiny.java contains 1 file node
40+
pos should have size (1) // managedRoot contains only a node JObject
4541

4642
it.close()
4743
it.hasNext() should be(false)
@@ -57,11 +53,26 @@ class FilterManagedTest extends BblfshClientBaseTest {
5753
iter.close()
5854
}
5955

60-
// TODO(#110) implement value type returns
61-
// "Filtering UAST" should "work for Value types" in {
62-
// val iter = BblfshClient.filterNumber(resp.get, "count(//*)")
63-
// iter.toList should have size (517) // total number of nodes (not the number of results which is 1)
64-
// }
56+
val valueFilters =
57+
Table(
58+
("filter", "expected"),
59+
(BblfshClient.filterInt _, List(JInt(24))),
60+
(BblfshClient.filterBool _, List(JBool(true), JBool(false))),
61+
(BblfshClient.filterString _, List(JString("file"), JString("v1"), JString("v2"))),
62+
(BblfshClient.filterFloat _, List(JFloat(1.0), JFloat(2.0)))
63+
)
64+
65+
"Filtering UAST" should "work for Value types" in {
66+
val iterCount = BblfshClient.filterInt(managedRoot, "count(//*)")
67+
val allNodesIt = BblfshClient.filter(managedRoot, "//*")
68+
val numNodes = allNodesIt.size
69+
iterCount.toList should be (List(JInt(numNodes))) // number of nodes
70+
71+
forAll (valueFilters) { (filter, expected: List[JNode]) =>
72+
val it = filter(managedRoot, "//*")
73+
it.toList should be (expected)
74+
}
75+
}
6576

6677
"Filtering UAST" should "work in Annotated mode" in {
6778
val fileContent = Source.fromFile(fileName).getLines.mkString("\n")

0 commit comments

Comments
 (0)