1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
This commit is contained in:
Benoit Daloze 2021-10-28 18:54:01 +02:00
parent 3bf36979d8
commit c75df796d8
25 changed files with 454 additions and 38 deletions

View file

@ -67,10 +67,52 @@ describe "String#encode" do
"\rfoo".encode(universal_newline: true).should == "\nfoo"
end
it "replaces invalid encoding" do
encoded = "\xE3\x81\xFF".encode("UTF-16LE", invalid: :replace, replace: "?")
encoded.should == "\u3061??".encode("UTF-16LE")
encoded.encode("UTF-8").should == "ち??"
it "replaces invalid encoding in source with default replacement" do
encoded = "\xE3\x81\xFF".encode("UTF-16LE", invalid: :replace)
encoded.should == "\u3061\ufffd\ufffd".encode("UTF-16LE")
encoded.encode("UTF-8").should == "\ufffd\ufffd"
end
it "replaces invalid encoding in source with a specified replacement" do
encoded = "\xE3\x81\xFF".encode("UTF-16LE", invalid: :replace, replace: "foo")
encoded.should == "\u3061foofoo".encode("UTF-16LE")
encoded.encode("UTF-8").should == "ちfoofoo"
end
it "replaces invalid encoding in source using a specified replacement even when a fallback is given" do
encoded = "\xE3\x81\xFF".encode("UTF-16LE", invalid: :replace, replace: "foo", fallback: -> c { "bar" })
encoded.should == "\u3061foofoo".encode("UTF-16LE")
encoded.encode("UTF-8").should == "ちfoofoo"
end
it "replaces undefined encoding in destination with default replacement" do
encoded = "B\ufffd".encode(Encoding::US_ASCII, undef: :replace)
encoded.should == "B?".encode(Encoding::US_ASCII)
encoded.encode("UTF-8").should == "B?"
end
it "replaces undefined encoding in destination with a specified replacement" do
encoded = "B\ufffd".encode(Encoding::US_ASCII, undef: :replace, replace: "foo")
encoded.should == "Bfoo".encode(Encoding::US_ASCII)
encoded.encode("UTF-8").should == "Bfoo"
end
it "replaces undefined encoding in destination with a specified replacement even if a fallback is given" do
encoded = "B\ufffd".encode(Encoding::US_ASCII, undef: :replace, replace: "foo", fallback: proc {|x| "bar"})
encoded.should == "Bfoo".encode(Encoding::US_ASCII)
encoded.encode("UTF-8").should == "Bfoo"
end
it "replaces undefined encoding in destination using a fallback proc" do
encoded = "B\ufffd".encode(Encoding::US_ASCII, fallback: proc {|x| "bar"})
encoded.should == "Bbar".encode(Encoding::US_ASCII)
encoded.encode("UTF-8").should == "Bbar"
end
it "replaces invalid encoding in source using replace even when fallback is given as proc" do
encoded = "\xE3\x81\xFF".encode("UTF-16LE", invalid: :replace, replace: "foo", fallback: proc {|x| "bar"})
encoded.should == "\u3061foofoo".encode("UTF-16LE")
encoded.encode("UTF-8").should == "ちfoofoo"
end
end