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

Commit f4ddc55

Browse files
authored
Merge pull request #187 from ncordon/iterators.update
Adds children order
2 parents 20518a7 + ba49286 commit f4ddc55

3 files changed

Lines changed: 20 additions & 12 deletions

File tree

bblfsh/test.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from bblfsh.node import NodeTypedGetException
1313
from bblfsh.result_context import (Node, NodeIterator, ResultContext)
1414
from bblfsh.pyuast import uast, decode
15+
from functools import cmp_to_key
1516

1617
class BblfshTests(unittest.TestCase):
1718
BBLFSH_SERVER_EXISTED = None
@@ -261,12 +262,11 @@ def _get_nodes(iterator: NodeIterator) -> t.List[dict]:
261262

262263
@staticmethod
263264
def _get_positions(iterator: NodeIterator):
264-
nodes = [ n.get() for n in iterator ]
265-
start_positions = [ n["@pos"]["start"] for n in
266-
filter(lambda x: isinstance(x, dict) and
267-
"@pos" in x.keys() and
268-
"start" in x["@pos"].keys(), nodes) ]
269-
return [ (int(n["offset"]), int(n["line"]), int(n["col"])) for n in start_positions ]
265+
startPositions = [ n["@pos"]["start"] for n in
266+
filter(lambda x: isinstance(x, dict) and
267+
"@pos" in x.keys() and
268+
"start" in x["@pos"].keys(), iterator) ]
269+
return [ (int(n["offset"]), int(n["line"]), int(n["col"])) for n in startPositions ]
270270

271271
def decrefAndGC(self, obj) -> None:
272272
del obj
@@ -306,10 +306,10 @@ def testIteratorPositionOrder(self) -> None:
306306
'son1_2', 'son2_2', 'son2'])
307307
# Check that when using the positional order the positions we get are
308308
# in fact sorted by (offset, line, col)
309-
ctx = self._parse_fixture()
310-
it = ctx.iterate(TreeOrder.POSITION_ORDER)
309+
it = iterator(root, TreeOrder.POSITION_ORDER)
311310
positions = self._get_positions(it)
312-
self.assertListEqual(positions, sorted(positions))
311+
self.assertListEqual(positions, [(0,1,1), (2,2,2), (5,5,1), (10,10,1),
312+
(10,10,1), (15,15,1), (100,100,1)])
313313

314314
def testAnyOrder(self) -> None:
315315
root = self._itTestTree()
@@ -320,6 +320,14 @@ def testAnyOrder(self) -> None:
320320
self.assertEqual(set(expanded), {'root', 'son1', 'son2', 'son1_1',
321321
'son1_2', 'son2_1', 'son2_2'})
322322

323+
def testChildrenOrder(self) -> None:
324+
root = self._itTestTree()
325+
it = iterator(root, TreeOrder.CHILDREN_ORDER)
326+
self.assertIsNotNone(it)
327+
expanded = self._get_nodetypes(it)
328+
# We only can test that the order gives us all the nodes
329+
self.assertEqual(expanded, ['son1', 'son2'])
330+
323331
# Iterating from the root node should give the same result as
324332
# iterating from the tree, for every available node
325333
def testNodeIteratorEqualsCtxIterator(self) -> None:
@@ -348,7 +356,6 @@ def testItersMixingIterations(self) -> None:
348356
it = ctx.iterate(TreeOrder.PRE_ORDER)
349357
next(it); next(it); next(it); next(it)
350358

351-
352359
it2 = it.iterate(TreeOrder.PRE_ORDER)
353360
next(it2)
354361

bblfsh/tree_order.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from enum import IntEnum
2-
from bblfsh.pyuast import AnyOrder, PreOrder, PostOrder, LevelOrder, PositionOrder
2+
from bblfsh.pyuast import AnyOrder, PreOrder, PostOrder, LevelOrder, ChildrenOrder, PositionOrder
33

44
class TreeOrder(IntEnum):
55
# Gives no assurances over the iteration order of the tree
@@ -9,6 +9,7 @@ class TreeOrder(IntEnum):
99
PRE_ORDER = PreOrder
1010
POST_ORDER = PostOrder
1111
LEVEL_ORDER = LevelOrder
12+
CHILDREN_ORDER = ChildrenOrder
1213
POSITION_ORDER = PositionOrder
1314

1415
@staticmethod

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# The VERSION line is edited automatically during deployments.
1616
# You may change the contents of the string, but do not otherwise edit the line.
1717
VERSION = "3.x.x-dev"
18-
LIBUAST_VERSION = "v3.4.1"
18+
LIBUAST_VERSION = "v3.4.2"
1919
SDK_V1_VERSION = "v1.17.0"
2020
SDK_V1_MAJOR = SDK_V1_VERSION.split('.')[0]
2121
SDK_V2_VERSION = "v2.16.4"

0 commit comments

Comments
 (0)