Skip to content

Commit 7020bf4

Browse files
committed
Add more specs for write transcoding
1 parent 90a9d8f commit 7020bf4

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

spec/ruby/core/io/shared/write.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@
120120

121121
result.bytes.should == expected
122122
end
123+
124+
it "transcodes the given string when the external encoding is set and the string encoding is BINARY" do
125+
str = "été".b
126+
127+
File.open(@transcode_filename, "w", external_encoding: Encoding::UTF_16BE) do |file|
128+
file.external_encoding.should == Encoding::UTF_16BE
129+
-> { file.send(@method, str) }.should raise_error(Encoding::UndefinedConversionError)
130+
end
131+
end
123132
end
124133

125134
describe :io_write_no_transcode, shared: true do

spec/ruby/library/stringio/shared/write.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,24 @@
9090
end
9191

9292
it "transcodes the given string when the external encoding is set and neither is BINARY" do
93-
io = StringIO.new.set_encoding(Encoding::UTF_16BE)
9493
utf8_str = "hello"
94+
io = StringIO.new.set_encoding(Encoding::UTF_16BE)
95+
io.external_encoding.should == Encoding::UTF_16BE
9596

9697
io.send(@method, utf8_str)
9798

98-
result = io.string
9999
expected = [0, 104, 0, 101, 0, 108, 0, 108, 0, 111] # UTF-16BE bytes for "hello"
100+
io.string.bytes.should == expected
101+
end
100102

103+
it "does not transcode the given string when the external encoding is set and the string encoding is BINARY" do
104+
str = "été".b
105+
io = StringIO.new.set_encoding(Encoding::UTF_16BE)
101106
io.external_encoding.should == Encoding::UTF_16BE
102-
result.bytes.should == expected
107+
108+
io.send(@method, str)
109+
110+
io.string.bytes.should == str.bytes
103111
end
104112
end
105113

0 commit comments

Comments
 (0)