Skip to content

Commit

Permalink
allow empty products and 0-argument properties
Browse files Browse the repository at this point in the history
... while at the same time making the code more Pythonic.
  • Loading branch information
rudymatela committed Aug 20, 2024
1 parent 1f23aea commit 83c00ff
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
TODO for leancheck.py
=====================

* make `check()` work for 0-argument properties?

* add a `leancheck.main()` similar to `unittest.main()`

* simplify code
Expand Down
8 changes: 4 additions & 4 deletions src/leancheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ def map(self, f):
return Enumerator(lambda: mmap(f, self.tiers()))

@classmethod
def product(cls, enumerator, *enumerators):
# TODO: FIXME: this seems quite unpythonic
def product(cls, *enumerators):
if len(enumerators) == 0:
return enumerator.map(lambda x: (x,))
return Enumerator(lambda: (xs for xs in [[()]]))
else:
return (enumerator * cls.product(*enumerators)).map(lambda t: (t[0],) + t[1])
e, *es = enumerators
return (e * cls.product(*es)).map(lambda t: (t[0],) + t[1])

_enumerators = None

Expand Down
5 changes: 5 additions & 0 deletions tests/test_leancheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ def test_tuple(self):
self.assertEnum(tuple[int,int,int], [(0, 0, 0), (0, 0, 1), (0, 1, 0), (1, 0, 0), (0, 0, 2), (0, 1, 1)])
self.assertEnum(tuple[bool,bool], [(False, False), (False, True), (True, False), (True, True)])

def test_empty_product(self):
e = Enumerator.product()
self.assertEqual(list(e), [()])
self.assertEqual(list(e.tiers()), [[()]])


if __name__ == '__main__':
unittest.main()

0 comments on commit 83c00ff

Please sign in to comment.