Skip to content

Commit

Permalink
Custom infix test.
Browse files Browse the repository at this point in the history
  • Loading branch information
coleifer committed Mar 4, 2015
1 parent adaa868 commit 5d899ee
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions playhouse/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def __init__(self, fn):
self._fn = fn

def __ror__(self, lhs):
return Infix(lambda r: self.fn(lhs, r))
return Infix(lambda r: self._fn(lhs, r))

def __or__(self, rhs):
return self.fn(rhs)
return self._fn(rhs)
23 changes: 23 additions & 0 deletions playhouse/tests/test_shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from playhouse.test_utils import assert_query_count
from playhouse.tests.base import database_initializer
from playhouse.tests.base import ModelTestCase
from playhouse.tests.base import PeeweeTestCase


db = database_initializer.get_in_memory_database()
Expand Down Expand Up @@ -373,3 +374,25 @@ def test_unknown_attributes(self):

inst = dict_to_model(User, data, ignore_unknown=True)
self.assertEqual(inst.xx, 'does not exist')

def add(lhs, rhs):
return lhs + rhs

def sub(lhs, rhs):
return lhs - rhs

P = Infix(add)
S = Infix(sub)

class TestInfix(PeeweeTestCase):
def test_infix(self):
result = 1 |P| 2
self.assertEqual(result, 3)
self.assertEqual(3 |P| 6, 9)

result = 4 |S| 5
self.assertEqual(result, -1)
self.assertEqual(4 |S| 1, 3)

result = 1 |P| 3 |S| 5 |P| 2 |S| 4
self.assertEqual(result, -3)

0 comments on commit 5d899ee

Please sign in to comment.