From abe75149d14d3286d3051c9e961ab6473a243a19 Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 20 Sep 2018 15:06:56 +0000 Subject: Enumerable#to_h with block and so on [Feature #15143] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64794 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- spec/ruby/core/array/to_h_spec.rb | 7 +++++++ spec/ruby/core/enumerable/to_h_spec.rb | 8 ++++++++ spec/ruby/core/env/to_h_spec.rb | 13 +++++++++++++ spec/ruby/core/hash/to_h_spec.rb | 6 ++++++ spec/ruby/core/struct/to_h_spec.rb | 8 ++++++++ 5 files changed, 42 insertions(+) (limited to 'spec/ruby') diff --git a/spec/ruby/core/array/to_h_spec.rb b/spec/ruby/core/array/to_h_spec.rb index 9104aed301..e845d2c950 100644 --- a/spec/ruby/core/array/to_h_spec.rb +++ b/spec/ruby/core/array/to_h_spec.rb @@ -34,4 +34,11 @@ describe "Array#to_h" do it "does not accept arguments" do lambda { [].to_h(:a, :b) }.should raise_error(ArgumentError) end + + ruby_version_is "2.6" do + it "converts [key, value] pairs returned by the block to a hash" do + i = 0 + [:a, :b].to_h {|k| [k, i += 1]}.should == { a: 1, b: 2 } + end + end end diff --git a/spec/ruby/core/enumerable/to_h_spec.rb b/spec/ruby/core/enumerable/to_h_spec.rb index 3b712d25d9..966325c736 100644 --- a/spec/ruby/core/enumerable/to_h_spec.rb +++ b/spec/ruby/core/enumerable/to_h_spec.rb @@ -43,4 +43,12 @@ describe "Enumerable#to_h" do enum = EnumerableSpecs::EachDefiner.new([:x]) lambda { enum.to_h }.should raise_error(ArgumentError) end + + ruby_version_is "2.6" do + it "converts [key, value] pairs returned by the block to a hash" do + enum = EnumerableSpecs::EachDefiner.new(:a, :b) + i = 0 + enum.to_h {|k| [k, i += 1]}.should == { a: 1, b: 2 } + end + end end diff --git a/spec/ruby/core/env/to_h_spec.rb b/spec/ruby/core/env/to_h_spec.rb index f7f4314335..f6c796b4d6 100644 --- a/spec/ruby/core/env/to_h_spec.rb +++ b/spec/ruby/core/env/to_h_spec.rb @@ -3,4 +3,17 @@ require_relative 'shared/to_hash' describe "ENV.to_hash" do it_behaves_like :env_to_hash, :to_h + + ruby_version_is "2.6" do + it "converts [key, value] pairs returned by the block to a hash" do + orig = ENV.to_hash + begin + ENV.replace "a" => "b", "c" => "d" + i = 0 + ENV.to_h {|k, v| [k.to_sym, v.upcase]}.should == {a:"B", c:"D"} + ensure + ENV.replace orig + end + end + end end diff --git a/spec/ruby/core/hash/to_h_spec.rb b/spec/ruby/core/hash/to_h_spec.rb index 55c5532fc0..40bd0d06d2 100644 --- a/spec/ruby/core/hash/to_h_spec.rb +++ b/spec/ruby/core/hash/to_h_spec.rb @@ -7,6 +7,12 @@ describe "Hash#to_h" do h.to_h.should equal(h) end + ruby_version_is "2.6" do + it "converts [key, value] pairs returned by the block to a hash" do + {a: 1, b: 2}.to_h {|k, v| [k.to_s, v*v]}.should == { "a" => 1, "b" => 4 } + end + end + describe "when called on a subclass of Hash" do before :each do @h = HashSpecs::MyHash.new diff --git a/spec/ruby/core/struct/to_h_spec.rb b/spec/ruby/core/struct/to_h_spec.rb index 9239c87988..ddfbfbca61 100644 --- a/spec/ruby/core/struct/to_h_spec.rb +++ b/spec/ruby/core/struct/to_h_spec.rb @@ -12,4 +12,12 @@ describe "Struct#to_h" do car.to_h[:make] = 'Suzuki' car.make.should == 'Ford' end + + ruby_version_is "2.6" do + it "converts [key, value] pairs returned by the block to a hash" do + car = StructClasses::Car.new('Ford', 'Ranger') + h = car.to_h {|k, v| [k.to_s, "#{v}".downcase]} + h.should == {"make" => "ford", "model" => "ranger", "year" => ""} + end + end end -- cgit v1.2.3