Skip to content

Commit

Permalink
Add a "table_from()" test that tries arbitrary data structures.
Browse files Browse the repository at this point in the history
We do not currently translate them transitively, but we might do that in the future.
  • Loading branch information
scoder committed Feb 20, 2024
1 parent 6ea4992 commit 1922482
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lupa/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ class LupaTestCase(unittest.TestCase):
"""
lupa = lupa

if sys.version_info < (3, 4):
from contextlib import contextmanager

@contextmanager
def subTest(self, message=None, **parameters):
"""Dummy implementation"""
yield


def find_lua_modules():
modules = [lupa]
Expand Down
17 changes: 17 additions & 0 deletions lupa/tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,23 @@ def test_table_from_bad(self):
self.assertRaises(TypeError, self.lua.table_from, None)
self.assertRaises(TypeError, self.lua.table_from, {"a": 5}, 123)

def test_table_from_nested_datastructures(self):
from itertools import count
def make_ds(*children):
yield list(children)
yield dict(zip(count(), children))
yield {chr(ord('A') + i): child for i, child in enumerate(children)}

elements = [1, 2, 'x', 'y']
for ds1 in make_ds(*elements):
for ds2 in make_ds(ds1):
for ds3 in make_ds(ds1, elements, ds2):
for ds in make_ds(ds1, ds2, ds3):
with self.subTest(ds=ds):
table = self.lua.table_from(ds)
# we don't translate transitively, so apply arbitrary test operation
self.assertTrue(list(table))

# def test_table_from_nested(self):
# table = self.lua.table_from({"obj": {"foo": "bar"}})
# lua_type = self.lua.eval("type")
Expand Down

0 comments on commit 1922482

Please sign in to comment.