summaryrefslogtreecommitdiff
path: root/spec/ruby/optional/capi/util_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/optional/capi/util_spec.rb')
-rw-r--r--spec/ruby/optional/capi/util_spec.rb163
1 files changed, 137 insertions, 26 deletions
diff --git a/spec/ruby/optional/capi/util_spec.rb b/spec/ruby/optional/capi/util_spec.rb
index c8d0401cbc..dd3cbb6549 100644
--- a/spec/ruby/optional/capi/util_spec.rb
+++ b/spec/ruby/optional/capi/util_spec.rb
@@ -1,4 +1,4 @@
-require File.expand_path('../spec_helper', __FILE__)
+require_relative 'spec_helper'
load_extension('util')
@@ -9,18 +9,23 @@ describe "C-API Util function" do
describe "rb_scan_args" do
before :each do
- @prc = lambda { 1 }
+ @prc = -> { 1 }
@acc = []
ScratchPad.record @acc
end
it "assigns the required arguments scanned" do
- @o.rb_scan_args([1, 2], "2", 2, @acc).should == 2
- ScratchPad.recorded.should == [1, 2]
+ obj = Object.new
+ @o.rb_scan_args([obj, 2], "2", 2, @acc).should == 2
+ ScratchPad.recorded.should == [obj, 2]
end
it "raises an ArgumentError if there are insufficient arguments" do
- lambda { @o.rb_scan_args([1, 2], "3", 0, @acc) }.should raise_error(ArgumentError)
+ -> { @o.rb_scan_args([1, 2], "3", 0, @acc) }.should.raise(ArgumentError, "wrong number of arguments (given 2, expected 3)")
+ end
+
+ it "raises an ArgumentError if there are too many arguments" do
+ -> { @o.rb_scan_args([1, 2, 3, 4], "3", 0, @acc) }.should.raise(ArgumentError, "wrong number of arguments (given 4, expected 3)")
end
it "assigns the required and optional arguments scanned" do
@@ -43,7 +48,7 @@ describe "C-API Util function" do
ScratchPad.recorded.should == [1, 2, [3, 4]]
end
- it "assigns the required and optional arguments and and empty Array when there are no arguments to splat" do
+ it "assigns the required and optional arguments and empty Array when there are no arguments to splat" do
@o.rb_scan_args([1, 2], "11*", 3, @acc).should == 2
ScratchPad.recorded.should == [1, 2, []]
end
@@ -95,50 +100,126 @@ describe "C-API Util function" do
it "assigns Hash arguments" do
h = {a: 1, b: 2}
- @o.rb_scan_args([h], "0:", 1, @acc).should == 0
+ @o.rb_scan_args([h], "k0:", 1, @acc).should == 0
ScratchPad.recorded.should == [h]
end
it "assigns required and Hash arguments" do
h = {a: 1, b: 2}
- @o.rb_scan_args([1, h], "1:", 2, @acc).should == 1
+ @o.rb_scan_args([1, h], "k1:", 2, @acc).should == 1
ScratchPad.recorded.should == [1, h]
end
+ it "assigns required and Hash arguments with optional Hash" do
+ @o.rb_scan_args([1], "1:", 2, @acc).should == 1
+ ScratchPad.recorded.should == [1, nil]
+ end
+
+ it "rejects the use of nil as a hash" do
+ -> {
+ @o.rb_scan_args([1, nil], "1:", 2, @acc)
+ }.should.raise(ArgumentError, "wrong number of arguments (given 2, expected 1)")
+ ScratchPad.recorded.should == []
+ end
+
+ it "rejects the use of of a non-Hash as keywords" do
+ -> {
+ @o.rb_scan_args([42], ":", 1, @acc)
+ }.should.raise(ArgumentError, "wrong number of arguments (given 1, expected 0)")
+ ScratchPad.recorded.should == []
+ end
+
+ it "assigns required and optional arguments with no hash argument given" do
+ @o.rb_scan_args([1, 7, 4], "21:", 3, @acc).should == 3
+ ScratchPad.recorded.should == [1, 7, 4]
+ end
+
+ it "assigns optional arguments with no hash argument given" do
+ @o.rb_scan_args([1, 7], "02:", 3, @acc).should == 2
+ ScratchPad.recorded.should == [1, 7, nil]
+ end
+
+ it "assigns optional arguments with no hash argument given and rejects the use of optional nil argument as a hash" do
+ -> {
+ @o.rb_scan_args([1, nil], "02:", 3, @acc).should == 2
+ }.should_not complain
+
+ ScratchPad.recorded.should == [1, nil, nil]
+ end
+
it "assigns required, optional, splat, post-splat, Hash and block arguments" do
h = {a: 1, b: 2}
- @o.rb_scan_args([1, 2, 3, 4, 5, h], "11*1:&", 6, @acc, &@prc).should == 5
+ @o.rb_scan_args([1, 2, 3, 4, 5, h], "k11*1:&", 6, @acc, &@prc).should == 5
ScratchPad.recorded.should == [1, 2, [3, 4], 5, h, @prc]
end
- # r43934
- it "rejects non-keyword arguments" do
+ it "does not reject non-symbol keys in keyword arguments" do
h = {1 => 2, 3 => 4}
- lambda {
- @o.rb_scan_args([h], "0:", 1, @acc)
- }.should raise_error(ArgumentError)
- ScratchPad.recorded.should == []
+ @o.rb_scan_args([h], "k0:", 1, @acc).should == 0
+ ScratchPad.recorded.should == [h]
end
- it "rejects required and non-keyword arguments" do
+ it "does not reject non-symbol keys in keyword arguments with required argument" do
h = {1 => 2, 3 => 4}
- lambda {
- @o.rb_scan_args([1, h], "1:", 2, @acc)
- }.should raise_error(ArgumentError)
- ScratchPad.recorded.should == []
+ @o.rb_scan_args([1, h], "k1:", 2, @acc).should == 1
+ ScratchPad.recorded.should == [1, h]
end
- it "considers the hash as a post argument when there is a splat" do
+ it "considers keyword arguments with non-symbol keys as keywords when using splat and post arguments" do
h = {1 => 2, 3 => 4}
- @o.rb_scan_args([1, 2, 3, 4, 5, h], "11*1:&", 6, @acc, &@prc).should == 6
- ScratchPad.recorded.should == [1, 2, [3, 4, 5], h, nil, @prc]
+ @o.rb_scan_args([1, 2, 3, 4, 5, h], "k11*1:&", 6, @acc, &@prc).should == 5
+ ScratchPad.recorded.should == [1, 2, [3, 4], 5, h, @prc]
+ end
+ end
+
+ describe "rb_get_kwargs" do
+ it "extracts required arguments in the order requested" do
+ h = { :a => 7, :b => 5 }
+ @o.rb_get_kwargs(h, [:b, :a], 2, 0).should == [5, 7]
+ h.should == {}
+ end
+
+ it "extracts required and optional arguments in the order requested" do
+ h = { :a => 7, :c => 12, :b => 5 }
+ @o.rb_get_kwargs(h, [:b, :a, :c], 2, 1).should == [5, 7, 12]
+ h.should == {}
+ end
+
+ it "accepts nil instead of a hash when only optional arguments are requested" do
+ h = nil
+ @o.rb_get_kwargs(h, [:b, :a, :c], 0, 3).should == []
+ h.should == nil
+ end
+
+ it "raises an error if a required argument is not in the hash" do
+ h = { :a => 7, :c => 12, :b => 5 }
+ -> { @o.rb_get_kwargs(h, [:b, :d], 2, 0) }.should.raise(ArgumentError, "missing keyword: :d")
+ h.should == {:a => 7, :c => 12}
+ end
+
+ it "does not raise an error for an optional argument not in the hash" do
+ h = { :a => 7, :b => 5 }
+ @o.rb_get_kwargs(h, [:b, :a, :c], 2, 1).should == [5, 7]
+ h.should == {}
+ end
+
+ it "raises an error if there are additional arguments and optional is positive" do
+ h = { :a => 7, :c => 12, :b => 5 }
+ -> { @o.rb_get_kwargs(h, [:b, :a], 2, 0) }.should.raise(ArgumentError, "unknown keyword: :c")
+ h.should == {:c => 12}
+ end
+
+ it "leaves additional arguments in the hash if optional is negative" do
+ h = { :a => 7, :c => 12, :b => 5 }
+ @o.rb_get_kwargs(h, [:b, :a], 2, -1).should == [5, 7]
+ h.should == {:c => 12}
end
end
- platform_is wordsize: 64 do
+ platform_is c_long_size: 64 do
describe "rb_long2int" do
it "raises a RangeError if the value is outside the range of a C int" do
- lambda { @o.rb_long2int(0xffff_ffff_ffff) }.should raise_error(RangeError)
+ -> { @o.rb_long2int(0xffff_ffff_ffff) }.should.raise(RangeError)
end
end
@@ -184,7 +265,37 @@ describe "C-API Util function" do
describe "rb_sourceline" do
it "returns the current ruby file" do
- @o.rb_sourceline.should be_kind_of(Fixnum)
+ @o.rb_sourceline.should.is_a?(Integer)
+ end
+ end
+
+ # ruby/util.h redefines strtod as a macro calling ruby_strtod
+
+ describe "strtod" do
+ it "converts a string to a double and returns the remaining string" do
+ d, s = @o.strtod("14.25test")
+ d.should == 14.25
+ s.should == "test"
+ end
+
+ it "returns 0 and the full string if there's no numerical value" do
+ d, s = @o.strtod("test")
+ d.should == 0
+ s.should == "test"
+ end
+ end
+
+ describe "ruby_strtod" do
+ it "converts a string to a double and returns the remaining string" do
+ d, s = @o.ruby_strtod("14.25test")
+ d.should == 14.25
+ s.should == "test"
+ end
+
+ it "returns 0 and the full string if there's no numerical value" do
+ d, s = @o.ruby_strtod("test")
+ d.should == 0
+ s.should == "test"
end
end