Skip to content

Commit 1b4d6a2

Browse files
ahmadsheriferegon
authored andcommitted
Add specs for rb_define_class_id_under
1 parent d61a24c commit 1b4d6a2

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

optional/capi/class_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
load_extension("class")
55

66
autoload :ClassUnderAutoload, "#{object_path}/class_under_autoload_spec"
7+
autoload :ClassIdUnderAutoload, "#{object_path}/class_id_under_autoload_spec"
78

89
describe :rb_path_to_class, shared: true do
910
it "returns a class or module from a scoped String" do
@@ -245,6 +246,39 @@
245246
end
246247
end
247248

249+
describe "rb_define_class_id_under" do
250+
it "creates a subclass of the superclass contained in a module" do
251+
cls = @s.rb_define_class_id_under(CApiClassSpecs, :ClassIdUnder1, CApiClassSpecs::Super)
252+
cls.should be_kind_of(Class)
253+
CApiClassSpecs::Super.should be_ancestor_of(CApiClassSpecs::ClassIdUnder1)
254+
end
255+
256+
it "uses Object as the superclass if NULL is passed" do
257+
@s.rb_define_class_id_under(CApiClassSpecs, :ClassIdUnder2, nil)
258+
Object.should be_ancestor_of(CApiClassSpecs::ClassIdUnder2)
259+
end
260+
261+
it "sets the class name" do
262+
cls = @s.rb_define_class_id_under(CApiClassSpecs, :ClassIdUnder3, nil)
263+
cls.name.should == "CApiClassSpecs::ClassIdUnder3"
264+
end
265+
266+
it "calls #inherited on the superclass" do
267+
CApiClassSpecs::Super.should_receive(:inherited)
268+
@s.rb_define_class_id_under(CApiClassSpecs, :ClassIdUnder4, CApiClassSpecs::Super)
269+
end
270+
271+
it "defines a class for an existing Autoload" do
272+
compile_extension("class_id_under_autoload")
273+
274+
ClassIdUnderAutoload.name.should == "ClassIdUnderAutoload"
275+
end
276+
277+
it "raises a TypeError if class is defined and its superclass mismatches the given one" do
278+
lambda { @s.rb_define_class_id_under(CApiClassSpecs, :Sub, nil) }.should raise_error(TypeError)
279+
end
280+
end
281+
248282
describe "rb_define_class_variable" do
249283
it "sets a class variable" do
250284
o = CApiClassSpecs::CVars.new
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "ruby.h"
2+
3+
void Init_class_id_under_autoload_spec(void) {
4+
rb_define_class_id_under(rb_cObject, rb_intern("ClassIdUnderAutoload"), 0);
5+
}

optional/capi/ext/class_spec.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@ static VALUE class_spec_rb_define_class_under(VALUE self, VALUE outer,
148148
}
149149
#endif
150150

151+
#ifdef HAVE_RB_DEFINE_CLASS_ID_UNDER
152+
static VALUE class_spec_rb_define_class_id_under(VALUE self, VALUE outer,
153+
VALUE name, VALUE super) {
154+
if(NIL_P(super)) super = 0;
155+
return rb_define_class_id_under(outer, SYM2ID(name), super);
156+
}
157+
#endif
158+
151159
#ifdef HAVE_RB_DEFINE_CLASS_VARIABLE
152160
static VALUE class_spec_define_class_variable(VALUE self, VALUE klass, VALUE name, VALUE val) {
153161
rb_define_class_variable(klass, StringValuePtr(name), val);
@@ -238,6 +246,10 @@ void Init_class_spec() {
238246
rb_define_method(cls, "rb_define_class_under", class_spec_rb_define_class_under, 3);
239247
#endif
240248

249+
#ifdef HAVE_RB_DEFINE_CLASS_ID_UNDER
250+
rb_define_method(cls, "rb_define_class_id_under", class_spec_rb_define_class_id_under, 3);
251+
#endif
252+
241253
#ifdef HAVE_RB_DEFINE_CLASS_VARIABLE
242254
rb_define_method(cls, "rb_define_class_variable", class_spec_define_class_variable, 3);
243255
#endif

optional/capi/ext/rubyspec.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@
394394
#define HAVE_RB_CONST_SET 1
395395
#define HAVE_RB_DEFINE_ALIAS 1
396396
#define HAVE_RB_DEFINE_CLASS_UNDER 1
397+
#define HAVE_RB_DEFINE_CLASS_ID_UNDER 1
397398
#define HAVE_RB_DEFINE_CONST 1
398399
#define HAVE_RB_DEFINE_GLOBAL_CONST 1
399400
#define HAVE_RB_DEFINE_GLOBAL_FUNCTION 1

0 commit comments

Comments
 (0)