Skip to content

Commit

Permalink
String#scrub returns String instances when called on a subclass
Browse files Browse the repository at this point in the history
  • Loading branch information
lxxxvi committed Oct 4, 2021
1 parent 9c8f9ec commit 439aaa7
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion core/string/scrub_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- encoding: utf-8 -*-
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "String#scrub with a default replacement" do
it "returns self for valid strings" do
Expand All @@ -19,12 +20,25 @@
input.scrub.should == "foo"
end


it "replaces invalid byte sequences when using ASCII as the input encoding" do
xE3x80 = [0xE3, 0x80].pack('CC').force_encoding 'utf-8'
input = "abc\u3042#{xE3x80}".force_encoding('ASCII')
input.scrub.should == "abc?????"
end

ruby_version_is '3.0' do
it "returns String instances when called on a subclass" do
StringSpecs::MyString.new("foo").scrub.should be_an_instance_of(String)
input = [0x81].pack('C').force_encoding('utf-8')
StringSpecs::MyString.new(input).scrub.should be_an_instance_of(String)
end
end

ruby_version_is ''...'3.0.0' do
it "returns subclass instances when called on a subclass" do
StringSpecs::MyString.new("foo").scrub.should be_an_instance_of(StringSpecs::MyString)
end
end
end

describe "String#scrub with a custom replacement" do
Expand Down Expand Up @@ -65,6 +79,14 @@

block.should raise_error(TypeError)
end

ruby_version_is '3.0' do
it "returns String instances when called on a subclass" do
StringSpecs::MyString.new("foo").scrub("*").should be_an_instance_of(String)
input = [0x81].pack('C').force_encoding('utf-8')
StringSpecs::MyString.new(input).scrub("*").should be_an_instance_of(String)
end
end
end

describe "String#scrub with a block" do
Expand All @@ -89,6 +111,14 @@

replaced.should == "€€"
end

ruby_version_is '3.0' do
it "returns String instances when called on a subclass" do
StringSpecs::MyString.new("foo").scrub { |b| "*" }.should be_an_instance_of(String)
input = [0x81].pack('C').force_encoding('utf-8')
StringSpecs::MyString.new(input).scrub { |b| "<#{b.unpack("H*")[0]}>" }.should be_an_instance_of(String)
end
end
end

describe "String#scrub!" do
Expand Down

0 comments on commit 439aaa7

Please sign in to comment.