46
46
'{"address": 7, "type": "tuple", "size": 20, "len": 2, "refs": [4, 5]}',
47
47
'{"address": 6, "type": "str", "size": 29, "len": 5, "value": "a str"'
49
'{"address": 8, "type": "module", "name": "mymod", "size": 60, "refs": [2]}',
49
'{"address": 8, "type": "module", "size": 60, "name": "mymod", "refs": [2]}',
52
52
# Note that this doesn't have a complete copy of the references. Namely when
87
87
'{"address": 8, "type": "tuple", "size": 28, "len": 0, "refs": []}',
91
'{"address": 2, "type": "str", "size": 25, "len": 1, "value": "a", "refs": []}',
92
'{"address": 3, "type": "str", "size": 25, "len": 1, "value": "b", "refs": []}',
93
'{"address": 4, "type": "str", "size": 25, "len": 1, "value": "c", "refs": []}',
94
'{"address": 5, "type": "str", "size": 25, "len": 1, "value": "d", "refs": []}',
95
'{"address": 6, "type": "dict", "size": 512, "refs": [2, 5, 5, 5, 4, 4, 3, 3]}',
96
'{"address": 7, "type": "dict", "size": 512, "refs": [8, 8, 5, 5, 4, 4, 3, 3]}',
97
'{"address": 8, "type": "dict", "size": 512, "refs": [2, 2, 5, 5, 4, 4, 3, 3]}',
91
101
class TestLoad(tests.TestCase):
102
112
manager = loader.load(t_file, show_prog=False)
103
113
test_dict_id = id(test_dict)
104
if test_dict_id > sys.maxint:
105
# We wrapped around to the negative value, note, this needs to be
106
# re-evaluated for 64-bit versions of python
107
test_dict_id = int(test_dict_id - 2 * (sys.maxint + 1))
108
self.assertTrue(test_dict_id in manager.objs)
114
self.assertTrue(test_dict_id in manager.objs,
115
'%s not found in %s' % (test_dict_id, manager.objs.keys()))
110
117
def test_load_one(self):
111
118
objs = loader.load([
119
126
# the objs dictionary.
120
127
self.assertTrue(keys[0] is obj.address)
129
def test_load_without_simplejson(self):
131
'{"address": 1234, "type": "int", "size": 12, "value": 10'
133
'{"address": 2345, "type": "module", "size": 60, "name": "mymod"'
135
'{"address": 4567, "type": "str", "size": 150, "len": 126'
136
', "value": "Test \\\'whoami\\\'\\u000a\\"Your name\\"'
138
], using_json=False, show_prog=False).objs
139
keys = sorted(objs.keys())
140
self.assertEqual([1234, 2345, 4567], keys)
142
self.assertTrue(isinstance(obj, _loader._MemObjectProxy))
143
# The address should be exactly the same python object as the key in
144
# the objs dictionary.
145
self.assertTrue(keys[0] is obj.address)
146
self.assertEqual(10, obj.value)
148
self.assertEqual("module", obj.type_str)
149
self.assertEqual("mymod", obj.value)
151
# Known failure? We don't unescape properly, also, I'm surprised this
152
# works. " should exit the " string, but \" seems to leave it. But the
153
# '\' is also left verbatim because it is a raw string...
154
self.assertEqual(r"Test \'whoami\'\u000a\"Your name\"", obj.value)
122
156
def test_load_example(self):
123
157
objs = loader.load(_example_dump, show_prog=False)
159
def test_load_defaults_to_computing_and_collapsing(self):
160
manager = loader.load(_instance_dump, show_prog=False, collapse=False)
161
instance_obj = manager[1]
162
self.assertEqual([2, 3], instance_obj.children)
163
manager = loader.load(_instance_dump, show_prog=False)
164
instance_obj = manager[1]
165
self.assertEqual([4, 5, 6, 7, 9, 10, 11, 12, 3], instance_obj.children)
125
167
def test_load_compressed(self):
126
168
# unfortunately NamedTemporaryFile's cannot be re-opened on Windows
127
169
fd, name = tempfile.mkstemp(prefix='meliae-')
272
314
', "value": "boo", "refs": []}')
273
315
lines.append('{"address": 12, "type": "dict", "size": 124'
275
manager = loader.load(lines, show_prog=False)
317
manager = loader.load(lines, show_prog=False, collapse=False)
276
318
mymod_dict = manager.objs[9]
277
319
self.assertEqual([10, 11], mymod_dict.children)
278
320
manager.remove_expensive_references()
283
325
self.assertEqual([11, 0], mymod_dict.children)
285
327
def test_collapse_instance_dicts(self):
286
manager = loader.load(_instance_dump, show_prog=False)
328
manager = loader.load(_instance_dump, show_prog=False, collapse=False)
287
329
# This should collapse all of the references from the instance's dict
288
330
# @2 into the instance @1
289
331
instance = manager.objs[1]
311
353
self.assertFalse(15 in manager.objs)
313
355
def test_collapse_old_instance_dicts(self):
314
manager = loader.load(_old_instance_dump, show_prog=False)
356
manager = loader.load(_old_instance_dump, show_prog=False,
315
358
instance = manager.objs[1]
316
359
self.assertEqual('instance', instance.type_str)
317
360
self.assertEqual(36, instance.size)
333
376
# TODO: This test fails if simplejson is not installed, because the
334
377
# regex extractor does not cast to integers (they stay as
335
378
# strings). We could fix the test, or fix the extractor.
336
manager = loader.load(_instance_dump, show_prog=False)
379
manager = loader.load(_instance_dump, show_prog=False, collapse=False)
337
380
as_dict = manager.refs_as_dict(manager[15])
338
381
self.assertEqual({1: 'c', 'b': 'c'}, as_dict)
339
382
manager.compute_parents()
348
391
# strings). We could fix the test, or fix the extractor.
349
392
manager = loader.load(_instance_dump, show_prog=False)
350
393
self.assertEqual([2], manager.refs_as_list(manager[12]))
395
def test_guess_intern_dict(self):
396
manager = loader.load(_intern_dict_dump, show_prog=False)
397
obj = manager.guess_intern_dict()
398
self.assertEqual(8, obj.address)