Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 2 additions & 25 deletions core/env/fetch_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../../../shared/hash/key_error', __FILE__)

describe "ENV.fetch" do
it "returns a value" do
Expand All @@ -12,31 +13,7 @@
end

context "when the key is not found" do
before :each do
@key = "should_never_be_set"
end

it "raises a KeyError" do
lambda { ENV.fetch @key }.should raise_error(KeyError)
end

ruby_version_is "2.5" do
it "sets the ENV as the receiver of KeyError" do
-> {
ENV.fetch @key
}.should raise_error(KeyError) { |err|
err.receiver.should == ENV
}
end

it "sets the non-existent key as the key of KeyError" do
-> {
ENV.fetch @key
}.should raise_error(KeyError) { |err|
err.key.should == @key
}
end
end
it_behaves_like :key_error, ->(obj, key) { obj.fetch(key) }, ENV
end

it "provides the given default parameter" do
Expand Down
39 changes: 8 additions & 31 deletions core/hash/fetch_spec.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,17 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
require File.expand_path('../../../shared/hash/key_error', __FILE__)

describe "Hash#fetch" do
it "returns the value for key" do
{ a: 1, b: -1 }.fetch(:b).should == -1
end

context "when the key is not found" do
it "raises a KeyError" do
lambda { {}.fetch(:a) }.should raise_error(KeyError)
lambda { Hash.new(5).fetch(:a) }.should raise_error(KeyError)
lambda { Hash.new { 5 }.fetch(:a) }.should raise_error(KeyError)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to keep these examples which use different kinds of Hash.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added 3 more it_behaves_like calls so it can test all the shared specs in each of the Hash creation methods.


ruby_version_is "2.5" do
before :each do
@hsh = { }
@key = :a
end

it "sets the Hash as the receiver of KeyError" do
-> {
@hsh.fetch(@key)
}.should raise_error(KeyError) { |err|
err.receiver.should == @hsh
}
end
it_behaves_like :key_error, ->(obj, key) { obj.fetch(key) }, Hash.new(a: 5)
it_behaves_like :key_error, ->(obj, key) { obj.fetch(key) }, {}
it_behaves_like :key_error, ->(obj, key) { obj.fetch(key) }, Hash.new { 5 }
it_behaves_like :key_error, ->(obj, key) { obj.fetch(key) }, Hash.new(5)
end

it "sets the not-found key as key of KeyError" do
-> {
@hsh.fetch(@key)
}.should raise_error(KeyError) { |err|
err.key.should == @key
}
end
end
it "returns the value for key" do
{ a: 1, b: -1 }.fetch(:b).should == -1
end

it "returns default if key is not found when passed a default" do
Expand Down
34 changes: 2 additions & 32 deletions core/hash/fetch_values_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
require File.expand_path('../../../shared/hash/key_error', __FILE__)

ruby_version_is "2.3" do
describe "Hash#fetch_values" do
Expand All @@ -15,10 +16,7 @@
end

describe "with unmatched keys" do
it "raises a KeyError" do
->{ @hash.fetch_values :z }.should raise_error(KeyError)
->{ @hash.fetch_values :a, :z }.should raise_error(KeyError)
end
it_behaves_like :key_error, ->(obj, key) { obj.fetch_values(key) }, Hash.new(a: 5)

it "returns the default value from block" do
@hash.fetch_values(:z) { |key| "`#{key}' is not found" }.should == ["`z' is not found"]
Expand All @@ -33,31 +31,3 @@
end
end
end

ruby_version_is "2.5" do
describe "Hash#fetch_values" do
before :each do
@hash = { a: 1, b: 2, c: 3 }
end

describe "with unmatched keys" do
before :each do
end
it "sets the Hash as the receiver of KeyError" do
-> {
@hash.fetch_values :a, :z
}.should raise_error(KeyError) { |err|
err.receiver.should == @hash
}
end

it "sets the unmatched key as the key of KeyError" do
-> {
@hash.fetch_values :a, :z
}.should raise_error(KeyError) { |err|
err.key.should == :z
}
end
end
end
end
42 changes: 12 additions & 30 deletions core/kernel/shared/sprintf.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require File.expand_path('../../../../shared/hash/key_error', __FILE__)

describe :kernel_sprintf, shared: true do
def format(*args)
@method.call(*args)
Expand Down Expand Up @@ -823,36 +825,6 @@ def obj.to_str
format("%d %<foo>d", 1, foo: "123")
}.should raise_error(ArgumentError)
end

context "when there is no matching key" do
it "raises KeyError" do
-> () {
format("%<foo>s", {})
}.should raise_error(KeyError)
end

ruby_version_is "2.5" do
before :each do
@hash = { fooo: 1 }
end

it "sets the Hash attempting to format on as receiver of KeyError" do
-> () {
format("%<foo>s", @hash)
}.should raise_error(KeyError) { |err|
err.receiver.should == @hash
}
end

it "sets the faulty key in the formatter as key of KeyError" do
-> () {
format("%<foo>s", @hash)
}.should raise_error(KeyError) { |err|
err.key.should == :foo
}
end
end
end
end

describe "%{name} style" do
Expand Down Expand Up @@ -892,4 +864,14 @@ def obj.to_str; end
end
end
end

describe "faulty key" do
before :all do
@base_method = @method
end

it_behaves_like :key_error, -> (obj, key) {
@base_method.call("%<#{key}>s", obj)
}, { foooo: 1 }
end
end
33 changes: 5 additions & 28 deletions core/string/modulo_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes.rb', __FILE__)
require File.expand_path('../../../shared/hash/key_error', __FILE__)

describe "String#%" do
context "when key is missing from passed-in hash" do
it_behaves_like :key_error, -> (obj, key) { "%{#{key}}" % obj }, { a: 5 }
end

it "formats multiple expressions" do
("%b %x %d %s" % [10, 10, 10, 10]).should == "1010 a 10 10"
end
Expand Down Expand Up @@ -764,34 +769,6 @@ def obj.to_s() "obj" end
("%{foo}bar" % {foo: 'oof'}).should == "oofbar"
end

context "when key is missing from passed-in hash" do
it "raises KeyError" do
lambda {"%{foo}" % {}}.should raise_error(KeyError)
end

ruby_version_is "2.5" do
before :each do
@hash = { fooo: 1 }
end

it "sets the passed-in hash as receiver for KeyError" do
-> {
"%{foo}" % @hash
}.should raise_error(KeyError) { |err|
err.receiver.should == @hash
}
end

it "sets the missing key as key in KeyError" do
-> {
"%{foo}" % @hash
}.should raise_error(KeyError) { |err|
err.key.should == :foo
}
end
end
end

it "should raise ArgumentError if no hash given" do
lambda {"%{foo}" % []}.should raise_error(ArgumentError)
end
Expand Down
21 changes: 4 additions & 17 deletions optional/capi/hash_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require File.expand_path('../spec_helper', __FILE__)
require File.expand_path('../../../shared/hash/key_error', __FILE__)

load_extension("hash")

Expand Down Expand Up @@ -138,23 +139,9 @@
end

context "when key is not found" do
ruby_version_is "2.5" do
it "sets the hash as receiver for KeyError" do
-> {
@s.rb_hash_fetch(@hsh, :c)
}.should raise_error(KeyError) { |err|
err.receiver.should == @hsh
}
end

it "sets the key as key for KeyError" do
-> {
@s.rb_hash_fetch(@hsh, :c)
}.should raise_error(KeyError) { |err|
err.key.should == :c
}
end
end
it_behaves_like :key_error, -> (obj, key) {
@s.rb_hash_fetch(obj, key)
}, { a: 1 }
end
end

Expand Down
25 changes: 25 additions & 0 deletions shared/hash/key_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
describe :key_error, shared: true do
it "raises a KeyError" do
-> {
@method.call(@object, 'foo')
}.should raise_error(KeyError)
end

ruby_version_is "2.5" do
it "sets the Hash as the receiver of KeyError" do
-> {
@method.call(@object, 'foo')
}.should raise_error(KeyError) { |err|
err.receiver.should equal(@object)
}
end

it "sets the unmatched key as the key of KeyError" do
-> {
@method.call(@object, 'foo')
}.should raise_error(KeyError) { |err|
err.key.to_s.should == 'foo'
}
end
end
end