Skip to content

Commit 3fe5fd1

Browse files
committed
Add specs for rb_ivar_foreach
1 parent 55873c0 commit 3fe5fd1

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

spec/ruby/optional/capi/ext/object_spec.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,18 @@ static VALUE object_spec_rb_class_inherited_p(VALUE self, VALUE mod, VALUE arg)
393393
return rb_class_inherited_p(mod, arg);
394394
}
395395

396+
static int foreach_f(ID key, VALUE val, VALUE ary) {
397+
rb_ary_push(ary, ID2SYM(key));
398+
rb_ary_push(ary, val);
399+
return ST_CONTINUE;
400+
}
401+
402+
static VALUE object_spec_rb_ivar_foreach(VALUE self, VALUE obj) {
403+
VALUE ary = rb_ary_new();
404+
rb_ivar_foreach(obj, foreach_f, ary);
405+
return ary;
406+
}
407+
396408
static VALUE speced_allocator(VALUE klass) {
397409
VALUE flags = 0;
398410
VALUE instance;
@@ -508,6 +520,7 @@ void Init_object_spec(void) {
508520
rb_define_method(cls, "speced_allocator?", speced_allocator_p, 1);
509521
rb_define_method(cls, "custom_alloc_func?", custom_alloc_func_p, 1);
510522
rb_define_method(cls, "not_implemented_method", rb_f_notimplement, -1);
523+
rb_define_method(cls, "rb_ivar_foreach", object_spec_rb_ivar_foreach, 1);
511524
}
512525

513526
#ifdef __cplusplus
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class CApiObjectSpecs
2+
class IVars
3+
def initialize
4+
@a = 3
5+
@b = 7
6+
@c = 4
7+
end
8+
9+
def self.set_class_variables
10+
@@foo = :a
11+
@@bar = :b
12+
@@baz = :c
13+
end
14+
end
15+
16+
module MVars
17+
@@mvar = :foo
18+
@@mvar2 = :bar
19+
20+
@ivar = :baz
21+
end
22+
23+
module CVars
24+
@@cvar = :foo
25+
@@cvar2 = :bar
26+
27+
@ivar = :baz
28+
end
29+
end

spec/ruby/optional/capi/object_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require_relative 'spec_helper'
2+
require_relative 'fixtures/object'
23

34
load_extension("object")
45

@@ -983,5 +984,24 @@ def reach
983984
@o.speced_allocator?(parent).should == true
984985
end
985986
end
987+
988+
describe "rb_ivar_foreach" do
989+
it "calls the callback function for each instance variable on an object" do
990+
o = CApiObjectSpecs::IVars.new
991+
ary = @o.rb_ivar_foreach(o)
992+
ary.should == [:@a, 3, :@b, 7, :@c, 4]
993+
end
994+
995+
it "calls the callback function for each cvar and ivar on a class" do
996+
ary = @o.rb_ivar_foreach(CApiObjectSpecs::CVars)
997+
ary.should == [:__classpath__, 'CApiObjectSpecs::CVars', :@@cvar, :foo, :@@cvar2, :bar, :@ivar, :baz]
998+
end
999+
1000+
it "calls the callback function for each cvar and ivar on a module" do
1001+
ary = @o.rb_ivar_foreach(CApiObjectSpecs::MVars)
1002+
ary.should == [:__classpath__, 'CApiObjectSpecs::MVars', :@@mvar, :foo, :@@mvar2, :bar, :@ivar, :baz]
1003+
end
1004+
1005+
end
9861006
end
9871007
end

0 commit comments

Comments
 (0)