11# Copyright 2009-2014 Ram Rachum.
22# This program is distributed under the MIT license.
33
4- '''Testing module for `python_toolbox.nifty_collections.LazyTuple`.'''
5-
64import uuid
75import re
86import pickle
1513from python_toolbox import cute_testing
1614
1715
18- from python_toolbox .nifty_collections import (FrozenTally ,
19- FrozenOrderedTally ,
16+ from python_toolbox .nifty_collections import (Tally , OrderedTally ,
17+ FrozenTally , FrozenOrderedTally ,
2018 OrderedDict )
2119
2220infinity = float ('inf' )
2321infinities = (infinity , - infinity )
2422
23+ _check_functions = []
24+ def _test_on (arguments ):
25+ def decorator (check_function ):
26+ check_function .arguments = arguments
27+ _check_functions .append (check_function )
28+ return check_function
29+ return decorator
30+
2531
26- def test_common ():
27- _check_common (FrozenTally )
28- _check_common (FrozenOrderedTally )
29- _check_comparison (FrozenTally )
30- _check_comparison (FrozenOrderedTally )
31- _check_ignores_zero (FrozenTally )
32- _check_ignores_zero (FrozenOrderedTally )
33- _check_immutable (FrozenTally )
34- _check_immutable (FrozenOrderedTally )
35- _check_only_positive_ints_or_zero (FrozenTally )
36- _check_only_positive_ints_or_zero (FrozenOrderedTally )
37-
3832
39- def _check_common (frozen_tally_type ):
40- frozen_tally = frozen_tally_type ('abracadabra' )
41- assert frozen_tally == collections .Counter ('abracadabra' ) == \
42- collections .Counter (frozen_tally ) == \
43- frozen_tally_type (collections .Counter ('abracadabra' ))
44-
45- assert len (frozen_tally ) == 5
46- assert set (frozen_tally ) == set (frozen_tally .keys ()) == set ('abracadabra' )
47- assert set (frozen_tally .values ()) == {1 , 2 , 5 }
48- assert set (frozen_tally .items ()) == \
33+ def test ():
34+ assert Tally .is_ordered is False
35+ assert Tally .is_frozen is False
36+ assert OrderedTally .is_ordered is True
37+ assert OrderedTally .is_frozen is False
38+ assert FrozenTally .is_ordered is False
39+ assert FrozenTally .is_frozen is True
40+ assert FrozenOrderedTally .is_ordered is True
41+ assert FrozenOrderedTally .is_frozen is True
42+ assert OrderedDict .is_ordered is True
43+ assert OrderedDict .is_frozen is False
44+
45+ for check_function in _check_functions :
46+ for argument in _check_function .arguments :
47+ _check_function (argument )
48+
49+
50+ @_test_on (Tally , OrderedTally , FrozenTally , FrozenOrderedTally )
51+ def _check_common (tally_type ):
52+ tally = tally_type ('abracadabra' )
53+ assert tally == collections .Counter ('abracadabra' ) == \
54+ collections .Counter (tally ) == \
55+ tally_type (collections .Counter ('abracadabra' ))
56+
57+ assert len (tally ) == 5
58+ assert set (tally ) == set (tally .keys ()) == set ('abracadabra' )
59+ assert set (tally .values ()) == {1 , 2 , 5 }
60+ assert set (tally .items ()) == \
4961 {('a' , 5 ), ('r' , 2 ), ('b' , 2 ), ('c' , 1 ), ('d' , 1 )}
50- assert frozen_tally ['a' ] == 5
51- assert frozen_tally ['missing value' ] == 0
52- assert len (frozen_tally ) == 5
53- assert {frozen_tally , frozen_tally } == {frozen_tally }
54- assert {frozen_tally : frozen_tally } == {frozen_tally : frozen_tally }
55- assert isinstance (hash (frozen_tally ), int )
56-
57- assert set (frozen_tally .most_common ()) == \
58- set (collections .Counter (frozen_tally ).most_common ())
59-
60- assert frozen_tally + frozen_tally == \
61- frozen_tally_type ('abracadabra' * 2 )
62- assert frozen_tally - frozen_tally == frozen_tally_type ()
63- assert frozen_tally - frozen_tally_type ('a' ) == \
64- frozen_tally_type ('abracadabr' )
65- assert frozen_tally - frozen_tally_type ('a' ) == \
66- frozen_tally_type ('abracadabr' )
67- assert frozen_tally | frozen_tally_type ('a' ) == frozen_tally
68- assert frozen_tally | frozen_tally == \
69- frozen_tally | frozen_tally | frozen_tally == frozen_tally
70- assert frozen_tally & frozen_tally_type ('a' ) == frozen_tally_type ('a' )
71- assert frozen_tally & frozen_tally == \
72- frozen_tally & frozen_tally & frozen_tally == frozen_tally
73-
74- assert frozen_tally_type (frozen_tally .elements ()) == frozen_tally
75-
76- assert + frozen_tally == frozen_tally
77- with cute_testing .RaiseAssertor (TypeError ):
78- - frozen_tally
79-
80- assert re .match ('^Frozen(Ordered)?Tally\(.*$' ,
81- repr (frozen_tally ))
82-
83- assert frozen_tally .copy ({'meow' : 9 }) == \
84- frozen_tally .copy (meow = 9 ) == \
85- frozen_tally_type (OrderedDict (
62+ assert tally ['a' ] == 5
63+ assert tally ['missing value' ] == 0
64+ assert len (tally ) == 5
65+ if tally .is_frozen :
66+ assert {tally , tally } == {tally }
67+ assert {tally : tally } == {tally : tally }
68+ assert isinstance (hash (tally ), int )
69+ else :
70+ with cute_testing .RaiseAssertor (TypeError ):
71+ {tally }
72+ with cute_testing .RaiseAssertor (TypeError ):
73+ {tally : None ,}
74+ with cute_testing .RaiseAssertor (TypeError ):
75+ assert isinstance (hash (tally ), int )
76+
77+
78+ assert set (tally .most_common ()) == \
79+ set (collections .Counter (tally ).most_common ()) == \
80+ set (collections .Counter (tally .elements ()).most_common ())
81+
82+ assert tally + tally == tally_type ('abracadabra' * 2 )
83+ assert tally - tally == tally_type ()
84+ assert tally - tally_type ('a' ) == tally_type ('abracadabr' )
85+ assert tally - tally_type ('a' ) == tally_type ('abracadabr' )
86+ assert tally | tally_type ('a' ) == tally
87+ assert tally | tally == tally | tally | tally == tally
88+ assert tally & tally_type ('a' ) == tally_type ('a' )
89+ assert tally & tally == \
90+ tally & tally & tally == tally
91+
92+ assert tally_type (tally .elements ()) == tally
93+
94+ assert + tally == tally
95+ with cute_testing .RaiseAssertor (TypeError ):
96+ - tally
97+
98+ assert re .match ('^(Frozen)?(Ordered)?Tally\(.*$' , repr (tally ))
99+
100+ assert tally .copy ({'meow' : 9 }) == \
101+ tally .copy (meow = 9 ) == \
102+ tally_type (OrderedDict (
86103 [('a' , 5 ), ('b' , 2 ), ('r' , 2 ), ('c' , 1 ), ('d' , 1 ), ('meow' , 9 )])
87104 )
88105
89- assert pickle .loads (pickle .dumps (frozen_tally )) == frozen_tally
106+ assert pickle .loads (pickle .dumps (tally )) == tally
90107
91- assert frozen_tally_type ({'a' : 0 , 'b' : 1 ,}) == \
92- frozen_tally_type ({'c' : 0 , 'b' : 1 ,})
108+ assert tally_type ({'a' : 0 , 'b' : 1 ,}) == \
109+ tally_type ({'c' : 0 , 'b' : 1 ,})
93110
111+ @_test_on (Tally , OrderedTally , FrozenTally , FrozenOrderedTally )
94112def _check_comparison (frozen_tally_type ):
95113 tally_0 = frozen_tally_type ('c' )
96114 tally_1 = frozen_tally_type ('abc' )
@@ -120,6 +138,7 @@ def _check_comparison(frozen_tally_type):
120138 for not_smaller_item in not_smaller_items :
121139 assert not item < smaller_item
122140
141+ @_test_on (Tally , OrderedTally , FrozenTally , FrozenOrderedTally )
123142def _check_ignores_zero (frozen_tally_type ):
124143 frozen_tally_0 = frozen_tally_type ({'a' : 0 ,})
125144 frozen_tally_1 = frozen_tally_type ()
@@ -138,6 +157,7 @@ def _check_ignores_zero(frozen_tally_type):
138157 {frozen_tally_3 }
139158
140159
160+ @_test_on (Tally , OrderedTally , FrozenTally , FrozenOrderedTally )
141161def _check_immutable (frozen_tally_type ):
142162 frozen_tally = frozen_tally_type ('abracadabra' )
143163 with cute_testing .RaiseAssertor (TypeError ):
@@ -148,6 +168,7 @@ def _check_immutable(frozen_tally_type):
148168 frozen_tally ['a' ] = 7
149169
150170
171+ @_test_on (Tally , OrderedTally , FrozenTally , FrozenOrderedTally )
151172def _check_immutable (frozen_tally_type ):
152173 frozen_tally = frozen_tally_type ('abracadabra' )
153174 with cute_testing .RaiseAssertor (TypeError ):
@@ -157,6 +178,7 @@ def _check_immutable(frozen_tally_type):
157178 with cute_testing .RaiseAssertor (TypeError ):
158179 frozen_tally ['a' ] = 7
159180
181+ @_test_on (Tally , OrderedTally , FrozenTally , FrozenOrderedTally )
160182def _check_only_positive_ints_or_zero (frozen_tally_type ):
161183 assert frozen_tally_type (
162184 OrderedDict ([('a' , 0 ), ('b' , 0.0 ), ('c' , 1 ), ('d' , 2.0 ),
@@ -182,8 +204,9 @@ def _check_only_positive_ints_or_zero(frozen_tally_type):
182204 frozen_tally_type ({'a' : ('still' , 'nope' ),})
183205
184206
185-
186- def test_ordered ():
207+
208+ @_test_on (Tally , OrderedTally , FrozenTally , FrozenOrderedTally )
209+ def _check_ordered ():
187210 frozen_tally_0 = FrozenTally ('ababb' )
188211 frozen_tally_1 = FrozenTally ('bbbaa' )
189212 assert frozen_tally_0 == frozen_tally_1
@@ -199,6 +222,8 @@ def test_ordered():
199222 assert frozen_ordered_tally_0 <= frozen_ordered_tally_1
200223 assert frozen_ordered_tally_0 >= frozen_ordered_tally_1
201224
225+
226+ @_test_on (Tally , OrderedTally , FrozenTally , FrozenOrderedTally )
202227def test_repr ():
203228 assert re .match (
204229 "^FrozenTally\\ ({(?:(?:'b': 3, 'a': 2)|(?:'a': 2, 'b': 3))}\\ )$" ,
0 commit comments