Skip to content

Commit 5c7340f

Browse files
committed
Add corresponding transcoding specs for IO too
1 parent 1274c02 commit 5c7340f

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,55 @@
9797
end
9898
end
9999
end
100+
101+
describe :io_write_transcode, shared: true do
102+
before :each do
103+
@transcode_filename = tmp("io_write_transcode")
104+
end
105+
106+
after :each do
107+
rm_r @transcode_filename
108+
end
109+
110+
describe "transcoding when UTF-16 encoding is set" do
111+
it "accepts a UTF-8-encoded string and transcodes it" do
112+
utf8_str = "hello"
113+
114+
File.open(@transcode_filename, "w", external_encoding: Encoding::UTF_16BE) do |file|
115+
file.external_encoding.should == Encoding::UTF_16BE
116+
file.send(@method, utf8_str)
117+
end
118+
119+
result = File.binread(@transcode_filename)
120+
expected = [0, 104, 0, 101, 0, 108, 0, 108, 0, 111] # double-width "hello"
121+
122+
result.bytes.should == expected
123+
end
124+
end
125+
end
126+
127+
describe :io_write_no_transcode, shared: true do
128+
before :each do
129+
@transcode_filename = tmp("io_write_no_transcode")
130+
end
131+
132+
after :each do
133+
rm_r @transcode_filename
134+
end
135+
136+
describe "transcoding when UTF-16 encoding is set" do
137+
it "accepts a UTF-8-encoded string and transcodes it" do
138+
utf8_str = "hello"
139+
140+
File.open(@transcode_filename, "w", external_encoding: Encoding::UTF_16BE) do |file|
141+
file.external_encoding.should == Encoding::UTF_16BE
142+
file.send(@method, utf8_str)
143+
end
144+
145+
result = File.binread(@transcode_filename)
146+
expected = [104, 101, 108, 108, 111] # not transcoded to UTF-16BE
147+
148+
result.bytes.should == expected
149+
end
150+
end
151+
end

spec/ruby/core/io/syswrite_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,5 @@
7878

7979
describe "IO#syswrite" do
8080
it_behaves_like :io_write, :syswrite
81+
it_behaves_like :io_write_no_transcode, :syswrite
8182
end

spec/ruby/core/io/write_nonblock_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050

5151
describe "IO#write_nonblock" do
5252
it_behaves_like :io_write, :write_nonblock
53+
it_behaves_like :io_write_no_transcode, :write_nonblock
5354
end
5455
end
5556

spec/ruby/core/io/write_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@
220220

221221
describe "IO#write" do
222222
it_behaves_like :io_write, :write
223+
it_behaves_like :io_write_transcode, :write
223224

224225
it "accepts multiple arguments" do
225226
IO.pipe do |r, w|

0 commit comments

Comments
 (0)