Skip to content

Commit 84b0fdf

Browse files
Lillian Zhangchrisseaton
authored andcommitted
fix and add spec for a string#encode case
1 parent 8033925 commit 84b0fdf

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

spec/ruby/core/string/encode_spec.rb

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@
1919
it "returns a copy when Encoding.default_internal is nil" do
2020
Encoding.default_internal = nil
2121
str = "あ"
22-
str.encode.should_not equal(str)
22+
encoded = str.encode
23+
encoded.should_not equal(str)
24+
encoded.should eql(str)
2325
end
2426

2527
it "returns a copy for a ASCII-only String when Encoding.default_internal is nil" do
2628
Encoding.default_internal = nil
2729
str = "abc"
28-
str.encode.should_not equal(str)
30+
encoded = str.encode
31+
encoded.should_not equal(str)
32+
encoded.should eql(str)
2933
end
3034

3135
it "encodes an ascii substring of a binary string to UTF-8" do
@@ -39,7 +43,9 @@
3943
describe "when passed to encoding" do
4044
it "returns a copy when passed the same encoding as the String" do
4145
str = "あ"
42-
str.encode(Encoding::UTF_8).should_not equal(str)
46+
encoded = str.encode(Encoding::UTF_8)
47+
encoded.should_not equal(str)
48+
encoded.should eql(str)
4349
end
4450

4551
it "round trips a String" do
@@ -75,6 +81,7 @@
7581
encoded = str.encode("utf-8", "utf-8")
7682

7783
encoded.should_not equal(str)
84+
encoded.should eql(str.force_encoding("utf-8"))
7885
encoded.encoding.should == Encoding::UTF_8
7986
end
8087

@@ -87,14 +94,28 @@
8794
describe "when passed to, options" do
8895
it "returns a copy when the destination encoding is the same as the String encoding" do
8996
str = "あ"
90-
str.encode(Encoding::UTF_8, undef: :replace).should_not equal(str)
97+
encoded = str.encode(Encoding::UTF_8, undef: :replace)
98+
encoded.should_not equal(str)
99+
encoded.should eql(str)
91100
end
92101
end
93102

94103
describe "when passed to, from, options" do
95104
it "returns a copy when both encodings are the same" do
96105
str = "あ"
97-
str.encode("utf-8", "utf-8", invalid: :replace).should_not equal(str)
106+
encoded = str.encode("utf-8", "utf-8", invalid: :replace)
107+
encoded.should_not equal(str)
108+
encoded.should eql(str)
109+
end
110+
111+
it "returns a copy in the destination encoding when both encodings are the same" do
112+
str = "あ"
113+
str.force_encoding("binary")
114+
encoded = str.encode("utf-8", "utf-8", invalid: :replace)
115+
116+
encoded.should_not equal(str)
117+
encoded.should eql(str.force_encoding("utf-8"))
118+
encoded.encoding.should == Encoding::UTF_8
98119
end
99120
end
100121
end

src/main/ruby/truffleruby/core/string.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ def encode!(to=undefined, from=undefined, options=undefined)
457457
status = ec.primitive_convert self.dup, dest, nil, nil, ec.options
458458
raise ec.last_error unless status == :finished
459459
return replace(dest)
460-
elsif options == 0
460+
else
461461
force_encoding to_enc
462462
end
463463
end

0 commit comments

Comments
 (0)