Skip to content

Commit

Permalink
Add specs for String#{sub,gsub,index,rindex} where the source and pat…
Browse files Browse the repository at this point in the history
…tern encodings differ

* For #2749
  • Loading branch information
eregon committed Oct 3, 2022
1 parent c4730d7 commit 01cc9a9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
14 changes: 12 additions & 2 deletions spec/ruby/core/string/gsub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,6 @@ def replacement.to_str() "hello_replacement" end
end
end

# Note: $~ cannot be tested because mspec messes with it

it "sets $~ to MatchData of last match and nil when there's none" do
'hello.'.gsub('hello', 'x')
$~[0].should == 'hello'
Expand All @@ -225,6 +223,18 @@ def replacement.to_str() "hello_replacement" end
'hello.'.gsub(/not/, 'x')
$~.should == nil
end

it "handles a pattern in a superset encoding" do
result = 'abc'.force_encoding(Encoding::US_ASCII).gsub('é', 'è')
result.should == 'abc'
result.encoding.should == Encoding::US_ASCII
end

it "handles a pattern in a subset encoding" do
result = 'été'.gsub('t'.force_encoding(Encoding::US_ASCII), 'u')
result.should == 'éué'
result.encoding.should == Encoding::UTF_8
end
end

describe "String#gsub with pattern and Hash" do
Expand Down
8 changes: 8 additions & 0 deletions spec/ruby/core/string/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@
"あれ".index char
end.should raise_error(Encoding::CompatibilityError)
end

it "handles a substring in a superset encoding" do
'abc'.force_encoding(Encoding::US_ASCII).index('é').should == nil
end

it "handles a substring in a subset encoding" do
'été'.index('t'.force_encoding(Encoding::US_ASCII)).should == 1
end
end

describe "String#index with Regexp" do
Expand Down
8 changes: 8 additions & 0 deletions spec/ruby/core/string/rindex_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ def obj.method_missing(*args) 5 end
it "raises a TypeError when given offset is nil" do
-> { "str".rindex("st", nil) }.should raise_error(TypeError)
end

it "handles a substring in a superset encoding" do
'abc'.force_encoding(Encoding::US_ASCII).rindex('é').should == nil
end

it "handles a substring in a subset encoding" do
'été'.rindex('t'.force_encoding(Encoding::US_ASCII)).should == 1
end
end

describe "String#rindex with Regexp" do
Expand Down
11 changes: 11 additions & 0 deletions spec/ruby/core/string/sub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,17 @@
"ababa".sub(/(b)/, '\\\\\1').should == "a\\baba"
end

it "handles a pattern in a superset encoding" do
result = 'abc'.force_encoding(Encoding::US_ASCII).sub('é', 'è')
result.should == 'abc'
result.encoding.should == Encoding::US_ASCII
end

it "handles a pattern in a subset encoding" do
result = 'été'.sub('t'.force_encoding(Encoding::US_ASCII), 'u')
result.should == 'éué'
result.encoding.should == Encoding::UTF_8
end
end

describe "String#sub with pattern and block" do
Expand Down

0 comments on commit 01cc9a9

Please sign in to comment.