Skip to content

Commit

Permalink
Merge pull request #1165 from seven1m/string-encode-fallback-return
Browse files Browse the repository at this point in the history
Test different return values of String#encode :fallback
  • Loading branch information
andrykonchin authored Jun 24, 2024
2 parents 2cde58f + 513e825 commit 474d3b8
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions core/string/shared/encode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,21 @@
encoded.should == "Bbar"
end

it "calls to_str on the returned value" do
obj = Object.new
obj.should_receive(:to_str).and_return("bar")
encoded = "B\ufffd".encode(Encoding::US_ASCII, fallback: { "\ufffd" => obj })
encoded.should == "Bbar"
end

it "does not call to_s on the returned value" do
obj = Object.new
obj.should_not_receive(:to_s)
-> {
"B\ufffd".encode(Encoding::US_ASCII, fallback: { "\ufffd" => obj })
}.should raise_error(TypeError, "no implicit conversion of Object into String")
end

it "raises an error if the key is not present in the hash" do
-> {
"B\ufffd".encode(Encoding::US_ASCII, fallback: { "foo" => "bar" })
Expand Down Expand Up @@ -273,6 +288,21 @@ def [](c) = c.bytes.inspect
encoded.should == "B[239, 191, 189]"
end

it "calls to_str on the returned value" do
obj = Object.new
obj.should_receive(:to_str).and_return("bar")
encoded = "B\ufffd".encode(Encoding::US_ASCII, fallback: proc { |c| obj })
encoded.should == "Bbar"
end

it "does not call to_s on the returned value" do
obj = Object.new
obj.should_not_receive(:to_s)
-> {
"B\ufffd".encode(Encoding::US_ASCII, fallback: proc { |c| obj })
}.should raise_error(TypeError, "no implicit conversion of Object into String")
end

it "raises an error if the returned value is itself invalid" do
-> {
"B\ufffd".encode(Encoding::US_ASCII, fallback: -> c { "\uffee" })
Expand All @@ -286,6 +316,21 @@ def [](c) = c.bytes.inspect
encoded.should == "B[239, 191, 189]"
end

it "calls to_str on the returned value" do
obj = Object.new
obj.should_receive(:to_str).and_return("bar")
encoded = "B\ufffd".encode(Encoding::US_ASCII, fallback: -> c { obj })
encoded.should == "Bbar"
end

it "does not call to_s on the returned value" do
obj = Object.new
obj.should_not_receive(:to_s)
-> {
"B\ufffd".encode(Encoding::US_ASCII, fallback: -> c { obj })
}.should raise_error(TypeError, "no implicit conversion of Object into String")
end

it "raises an error if the returned value is itself invalid" do
-> {
"B\ufffd".encode(Encoding::US_ASCII, fallback: -> c { "\uffee" })
Expand All @@ -297,11 +342,34 @@ def [](c) = c.bytes.inspect
def replace(c) = c.bytes.inspect
def replace_bad(c) = "\uffee"

def replace_to_str(c)
obj = Object.new
obj.should_receive(:to_str).and_return("bar")
obj
end

def replace_to_s(c)
obj = Object.new
obj.should_not_receive(:to_s)
obj
end

it "calls the method to get the replacement value, passing in the invalid character" do
encoded = "B\ufffd".encode(Encoding::US_ASCII, fallback: method(:replace))
encoded.should == "B[239, 191, 189]"
end

it "calls to_str on the returned value" do
encoded = "B\ufffd".encode(Encoding::US_ASCII, fallback: method(:replace_to_str))
encoded.should == "Bbar"
end

it "does not call to_s on the returned value" do
-> {
"B\ufffd".encode(Encoding::US_ASCII, fallback: method(:replace_to_s))
}.should raise_error(TypeError, "no implicit conversion of Object into String")
end

it "raises an error if the returned value is itself invalid" do
-> {
"B\ufffd".encode(Encoding::US_ASCII, fallback: method(:replace_bad))
Expand Down

0 comments on commit 474d3b8

Please sign in to comment.