Skip to content

Commit 78d5070

Browse files
committed
Add String#split specs to verify behavior when input string has trailing whitespace and a limit > 0 is supplied.
1 parent c33b0ab commit 78d5070

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

spec/ruby/core/string/split_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,29 @@
3434
",".split(",", 0).should == []
3535
end
3636

37+
it "does not suppress trailing empty fields when a positive limit is given" do
38+
" 1 2 ".split(" ", 2).should == ["1", "2 "]
39+
" 1 2 ".split(" ", 3).should == ["1", "2", ""]
40+
" 1 2 ".split(" ", 4).should == ["1", "2", ""]
41+
" 1 あ ".split(" ", 2).should == ["1", "あ "]
42+
" 1 あ ".split(" ", 3).should == ["1", "あ", ""]
43+
" 1 あ ".split(" ", 4).should == ["1", "あ", ""]
44+
45+
"1,2,".split(',', 2).should == ["1", "2,"]
46+
"1,2,".split(',', 3).should == ["1", "2", ""]
47+
"1,2,".split(',', 4).should == ["1", "2", ""]
48+
"1,あ,".split(',', 2).should == ["1", "あ,"]
49+
"1,あ,".split(',', 3).should == ["1", "あ", ""]
50+
"1,あ,".split(',', 4).should == ["1", "あ", ""]
51+
52+
"1 2 ".split(/ /, 2).should == ["1", "2 "]
53+
"1 2 ".split(/ /, 3).should == ["1", "2", ""]
54+
"1 2 ".split(/ /, 4).should == ["1", "2", ""]
55+
"1 あ ".split(/ /, 2).should == ["1", "あ "]
56+
"1 あ ".split(/ /, 3).should == ["1", "あ", ""]
57+
"1 あ ".split(/ /, 4).should == ["1", "あ", ""]
58+
end
59+
3760
it "returns an array with one entry if limit is 1: the original string" do
3861
"hai".split("hai", 1).should == ["hai"]
3962
"x.y.z".split(".", 1).should == ["x.y.z"]

0 commit comments

Comments
 (0)