Skip to content

Instantly share code, notes, and snippets.

@s-kostyuk
Created October 6, 2017 21:43
Show Gist options
  • Save s-kostyuk/c4d74d2f36fe93d1e235a968427fbd22 to your computer and use it in GitHub Desktop.
Save s-kostyuk/c4d74d2f36fe93d1e235a968427fbd22 to your computer and use it in GitHub Desktop.
import unittest
from functools import singledispatch
class TestClass1(object):
@property
def test_prop(self):
return "tc1"
class TestClass2(object):
@property
def test_prop(self):
return "tc2"
class TestClass3(object):
@property
def test_prop(self):
return "tc3"
@singledispatch
def build(obj: object) -> object:
raise NotImplementedError()
@build.register(TestClass1)
def _(obj: TestClass1) -> object:
return obj.test_prop
@build.register(TestClass2)
def _(obj: TestClass2) -> object:
return obj.test_prop
class TestSingleDisplatch(unittest.TestCase):
def setUp(self):
self.tc1 = TestClass1()
self.tc2 = TestClass2()
self.tc3 = TestClass3()
def test_init(self):
self.assertEqual(
build(self.tc1), "tc1"
)
self.assertEqual(
build(self.tc2), "tc2"
)
with self.assertRaises(NotImplementedError):
build(self.tc3)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment