@@ -97,6 +97,110 @@ def test_get_or_default(self):
97
97
assert t1 .get_or_default ('fff' , delayed_partial (lambda : 2345 )) == 2345
98
98
assert not t1 .contains ('fff' )
99
99
100
+ def test_pop (self ):
101
+ t = create_storage ({'a' : 1 , 'b' : 2 , 'c' : raw ({'x' : 3 , 'y' : 4 }), 'd' : {'x' : 3 , 'y' : 4 }})
102
+ assert t .pop ('a' ) == 1
103
+ with pytest .raises (KeyError ):
104
+ t .pop ('a' )
105
+
106
+ assert t .pop ('b' ) == 2
107
+ assert t .pop ('c' ) == {'x' : 3 , 'y' : 4 }
108
+
109
+ td = t .pop ('d' )
110
+ assert isinstance (td , TreeStorage )
111
+ assert td .get ('x' ) == 3
112
+ assert td .get ('y' ) == 4
113
+
114
+ with pytest .raises (KeyError ):
115
+ t .pop ('aksjdlasdkjf' )
116
+
117
+ cnt1 , cnt2 , cnt3 = 0 , 0 , 0
118
+
119
+ def f1 ():
120
+ nonlocal cnt1
121
+ cnt1 += 1
122
+ return 2
123
+
124
+ def f2 (x , y ):
125
+ nonlocal cnt2
126
+ cnt2 += 1
127
+ return {'x' : x , 'y' : y }
128
+
129
+ def f3 (x , y ):
130
+ nonlocal cnt3
131
+ cnt3 += 1
132
+ return create_storage ({'x' : x , 'y' : raw (y )})
133
+
134
+ t2 = create_storage ({
135
+ 'a' : 1 ,
136
+ 'b' : delayed_partial (f1 ),
137
+ 'c' : delayed_partial (f2 , delayed_partial (f1 ), 3 ),
138
+ 'd' : delayed_partial (f3 , 3 , delayed_partial (f2 , 3 , 4 ))
139
+ })
140
+
141
+ assert t2 .pop ('a' ) == 1
142
+
143
+ assert cnt1 == 0
144
+ assert t2 .pop ('b' ) == 2
145
+ assert cnt1 == 1
146
+ with pytest .raises (KeyError ):
147
+ t2 .pop ('b' )
148
+ assert cnt1 == 1
149
+
150
+ assert (cnt1 , cnt2 ) == (1 , 0 )
151
+ assert t2 .pop ('c' ) == {'x' : 2 , 'y' : 3 }
152
+ assert (cnt1 , cnt2 ) == (2 , 1 )
153
+ with pytest .raises (KeyError ):
154
+ t2 .pop ('c' )
155
+ assert (cnt1 , cnt2 ) == (2 , 1 )
156
+
157
+ assert (cnt1 , cnt2 , cnt3 ) == (2 , 1 , 0 )
158
+ assert t2 .get ('d' ).pop ('x' ) == 3
159
+ assert t2 .get ('d' ).pop ('y' ) == {'x' : 3 , 'y' : 4 }
160
+ assert (cnt1 , cnt2 , cnt3 ) == (2 , 2 , 1 )
161
+ with pytest .raises (KeyError ):
162
+ t2 .get ('d' ).pop ('x' )
163
+ with pytest .raises (KeyError ):
164
+ t2 .get ('d' ).pop ('y' )
165
+ assert (cnt1 , cnt2 , cnt3 ) == (2 , 2 , 1 )
166
+
167
+ def test_pop_or_default (self ):
168
+ t = create_storage ({'a' : 1 , 'b' : 2 , 'c' : raw ({'x' : 3 , 'y' : 4 }), 'd' : {'x' : 3 , 'y' : 4 }})
169
+ assert t .pop_or_default ('a' , 233 ) == 1
170
+ with pytest .raises (KeyError ):
171
+ t .pop ('a' )
172
+ assert t .pop_or_default ('a' , 233 ) == 233
173
+
174
+ assert t .pop_or_default ('b' , 233 ) == 2
175
+ assert t .pop_or_default ('c' , 233 ) == {'x' : 3 , 'y' : 4 }
176
+
177
+ td = t .pop_or_default ('d' , 233 )
178
+ assert isinstance (td , TreeStorage )
179
+ assert td .pop_or_default ('x' , 233 ) == 3
180
+ assert td .pop_or_default ('y' , 233 ) == 4
181
+
182
+ assert t .pop_or_default ('fff' , 233 ) == 233
183
+
184
+ t = create_storage ({'a' : 1 , 'b' : 2 , 'c' : raw ({'x' : 3 , 'y' : 4 }), 'd' : {'x' : 3 , 'y' : 4 }})
185
+ t1 = create_storage ({
186
+ 'a' : delayed_partial (lambda : t .get ('a' )),
187
+ 'b' : delayed_partial (lambda : t .get ('b' )),
188
+ 'c' : delayed_partial (lambda : t .get ('c' )),
189
+ 'd' : delayed_partial (lambda : t .get ('d' )),
190
+ })
191
+ assert t1 .pop_or_default ('a' , 233 ) == 1
192
+ assert t1 .pop_or_default ('b' , 233 ) == 2
193
+ assert t1 .pop_or_default ('c' , 233 ) == {'x' : 3 , 'y' : 4 }
194
+
195
+ t1d = t1 .pop_or_default ('d' , 233 )
196
+ assert isinstance (t1d , TreeStorage )
197
+ assert t1d .pop_or_default ('x' , 233 ) == 3
198
+ assert t1d .pop_or_default ('y' , 233 ) == 4
199
+
200
+ assert t1 .pop_or_default ('fff' , 233 ) == 233
201
+ assert t1 .pop_or_default ('fff' , delayed_partial (lambda : 2345 )) == 2345
202
+ assert not t1 .contains ('fff' )
203
+
100
204
def test_set (self ):
101
205
t = create_storage ({})
102
206
t .set ('a' , 1 )
0 commit comments