-
-
Notifications
You must be signed in to change notification settings - Fork 388
/
Copy pathsingleton_class_spec.rb
317 lines (255 loc) · 8.48 KB
/
singleton_class_spec.rb
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
require_relative '../spec_helper'
require_relative '../fixtures/class'
describe "A singleton class" do
it "is TrueClass for true" do
true.singleton_class.should == TrueClass
end
it "is FalseClass for false" do
false.singleton_class.should == FalseClass
end
it "is NilClass for nil" do
nil.singleton_class.should == NilClass
end
it "raises a TypeError for Integer's" do
-> { 1.singleton_class }.should raise_error(TypeError)
end
it "raises a TypeError for symbols" do
-> { :symbol.singleton_class }.should raise_error(TypeError)
end
it "is a singleton Class instance" do
o = mock('x')
o.singleton_class.should be_kind_of(Class)
o.singleton_class.should_not equal(Object)
o.should be_kind_of(o.singleton_class)
end
it "is a Class for classes" do
ClassSpecs::A.singleton_class.should be_kind_of(Class)
end
it "inherits from Class for classes" do
Class.should be_ancestor_of(Object.singleton_class)
end
it "is a subclass of Class's singleton class" do
ec = ClassSpecs::A.singleton_class
ec.should be_kind_of(Class.singleton_class)
end
it "is a subclass of the same level of Class's singleton class" do
ecec = ClassSpecs::A.singleton_class.singleton_class
class_ec = Class.singleton_class
ecec.should be_kind_of(class_ec.singleton_class)
ecec.should be_kind_of(class_ec)
end
it "is a subclass of a superclass's singleton class" do
ClassSpecs::K.singleton_class.superclass.should ==
ClassSpecs::H.singleton_class
end
it "is a subclass of the same level of superclass's singleton class" do
ClassSpecs::K.singleton_class.singleton_class.superclass.should ==
ClassSpecs::H.singleton_class.singleton_class
end
it "for BasicObject has Class as it's superclass" do
BasicObject.singleton_class.superclass.should == Class
end
it "for BasicObject has the proper level of superclass for Class" do
BasicObject.singleton_class.singleton_class.superclass.should ==
Class.singleton_class
end
it "has class String as the superclass of a String instance" do
"blah".dup.singleton_class.superclass.should == String
end
it "doesn't have singleton class" do
-> { bignum_value.singleton_class }.should raise_error(TypeError)
end
end
describe "A constant on a singleton class" do
before :each do
@object = Object.new
class << @object
CONST = self
end
end
it "can be accessed after the singleton class body is reopened" do
class << @object
CONST.should == self
end
end
it "can be accessed via self::CONST" do
class << @object
self::CONST.should == self
end
end
it "can be accessed via const_get" do
class << @object
const_get(:CONST).should == self
end
end
it "is not defined on the object's class" do
@object.class.const_defined?(:CONST).should be_false
end
it "is not defined in the singleton class opener's scope" do
class << @object
CONST
end
-> { CONST }.should raise_error(NameError)
end
it "cannot be accessed via object::CONST" do
-> do
@object::CONST
end.should raise_error(TypeError)
end
it "raises a NameError for anonymous_module::CONST" do
@object = Class.new
class << @object
CONST = 100
end
-> do
@object::CONST
end.should raise_error(NameError)
end
it "appears in the singleton class constant list" do
@object.singleton_class.should have_constant(:CONST)
end
it "does not appear in the object's class constant list" do
@object.class.should_not have_constant(:CONST)
end
it "is not preserved when the object is duped" do
@object = @object.dup
-> do
class << @object; CONST; end
end.should raise_error(NameError)
end
it "is preserved when the object is cloned" do
@object = @object.clone
class << @object
CONST.should_not be_nil
end
end
end
describe "Defining instance methods on a singleton class" do
before :each do
@k = ClassSpecs::K.new
class << @k
def singleton_method; 1 end
end
@k_sc = @k.singleton_class
end
it "defines public methods" do
@k_sc.should have_public_instance_method(:singleton_method)
end
end
describe "Instance methods of a singleton class" do
before :each do
k = ClassSpecs::K.new
@k_sc = k.singleton_class
@a_sc = ClassSpecs::A.new.singleton_class
@a_c_sc = ClassSpecs::A.singleton_class
end
it "include ones of the object's class" do
@k_sc.should have_instance_method(:example_instance_method)
end
it "does not include class methods of the object's class" do
@k_sc.should_not have_instance_method(:example_class_method)
end
it "include instance methods of Object" do
@a_sc.should have_instance_method(:example_instance_method_of_object)
end
it "does not include class methods of Object" do
@a_sc.should_not have_instance_method(:example_class_method_of_object)
end
describe "for a class" do
it "include instance methods of Class" do
@a_c_sc.should have_instance_method(:example_instance_method_of_class)
end
it "does not include class methods of Class" do
@a_c_sc.should_not have_instance_method(:example_class_method_of_class)
end
it "does not include instance methods of the singleton class of Class" do
@a_c_sc.should_not have_instance_method(:example_instance_method_of_singleton_class)
end
it "does not include class methods of the singleton class of Class" do
@a_c_sc.should_not have_instance_method(:example_class_method_of_singleton_class)
end
end
describe "for a singleton class" do
it "includes instance methods of the singleton class of Class" do
@a_c_sc.singleton_class.should have_instance_method(:example_instance_method_of_singleton_class)
end
it "does not include class methods of the singleton class of Class" do
@a_c_sc.singleton_class.should_not have_instance_method(:example_class_method_of_singleton_class)
end
end
end
describe "Class methods of a singleton class" do
before :each do
k = ClassSpecs::K.new
@k_sc = k.singleton_class
@a_sc = ClassSpecs::A.new.singleton_class
@a_c_sc = ClassSpecs::A.singleton_class
end
it "include ones of the object's class" do
@k_sc.should have_method(:example_class_method)
end
it "does not include instance methods of the object's class" do
@k_sc.should_not have_method(:example_instance_method)
end
it "include instance methods of Class" do
@a_sc.should have_method(:example_instance_method_of_class)
end
it "does not include class methods of Class" do
@a_sc.should_not have_method(:example_class_method_of_class)
end
describe "for a class" do
it "include instance methods of Class" do
@a_c_sc.should have_method(:example_instance_method_of_class)
end
it "include class methods of Class" do
@a_c_sc.should have_method(:example_class_method_of_class)
end
it "include instance methods of the singleton class of Class" do
@a_c_sc.should have_method(:example_instance_method_of_singleton_class)
end
it "does not include class methods of the singleton class of Class" do
@a_c_sc.should_not have_method(:example_class_method_of_singleton_class)
end
end
describe "for a singleton class" do
it "include instance methods of the singleton class of Class" do
@a_c_sc.singleton_class.should have_method(:example_instance_method_of_singleton_class)
end
it "include class methods of the singleton class of Class" do
@a_c_sc.singleton_class.should have_method(:example_class_method_of_singleton_class)
end
end
end
describe "Instantiating a singleton class" do
it "raises a TypeError when new is called" do
-> {
Object.new.singleton_class.new
}.should raise_error(TypeError)
end
it "raises a TypeError when allocate is called" do
-> {
Object.new.singleton_class.allocate
}.should raise_error(TypeError)
end
end
describe "Frozen properties" do
it "is frozen if the object it is created from is frozen" do
o = Object.new
o.freeze
klass = o.singleton_class
klass.frozen?.should == true
end
it "will be frozen if the object it is created from becomes frozen" do
o = Object.new
klass = o.singleton_class
klass.frozen?.should == false
o.freeze
klass.frozen?.should == true
end
it "will be unfrozen if the frozen object is cloned with freeze set to false" do
o = Object.new
o.freeze
o2 = o.clone(freeze: false)
o2.singleton_class.frozen?.should == false
end
end