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

Commit 05f7ac2

Browse files
committed
Adds encode to the Context class and tests encode / decode
Signed-off-by: ncordon <[email protected]>
1 parent 6806c93 commit 05f7ac2

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

bblfsh/context.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@ def iterate(self, order: int) -> iterator:
1515
TreeOrder.check_order(order)
1616
return iterator(self.root, order)
1717

18+
def encode(self, fmt: int):
19+
encoded = self.ctx.encode(self.root, fmt)
20+
return encoded
21+
1822
def context(root: dict) -> Context:
1923
return Context(root)

bblfsh/test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,16 @@ def testPythonContextFilter(self) -> None:
560560
for nodeC, nodePy in zip(itC, itPy):
561561
self.assertEqual(nodeC.get(), nodePy)
562562

563+
def testEncodeDecodePythonContext(self) -> None:
564+
# C++ memory context
565+
ctxC = self._parse_fixture()
566+
# Python memory context
567+
pyDict = ctxC.root.get()
568+
ctxPy = bblfsh.context(pyDict)
569+
encoded = ctxPy.encode(fmt = 1) # YAML
570+
decoded = decode(encoded, format = 1)
571+
572+
self.assertEqual(encoded, decoded.load())
563573

564574
if __name__ == "__main__":
565575
unittest.main()

0 commit comments

Comments
 (0)