~brian-thomason/+junk/groovy-1.7.2

« back to all changes in this revision

Viewing changes to src/test/groovy/lang/MetaClassRegistryTest

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:31:12 UTC
  • Revision ID: brian.thomason@canonical.com-20111220173112-u53pbzhiy5y1hau0
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
class MetaClassRegistryTest extends GroovyTestCase {
 
2
     def registry = GroovySystem.metaClassRegistry
 
3
     
 
4
     void testListenerAdditionAndRemoval() {
 
5
         def called = null
 
6
         def registry = GroovySystem.metaClassRegistry
 
7
         registry.updateConstantMetaClass = {event -> called = event}  
 
8
         Integer.metaClass.foo = {->}
 
9
         assert 1.foo() == null
 
10
         assert called!=null
 
11
         assert registry.constantMetaClassChangeListeners.size() == 2
 
12
         registry.removeConstantMetaClassChangeListener(registry.constantMetaClassChangeListeners[1])
 
13
         assert registry.constantMetaClassChangeListeners.size() == 1
 
14
          
 
15
         def oldCalled = called;
 
16
         Integer.metaClass = null
 
17
         Integer.metaClass.bar = {}
 
18
         assert 1.bar()== null
 
19
         shouldFail(MissingMethodException) {
 
20
             1.foo()
 
21
         }
 
22
         assert called == oldCalled
 
23
         Integer.metaClass = null
 
24
         shouldFail(MissingMethodException) {
 
25
             1.bar()
 
26
         }
 
27
     }
 
28
     
 
29
     void testDefaultListenerRemoval() {
 
30
         assert registry.constantMetaClassChangeListeners.size() == 1
 
31
         registry.removeConstantMetaClassChangeListener(registry.constantMetaClassChangeListeners[0])
 
32
         assert registry.constantMetaClassChangeListeners.size() == 1
 
33
     }
 
34
     
 
35
     void testIteratorIteration(){
 
36
         // at the start the iteration might show elements, even if 
 
37
         // they are no longer in use. After they are added to the list,
 
38
         // they can not be collected for now. 
 
39
         def metaClasses = []        
 
40
         registry.each { metaClasses <<it }
 
41
         
 
42
         // we add one more constant meta class and then count them to 
 
43
         // see if the numberfits
 
44
         Integer.metaClass.foo = {}
 
45
         def count = 0;
 
46
         registry.each{count++}
 
47
         assert count == 1+metaClasses.size()
 
48
         
 
49
         // we remove the class again, but it might still show up
 
50
         // in the list.. so we don't test that
 
51
         Integer.metaClass = null
 
52
     }
 
53
     
 
54
     void testIteratorRemove() {
 
55
         Integer.metaClass.foo {->1}
 
56
         assert 1.foo() == 1
 
57
         for (def it = registry.iterator(); it.hasNext;) {
 
58
             it.remove()
 
59
         }
 
60
         shouldFail(MissingMethodException) {
 
61
             1.foo()
 
62
         }
 
63
     }
 
64
}
 
 
b'\\ No newline at end of file'