Skip to content

Instantly share code, notes, and snippets.

@bulv1ne
Last active January 1, 2016 00:08
Show Gist options
  • Save bulv1ne/8064219 to your computer and use it in GitHub Desktop.
Save bulv1ne/8064219 to your computer and use it in GitHub Desktop.
from contextlib import contextmanager
@contextmanager
def switch(value):
yield Case(value)
class Case(object):
def __init__(self, value):
self.value = value
def __call__(self, value):
return self.value == value
import unittest
from switchy import switch
class TestSwitch(unittest.TestCase):
def test(self):
with switch('a') as case:
self.assertFalse(case('b'))
self.assertTrue(case('a'))
with switch(None) as case:
self.assertTrue(case(None))
def test_nested(self):
with switch('a') as case1:
with switch('b') as case2:
self.assertTrue(case1('a'))
self.assertTrue(case2('b'))
self.assertFalse(case2('a'))
self.assertFalse(case1('b'))
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment