Skip to content

Commit 56fc5a8

Browse files
committed
Add specs for methods in C taking variable arguments.
1 parent 989cc8a commit 56fc5a8

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

spec/ruby/optional/capi/class_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@
109109
end
110110
end
111111

112+
describe "rb_define_method" do
113+
it "defines a method taking variable arguments as a C array if the argument count is -1" do
114+
@s.rb_method_varargs_1(1, 3, 7, 4).should == [1, 3, 7, 4]
115+
end
116+
117+
it "defines a method taking variable arguments as a Ruby array if the argument count is -2" do
118+
@s.rb_method_varargs_2(1, 3, 7, 4).should == [1, 3, 7, 4]
119+
end
120+
end
121+
112122
describe "rb_class2name" do
113123
it "returns the class name" do
114124
@s.rb_class2name(CApiClassSpecs).should == "CApiClassSpecs"

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,19 @@ static VALUE class_spec_include_module(VALUE self, VALUE klass, VALUE module) {
167167
}
168168
#endif
169169

170+
static VALUE class_spec_method_var_args_1(int argc, VALUE *argv, VALUE self) {
171+
VALUE ary = rb_ary_new();
172+
int i;
173+
for (i = 0; i < argc; i++) {
174+
rb_ary_push(ary, argv[i]);
175+
}
176+
return ary;
177+
}
178+
179+
static VALUE class_spec_method_var_args_2(VALUE self, VALUE argv) {
180+
return argv;
181+
}
182+
170183
void Init_class_spec(void) {
171184
VALUE cls;
172185
cls = rb_define_class("CApiClassSpecs", rb_cObject);
@@ -254,6 +267,9 @@ void Init_class_spec(void) {
254267
#ifdef HAVE_RB_INCLUDE_MODULE
255268
rb_define_method(cls, "rb_include_module", class_spec_include_module, 2);
256269
#endif
270+
271+
rb_define_method(cls, "rb_method_varargs_1", class_spec_method_var_args_1, -1);
272+
rb_define_method(cls, "rb_method_varargs_2", class_spec_method_var_args_2, -2);
257273
}
258274

259275
#ifdef __cplusplus

0 commit comments

Comments
 (0)