Skip to content

Commit

Permalink
Merge 98e1fc9 into e12293c
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelAz committed Jun 27, 2014
2 parents e12293c + 98e1fc9 commit 052da05
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
7 changes: 6 additions & 1 deletion goless/selecting.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def ready(self):
return False


def select(cases):
def select(*cases):
"""
Select the first case that becomes ready.
If a default case (:class:`goless.dcase`) is present,
Expand All @@ -54,6 +54,11 @@ def select(cases):
:return: ``(chosen case, received value)``.
If the chosen case is not an :class:`goless.rcase`, it will be None.
"""
# Sanity check - if the first argument is a list, it should be the only argument
if isinstance(cases[0], list):
assert len(cases) == 1
cases = cases[0]

default = None
for c in cases:
if c.ready():
Expand Down
15 changes: 15 additions & 0 deletions tests/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,18 @@ def test_main_tasklet_can_select(self):
def test_raises_if_multiple_default_cases(self):
with self.assertRaises(AssertionError):
goless.select([goless.dcase(), goless.dcase()])

def test_select_accepts_args(self):
chan1 = goless.chan(1)
chosen, val = goless.select(goless.scase(chan1, 1))
self.assertIs(type(chosen), goless.scase)
self.assertIsNone(val)

def test_select_raises_for_list_and_args(self):
chan1 = goless.chan(1)
chan2 = goless.chan(1)
chan3 = goless.chan(1)
cases = [goless.scase(chan1, 1), goless.scase(chan2, 2)]

with self.assertRaises(AssertionError):
goless.select(cases, chan3)

0 comments on commit 052da05

Please sign in to comment.