/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_lazy_import.py

  • Committer: John Arbash Meinel
  • Date: 2006-10-06 05:53:44 UTC
  • mfrom: (2063 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2071.
  • Revision ID: john@arbash-meinel.com-20061006055344-e73b97b7c6ca6e72
[merge] bzr.dev 2063

Show diffs side-by-side

added added

removed removed

Lines of Context:
97
97
    """
98
98
 
99
99
    def test_object(self):
100
 
 
 
100
        """ScopeReplacer can create an instance in local scope.
 
101
        
 
102
        An object should appear in globals() by constructing a ScopeReplacer,
 
103
        and it will be replaced with the real object upon the first request.
 
104
        """
101
105
        actions = []
102
106
        InstrumentedReplacer.use_actions(actions)
103
107
        TestClass.use_actions(actions)
114
118
        else:
115
119
            self.fail('test_obj1 was not supposed to exist yet')
116
120
 
 
121
        orig_globals = set(globals().keys())
 
122
 
117
123
        InstrumentedReplacer(scope=globals(), name='test_obj1',
118
124
                             factory=factory)
119
125
 
 
126
        new_globals = set(globals().keys())
 
127
 
120
128
        # We can't use isinstance() because that uses test_obj1.__class__
121
129
        # and that goes through __getattribute__ which would activate
122
130
        # the replacement
133
141
                          ('foo', 2),
134
142
                         ], actions)
135
143
 
 
144
    def test_replace_side_effects(self):
 
145
        """Creating a new object should only create one entry in globals.
 
146
 
 
147
        And only that entry even after replacement.
 
148
        """
 
149
        try:
 
150
            test_scope1
 
151
        except NameError:
 
152
            # test_scope1 shouldn't exist yet
 
153
            pass
 
154
        else:
 
155
            self.fail('test_scope1 was not supposed to exist yet')
 
156
 
 
157
        # ignore the logged actions
 
158
        TestClass.use_actions([])
 
159
 
 
160
        def factory(replacer, scope, name):
 
161
            return TestClass()
 
162
 
 
163
        orig_globals = set(globals().keys())
 
164
 
 
165
        lazy_import.ScopeReplacer(scope=globals(), name='test_scope1',
 
166
                                  factory=factory)
 
167
 
 
168
        new_globals = set(globals().keys())
 
169
 
 
170
        self.assertEqual(lazy_import.ScopeReplacer,
 
171
                         object.__getattribute__(test_scope1, '__class__'))
 
172
        self.assertEqual('foo', test_scope1.foo(1))
 
173
        self.assertIsInstance(test_scope1, TestClass)
 
174
 
 
175
        final_globals = set(globals().keys())
 
176
 
 
177
        self.assertEqual(set(['test_scope1']), new_globals - orig_globals)
 
178
        self.assertEqual(set(), orig_globals - new_globals)
 
179
        self.assertEqual(set(), final_globals - new_globals)
 
180
        self.assertEqual(set(), new_globals - final_globals)
 
181
 
136
182
    def test_class(self):
137
183
        actions = []
138
184
        InstrumentedReplacer.use_actions(actions)