@@ -249,3 +249,71 @@ def test_pprint(self, capfd, fake_manager):
249
249
" 'ham': 'eggseggseggseggseggseggseggseggseggseggseggseggseggseggseggs'}\n "
250
250
)
251
251
assert stderr == ""
252
+
253
+ def test_asdict (self , fake_manager ):
254
+ fake_object = FakeObject (fake_manager , {"attr1" : "foo" , "alist" : [1 , 2 , 3 ]})
255
+ assert fake_object .attr1 == "foo"
256
+ result = fake_object .asdict ()
257
+ assert result == {"attr1" : "foo" , "alist" : [1 , 2 , 3 ]}
258
+ # Demonstrate modifying the dictionary does not modify the object
259
+ result ["attr1" ] = "testing"
260
+ result ["alist" ].append (4 )
261
+ assert result == {"attr1" : "testing" , "alist" : [1 , 2 , 3 , 4 ]}
262
+ assert fake_object .attr1 == "foo"
263
+ assert fake_object .alist == [1 , 2 , 3 ]
264
+ # asdict() returns the updated value
265
+ fake_object .attr1 = "spam"
266
+ assert fake_object .asdict () == {"attr1" : "spam" , "alist" : [1 , 2 , 3 ]}
267
+ # Modify attribute and then ensure modifying a list in the returned dict won't
268
+ # modify the list in the object.
269
+ fake_object .attr1 = [9 , 7 , 8 ]
270
+ assert fake_object .asdict () == {
271
+ "attr1" : [9 , 7 , 8 ],
272
+ "alist" : [1 , 2 , 3 ],
273
+ }
274
+ result = fake_object .asdict ()
275
+ result ["attr1" ].append (1 )
276
+ assert fake_object .asdict () == {
277
+ "attr1" : [9 , 7 , 8 ],
278
+ "alist" : [1 , 2 , 3 ],
279
+ }
280
+
281
+ def test_attributes (self , fake_manager ):
282
+ fake_object = FakeObject (fake_manager , {"attr1" : [1 , 2 , 3 ]})
283
+ assert fake_object .attr1 == [1 , 2 , 3 ]
284
+ result = fake_object .attributes
285
+ assert result == {"attr1" : [1 , 2 , 3 ]}
286
+
287
+ # Updated attribute value is not reflected in `attributes`
288
+ fake_object .attr1 = "hello"
289
+ assert fake_object .attributes == {"attr1" : [1 , 2 , 3 ]}
290
+ assert fake_object .attr1 == "hello"
291
+ # New attribute is in `attributes`
292
+ fake_object .new_attrib = "spam"
293
+ assert fake_object .attributes == {"attr1" : [1 , 2 , 3 ], "new_attrib" : "spam" }
294
+
295
+ # Modifying the dictionary can cause modification to the object :(
296
+ result = fake_object .attributes
297
+ result ["attr1" ].append (10 )
298
+ assert result == {"attr1" : [1 , 2 , 3 , 10 ], "new_attrib" : "spam" }
299
+ assert fake_object .attributes == {"attr1" : [1 , 2 , 3 , 10 ], "new_attrib" : "spam" }
300
+ assert fake_object .attr1 == "hello"
301
+
302
+
303
+ def test_asdict_vs_attributes (self , fake_manager ):
304
+ fake_object = FakeObject (fake_manager , {"attr1" : "foo" })
305
+ assert fake_object .attr1 == "foo"
306
+ result = fake_object .asdict ()
307
+ assert result == {"attr1" : "foo" }
308
+
309
+ # New attribute added, return same result
310
+ assert fake_object .attributes == fake_object .asdict ()
311
+ fake_object .attr2 = "eggs"
312
+ assert fake_object .attributes == fake_object .asdict ()
313
+ # Update attribute, return different result
314
+ fake_object .attr1 = "hello"
315
+ assert fake_object .attributes != fake_object .asdict ()
316
+ # asdict() returns the updated value
317
+ assert fake_object .asdict () == {"attr1" : "hello" , "attr2" : "eggs" }
318
+ # `attributes` returns original value
319
+ assert fake_object .attributes == {"attr1" : "foo" , "attr2" : "eggs" }
0 commit comments