Skip to content

Commit

Permalink
Merge pull request #268 from rspec/handle_jruby_split_issue
Browse files Browse the repository at this point in the history
Fix JRuby bug with encoded string splitting
  • Loading branch information
myronmarston committed Mar 18, 2016
2 parents 3c86358 + a13fa77 commit 635f4b5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/rspec/support/encoded_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,18 @@ def <<(string)
@string << matching_encoding(string)
end

def split(regex_or_string)
@string.split(matching_encoding(regex_or_string))
if Ruby.jruby?
def split(regex_or_string)
@string.split(matching_encoding(regex_or_string))
rescue ArgumentError
# JRuby raises an ArgumentError when splitting a source string that
# contains invalid bytes.
remove_invalid_bytes(@string).split regex_or_string
end
else
def split(regex_or_string)
@string.split(matching_encoding(regex_or_string))
end
end

def to_s
Expand Down
10 changes: 10 additions & 0 deletions spec/rspec/support/encoded_string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,16 @@ module RSpec::Support
a_string_identical_to(exp2).with_same_encoding
]
end

it 'handles invalidly encoded strings' do
source_string = "an\xAE\nother".force_encoding('US-ASCII')
expect(
build_encoded_string(source_string, utf8_encoding).split("\n")
).to eq([
'an?',
'other'
])
end
end

# see https://github.com/rspec/rspec-expectations/blob/f8a1232/spec/rspec/expectations/fail_with_spec.rb#L50
Expand Down

0 comments on commit 635f4b5

Please sign in to comment.