Skip to content

Commit

Permalink
More specs for rb_scan_args with 1.9 features.
Browse files Browse the repository at this point in the history
  • Loading branch information
brixen committed Jul 5, 2012
1 parent 29f26f5 commit 267d050
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
10 changes: 7 additions & 3 deletions spec/ruby/optional/capi/ext/util_spec.c
Expand Up @@ -8,18 +8,22 @@ extern "C" {
#ifdef HAVE_RB_SCAN_ARGS
VALUE util_spec_rb_scan_args(VALUE self, VALUE argv, VALUE fmt, VALUE expected, VALUE acc) {
int i, result, argc = (int)RARRAY_LEN(argv);
VALUE args[4], failed, a1, a2, a3, a4;
VALUE args[5], failed, a1, a2, a3, a4, a5, a6;

failed = rb_intern("failed");
a1 = a2 = a3 = a4 = failed;
a1 = a2 = a3 = a4 = a5 = a6 = failed;

for(i = 0; i < argc; i++) {
args[i] = rb_ary_entry(argv, i);
}

result = rb_scan_args(argc, args, RSTRING_PTR(fmt), &a1, &a2, &a3, &a4);
result = rb_scan_args(argc, args, RSTRING_PTR(fmt), &a1, &a2, &a3, &a4, &a5, &a6);

switch(NUM2INT(expected)) {
case 6:
rb_ary_unshift(acc, a6);
case 5:
rb_ary_unshift(acc, a5);
case 4:
rb_ary_unshift(acc, a4);
case 3:
Expand Down
42 changes: 42 additions & 0 deletions spec/ruby/optional/capi/util_spec.rb
Expand Up @@ -67,6 +67,48 @@
@o.rb_scan_args([1, 2, 3], "1*&", 3, @acc, &@prc).should == 3
ScratchPad.recorded.should == [1, [2, 3], @prc]
end

ruby_version_is "1.9" do
it "assigns post-splat arguments" do
@o.rb_scan_args([1, 2, 3], "00*1", 2, @acc).should == 3
ScratchPad.recorded.should == [[1, 2], 3]
end

it "assigns required, optional, splat and post-splat arguments" do
@o.rb_scan_args([1, 2, 3, 4, 5], "11*1", 4, @acc).should == 5
ScratchPad.recorded.should == [1, 2, [3, 4], 5]
end

it "assigns required, splat, post-splat arguments" do
@o.rb_scan_args([1, 2, 3, 4], "10*1", 3, @acc).should == 4
ScratchPad.recorded.should == [1, [2, 3], 4]
end

it "assigns optional, splat, post-splat arguments" do
@o.rb_scan_args([1, 2, 3, 4], "01*1", 3, @acc).should == 4
ScratchPad.recorded.should == [1, [2, 3], 4]
end

it "assigns required, optional, splat, post-splat and block arguments" do
@o.rb_scan_args([1, 2, 3, 4, 5], "11*1&", 5, @acc, &@prc).should == 5
ScratchPad.recorded.should == [1, 2, [3, 4], 5, @prc]
end

it "assigns Hash arguments" do
@o.rb_scan_args([{1 => 2, 3 => 4}], "0:", 1, @acc).should == 0
ScratchPad.recorded.should == [{1 => 2, 3 => 4}]
end

it "assigns required and Hash arguments" do
@o.rb_scan_args([1, {1 => 2, 3 => 4}], "1:", 2, @acc).should == 1
ScratchPad.recorded.should == [1, {1 => 2, 3 => 4}]
end

it "assigns required, optional, splat, post-splat, Hash and block arguments" do
@o.rb_scan_args([1, 2, 3, 4, 5, {6 => 7}], "11*1:&", 6, @acc, &@prc).should == 5
ScratchPad.recorded.should == [1, 2, [3, 4], 5, {6 => 7}, @prc]
end
end
end

ruby_version_is "1.9" do
Expand Down

0 comments on commit 267d050

Please sign in to comment.