Permalink
Browse files

Add specs for writing StringIO with binary strings

  • Loading branch information...
1 parent c7b1d93 commit 288dea7f01374440d2bc5621efa07037848b789d @dbussink dbussink committed Mar 26, 2013
Showing with 57 additions and 0 deletions.
  1. +17 −0 spec/ruby/library/stringio/putc_spec.rb
  2. +40 −0 spec/ruby/library/stringio/shared/write.rb
@@ -1,3 +1,5 @@
+# -*- encoding: utf-8 -*-
+
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
@@ -35,6 +37,21 @@
@io.putc("t")
@io.pos.should == 3
end
+
+
+ with_feature :encoding do
+
+ before :each do
+ @enc_io = StringIO.new("ëllø")
+ end
+
+ it "writes a byte into the io" do
+ @enc_io.putc("t")
+ @enc_io.string.should == "t\xABllø"
+ end
+
+ end
+
end
describe "StringIO#putc when passed [Object]" do
@@ -1,3 +1,5 @@
+# -*- encoding: utf-8 -*-
+
describe :stringio_write, :shared => true do
before(:each) do
@io = StringIO.new('12345')
@@ -61,6 +63,44 @@
@io.send(@method, "test".taint)
@io.tainted?.should be_false
end
+
+
+ with_feature :encoding do
+
+ before :each do
+ @enc_io = StringIO.new("Hëllø")
+ end
+
+ it "writes binary data into the io" do
+ data = "Hëll\xFF"
+ data.force_encoding("ASCII-8BIT")
+ @enc_io.send(@method, data)
+ @enc_io.string.should == "Hëll\xFF\xB8"
+ end
+
+ it "retains the original encoding" do
+ data = "Hëll\xFF"
+ data.force_encoding("ASCII-8BIT")
+ @enc_io.send(@method, data)
+ @enc_io.string.encoding.should == Encoding::UTF_8
+ end
+
+ it "returns the number of bytes written" do
+ data = "Hëll\xFF"
+ data.force_encoding("ASCII-8BIT")
+ @enc_io.send(@method, data).should == 6
+ end
+
+ it "pads multibyte characters properly" do
+ @enc_io.pos = 8
+ @enc_io.send(@method, 'x')
+ @enc_io.string.should == "Hëllø\000x"
+ @enc_io.send(@method, 9)
+ @enc_io.string.should == "Hëllø\000x9"
+ end
+
+ end
+
end
describe :stringio_write_not_writable, :shared => true do

0 comments on commit 288dea7

Please sign in to comment.