Skip to content

Commit 90a9d8f

Browse files
committed
Improve spec descriptions for write transcoding
1 parent 938aecb commit 90a9d8f

File tree

2 files changed

+29
-39
lines changed

2 files changed

+29
-39
lines changed

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

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,18 @@
107107
rm_r @transcode_filename
108108
end
109109

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"
110+
it "transcodes the given string when the external encoding is set and neither is BINARY" do
111+
utf8_str = "hello"
113112

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
113+
File.open(@transcode_filename, "w", external_encoding: Encoding::UTF_16BE) do |file|
114+
file.external_encoding.should == Encoding::UTF_16BE
115+
file.send(@method, utf8_str)
116+
end
118117

119-
result = File.binread(@transcode_filename)
120-
expected = [0, 104, 0, 101, 0, 108, 0, 108, 0, 111] # double-width "hello"
118+
result = File.binread(@transcode_filename)
119+
expected = [0, 104, 0, 101, 0, 108, 0, 108, 0, 111] # UTF-16BE bytes for "hello"
121120

122-
result.bytes.should == expected
123-
end
121+
result.bytes.should == expected
124122
end
125123
end
126124

@@ -133,19 +131,17 @@
133131
rm_r @transcode_filename
134132
end
135133

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"
134+
it "does not transcode the given string even when the external encoding is set" do
135+
utf8_str = "hello"
139136

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
137+
File.open(@transcode_filename, "w", external_encoding: Encoding::UTF_16BE) do |file|
138+
file.external_encoding.should == Encoding::UTF_16BE
139+
file.send(@method, utf8_str)
140+
end
144141

145-
result = File.binread(@transcode_filename)
146-
expected = [104, 101, 108, 108, 111] # not transcoded to UTF-16BE
142+
result = File.binread(@transcode_filename)
143+
expected = [104, 101, 108, 108, 111] # UTF-8 bytes for "hello", not transcoded to UTF-16BE
147144

148-
result.bytes.should == expected
149-
end
145+
result.bytes.should == expected
150146
end
151147
end

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

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,17 @@
8989
@io.string.should == "12fghi"
9090
end
9191

92-
describe "transcoding" do
93-
describe "when UTF-16 encoding is set" do
94-
it "accepts a UTF-8-encoded string and transcodes it" do
95-
io = StringIO.new.set_encoding(Encoding::UTF_16BE)
96-
utf8_str = "hello"
97-
98-
io.send(@method, utf8_str)
99-
100-
result = io.string
101-
expected = [
102-
0, 104, 0, 101, 0, 108, 0, 108, 0, 111, # double-width "hello"
103-
]
104-
105-
io.external_encoding.should == Encoding::UTF_16BE
106-
result.bytes.should == expected
107-
end
108-
end
92+
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)
94+
utf8_str = "hello"
95+
96+
io.send(@method, utf8_str)
97+
98+
result = io.string
99+
expected = [0, 104, 0, 101, 0, 108, 0, 108, 0, 111] # UTF-16BE bytes for "hello"
100+
101+
io.external_encoding.should == Encoding::UTF_16BE
102+
result.bytes.should == expected
109103
end
110104
end
111105

0 commit comments

Comments
 (0)