~ubuntu-branches/ubuntu/utopic/python-apptools/utopic

« back to all changes in this revision

Viewing changes to enthought/naming/tests/context_test_case.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-07-08 23:55:50 UTC
  • mfrom: (2.1.9 sid)
  • Revision ID: james.westby@ubuntu.com-20110708235550-yz5u79ubeo4dhyfx
Tags: 4.0.0-1
* New upstream release
* Update debian/watch file

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#------------------------------------------------------------------------------
2
 
# Copyright (c) 2005, Enthought, Inc.
3
 
# All rights reserved.
4
 
5
 
# This software is provided without warranty under the terms of the BSD
6
 
# license included in enthought/LICENSE.txt and may be redistributed only
7
 
# under the conditions described in the aforementioned license.  The license
8
 
# is also available online at http://www.enthought.com/licenses/BSD.txt
9
 
# Thanks for using Enthought open source!
10
 
11
 
# Author: Enthought, Inc.
12
 
# Description: <Enthought naming package component>
13
 
#------------------------------------------------------------------------------
14
 
""" Tests operations that span contexts. """
15
 
 
16
 
 
17
 
# Standard library imports.
18
 
import unittest
19
 
 
20
 
# Enthought library imports.
21
 
from enthought.naming.api import *
22
 
 
23
 
 
24
 
class ContextTestCase(unittest.TestCase):
25
 
    """ Tests naming operations that span contexts. """
26
 
 
27
 
    ###########################################################################
28
 
    # 'TestCase' interface.
29
 
    ###########################################################################
30
 
 
31
 
    def setUp(self):
32
 
        """ Prepares the test fixture before each test method is called. """
33
 
 
34
 
        self.context = self.create_context()
35
 
        self.context.create_subcontext('sub')
36
 
                       
37
 
        return
38
 
 
39
 
    def tearDown(self):
40
 
        """ Called immediately after each test method has been called. """
41
 
 
42
 
        self.context = None
43
 
        
44
 
        return
45
 
 
46
 
    ###########################################################################
47
 
    # 'ContextTestCase' interface.
48
 
    ###########################################################################
49
 
 
50
 
    def create_context(self):
51
 
        """ Creates the context that we are testing. """
52
 
 
53
 
        return Context()
54
 
    
55
 
    ###########################################################################
56
 
    # Tests.
57
 
    ###########################################################################
58
 
 
59
 
    def test_bind(self):
60
 
        """ bind """
61
 
 
62
 
        # Convenience.
63
 
        context = self.context
64
 
        sub = self.context.lookup('sub')
65
 
        self.assert_(isinstance(sub, Context))
66
 
 
67
 
        # Make sure that the sub-context is empty.
68
 
        self.assertEqual(len(sub.list_bindings('')), 0)
69
 
        
70
 
        # Empty name.
71
 
        self.failUnlessRaises(InvalidNameError, context.bind, '', 1)
72
 
 
73
 
        # Attempt to resolve via a non-existent context.
74
 
        self.failUnlessRaises(NameNotFoundError, context.bind, 'xx/a', 1)
75
 
        self.assertEqual(len(sub.list_bindings('')), 0)
76
 
 
77
 
        # Bind a name.
78
 
        context.bind('sub/a', 1)
79
 
        self.assertEqual(len(sub.list_bindings('')), 1)
80
 
 
81
 
        # Attempt to resolve via an existing name that is not a context.
82
 
        self.failUnlessRaises(NotContextError, context.bind, 'sub/a/xx', 1)
83
 
        self.assertEqual(len(sub.list_bindings('')), 1)
84
 
 
85
 
        # Try to bind it again.
86
 
        self.failUnlessRaises(NameAlreadyBoundError, context.bind, 'sub/a', 1)
87
 
 
88
 
        return
89
 
 
90
 
    def test_bind_with_make_contexts(self):
91
 
        """ bind with make contexts """
92
 
 
93
 
        # Convenience.
94
 
        context = self.context
95
 
        sub = self.context.lookup('sub')
96
 
        self.assert_(isinstance(sub, Context))
97
 
 
98
 
        # Make sure that the sub-context is empty.
99
 
        self.assertEqual(len(sub.list_bindings('')), 0)
100
 
        
101
 
        # Empty name.
102
 
        self.failUnlessRaises(InvalidNameError, context.bind, '', 1, True)
103
 
 
104
 
        # Attempt to resolve via a non-existent context - which should result
105
 
        # in the context being created automatically.
106
 
        context.bind('xx/a', 1, True)
107
 
        self.assertEqual(len(context.list_bindings('xx')), 1)
108
 
        self.assertEqual(1, context.lookup('xx/a'))
109
 
                         
110
 
        # Bind an even more 'nested' name.
111
 
        context.bind('xx/foo/bar/baz', 42, True)
112
 
        self.assertEqual(len(context.list_bindings('xx/foo/bar')), 1)
113
 
        self.assertEqual(42, context.lookup('xx/foo/bar/baz'))
114
 
 
115
 
        return
116
 
 
117
 
    def test_rebind(self):
118
 
        """ context rebind """
119
 
 
120
 
        # Convenience.
121
 
        context = self.context
122
 
        sub = self.context.lookup('sub')
123
 
 
124
 
        # Make sure that the sub-context is empty.
125
 
        self.assertEqual(len(sub.list_bindings('')), 0)
126
 
 
127
 
        # Empty name.
128
 
        self.failUnlessRaises(InvalidNameError, context.rebind, '', 1)
129
 
 
130
 
        # Attempt to resolve via a non-existent context.
131
 
        self.failUnlessRaises(NameNotFoundError, context.rebind, 'xx/a', 1)
132
 
        self.assertEqual(len(sub.list_bindings('')), 0)
133
 
        
134
 
        # Bind a name.
135
 
        context.bind('sub/a', 1)
136
 
        self.assertEqual(len(sub.list_bindings('')), 1)
137
 
 
138
 
        # Rebind it.
139
 
        context.rebind('sub/a', 1)
140
 
        self.assertEqual(len(sub.list_bindings('')), 1)
141
 
 
142
 
        # Attempt to resolve via an existing name that is not a context.
143
 
        self.failUnlessRaises(NotContextError, context.rebind, 'sub/a/xx', 1)
144
 
        self.assertEqual(len(sub.list_bindings('')), 1)
145
 
 
146
 
        return
147
 
 
148
 
    def test_rebind_with_make_contexts(self):
149
 
        """ rebind with make contexts """
150
 
 
151
 
        # Convenience.
152
 
        context = self.context
153
 
        sub = self.context.lookup('sub')
154
 
        self.assert_(isinstance(sub, Context))
155
 
 
156
 
        # Make sure that the sub-context is empty.
157
 
        self.assertEqual(len(sub.list_bindings('')), 0)
158
 
        
159
 
        # Empty name.
160
 
        self.failUnlessRaises(InvalidNameError, context.rebind, '', 1, True)
161
 
 
162
 
        # Attempt to resolve via a non-existent context - which should result
163
 
        # in the context being created automatically.
164
 
        context.rebind('xx/a', 1, True)
165
 
        self.assertEqual(len(context.list_bindings('xx')), 1)
166
 
        self.assertEqual(1, context.lookup('xx/a'))
167
 
                         
168
 
        # Rebind an even more 'nested' name.
169
 
        context.rebind('xx/foo/bar/baz', 42, True)
170
 
        self.assertEqual(len(context.list_bindings('xx/foo/bar')), 1)
171
 
        self.assertEqual(42, context.lookup('xx/foo/bar/baz'))
172
 
 
173
 
        # And do it again... (this is REbind after all).
174
 
        context.rebind('xx/foo/bar/baz', 42, True)
175
 
        self.assertEqual(len(context.list_bindings('xx/foo/bar')), 1)
176
 
        self.assertEqual(42, context.lookup('xx/foo/bar/baz'))
177
 
 
178
 
        return
179
 
 
180
 
    def test_unbind(self):
181
 
        """ context unbind """
182
 
 
183
 
        # Convenience.
184
 
        context = self.context
185
 
        sub = self.context.lookup('sub')
186
 
 
187
 
        # Make sure that the sub-context is empty.
188
 
        self.assertEqual(len(sub.list_bindings('')), 0)
189
 
 
190
 
        # Empty name.
191
 
        self.failUnlessRaises(InvalidNameError, context.unbind, '')
192
 
 
193
 
        # Attempt to resolve via a non-existent context.
194
 
        self.failUnlessRaises(NameNotFoundError, context.unbind, 'xx/a')
195
 
        self.assertEqual(len(sub.list_bindings('')), 0)
196
 
 
197
 
        # Bind a name.
198
 
        context.bind('sub/a', 1)
199
 
        self.assertEqual(len(sub.list_bindings('')), 1)
200
 
 
201
 
        # Attempt to resolve via an existing name that is not a context.
202
 
        self.failUnlessRaises(NotContextError, context.unbind, 'sub/a/xx')
203
 
        self.assertEqual(len(sub.list_bindings('')), 1)
204
 
 
205
 
        # Unbind it.
206
 
        context.unbind('sub/a')
207
 
        self.assertEqual(len(sub.list_bindings('')), 0)
208
 
 
209
 
        # Try to unbind a non-existent name.
210
 
        self.failUnlessRaises(NameNotFoundError, context.unbind, 'sub/b')
211
 
 
212
 
        return
213
 
 
214
 
    def test_rename_object(self):
215
 
        """ rename an object """
216
 
 
217
 
        # Convenience.
218
 
        context = self.context
219
 
        sub = self.context.lookup('sub')
220
 
 
221
 
        # Make sure that the sub-context is empty.
222
 
        self.assertEqual(len(sub.list_bindings('')), 0)
223
 
 
224
 
        # Empty name.
225
 
        self.failUnlessRaises(InvalidNameError, context.rename, '', 'x')
226
 
        self.failUnlessRaises(InvalidNameError, context.rename, 'x', '')
227
 
 
228
 
        # Attempt to resolve via a non-existent context.
229
 
        self.failUnlessRaises(NameNotFoundError, context.rename, 'x/a', 'x/b')
230
 
        self.assertEqual(len(sub.list_bindings('')), 0)
231
 
        
232
 
        # Bind a name.
233
 
        context.bind('sub/a', 1)
234
 
        self.assertEqual(len(sub.list_bindings('')), 1)
235
 
 
236
 
        # Attempt to resolve via an existing name that is not a context.
237
 
        self.failUnlessRaises(NotContextError, context.bind, 'sub/a/xx', 1)
238
 
        self.assertEqual(len(sub.list_bindings('')), 1)
239
 
 
240
 
        # Rename it.
241
 
        context.rename('sub/a', 'sub/b')
242
 
        self.assertEqual(len(sub.list_bindings('')), 1)
243
 
 
244
 
        # Lookup using the new name.
245
 
        self.assertEqual(context.lookup('sub/b'), 1)
246
 
        
247
 
        # Lookup using the old name.
248
 
        self.failUnlessRaises(NameNotFoundError, context.lookup, 'sub/a')
249
 
 
250
 
        return
251
 
 
252
 
    def test_rename_context(self):
253
 
        """ rename a context """
254
 
 
255
 
        # Convenience.
256
 
        context = self.context
257
 
        sub = self.context.lookup('sub')
258
 
 
259
 
        # Make sure that the sub-context is empty.
260
 
        self.assertEqual(len(sub.list_bindings('')), 0)
261
 
 
262
 
        # Empty name.
263
 
        self.failUnlessRaises(InvalidNameError, context.rename, '', 'x')
264
 
        self.failUnlessRaises(InvalidNameError, context.rename, 'x', '')
265
 
 
266
 
        # Attempt to resolve via a non-existent context.
267
 
        self.failUnlessRaises(NameNotFoundError, context.rename, 'x/a', 'x/b')
268
 
        self.assertEqual(len(sub.list_bindings('')), 0)
269
 
        
270
 
        # Create a context.
271
 
        context.create_subcontext('sub/a')
272
 
        self.assertEqual(len(sub.list_bindings('')), 1)
273
 
 
274
 
        # Rename it.
275
 
        context.rename('sub/a', 'sub/b')
276
 
        self.assertEqual(len(sub.list_bindings('')), 1)
277
 
 
278
 
        # Lookup using the new name.
279
 
        context.lookup('sub/b')
280
 
        
281
 
        # Lookup using the old name.
282
 
        self.failUnlessRaises(NameNotFoundError, context.lookup, 'sub/a')
283
 
 
284
 
        return
285
 
 
286
 
    def test_lookup(self):
287
 
        """ lookup """
288
 
 
289
 
        # Convenience.
290
 
        context = self.context
291
 
        sub = self.context.lookup('sub')
292
 
 
293
 
        # Make sure that the sub-context is empty.
294
 
        self.assertEqual(len(sub.list_bindings('')), 0)
295
 
 
296
 
        # Attempt to resolve via a non-existent context.
297
 
        self.failUnlessRaises(NameNotFoundError, context.lookup, 'xx/a')
298
 
        self.assertEqual(len(sub.list_bindings('')), 0)
299
 
 
300
 
        # Bind a name.
301
 
        context.bind('sub/a', 1)
302
 
        self.assertEqual(len(sub.list_bindings('')), 1)
303
 
 
304
 
        # Attempt to resolve via an existing name that is not a context.
305
 
        self.failUnlessRaises(NotContextError, context.lookup, 'sub/a/xx')
306
 
        self.assertEqual(len(sub.list_bindings('')), 1)
307
 
 
308
 
        # Look it up.
309
 
        self.assertEqual(context.lookup('sub/a'), 1)
310
 
 
311
 
        # Looking up the Empty name returns the context itself.
312
 
        self.assertEqual(context.lookup(''), context)
313
 
 
314
 
        # Non-existent name.
315
 
        self.failUnlessRaises(NameNotFoundError, context.lookup, 'sub/b')
316
 
 
317
 
        return
318
 
 
319
 
    def test_create_subcontext(self):
320
 
        """ create sub-context """
321
 
 
322
 
        # Convenience.
323
 
        context = self.context
324
 
        sub = self.context.lookup('sub')
325
 
 
326
 
        # Make sure that the sub-context is empty.
327
 
        self.assertEqual(len(sub.list_bindings('')), 0)
328
 
 
329
 
        # Empty name.
330
 
        self.failUnlessRaises(InvalidNameError, context.create_subcontext, '')
331
 
 
332
 
        # Attempt to resolve via a non-existent context.
333
 
        self.failUnlessRaises(
334
 
            NameNotFoundError, context.create_subcontext, 'xx/a'
335
 
        )
336
 
        self.assertEqual(len(sub.list_bindings('')), 0)
337
 
        
338
 
        # Create a sub-context.
339
 
        context.create_subcontext('sub/a')
340
 
        self.assertEqual(len(sub.list_bindings('')), 1)
341
 
 
342
 
        # Try to bind it again.
343
 
        self.failUnlessRaises(
344
 
            NameAlreadyBoundError, context.create_subcontext, 'sub/a'
345
 
        )
346
 
 
347
 
        # Bind a name.
348
 
        context.bind('sub/b', 1)
349
 
        self.assertEqual(len(sub.list_bindings('')), 2)
350
 
 
351
 
        # Attempt to resolve via an existing name that is not a context.
352
 
        self.failUnlessRaises(
353
 
            NotContextError, context.create_subcontext, 'sub/b/xx'
354
 
        )
355
 
        self.assertEqual(len(sub.list_bindings('')), 2)
356
 
 
357
 
        return
358
 
 
359
 
    def test_destroy_subcontext(self):
360
 
        """ single context destroy sub-context """
361
 
 
362
 
        # Convenience.
363
 
        context = self.context
364
 
        sub = self.context.lookup('sub')
365
 
 
366
 
        # Make sure that the sub-context is empty.
367
 
        self.assertEqual(len(sub.list_bindings('')), 0)
368
 
 
369
 
        # Empty name.
370
 
        self.failUnlessRaises(InvalidNameError, context.destroy_subcontext, '')
371
 
 
372
 
        # Attempt to resolve via a non-existent context.
373
 
        self.failUnlessRaises(
374
 
            NameNotFoundError, context.destroy_subcontext, 'xx/a'
375
 
        )
376
 
        self.assertEqual(len(sub.list_bindings('')), 0)
377
 
 
378
 
        # Create a sub-context.
379
 
        context.create_subcontext('sub/a')
380
 
        self.assertEqual(len(sub.list_bindings('')), 1)
381
 
 
382
 
        # Destroy it.
383
 
        context.destroy_subcontext('sub/a')
384
 
        self.assertEqual(len(sub.list_bindings('')), 0)
385
 
 
386
 
        # Bind a name.
387
 
        context.bind('sub/a', 1)
388
 
        self.assertEqual(len(sub.list_bindings('')), 1)
389
 
 
390
 
        # Try to destroy it.
391
 
        self.failUnlessRaises(
392
 
            NotContextError, context.destroy_subcontext, 'sub/a'
393
 
        )
394
 
 
395
 
        # Try to destroy a non-existent name.
396
 
        self.failUnlessRaises(
397
 
            NameNotFoundError, context.destroy_subcontext, 'sub/b'
398
 
        )
399
 
 
400
 
        # Attempt to resolve via an existing name that is not a context.
401
 
        self.failUnlessRaises(
402
 
            NotContextError, context.destroy_subcontext, 'sub/a/xx'
403
 
        )
404
 
        self.assertEqual(len(sub.list_bindings('')), 1)
405
 
        
406
 
        return
407
 
 
408
 
    def test_list_bindings(self):
409
 
        """ list bindings """
410
 
        
411
 
        # Convenience.
412
 
        context = self.context
413
 
        sub = self.context.lookup('sub')
414
 
 
415
 
        # List the bindings in the root.
416
 
        bindings = context.list_bindings('')
417
 
        self.assertEqual(len(bindings), 1)
418
 
 
419
 
        # List the names in the sub-context.
420
 
        bindings = context.list_bindings('sub')
421
 
        self.assertEqual(len(bindings), 0)
422
 
 
423
 
        # Attempt to resolve via a non-existent context.
424
 
        self.failUnlessRaises(NameNotFoundError, context.list_bindings, 'xx/a')
425
 
        self.assertEqual(len(sub.list_bindings('')), 0)
426
 
 
427
 
        # Bind a name.
428
 
        context.bind('sub/a', 1)
429
 
        self.assertEqual(len(sub.list_bindings('')), 1)
430
 
 
431
 
        # Attempt to resolve via an existing name that is not a context.
432
 
        self.failUnlessRaises(
433
 
            NotContextError, context.list_bindings, 'sub/a/xx'
434
 
        )
435
 
        self.assertEqual(len(sub.list_bindings('')), 1)
436
 
 
437
 
        return
438
 
 
439
 
    def test_list_names(self):
440
 
        """ list names """
441
 
        
442
 
        # Convenience.
443
 
        context = self.context
444
 
        sub = self.context.lookup('sub')
445
 
 
446
 
        # List the names in the root.
447
 
        names = context.list_names('')
448
 
        self.assertEqual(len(names), 1)
449
 
 
450
 
        # List the names in the sub-context.
451
 
        names = context.list_names('sub')
452
 
        self.assertEqual(len(names), 0)
453
 
 
454
 
        # Attempt to resolve via a non-existent context.
455
 
        self.failUnlessRaises(NameNotFoundError, context.list_names, 'xx/a')
456
 
        self.assertEqual(len(sub.list_bindings('')), 0)
457
 
 
458
 
        # Bind a name.
459
 
        context.bind('sub/a', 1)
460
 
        self.assertEqual(len(sub.list_bindings('')), 1)
461
 
 
462
 
        # Attempt to resolve via an existing name that is not a context.
463
 
        self.failUnlessRaises(
464
 
            NotContextError, context.list_names, 'sub/a/xx'
465
 
        )
466
 
        self.assertEqual(len(sub.list_bindings('')), 1)
467
 
 
468
 
        return
469
 
 
470
 
    def test_default_factories(self):
471
 
        """ default object and state factories. """
472
 
 
473
 
        object_factory = ObjectFactory()
474
 
        self.failUnlessRaises(
475
 
            NotImplementedError, object_factory.get_object_instance, 0, 0, 0
476
 
        )
477
 
 
478
 
        state_factory = StateFactory()
479
 
        self.failUnlessRaises(
480
 
            NotImplementedError, state_factory.get_state_to_bind, 0, 0, 0
481
 
        )
482
 
 
483
 
        return
484
 
    
485
 
    def test_search(self):
486
 
        """ test retrieving the names of bound objects """
487
 
        
488
 
        # Convenience.
489
 
        context = self.context
490
 
        sub = self.context.lookup('sub')
491
 
        sub_sibling = context.create_subcontext('sub sibling')
492
 
        sub_sub = sub.create_subcontext('sub sub')
493
 
        
494
 
        context.bind('one',1)
495
 
        names = context.search( 1 )
496
 
        self.assertEqual( len(names), 1)
497
 
        self.assertEqual( names[0], 'one' )
498
 
 
499
 
        names = sub.search(1)
500
 
        self.assertEqual( len(names), 0)
501
 
        
502
 
        context.bind('sub/two',2)
503
 
        names = context.search( 2 )
504
 
        self.assertEqual( len(names), 1)
505
 
        self.assertEqual( names[0], 'sub/two' )
506
 
 
507
 
        names = sub.search( 2 )
508
 
        self.assertEqual( len(names), 1)
509
 
        self.assertEqual( names[0], 'two' )
510
 
 
511
 
        context.bind('sub/sub sub/one',1)
512
 
        names = context.search( 1 )
513
 
        self.assertEqual( len(names), 2)
514
 
        self.assertEqual( names, ['one', 'sub/sub sub/one'] )
515
 
        
516
 
        names = sub.search(None)
517
 
        self.assertEqual( len(names), 0)
518
 
 
519
 
        names = context.search( sub_sub )
520
 
        self.assertEqual( len(names), 1)
521
 
        self.assertEqual( names[0], 'sub/sub sub' )
522
 
 
523
 
        return
524
 
        
525
 
#### EOF ######################################################################