~ubuntu-branches/debian/sid/meliae/sid

« back to all changes in this revision

Viewing changes to meliae/tests/test_loader.py

  • Committer: Bazaar Package Importer
  • Author(s): Jelmer Vernooij
  • Date: 2010-07-20 18:26:22 UTC
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100720182622-b0ks0nu34mify1lj
Tags: upstream-0.2.1
ImportĀ upstreamĀ versionĀ 0.2.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
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"'
48
48
 ', "refs": []}',
49
 
'{"address": 8, "type": "module", "name": "mymod", "size": 60, "refs": [2]}',
 
49
'{"address": 8, "type": "module", "size": 60, "name": "mymod", "refs": [2]}',
50
50
]
51
51
 
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": []}',
88
88
]
89
89
 
 
90
_intern_dict_dump = [
 
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]}',
 
98
]
 
99
 
90
100
 
91
101
class TestLoad(tests.TestCase):
92
102
 
101
111
        t_file.seek(0)
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()))
109
116
 
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)
121
128
 
 
129
    def test_load_without_simplejson(self):
 
130
        objs = loader.load([
 
131
            '{"address": 1234, "type": "int", "size": 12, "value": 10'
 
132
                ', "refs": []}',
 
133
            '{"address": 2345, "type": "module", "size": 60, "name": "mymod"'
 
134
                ', "refs": [1234]}',
 
135
            '{"address": 4567, "type": "str", "size": 150, "len": 126'
 
136
                ', "value": "Test \\\'whoami\\\'\\u000a\\"Your name\\"'
 
137
                ', "refs": []}'
 
138
            ], using_json=False, show_prog=False).objs
 
139
        keys = sorted(objs.keys())
 
140
        self.assertEqual([1234, 2345, 4567], keys)
 
141
        obj = objs[1234]
 
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)
 
147
        obj = objs[2345]
 
148
        self.assertEqual("module", obj.type_str)
 
149
        self.assertEqual("mymod", obj.value)
 
150
        obj = objs[4567]
 
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)
 
155
 
122
156
    def test_load_example(self):
123
157
        objs = loader.load(_example_dump, show_prog=False)
124
158
 
 
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)
 
166
 
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'
274
316
                     ', "refs": []}')
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)
284
326
 
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)
312
354
 
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,
 
357
                              collapse=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]))
 
394
 
 
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)