diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
commit | a3736e97a6ca517c2cd7d3d93a8f2ef86e39e5b5 (patch) | |
tree | 9eef7f720314ebaff56845a74e203770e62284e4 /spec/rubyspec/core/string/scrub_spec.rb | |
parent | 52df1d0d3370919711c0577aaa42d1a864709885 (diff) | |
download | ruby-a3736e97a6ca517c2cd7d3d93a8f2ef86e39e5b5.tar.gz |
Add in-tree mspec and ruby/spec
* For easier modifications of ruby/spec by MRI developers.
* .gitignore: track changes under spec.
* spec/mspec, spec/rubyspec: add in-tree mspec and ruby/spec.
These files can therefore be updated like any other file in MRI.
Instructions are provided in spec/README.
[Feature #13156] [ruby-core:79246]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58595 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/rubyspec/core/string/scrub_spec.rb')
-rw-r--r-- | spec/rubyspec/core/string/scrub_spec.rb | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/spec/rubyspec/core/string/scrub_spec.rb b/spec/rubyspec/core/string/scrub_spec.rb new file mode 100644 index 0000000000..815eb0fbb7 --- /dev/null +++ b/spec/rubyspec/core/string/scrub_spec.rb @@ -0,0 +1,101 @@ +# -*- encoding: utf-8 -*- +require File.expand_path("../../../spec_helper", __FILE__) + +describe "String#scrub with a default replacement" do + it "returns self for valid strings" do + input = "foo" + + input.scrub.should == input + end + + it "replaces invalid byte sequences" do + x81 = [0x81].pack('C').force_encoding('utf-8') + "abc\u3042#{x81}".scrub.should == "abc\u3042\uFFFD" + end + + it "returns a copy of self when the input encoding is BINARY" do + input = "foo".encode('BINARY') + + 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 +end + +describe "String#scrub with a custom replacement" do + it "returns self for valid strings" do + input = "foo" + + input.scrub("*").should == input + end + + it "replaces invalid byte sequences" do + x81 = [0x81].pack('C').force_encoding('utf-8') + "abc\u3042#{x81}".scrub("*").should == "abc\u3042*" + end + + it "replaces an incomplete character at the end with a single replacement" do + xE3x80 = [0xE3, 0x80].pack('CC').force_encoding 'utf-8' + xE3x80.scrub("*").should == "*" + end + + it "raises ArgumentError for replacements with an invalid encoding" do + x81 = [0x81].pack('C').force_encoding('utf-8') + xE4 = [0xE4].pack('C').force_encoding('utf-8') + block = lambda { "foo#{x81}".scrub(xE4) } + + block.should raise_error(ArgumentError) + end + + it "raises TypeError when a non String replacement is given" do + x81 = [0x81].pack('C').force_encoding('utf-8') + block = lambda { "foo#{x81}".scrub(1) } + + block.should raise_error(TypeError) + end +end + +describe "String#scrub with a block" do + it "returns self for valid strings" do + input = "foo" + + input.scrub { |b| "*" }.should == input + end + + it "replaces invalid byte sequences" do + xE3x80 = [0xE3, 0x80].pack('CC').force_encoding 'utf-8' + replaced = "abc\u3042#{xE3x80}".scrub { |b| "<#{b.unpack("H*")[0]}>" } + + replaced.should == "abc\u3042<e380>" + end + + it "replaces invalid byte sequences using a custom encoding" do + x80x80 = [0x80, 0x80].pack('CC').force_encoding 'utf-8' + replaced = x80x80.scrub do |bad| + bad.encode(Encoding::UTF_8, Encoding::Windows_1252) + end + + replaced.should == "€€" + end +end + +describe "String#scrub!" do + it "modifies self for valid strings" do + x81 = [0x81].pack('C').force_encoding('utf-8') + input = "a#{x81}" + input.scrub! + input.should == "a\uFFFD" + end + + it "accepts blocks" do + x81 = [0x81].pack('C').force_encoding('utf-8') + input = "a#{x81}" + input.scrub! { |b| "<?>" } + input.should == "a<?>" + end +end |