|
8 | 8 | import collections |
9 | 9 | import decimal as decimal_module |
10 | 10 |
|
| 11 | +import nose |
| 12 | + |
11 | 13 | from python_toolbox import cute_iter_tools |
12 | 14 | from python_toolbox import sequence_tools |
13 | 15 | from python_toolbox import cute_testing |
|
20 | 22 | infinity = float('inf') |
21 | 23 | infinities = (infinity, -infinity) |
22 | 24 |
|
| 25 | +class BaseTallyTestCase(cute_testing.TestCase): |
| 26 | + pass |
| 27 | + |
| 28 | +class BaseMutableTallyTestCase(BaseTallyTestCase): |
| 29 | + is_frozen = False |
| 30 | + |
| 31 | +class BaseFrozenTallyTestCase(BaseTallyTestCase): |
| 32 | + is_frozen = True |
| 33 | + |
| 34 | +class BaseOrderedTallyTestCase(BaseTallyTestCase): |
| 35 | + is_ordered = True |
| 36 | + |
| 37 | +class BaseUnorderedTallyTestCase(BaseTallyTestCase): |
| 38 | + is_ordered = False |
| 39 | + |
| 40 | + |
| 41 | +class TallyTestCase(BaseMutableTallyTestCase, BaseUnorderedTallyTestCase): |
| 42 | + tally_type = Tally |
| 43 | + |
| 44 | +class OrderedTallyTestCase(BaseMutableTallyTestCase, |
| 45 | + BaseOrderedTallyTestCase): |
| 46 | + tally_type = OrderedTally |
| 47 | + |
| 48 | +class FrozenTallyTestCase(BaseFrozenTallyTestCase, BaseUnorderedTallyTestCase): |
| 49 | + tally_type = FrozenTally |
| 50 | + |
| 51 | +class OrderedTallyTestCase(BaseFrozenTallyTestCase, |
| 52 | + BaseOrderedTallyTestCase): |
| 53 | + tally_type = FrozenOrderedTally |
| 54 | + |
| 55 | + |
| 56 | + |
23 | 57 | _check_functions = [] |
24 | 58 | def _test_on(arguments): |
25 | 59 | def decorator(check_function): |
@@ -159,32 +193,32 @@ def _check_comparison(tally_type): |
159 | 193 |
|
160 | 194 | @_test_on(Tally, OrderedTally, FrozenTally, FrozenOrderedTally) |
161 | 195 | def _check_ignores_zero(tally_type): |
162 | | - frozen_tally_0 = tally_type({'a': 0,}) |
163 | | - frozen_tally_1 = tally_type() |
164 | | - assert frozen_tally_0 == frozen_tally_1 |
| 196 | + tally_0 = tally_type({'a': 0,}) |
| 197 | + tally_1 = tally_type() |
| 198 | + assert tally_0 == tally_1 |
165 | 199 |
|
166 | | - assert hash(frozen_tally_0) == hash(frozen_tally_1) |
167 | | - assert {frozen_tally_0, frozen_tally_1} == {frozen_tally_0} == \ |
168 | | - {frozen_tally_1} |
| 200 | + if tally_type.is_frozen: |
| 201 | + assert hash(tally_0) == hash(tally_1) |
| 202 | + assert {tally_0, tally_1} == {tally_0} == {tally_1} |
169 | 203 |
|
170 | | - frozen_tally_2 = tally_type( |
| 204 | + tally_2 = tally_type( |
171 | 205 | {'a': 0.0, 'b': 2, 'c': decimal_module.Decimal('0.0'),}) |
172 | | - frozen_tally_3 = tally_type('bb') |
| 206 | + tally_3 = tally_type('bb') |
173 | 207 |
|
174 | | - assert hash(frozen_tally_2) == hash(frozen_tally_3) |
175 | | - assert {frozen_tally_2, frozen_tally_3} == {frozen_tally_2} == \ |
176 | | - {frozen_tally_3} |
| 208 | + if tally_type.is_frozen: |
| 209 | + assert hash(tally_2) == hash(tally_3) |
| 210 | + assert {tally_2, tally_3} == {tally_2} == {tally_3} |
177 | 211 |
|
178 | 212 |
|
179 | 213 | @_test_on(Tally, OrderedTally, FrozenTally, FrozenOrderedTally) |
180 | 214 | def _check_mutating(tally_type): |
181 | | - frozen_tally = tally_type('abracadabra') |
| 215 | + tally = tally_type('abracadabra') |
182 | 216 | with cute_testing.RaiseAssertor(TypeError): |
183 | | - frozen_tally['a'] += 1 |
| 217 | + tally['a'] += 1 |
184 | 218 | with cute_testing.RaiseAssertor(TypeError): |
185 | | - frozen_tally['a'] -= 1 |
| 219 | + tally['a'] -= 1 |
186 | 220 | with cute_testing.RaiseAssertor(TypeError): |
187 | | - frozen_tally['a'] = 7 |
| 221 | + tally['a'] = 7 |
188 | 222 |
|
189 | 223 |
|
190 | 224 |
|
@@ -217,10 +251,10 @@ def _check_only_positive_ints_or_zero(tally_type): |
217 | 251 |
|
218 | 252 | @_test_on(Tally, OrderedTally, FrozenTally, FrozenOrderedTally) |
219 | 253 | def _check_ordered(): |
220 | | - frozen_tally_0 = FrozenTally('ababb') |
221 | | - frozen_tally_1 = FrozenTally('bbbaa') |
222 | | - assert frozen_tally_0 == frozen_tally_1 |
223 | | - assert hash(frozen_tally_0) == hash(frozen_tally_1) |
| 254 | + tally_0 = FrozenTally('ababb') |
| 255 | + tally_1 = FrozenTally('bbbaa') |
| 256 | + assert tally_0 == tally_1 |
| 257 | + assert hash(tally_0) == hash(tally_1) |
224 | 258 |
|
225 | 259 | frozen_ordered_tally_0 = FrozenOrderedTally('ababb') |
226 | 260 | frozen_ordered_tally_1 = FrozenOrderedTally('bbbaa') |
|
0 commit comments