-
-
Notifications
You must be signed in to change notification settings - Fork 400
Expand file tree
/
Copy pathclass_spec.rb
More file actions
684 lines (565 loc) · 25 KB
/
Copy pathclass_spec.rb
File metadata and controls
684 lines (565 loc) · 25 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
require_relative 'spec_helper'
require_relative 'fixtures/class'
require_relative '../../core/module/fixtures/classes'
load_extension("class")
compile_extension("class_under_autoload")
compile_extension("class_id_under_autoload")
autoload :ClassUnderAutoload, "#{object_path}/class_under_autoload_spec"
autoload :ClassIdUnderAutoload, "#{object_path}/class_id_under_autoload_spec"
describe :rb_path_to_class, shared: true do
it "returns a class or module from a scoped String" do
@s.send(@method, "CApiClassSpecs::A::B").should.equal?(CApiClassSpecs::A::B)
@s.send(@method, "CApiClassSpecs::A::M").should.equal?(CApiClassSpecs::A::M)
end
it "resolves autoload constants" do
@s.send(@method, "CApiClassSpecs::A::D").name.should == "CApiClassSpecs::A::D"
end
it "raises an ArgumentError if a constant in the path does not exist" do
-> { @s.send(@method, "CApiClassSpecs::NotDefined::B") }.should.raise(ArgumentError)
end
it "raises an ArgumentError if the final constant does not exist" do
-> { @s.send(@method, "CApiClassSpecs::NotDefined") }.should.raise(ArgumentError)
end
it "raises a TypeError if the constant is not a class or module" do
-> {
@s.send(@method, "CApiClassSpecs::A::C")
}.should.raise(TypeError, 'CApiClassSpecs::A::C does not refer to class/module')
end
it "raises an ArgumentError even if a constant in the path exists on toplevel" do
-> { @s.send(@method, "CApiClassSpecs::Object") }.should.raise(ArgumentError)
end
end
describe "C-API Class function" do
before :each do
@s = CApiClassSpecs.new
end
describe "rb_class_instance_methods" do
it "returns the public and protected methods of self and its ancestors" do
methods = @s.rb_class_instance_methods(ModuleSpecs::Basic)
methods.should.include?(:protected_module)
methods.should.include?(:public_module)
methods = @s.rb_class_instance_methods(ModuleSpecs::Basic, true)
methods.should.include?(:protected_module)
methods.should.include?(:public_module)
end
it "when passed false as a parameter, returns the instance methods of the class" do
methods = @s.rb_class_instance_methods(ModuleSpecs::Child, false)
methods.to_set.should >= Set[:protected_child, :public_child]
end
end
describe "rb_class_public_instance_methods" do
it "returns a list of public methods in module and its ancestors" do
methods = @s.rb_class_public_instance_methods(ModuleSpecs::CountsChild)
methods.should.include?(:public_3)
methods.should.include?(:public_2)
methods.should.include?(:public_1)
methods = @s.rb_class_public_instance_methods(ModuleSpecs::CountsChild, true)
methods.should.include?(:public_3)
methods.should.include?(:public_2)
methods.should.include?(:public_1)
end
it "when passed false as a parameter, should return only methods defined in that module" do
@s.rb_class_public_instance_methods(ModuleSpecs::CountsChild, false).should == [:public_1]
end
end
describe "rb_class_protected_instance_methods" do
it "returns a list of protected methods in module and its ancestors" do
methods = @s.rb_class_protected_instance_methods(ModuleSpecs::CountsChild)
methods.should.include?(:protected_3)
methods.should.include?(:protected_2)
methods.should.include?(:protected_1)
methods = @s.rb_class_protected_instance_methods(ModuleSpecs::CountsChild, true)
methods.should.include?(:protected_3)
methods.should.include?(:protected_2)
methods.should.include?(:protected_1)
end
it "when passed false as a parameter, should return only methods defined in that module" do
@s.rb_class_public_instance_methods(ModuleSpecs::CountsChild, false).should == [:public_1]
end
end
describe "rb_class_private_instance_methods" do
it "returns a list of private methods in module and its ancestors" do
@s.rb_class_private_instance_methods(ModuleSpecs::CountsChild).should == ModuleSpecs::CountsChild.private_instance_methods
@s.rb_class_private_instance_methods(ModuleSpecs::CountsChild, true).should == ModuleSpecs::CountsChild.private_instance_methods
end
it "when passed false as a parameter, should return only methods defined in that module" do
methods = @s.rb_class_private_instance_methods(ModuleSpecs::CountsChild, false)
methods.should == [:private_1]
end
end
describe "rb_class_new_instance" do
it "allocates and initializes a new object" do
o = @s.rb_class_new_instance([], CApiClassSpecs::Alloc)
o.class.should == CApiClassSpecs::Alloc
o.initialized.should == true
end
it "passes arguments to the #initialize method" do
o = @s.rb_class_new_instance([:one, :two], CApiClassSpecs::Alloc)
o.arguments.should == [:one, :two]
end
end
describe "rb_class_new_instance_kw" do
it "passes arguments and keywords to the #initialize method" do
obj = @s.rb_class_new_instance_kw([{pos: 1}, {kw: 2}], CApiClassSpecs::KeywordAlloc)
obj.args.should == [{pos: 1}]
obj.kwargs.should == {kw: 2}
obj = @s.rb_class_new_instance_kw([{}], CApiClassSpecs::KeywordAlloc)
obj.args.should == []
obj.kwargs.should == {}
end
it "coerces the last argument to a hash by calling #to_hash" do
h = mock('to_hash')
h.should_receive(:to_hash).and_return(kw: 2)
obj = @s.rb_class_new_instance_kw([h], CApiClassSpecs::KeywordAlloc)
obj.kwargs.should == {kw: 2}
end
it "raises a TypeError if the last argument does not respond to #to_hash" do
-> {
@s.rb_class_new_instance_kw([42], CApiClassSpecs::KeywordAlloc)
}.should raise_consistent_error(TypeError, 'no implicit conversion of Integer into Hash')
end
it "raises a TypeError if #to_hash does not return a hash" do
h = mock('to_hash')
h.should_receive(:to_hash).and_return(42)
-> {
@s.rb_class_new_instance_kw([h], CApiClassSpecs::KeywordAlloc)
}.should raise_consistent_error(TypeError, "can't convert MockObject into Hash (MockObject#to_hash gives Integer)")
end
end
describe "rb_include_module" do
it "includes a module into a class" do
c = Class.new
o = c.new
-> { o.included? }.should.raise(NameError)
@s.rb_include_module(c, CApiClassSpecs::M)
o.included?.should == true
end
end
describe "rb_prepend_module" do
it "prepends a module into a class" do
klass = Class.new
mod = Module.new
@s.rb_prepend_module(klass, mod)
klass.ancestors[0, 2].should == [mod, klass]
end
end
describe "rb_define_attr" do
before :each do
@a = CApiClassSpecs::Attr.new
end
it "defines an attr_reader when passed true, false" do
@s.rb_define_attr(CApiClassSpecs::Attr, :foo, true, false)
@a.foo.should == 1
-> { @a.foo = 5 }.should.raise(NameError)
end
it "defines an attr_writer when passed false, true" do
@s.rb_define_attr(CApiClassSpecs::Attr, :bar, false, true)
-> { @a.bar }.should.raise(NameError)
@a.bar = 5
@a.instance_variable_get(:@bar).should == 5
end
it "defines an attr_accessor when passed true, true" do
@s.rb_define_attr(CApiClassSpecs::Attr, :baz, true, true)
@a.baz.should == 3
@a.baz = 6
@a.baz.should == 6
end
end
describe "rb_call_super" do
it "calls the method in the superclass" do
@s.define_call_super_method CApiClassSpecs::Sub, "call_super_method"
obj = CApiClassSpecs::Sub.new
obj.call_super_method.should == :super_method
end
it "calls the method in the superclass with the correct self" do
@s.define_call_super_method CApiClassSpecs::SubSelf, "call_super_method"
obj = CApiClassSpecs::SubSelf.new
obj.call_super_method.should.equal? obj
end
it "calls the method in the superclass through two native levels" do
@s.define_call_super_method CApiClassSpecs::Sub, "call_super_method"
@s.define_call_super_method CApiClassSpecs::SubSub, "call_super_method"
obj = CApiClassSpecs::SubSub.new
obj.call_super_method.should == :super_method
end
it "passes block argument as is" do
@s.define_call_super_method CApiClassSpecs::Sub, "call_super_method_block"
obj = CApiClassSpecs::Sub.new
obj.call_super_method_block { :block_val }.should == :block_val
end
it "calls #method_missing if there is no super method and #method_missing is defined" do
@s.define_call_super_method CApiClassSpecs::Sub, "non_existent_method"
obj = CApiClassSpecs::Sub.new
def obj.method_missing(name, *args)
[name, args]
end
obj.non_existent_method(1, 2).should == [:non_existent_method, [1, 2]]
end
it "raises a NoMethodError if there is no super method and no #method_missing defined" do
@s.define_call_super_method CApiClassSpecs::Sub, "non_existent_method"
obj = CApiClassSpecs::Sub.new
-> { obj.non_existent_method }.should.raise(NoMethodError)
end
end
describe "rb_call_super_kw" do
it "calls the method in the superclass" do
@s.define_call_super_kw_method CApiClassSpecs::SubKw, "call_super_method", :RB_NO_KEYWORDS
obj = CApiClassSpecs::SubKw.new
obj.call_super_method.should == :super_method
@s.define_call_super_kw_method CApiClassSpecs::SubKw, "call_super_method", :RB_PASS_KEYWORDS
obj = CApiClassSpecs::SubKw.new
obj.call_super_method({a: 3}).should == :super_method
end
it "calls the method in the superclass with correct self" do
@s.define_call_super_kw_method CApiClassSpecs::SubKw, "call_super_method_self", :RB_NO_KEYWORDS
obj = CApiClassSpecs::SubKw.new
obj.call_super_method_self.should.equal? obj
@s.define_call_super_kw_method CApiClassSpecs::SubKw, "call_super_method_self", :RB_PASS_KEYWORDS
obj = CApiClassSpecs::SubKw.new
obj.call_super_method_self({a: 1}).should.equal? obj
end
it "passes the last argument as a positional parameter when called with RB_NO_KEYWORDS" do
@s.define_call_super_kw_method CApiClassSpecs::SubKw, "call_super_method_args", :RB_NO_KEYWORDS
obj = CApiClassSpecs::SubKw.new
obj.call_super_method_args(1, 2, {a: 3}).should == [[1, 2, {a: 3}], {}]
end
it "passes the last argument as keyword arguments when called with RB_PASS_KEYWORDS" do
@s.define_call_super_kw_method CApiClassSpecs::SubKw, "call_super_method_args", :RB_PASS_KEYWORDS
obj = CApiClassSpecs::SubKw.new
obj.call_super_method_args(1, 2, {a: 3}).should == [[1, 2], {a: 3}]
end
it "passes the last argument as keyword arguments when called with RB_PASS_CALLED_KEYWORDS and with keyword arguments" do
@s.define_call_super_kw_method CApiClassSpecs::SubKw, "call_super_method_args", :RB_PASS_CALLED_KEYWORDS
obj = CApiClassSpecs::SubKw.new
obj.call_super_method_args(1, 2, a: 3).should == [[1, 2], {a: 3}]
end
it "passes the last argument as a positional parameter when called with RB_PASS_CALLED_KEYWORDS and with a positional Hash" do
@s.define_call_super_kw_method CApiClassSpecs::SubKw, "call_super_method_args", :RB_PASS_CALLED_KEYWORDS
obj = CApiClassSpecs::SubKw.new
hash_obj = {a: 3}
obj.call_super_method_args(1, 2, hash_obj).should == [[1, 2, {a: 3}], {}]
end
it "passes block argument as is" do
@s.define_call_super_kw_method CApiClassSpecs::SubKw, "call_super_method_block", :RB_NO_KEYWORDS
obj = CApiClassSpecs::SubKw.new
obj.call_super_method_block { :block_val }.should == :block_val
@s.define_call_super_kw_method CApiClassSpecs::SubKw, "call_super_method_block", :RB_PASS_KEYWORDS
obj = CApiClassSpecs::SubKw.new
obj.call_super_method_block({a: 3}) { :block_val }.should == :block_val
end
it "calls #method_missing if there is no super method and #method_missing is defined" do
@s.define_call_super_kw_method CApiClassSpecs::SubKw, "non_existent_method", :RB_NO_KEYWORDS
obj = CApiClassSpecs::SubKw.new
def obj.method_missing(name, *args)
[name, args]
end
obj.non_existent_method(1, 2).should == [:non_existent_method, [1, 2]]
@s.define_call_super_kw_method CApiClassSpecs::SubKw, "non_existent_method", :RB_PASS_KEYWORDS
obj = CApiClassSpecs::SubKw.new
def obj.method_missing(name, *args)
[name, args]
end
obj.non_existent_method(1, 2, {a: 3}).should == [:non_existent_method, [1, 2, {a: 3}]]
end
it "raises a NoMethodError if there is no super method and no #method_missing defined" do
@s.define_call_super_kw_method CApiClassSpecs::SubKw, "non_existent_method", :RB_NO_KEYWORDS
obj = CApiClassSpecs::SubKw.new
-> { obj.non_existent_method }.should.raise(NoMethodError)
@s.define_call_super_kw_method CApiClassSpecs::SubKw, "non_existent_method", :RB_PASS_KEYWORDS
obj = CApiClassSpecs::SubKw.new
-> { obj.non_existent_method({a: 3}) }.should.raise(NoMethodError)
end
it "tolerates giving no positional or keyword arguments when called with RB_PASS_KEYWORDS" do
@s.define_call_super_kw_method CApiClassSpecs::SubKw, "call_super_method_args", :RB_PASS_KEYWORDS
obj = CApiClassSpecs::SubKw.new
obj.call_super_method_args.should == [[], {}]
end
it "tolerates giving {} as the last positional argument when called with RB_PASS_KEYWORDS" do
@s.define_call_super_kw_method CApiClassSpecs::SubKw, "call_super_method_args", :RB_PASS_KEYWORDS
obj = CApiClassSpecs::SubKw.new
obj.call_super_method_args({}).should == [[], {}]
end
it "coerces the last argument to a hash by calling #to_hash when called with RB_PASS_KEYWORDS" do
@s.define_call_super_kw_method CApiClassSpecs::SubKw, "call_super_method_args", :RB_PASS_KEYWORDS
obj = CApiClassSpecs::SubKw.new
h = mock('to_hash')
h.should_receive(:to_hash).and_return({a: 3})
obj.call_super_method_args(1, 2, h).should == [[1, 2], {a: 3}]
end
it "raises a TypeError if the last argument does not respond to #to_hash when called with RB_PASS_KEYWORDS" do
@s.define_call_super_kw_method CApiClassSpecs::SubKw, "call_super_method_args", :RB_PASS_KEYWORDS
obj = CApiClassSpecs::SubKw.new
-> {
obj.call_super_method_args(1, 2, 3)
}.should raise_consistent_error(TypeError, 'no implicit conversion of Integer into Hash')
end
it "raises a TypeError if #to_hash does not return a hash when called with RB_PASS_KEYWORDS" do
@s.define_call_super_kw_method CApiClassSpecs::SubKw, "call_super_method_args", :RB_PASS_KEYWORDS
obj = CApiClassSpecs::SubKw.new
h = mock('to_hash')
h.should_receive(:to_hash).and_return(42)
-> {
obj.call_super_method_args(1, 2, h)
}.should raise_consistent_error(TypeError, "can't convert MockObject into Hash (MockObject#to_hash gives Integer)")
end
end
describe "rb_class2name" do
it "returns the class name" do
@s.rb_class2name(CApiClassSpecs).should == "CApiClassSpecs"
end
it "returns a string for an anonymous class" do
@s.rb_class2name(Class.new).should.is_a?(String)
end
it "returns a string beginning with # for an anonymous class" do
@s.rb_class2name(Struct.new(:x, :y).new(1, 2).class).should.start_with?('#')
end
end
describe "rb_class_path" do
it "returns a String of a class path with no scope modifiers" do
@s.rb_class_path(Array).should == "Array"
end
it "returns a String of a class path with scope modifiers" do
@s.rb_class_path(File::Stat).should == "File::Stat"
end
end
describe "rb_class_name" do
it "returns the class name" do
@s.rb_class_name(CApiClassSpecs).should == "CApiClassSpecs"
end
it "returns a string for an anonymous class" do
@s.rb_class_name(Class.new).should.is_a?(String)
end
end
describe "rb_path2class" do
it_behaves_like :rb_path_to_class, :rb_path2class
end
describe "rb_path_to_class" do
it_behaves_like :rb_path_to_class, :rb_path_to_class
end
describe "rb_cvar_defined" do
it "returns false when the class variable is not defined" do
@s.rb_cvar_defined(CApiClassSpecs::CVars, "@@nocvar").should == false
end
it "returns true when the class variable is defined" do
@s.rb_cvar_defined(CApiClassSpecs::CVars, "@@cvar").should == true
end
it "returns true if the class instance variable is defined" do
@s.rb_cvar_defined(CApiClassSpecs::CVars, "@c_ivar").should == true
end
end
describe "rb_cv_set" do
it "sets a class variable" do
o = CApiClassSpecs::CVars.new
o.new_cv.should == nil
@s.rb_cv_set(CApiClassSpecs::CVars, "@@new_cv", 1)
o.new_cv.should == 1
CApiClassSpecs::CVars.remove_class_variable :@@new_cv
end
end
describe "rb_cv_get" do
it "returns the value of the class variable" do
@s.rb_cvar_get(CApiClassSpecs::CVars, "@@cvar").should == :cvar
end
it "raises a NameError if the class variable is not defined" do
-> {
@s.rb_cv_get(CApiClassSpecs::CVars, "@@no_cvar")
}.should.raise(NameError, /class variable @@no_cvar/)
end
end
describe "rb_cvar_set" do
it "sets a class variable" do
o = CApiClassSpecs::CVars.new
o.new_cvar.should == nil
@s.rb_cvar_set(CApiClassSpecs::CVars, "@@new_cvar", 1)
o.new_cvar.should == 1
CApiClassSpecs::CVars.remove_class_variable :@@new_cvar
end
end
describe "rb_define_class" do
before :each do
@cls = @s.rb_define_class("ClassSpecDefineClass", CApiClassSpecs::Super)
end
it "creates a subclass of the superclass" do
@cls.should.is_a?(Class)
ClassSpecDefineClass.should.equal?(@cls)
@cls.superclass.should == CApiClassSpecs::Super
end
it "sets the class name" do
@cls.name.should == "ClassSpecDefineClass"
end
it "calls #inherited on the superclass" do
CApiClassSpecs::Super.should_receive(:inherited)
@s.rb_define_class("ClassSpecDefineClass2", CApiClassSpecs::Super)
Object.send(:remove_const, :ClassSpecDefineClass2)
end
it "raises a TypeError when given a non class object to superclass" do
-> {
@s.rb_define_class("ClassSpecDefineClass3", Module.new)
}.should.raise(TypeError)
end
it "raises a TypeError when given a mismatched class to superclass" do
-> {
@s.rb_define_class("ClassSpecDefineClass", Object)
}.should.raise(TypeError)
end
it "raises a ArgumentError when given NULL as superclass" do
-> {
@s.rb_define_class("ClassSpecDefineClass4", nil)
}.should.raise(ArgumentError)
end
it "allows arbitrary names, including constant names not valid in Ruby" do
cls = @s.rb_define_class("_INVALID_CLASS", CApiClassSpecs::Super)
cls.name.should == "_INVALID_CLASS"
-> {
Object.const_get(cls.name)
}.should.raise(NameError, /wrong constant name/)
end
end
describe "rb_define_class_under" do
it "creates a subclass of the superclass contained in a module" do
cls = @s.rb_define_class_under(CApiClassSpecs,
"ClassUnder1",
CApiClassSpecs::Super)
cls.should.is_a?(Class)
CApiClassSpecs::ClassUnder1.ancestors.should.include?(CApiClassSpecs::Super)
end
it "sets the class name" do
cls = @s.rb_define_class_under(CApiClassSpecs, "ClassUnder3", Object)
cls.name.should == "CApiClassSpecs::ClassUnder3"
end
it "calls #inherited on the superclass" do
CApiClassSpecs::Super.should_receive(:inherited)
@s.rb_define_class_under(CApiClassSpecs, "ClassUnder4", CApiClassSpecs::Super)
CApiClassSpecs.send(:remove_const, :ClassUnder4)
end
it "raises a TypeError when given a non class object to superclass" do
-> { @s.rb_define_class_under(CApiClassSpecs,
"ClassUnder5",
Module.new)
}.should.raise(TypeError)
end
it "raises a TypeError when given a mismatched class to superclass" do
CApiClassSpecs::ClassUnder6 = Class.new(CApiClassSpecs::Super)
-> { @s.rb_define_class_under(CApiClassSpecs,
"ClassUnder6",
Class.new)
}.should.raise(TypeError)
ensure
CApiClassSpecs.send(:remove_const, :ClassUnder6)
end
it "defines a class for an existing Autoload" do
ClassUnderAutoload.name.should == "ClassUnderAutoload"
end
it "raises a TypeError if class is defined and its superclass mismatches the given one" do
-> { @s.rb_define_class_under(CApiClassSpecs, "Sub", Object) }.should.raise(TypeError)
end
it "allows arbitrary names, including constant names not valid in Ruby" do
cls = @s.rb_define_class_under(CApiClassSpecs, "_INVALID_CLASS", CApiClassSpecs::Super)
cls.name.should == "CApiClassSpecs::_INVALID_CLASS"
-> {
CApiClassSpecs.const_get(cls.name)
}.should.raise(NameError, /wrong constant name/)
end
end
describe "rb_define_class_id_under" do
it "creates a subclass of the superclass contained in a module" do
cls = @s.rb_define_class_id_under(CApiClassSpecs, :ClassIdUnder1, CApiClassSpecs::Super)
cls.should.is_a?(Class)
CApiClassSpecs::ClassIdUnder1.ancestors.should.include?(CApiClassSpecs::Super)
end
it "sets the class name" do
cls = @s.rb_define_class_id_under(CApiClassSpecs, :ClassIdUnder3, Object)
cls.name.should == "CApiClassSpecs::ClassIdUnder3"
end
it "calls #inherited on the superclass" do
CApiClassSpecs::Super.should_receive(:inherited)
@s.rb_define_class_id_under(CApiClassSpecs, :ClassIdUnder4, CApiClassSpecs::Super)
CApiClassSpecs.send(:remove_const, :ClassIdUnder4)
end
it "defines a class for an existing Autoload" do
ClassIdUnderAutoload.name.should == "ClassIdUnderAutoload"
end
it "raises a TypeError if class is defined and its superclass mismatches the given one" do
-> { @s.rb_define_class_id_under(CApiClassSpecs, :Sub, Object) }.should.raise(TypeError)
end
it "allows arbitrary names, including constant names not valid in Ruby" do
cls = @s.rb_define_class_id_under(CApiClassSpecs, :_INVALID_CLASS2, CApiClassSpecs::Super)
cls.name.should == "CApiClassSpecs::_INVALID_CLASS2"
-> {
CApiClassSpecs.const_get(cls.name)
}.should.raise(NameError, /wrong constant name/)
end
end
describe "rb_define_class_variable" do
it "sets a class variable" do
o = CApiClassSpecs::CVars.new
o.rbdcv_cvar.should == nil
@s.rb_define_class_variable(CApiClassSpecs::CVars, "@@rbdcv_cvar", 1)
o.rbdcv_cvar.should == 1
CApiClassSpecs::CVars.remove_class_variable :@@rbdcv_cvar
end
end
describe "rb_cvar_get" do
it "returns the value of the class variable" do
@s.rb_cvar_get(CApiClassSpecs::CVars, "@@cvar").should == :cvar
end
it "raises a NameError if the class variable is not defined" do
-> {
@s.rb_cvar_get(CApiClassSpecs::CVars, "@@no_cvar")
}.should.raise(NameError, /class variable @@no_cvar/)
end
end
describe "rb_class_new" do
it "returns a new subclass of the superclass" do
subclass = @s.rb_class_new(CApiClassSpecs::NewClass)
subclass.ancestors.should.include?(CApiClassSpecs::NewClass)
end
it "raises a TypeError if passed Class as the superclass" do
-> { @s.rb_class_new(Class) }.should.raise(TypeError)
end
it "raises a TypeError if passed a singleton class as the superclass" do
metaclass = Object.new.singleton_class
-> { @s.rb_class_new(metaclass) }.should.raise(TypeError)
end
end
describe "rb_class_superclass" do
it "returns the superclass of a class" do
cls = @s.rb_class_superclass(CApiClassSpecs::Sub)
cls.should == CApiClassSpecs::Super
end
it "returns nil if the class has no superclass" do
@s.rb_class_superclass(BasicObject).should == nil
end
end
describe "rb_class_real" do
it "returns the class of an object ignoring the singleton class" do
obj = CApiClassSpecs::Sub.new
def obj.some_method() end
@s.rb_class_real(obj).should == CApiClassSpecs::Sub
end
it "returns the class of an object ignoring included modules" do
obj = CApiClassSpecs::SubM.new
@s.rb_class_real(obj).should == CApiClassSpecs::SubM
end
it "returns 0 if passed 0" do
@s.rb_class_real(0).should == 0
end
end
describe "rb_class_get_superclass" do
it "returns parent class for a provided class" do
a = Class.new
@s.rb_class_get_superclass(Class.new(a)).should == a
end
it "returns false when there is no parent class" do
@s.rb_class_get_superclass(BasicObject).should == false
@s.rb_class_get_superclass(Module.new).should == false
end
end
describe "a constant defined in C" do
it "raises TypeError if constant given as class name exists and is a Number" do
-> {
class CApiClassSpecs::CONST_DEFINED_IN_NATIVE_CODE; end
}.should.raise(TypeError, /CONST_DEFINED_IN_NATIVE_CODE is not a class/)
end
end
end