diff options
Diffstat (limited to 'spec/ruby/core/io/shared/write.rb')
| -rw-r--r-- | spec/ruby/core/io/shared/write.rb | 81 |
1 files changed, 63 insertions, 18 deletions
diff --git a/spec/ruby/core/io/shared/write.rb b/spec/ruby/core/io/shared/write.rb index 5e53d981c5..5de9fe4335 100644 --- a/spec/ruby/core/io/shared/write.rb +++ b/spec/ruby/core/io/shared/write.rb @@ -23,7 +23,7 @@ describe :io_write, shared: true do end it "checks if the file is writable if writing more than zero bytes" do - -> { @readonly_file.send(@method, "abcde") }.should raise_error(IOError) + -> { @readonly_file.send(@method, "abcde") }.should.raise(IOError) end it "returns the number of bytes written" do @@ -66,17 +66,7 @@ describe :io_write, shared: true do end it "raises IOError on closed stream" do - -> { IOSpecs.closed_io.send(@method, "hello") }.should raise_error(IOError) - end - - it "does not modify the passed argument" do - File.open(@filename, "w") do |f| - f.set_encoding(Encoding::IBM437) - # A character whose codepoint differs between UTF-8 and IBM437 - f.write "ƒ".freeze - end - - File.binread(@filename).bytes.should == [159] + -> { IOSpecs.closed_io.send(@method, "hello") }.should.raise(IOError) end describe "on a pipe" do @@ -95,15 +85,70 @@ describe :io_write, shared: true do @r.read.should == "foo" end - # [ruby-core:90895] MJIT worker may leave fd open in a forked child. - # For instance, MJIT creates a worker before @r.close with fork(), @r.close happens, - # and the MJIT worker keeps the pipe open until the worker execve(). - # TODO: consider acquiring GVL from MJIT worker. - guard_not -> { defined?(RubyVM::JIT) && RubyVM::JIT.enabled? } do + # [ruby-core:90895] RJIT worker may leave fd open in a forked child. + # For instance, RJIT creates a worker before @r.close with fork(), @r.close happens, + # and the RJIT worker keeps the pipe open until the worker execve(). + # TODO: consider acquiring GVL from RJIT worker. + guard_not -> { defined?(RubyVM::RJIT) && RubyVM::RJIT.enabled? } do it "raises Errno::EPIPE if the read end is closed and does not die from SIGPIPE" do @r.close - -> { @w.send(@method, "foo") }.should raise_error(Errno::EPIPE, /Broken pipe/) + -> { @w.send(@method, "foo") }.should.raise(Errno::EPIPE, /Broken pipe/) end end end end + +describe :io_write_transcode, shared: true do + before :each do + @transcode_filename = tmp("io_write_transcode") + end + + after :each do + rm_r @transcode_filename + end + + it "transcodes the given string when the external encoding is set and neither is BINARY" do + utf8_str = "hello" + + File.open(@transcode_filename, "w", external_encoding: Encoding::UTF_16BE) do |file| + file.external_encoding.should == Encoding::UTF_16BE + file.send(@method, utf8_str) + end + + result = File.binread(@transcode_filename) + expected = [0, 104, 0, 101, 0, 108, 0, 108, 0, 111] # UTF-16BE bytes for "hello" + + result.bytes.should == expected + end + + it "transcodes the given string when the external encoding is set and the string encoding is BINARY" do + str = "été".b + + File.open(@transcode_filename, "w", external_encoding: Encoding::UTF_16BE) do |file| + file.external_encoding.should == Encoding::UTF_16BE + -> { file.send(@method, str) }.should.raise(Encoding::UndefinedConversionError) + end + end +end + +describe :io_write_no_transcode, shared: true do + before :each do + @transcode_filename = tmp("io_write_no_transcode") + end + + after :each do + rm_r @transcode_filename + end + + it "does not transcode the given string even when the external encoding is set" do + utf8_str = "hello" + + File.open(@transcode_filename, "w", external_encoding: Encoding::UTF_16BE) do |file| + file.external_encoding.should == Encoding::UTF_16BE + file.send(@method, utf8_str) + end + + result = File.binread(@transcode_filename) + result.bytes.should == utf8_str.bytes + end +end |
