diff --git a/core/env/fetch_spec.rb b/core/env/fetch_spec.rb index a9c136986e..cc74e32da3 100644 --- a/core/env/fetch_spec.rb +++ b/core/env/fetch_spec.rb @@ -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 @@ -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 diff --git a/core/hash/fetch_spec.rb b/core/hash/fetch_spec.rb index 38f1a873a0..24662c2b0e 100644 --- a/core/hash/fetch_spec.rb +++ b/core/hash/fetch_spec.rb @@ -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 - - 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 diff --git a/core/hash/fetch_values_spec.rb b/core/hash/fetch_values_spec.rb index 1c84697b33..0d7c9599e7 100644 --- a/core/hash/fetch_values_spec.rb +++ b/core/hash/fetch_values_spec.rb @@ -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 @@ -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"] @@ -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 diff --git a/core/kernel/shared/sprintf.rb b/core/kernel/shared/sprintf.rb index 1eacf52485..9935d27db9 100644 --- a/core/kernel/shared/sprintf.rb +++ b/core/kernel/shared/sprintf.rb @@ -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) @@ -823,36 +825,6 @@ def obj.to_str format("%d %d", 1, foo: "123") }.should raise_error(ArgumentError) end - - context "when there is no matching key" do - it "raises KeyError" do - -> () { - format("%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("%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("%s", @hash) - }.should raise_error(KeyError) { |err| - err.key.should == :foo - } - end - end - end end describe "%{name} style" do @@ -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 diff --git a/core/string/modulo_spec.rb b/core/string/modulo_spec.rb index b6a6918880..7cb3561b17 100644 --- a/core/string/modulo_spec.rb +++ b/core/string/modulo_spec.rb @@ -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 @@ -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 diff --git a/optional/capi/hash_spec.rb b/optional/capi/hash_spec.rb index 4d818bb9ec..1a117bfbf8 100644 --- a/optional/capi/hash_spec.rb +++ b/optional/capi/hash_spec.rb @@ -1,4 +1,5 @@ require File.expand_path('../spec_helper', __FILE__) +require File.expand_path('../../../shared/hash/key_error', __FILE__) load_extension("hash") @@ -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 diff --git a/shared/hash/key_error.rb b/shared/hash/key_error.rb new file mode 100644 index 0000000000..061c88c483 --- /dev/null +++ b/shared/hash/key_error.rb @@ -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