Skip to content

Commit 64f6076

Browse files
nirvdrumeregon
authored andcommitted
Add specs for broken string behavior.
1 parent 0f63df6 commit 64f6076

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

spec/ruby/core/string/lstrip_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,10 @@
5050
-> { "hello".freeze.lstrip! }.should raise_error(FrozenError)
5151
-> { "".freeze.lstrip! }.should raise_error(FrozenError)
5252
end
53+
54+
it "raises an ArgumentError if the first codepoint is invalid" do
55+
s = "\xDFabc".force_encoding(Encoding::UTF_8)
56+
s.valid_encoding?.should be_false
57+
-> { s.send(@method) { } }.should raise_error(ArgumentError)
58+
end
5359
end

spec/ruby/core/string/rstrip_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,10 @@
4646
-> { "hello".freeze.rstrip! }.should raise_error(FrozenError)
4747
-> { "".freeze.rstrip! }.should raise_error(FrozenError)
4848
end
49+
50+
it "raises an ArgumentError if the last codepoint is invalid" do
51+
s = "abc\xDF".force_encoding(Encoding::UTF_8)
52+
s.valid_encoding?.should be_false
53+
-> { s.send(@method) { } }.should raise_error(ArgumentError)
54+
end
4955
end

spec/ruby/core/string/split_spec.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
require_relative 'fixtures/classes'
44

55
describe "String#split with String" do
6+
it "throws an ArgumentError if the string is not a valid" do
7+
s = "\xDF".force_encoding(Encoding::UTF_8)
8+
9+
-> { s.split }.should raise_error(ArgumentError)
10+
-> { s.split(':') }.should raise_error(ArgumentError)
11+
end
12+
613
it "throws an ArgumentError if the pattern is not a valid string" do
714
str = 'проверка'
8-
broken_str = 'проверка'
9-
broken_str.force_encoding('binary')
10-
broken_str.chop!
11-
broken_str.force_encoding('utf-8')
15+
broken_str = "\xDF".force_encoding(Encoding::UTF_8)
16+
1217
-> { str.split(broken_str) }.should raise_error(ArgumentError)
1318
end
1419

@@ -218,6 +223,12 @@
218223
end
219224

220225
describe "String#split with Regexp" do
226+
it "throws an ArgumentError if the string is not a valid" do
227+
s = "\xDF".force_encoding(Encoding::UTF_8)
228+
229+
-> { s.split(/./) }.should raise_error(ArgumentError)
230+
end
231+
221232
it "divides self on regexp matches" do
222233
" now's the time".split(/ /).should == ["", "now's", "", "the", "time"]
223234
" x\ny ".split(/ /).should == ["", "x\ny"]

0 commit comments

Comments
 (0)