diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
commit | 1d15d5f08032acf1b7bceacbb450d617ff6e0931 (patch) | |
tree | a3785a79899302bc149e4a6e72f624ac27dc1f10 /spec/ruby/core/enumerable/to_h_spec.rb | |
parent | 75bfc6440d595bf339007f4fb280fd4d743e89c1 (diff) |
Move spec/rubyspec to spec/ruby for consistency
* Other ruby implementations use the spec/ruby directory.
[Misc #13792] [ruby-core:82287]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/core/enumerable/to_h_spec.rb')
-rw-r--r-- | spec/ruby/core/enumerable/to_h_spec.rb | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/spec/ruby/core/enumerable/to_h_spec.rb b/spec/ruby/core/enumerable/to_h_spec.rb new file mode 100644 index 0000000000..b5b301b882 --- /dev/null +++ b/spec/ruby/core/enumerable/to_h_spec.rb @@ -0,0 +1,46 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Enumerable#to_h" do + it "converts empty enumerable to empty hash" do + enum = EnumerableSpecs::EachDefiner.new + enum.to_h.should == {} + end + + it "converts yielded [key, value] pairs to a hash" do + enum = EnumerableSpecs::EachDefiner.new([:a, 1], [:b, 2]) + enum.to_h.should == { a: 1, b: 2 } + end + + it "uses the last value of a duplicated key" do + enum = EnumerableSpecs::EachDefiner.new([:a, 1], [:b, 2], [:a, 3]) + enum.to_h.should == { a: 3, b: 2 } + end + + it "calls #to_ary on contents" do + pair = mock('to_ary') + pair.should_receive(:to_ary).and_return([:b, 2]) + enum = EnumerableSpecs::EachDefiner.new([:a, 1], pair) + enum.to_h.should == { a: 1, b: 2 } + end + + it "forwards arguments to #each" do + enum = Object.new + def enum.each(*args) + yield(*args) + yield([:b, 2]) + end + enum.extend Enumerable + enum.to_h(:a, 1).should == { a: 1, b: 2 } + end + + it "raises TypeError if an element is not an array" do + enum = EnumerableSpecs::EachDefiner.new(:x) + lambda { enum.to_h }.should raise_error(TypeError) + end + + it "raises ArgumentError if an element is not a [key, value] pair" do + enum = EnumerableSpecs::EachDefiner.new([:x]) + lambda { enum.to_h }.should raise_error(ArgumentError) + end +end |