Last active
January 1, 2016 00:08
-
-
Save bulv1ne/8064219 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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