diff options
author | Benoit Daloze <eregontp@gmail.com> | 2020-07-27 21:41:08 +0200 |
---|---|---|
committer | Benoit Daloze <eregontp@gmail.com> | 2020-07-27 21:41:08 +0200 |
commit | 126fd5f15cff0d3bf314d90d8c21a3ae25ae8e68 (patch) | |
tree | 33350f7170436c32ed4c8e79f0be2c334c7bc8a9 /spec/ruby/core/string/split_spec.rb | |
parent | 7429841ab6494b849106e6d3b119f147adfee3b7 (diff) |
Update to ruby/spec@07164da
Diffstat (limited to 'spec/ruby/core/string/split_spec.rb')
-rw-r--r-- | spec/ruby/core/string/split_spec.rb | 68 |
1 files changed, 63 insertions, 5 deletions
diff --git a/spec/ruby/core/string/split_spec.rb b/spec/ruby/core/string/split_spec.rb index 34a50a5f13..7ac8d7815c 100644 --- a/spec/ruby/core/string/split_spec.rb +++ b/spec/ruby/core/string/split_spec.rb @@ -426,12 +426,70 @@ describe "String#split with Regexp" do end ruby_version_is "2.6" do - it "yields each split substrings if a block is given" do - a = [] - returned_object = "chunky bacon".split(" ") { |str| a << str.capitalize } + context "when a block is given" do + it "yields each split substring with default pattern" do + a = [] + returned_object = "chunky bacon".split { |str| a << str.capitalize } + + returned_object.should == "chunky bacon" + a.should == ["Chunky", "Bacon"] + end + + it "yields the string when limit is 1" do + a = [] + returned_object = "chunky bacon".split("", 1) { |str| a << str.capitalize } + + returned_object.should == "chunky bacon" + a.should == ["Chunky bacon"] + end + + it "yields each split letter" do + a = [] + returned_object = "chunky".split("", 0) { |str| a << str.capitalize } + + returned_object.should == "chunky" + a.should == %w(C H U N K Y) + end + + it "yields each split substring with a pattern" do + a = [] + returned_object = "chunky-bacon".split("-", 0) { |str| a << str.capitalize } + + returned_object.should == "chunky-bacon" + a.should == ["Chunky", "Bacon"] + end - returned_object.should == "chunky bacon" - a.should == ["Chunky", "Bacon"] + it "yields each split substring with empty regexp pattern" do + a = [] + returned_object = "chunky".split(//) { |str| a << str.capitalize } + + returned_object.should == "chunky" + a.should == %w(C H U N K Y) + end + + it "yields each split substring with empty regexp pattern and limit" do + a = [] + returned_object = "chunky".split(//, 3) { |str| a << str.capitalize } + + returned_object.should == "chunky" + a.should == %w(C H Unky) + end + + it "yields each split substring with a regexp pattern" do + a = [] + returned_object = "chunky:bacon".split(/:/) { |str| a << str.capitalize } + + returned_object.should == "chunky:bacon" + a.should == ["Chunky", "Bacon"] + end + + it "returns a string as is (and doesn't call block) if it is empty" do + a = [] + returned_object = "".split { |str| a << str.capitalize } + + returned_object.should == "" + a.should == [] + end end describe "for a String subclass" do |