summaryrefslogtreecommitdiff
path: root/test/ruby/test_metaclass.rb
blob: 6386a02dfac5decf02a24d7282ab6e4d66320df1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
require 'test/unit'

class TestMetaclass < Test::Unit::TestCase
  class Foo; end
  class Bar < Foo; end
  class Baz; end

  def setup
    Object.class_eval do
      def method_o; end
    end
    Module.class_eval do
      def method_m; end
    end
    Class.class_eval do
      def method_c; end
    end
  end
  def teardown
    Object.class_eval do
      remove_method :method_o rescue nil
    end
    Module.class_eval do
      remove_method :method_m rescue nil
    end
    Class.class_eval do
      remove_method :method_c rescue nil
    end
    Object.class_eval do
      class << self
        remove_method :class_method_o rescue nil
      end
    end
    Module.class_eval do
      class << self
        remove_method :class_method_m rescue nil
      end
    end
    Class.class_eval do
      class << self
        remove_method :class_method_c rescue nil
      end
    end
    Object.class_eval do
      class << self
        class << self
          remove_method :metaclass_method_o rescue nil
        end
      end
    end
    Module.class_eval do
      class << self
        class << self
          remove_method :metaclass_method_m rescue nil
        end
      end
    end
    Class.class_eval do
      class << self
        class << self
          remove_method :metaclass_method_c rescue nil
        end
      end
    end
  end

  def test_metaclass
    class << Object
      def class_method_o; end
    end
    class << Foo
      def class_method_f; end
    end
    class << Baz
      def class_method_b; end
    end
    assert_nothing_raised{ Bar.method_o }
    assert_nothing_raised{ Bar.method_m }
    assert_nothing_raised{ Bar.method_c }
    assert_nothing_raised{ Bar.class_method_o }
    assert_nothing_raised{ Bar.class_method_f }
    assert_raise(NoMethodError){ Bar.class_method_b }

    class << Module
      def class_method_m; end
    end
    class << Class
      def class_method_c; end
    end
    class << Object
      class << self
        def metaclass_method_o; end
      end
    end
    class << Foo
      class << self
        def metaclass_method_f; end
      end
    end
    class << Baz
      class << self
        def metaclass_method_b; end
      end
    end
    metaclass_of_bar = class << Bar; self end
    assert_nothing_raised{ metaclass_of_bar.method_o }
    assert_nothing_raised{ metaclass_of_bar.method_m }
    assert_nothing_raised{ metaclass_of_bar.method_c }
    assert_nothing_raised{ metaclass_of_bar.class_method_o }
    assert_raise(NoMethodError){ metaclass_of_bar.class_method_f }
    assert_raise(NoMethodError){ metaclass_of_bar.class_method_b }
    assert_nothing_raised{ metaclass_of_bar.class_method_m }
    assert_nothing_raised{ metaclass_of_bar.class_method_c }
    assert_nothing_raised{ metaclass_of_bar.metaclass_method_o }
    assert_nothing_raised{ metaclass_of_bar.metaclass_method_f }
    assert_raise(NoMethodError){ metaclass_of_bar.metaclass_method_b }

    class << Module
      class << self
        def metaclass_method_m; end
      end
    end
    class << Class
      class << self
        def metaclass_method_c; end
      end
    end
    class << Object
      class << self
        class << self
          def metametaclass_method_o; end
        end
      end
    end
    class << Foo
      class << self
        class << self
          def metametaclass_method_f; end
        end
      end
    end
    class << Baz
      class << self
        class << self
          def metametaclass_method_b; end
        end
      end
    end
    metametaclass_of_bar = class << metaclass_of_bar; self end
    assert_nothing_raised{ metametaclass_of_bar.method_o }
    assert_nothing_raised{ metametaclass_of_bar.method_m }
    assert_nothing_raised{ metametaclass_of_bar.method_c }
    assert_nothing_raised{ metametaclass_of_bar.class_method_o }
    assert_raise(NoMethodError){ metametaclass_of_bar.class_method_f }
    assert_raise(NoMethodError){ metametaclass_of_bar.class_method_b }
    assert_nothing_raised{ metametaclass_of_bar.class_method_m }
    assert_nothing_raised{ metametaclass_of_bar.class_method_c }
    assert_nothing_raised{ metametaclass_of_bar.metaclass_method_o }
    assert_raise(NoMethodError){ metametaclass_of_bar.metaclass_method_f }
    assert_raise(NoMethodError){ metametaclass_of_bar.metaclass_method_b }
    assert_nothing_raised{ metametaclass_of_bar.metaclass_method_m }
    assert_nothing_raised{ metametaclass_of_bar.metaclass_method_c }
    assert_nothing_raised{ metametaclass_of_bar.metametaclass_method_o }
    assert_nothing_raised{ metametaclass_of_bar.metametaclass_method_f }
    assert_raise(NoMethodError){ metametaclass_of_bar.metaclass_method_b }
  end
end