77import itertools
88import collections
99import decimal as decimal_module
10+ from python_toolbox .third_party import unittest2
1011
1112import nose
1213
2324infinity = float ('inf' )
2425infinities = (infinity , - infinity )
2526
26- class BaseBagTestCase (nose . Test ): # blocktodo: using cute testing class?1
27- def test_common (self , bag_type ):
27+ class BaseBagTestCase (cute_testing . TestCase ):
28+ def test_common (self ):
2829 bag = self .bag_type ('abracadabra' )
2930 assert bag == collections .Counter ('abracadabra' ) == \
3031 collections .Counter (bag ) == \
31- bag_type (collections .Counter ('abracadabra' ))
32+ self . bag_type (collections .Counter ('abracadabra' ))
3233
3334 assert len (bag ) == 5
3435 assert set (bag ) == set (bag .keys ()) == set ('abracadabra' )
@@ -45,27 +46,23 @@ def test_common(self, bag_type):
4546
4647 assert bag + bag == self .bag_type ('abracadabra' * 2 )
4748 assert bag - bag == self .bag_type ()
48- assert bag - bag_type ('a' ) == self .bag_type ('abracadabr' )
49- assert bag - bag_type ('a' ) == self .bag_type ('abracadabr' )
50- assert bag | bag_type ('a' ) == bag
49+ assert bag - self . bag_type ('a' ) == self .bag_type ('abracadabr' )
50+ assert bag - self . bag_type ('a' ) == self .bag_type ('abracadabr' )
51+ assert bag | self . bag_type ('a' ) == bag
5152 assert bag | bag == bag | bag | bag == bag
52- assert bag & bag_type ('a' ) == bag_type ('a' )
53+ assert bag & self . bag_type ('a' ) == self . bag_type ('a' )
5354 assert bag & bag == \
5455 bag & bag & bag == bag
5556
56- assert bag_type (bag .elements ()) == bag
57+ assert self . bag_type (bag .elements ()) == bag
5758
5859 assert + bag == bag
5960 with cute_testing .RaiseAssertor (TypeError ):
6061 - bag
6162
6263 assert re .match ('^(Frozen)?(Ordered)?Bag\(.*$' , repr (bag ))
6364
64- assert bag .copy ({'meow' : 9 }) == \
65- bag .copy (meow = 9 ) == \
66- bag_type (OrderedDict (
67- [('a' , 5 ), ('b' , 2 ), ('r' , 2 ), ('c' , 1 ), ('d' , 1 ), ('meow' , 9 )])
68- )
65+ assert bag .copy () == bag
6966
7067 assert pickle .loads (pickle .dumps (bag )) == bag
7168
@@ -93,11 +90,11 @@ def test_comparison(self):
9390 not_a_bag = {}
9491
9592 hierarchy = (
96- (bag_4 , { bag_3 , bag_2 , bag_1 , bag_0 } ),
97- (bag_3 , { bag_1 , bag_0 } ),
98- (bag_2 , { bag_1 , bag_0 } ),
99- (bag_1 , { bag_0 } ),
100- (bag_0 , set ()),
93+ (bag_4 , [ bag_3 , bag_2 , bag_1 , bag_0 ] ),
94+ (bag_3 , [ bag_1 , bag_0 ] ),
95+ (bag_2 , [ bag_1 , bag_0 ] ),
96+ (bag_1 , [ bag_0 ] ),
97+ (bag_0 , []),
10198 )
10299
103100 for item , smaller_items in hierarchy :
@@ -110,7 +107,7 @@ def test_comparison(self):
110107 assert item > smaller_item
111108 assert item != smaller_item
112109 not_smaller_items = [item for item in next (zip (* hierarchy )) if
113- item not in smaller_item ]
110+ item not in smaller_items ]
114111 for not_smaller_item in not_smaller_items :
115112 assert not item < smaller_item
116113
@@ -155,31 +152,31 @@ def test_only_positive_ints_or_zero(self):
155152 with cute_testing .RaiseAssertor (TypeError ):
156153 self .bag_type ({'a' : ('still' , 'nope' ),})
157154
158- def test_ignores_zero (bag_type ):
159- bag_0 = bag_type ({'a' : 0 ,})
160- bag_1 = bag_type ()
155+ def test_ignores_zero (self ):
156+ bag_0 = self . bag_type ({'a' : 0 ,})
157+ bag_1 = self . bag_type ()
161158 assert bag_0 == bag_1
162159
163- if bag_type . is_frozen :
160+ if issubclass ( self . bag_type , collections . Hashable ) :
164161 assert hash (bag_0 ) == hash (bag_1 )
165162 assert {bag_0 , bag_1 } == {bag_0 } == {bag_1 }
166163
167164 bag_2 = \
168- bag_type ({'a' : 0.0 , 'b' : 2 , 'c' : decimal_module .Decimal ('0.0' ),})
169- bag_3 = bag_type ('bb' )
165+ self . bag_type ({'a' : 0.0 , 'b' : 2 , 'c' : decimal_module .Decimal ('0.0' ),})
166+ bag_3 = self . bag_type ('bb' )
170167
171- if bag_type . is_frozen :
168+ if issubclass ( self . bag_type , collections . Hashable ) :
172169 assert hash (bag_2 ) == hash (bag_3 )
173170 assert {bag_2 , bag_3 } == {bag_2 } == {bag_3 }
174171
175172
176173
177174
178175class BaseMutableBagTestCase (BaseBagTestCase ):
179- is_frozen = False
180-
181176 def test_hash (self ):
182177 bag = self .bag_type ('abracadabra' )
178+ assert not isinstance (bag , collections .Hashable )
179+ assert not issubclass (self .bag_type , collections .Hashable )
183180 with cute_testing .RaiseAssertor (TypeError ):
184181 {bag }
185182 with cute_testing .RaiseAssertor (TypeError ):
@@ -266,10 +263,11 @@ def test_mutating(bag_type):
266263
267264
268265class BaseFrozenBagTestCase (BaseBagTestCase ):
269- is_frozen = True
270266
271267 def test_hash (self ):
272268 bag = self .bag_type ('abracadabra' )
269+ assert isinstance (bag , collections .Hashable )
270+ assert issubclass (self .bag_type , collections .Hashable )
273271 assert {bag , bag } == {bag }
274272 assert {bag : bag } == {bag : bag }
275273 assert isinstance (hash (bag ), int )
0 commit comments