summaryrefslogtreecommitdiffstats
path: root/bootstraptest/test_class.rb
blob: 93bb51d234f6bc3572c5dfb22e7e82c487a1c0b9 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# class
assert_equal 'true',    %q( class C; end
                            Object.const_defined?(:C) )
assert_equal 'Class',   %q( class C; end
                            C.class )
assert_equal 'C',       %q( class C; end
                            C.name )
assert_equal 'C',       %q( class C; end
                            C.new.class )
assert_equal 'C',       %q( class C; end
                            C.new.class.name )
assert_equal 'Class',   %q( class C; end
                            C.new.class.class )

# inherited class
assert_equal 'true',    %q( class A; end
                            class C < A; end
                            Object.const_defined?(:C) )
assert_equal 'Class',   %q( class A; end
                            class C < A; end
                            C.class )
assert_equal 'C',       %q( class A; end
                            class C < A; end
                            C.name )
assert_equal 'C',       %q( class A; end
                            class C < A; end
                            C.new.class )
assert_equal 'C',       %q( class A; end
                            class C < A; end
                            C.new.class.name )
assert_equal 'Class',   %q( class A; end
                            class C < A; end
                            C.new.class.class )

# module
assert_equal 'true',    %q( module M; end
                            Object.const_defined?(:M) )
assert_equal 'Module',  %q( module M; end
                            M.class )
assert_equal 'M',       %q( module M; end
                            M.name )
assert_equal 'C',       %q( module M; end
                            class C; include M; end
                            C.new.class )

# nested class
assert_equal 'A::B',    %q( class A; end
                            class A::B; end
                            A::B )
assert_equal 'A::B',    %q( class A; end
                            class A::B; end
                            A::B.name )
assert_equal 'A::B',    %q( class A; end
                            class A::B; end
                            A::B.new.class )
assert_equal 'Class',   %q( class A; end
                            class A::B; end
                            A::B.new.class.class )
assert_equal 'A::B::C', %q( class A; end
                            class A::B; end
                            class A::B::C; end
                            A::B::C )
assert_equal 'A::B::C', %q( class A; end
                            class A::B; end
                            class A::B::C; end
                            A::B::C.name )
assert_equal 'Class',   %q( class A; end
                            class A::B; end
                            class A::B::C; end
                            A::B::C.class )
assert_equal 'A::B::C', %q( class A; end
                            class A::B; end
                            class A::B::C; end
                            A::B::C.new.class )
assert_equal 'Class',   %q( class A; end
                            class A::B; end
                            class A::B::C; end
                            A::B::C.new.class.class )
assert_equal 'A::B2',   %q( class A; end
                            class A::B; end
                            class A::B2 < A::B; end
                            A::B2 )
assert_equal 'Class',   %q( class A; end
                            class A::B; end
                            class A::B2 < A::B; end
                            A::B2.class )

# reopen
assert_equal 'true',    %q( class C; end;  c1 = ::C
                            class C; end;  c2 = ::C
                            c1.equal?(c2) )
assert_equal '1',       %q( class C; end
                            class A; end
                            begin class C < A; end; rescue TypeError; 1 end )
assert_equal '1',       %q( class C; end
                            begin module C; end; rescue TypeError; 1 end )
assert_equal '1',       %q( C = 1   # [yarv-dev:782]
                            begin class C; end; rescue TypeError; 1 end )
assert_equal '1',       %q( C = 1   # [yarv-dev:800]
                            begin module C; end; rescue TypeError; 1 end )

# colon2, colon3
assert_equal '1',       %q( class A; end;  A::C = 1;  A::C )
assert_equal '1',       %q( A = 7;  begin A::C = 7; rescue TypeError; 1 end )
assert_equal '1',       %q( begin 7::C = 7; rescue TypeError; 1 end )
assert_equal 'C',       %q( class A; class ::C; end end;  C )
assert_equal 'Class',   %q( class A; class ::C; end end;  C.class )
assert_equal 'OK',      %q( class A; ::C = "OK"; end;  C )
assert_equal 'String',  %q( class A; ::C = "OK"; end;  C.class )

# class/module dup
assert_equal 'Class',   %q( class C; end;  C.dup.class )
assert_equal 'Module',  %q( module M; end;  M.dup.class )

__END__

  def test_singletonclass
    ae %q{
      obj = ''
      class << obj
        def m
          :OK
        end
      end
      obj.m
    }
    ae %q{
      obj = ''
      Const = :NG
      class << obj
        Const = :OK
        def m
          Const
        end
      end
      obj.m
    }
    ae %q{
      obj = ''
      class C
        def m
          :NG
        end
      end
      class << obj
        class C
          def m
            :OK
          end
        end
        def m
          C.new.m
        end
      end
      obj.m
    }
    ae %q{ # [yarv-dev:818]
      class A
      end
      class << A
        C = "OK"
        def m
          class << Object
            $a = C
          end
        end
      end
      A.m
      $a
    }
  end

  def test_initialize
      class C
        def initialize
          @a = :C
        end
        def a
          @a
        end
      end
      C.new.a
  end

  def test_attr
      class C
        def set
          @a = 1
        end
        def get
          @a
        end
      end
      c = C.new
      c.set
      c.get
  end

  def test_attr_accessor
      class C
        attr_accessor :a
        attr_reader   :b
        attr_writer   :c
        def b_write
          @b = 'huga'
        end
        def m a
          'test_attr_accessor' + @b + @c
        end
      end

      c = C.new
      c.a = true
      c.c = 'hoge'
      c.b_write
      c.m(c.b)
  end