From 6142710f3eaf3e45f52612b901c35da28d2c3168 Mon Sep 17 00:00:00 2001 From: Jeff McCarrell Date: Wed, 18 Jul 2018 08:11:37 -0700 Subject: [PATCH 1/2] add obj identitified by __str__ to the mix. --- main.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 2056411..127d75d 100644 --- a/main.py +++ b/main.py @@ -127,12 +127,55 @@ def e_scope_factory(marker): 'E': e_scope_factory } return Fixture(buf, vals_iter(), expected_vals_with_markers(), dispatch_table) +class _test_(): + 'simple class to test out dispatch' + def __init__(self, val): + self._val = val + + @property + def val(self): + return self._val + + @val.setter + def val(self, new_val): + self._val = new_val + + def __str__(self): + return str(self._val) + +def dispatch_by_class_with_str(): + 'a stream that uses objects defining __str__ as the key to dispatch on.' + + def expected_vals(): + return list((_test_(42), _test_(64))) + + def vals_iter(): + for v in expected_vals(): + # use the printed representation of the class instance for scoping. + yield str(v) + + def expected_vals_with_markers(): + return list(('start_42', '42', 'stop_42', 'start_64', '64', 'stop_64')) + + buf = list() + def scope_factory_42(marker): + return ContextMarker(marker, buf) + def scope_factory_64(marker): + return ContextMarker(marker, buf) + + dispatch_table = { '42': scope_factory_42, + '64': scope_factory_64 } + return Fixture(buf, vals_iter(), expected_vals_with_markers(), dispatch_table) + @pytest.fixture def fixtures(): - return list((empty_stream(), - typical_stream(), - partial_coverage())) + return list(( + empty_stream(), + typical_stream(), + dispatch_by_class_with_str(), + partial_coverage() + )) def test_by_fixtures(fixtures): for fix in fixtures: From 6a06ef881cf910f98692df3b92ad479574b9b1e2 Mon Sep 17 00:00:00 2001 From: Jeff McCarrell Date: Sat, 21 Jul 2018 12:56:37 -0700 Subject: [PATCH 2/2] show subclassing based on __eq__ works. --- main.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/main.py b/main.py index 127d75d..3ca3320 100644 --- a/main.py +++ b/main.py @@ -143,6 +143,20 @@ def val(self, new_val): def __str__(self): return str(self._val) + def __eq__(self, other): + return self.val == other.val + +class _sub_test_(_test_): + 'test whether subclassing will compare equal' + def __init__(self, val): + super().__init__(val) + +def test_class_eq(): + 'verify eq is working' + assert _test_(525) == _test_(525) + assert _test_('990R') == _sub_test_('990R') + + def dispatch_by_class_with_str(): 'a stream that uses objects defining __str__ as the key to dispatch on.'