diff options
Diffstat (limited to 'spec/ruby/library/stringio')
-rw-r--r-- | spec/ruby/library/stringio/initialize_spec.rb | 85 | ||||
-rw-r--r-- | spec/ruby/library/stringio/new_spec.rb | 8 | ||||
-rw-r--r-- | spec/ruby/library/stringio/shared/write.rb | 22 |
3 files changed, 115 insertions, 0 deletions
diff --git a/spec/ruby/library/stringio/initialize_spec.rb b/spec/ruby/library/stringio/initialize_spec.rb index 1e8096e3bb..c597e328d3 100644 --- a/spec/ruby/library/stringio/initialize_spec.rb +++ b/spec/ruby/library/stringio/initialize_spec.rb @@ -163,6 +163,91 @@ describe "StringIO#initialize when passed [Object]" do end end +# NOTE: Synchronise with core/io/new_spec.rb (core/io/shared/new.rb) +describe "StringIO#initialize when passed keyword arguments" do + it "sets the mode based on the passed :mode option" do + io = StringIO.new("example", "r") + io.closed_read?.should be_false + io.closed_write?.should be_true + end + + it "accepts a mode argument set to nil with a valid :mode option" do + @io = StringIO.new('', nil, mode: "w") + @io.write("foo").should == 3 + end + + it "accepts a mode argument with a :mode option set to nil" do + @io = StringIO.new('', "w", mode: nil) + @io.write("foo").should == 3 + end + + it "sets binmode from :binmode option" do + @io = StringIO.new('', 'w', binmode: true) + @io.external_encoding.to_s.should == "ASCII-8BIT" # #binmode? isn't implemented in StringIO + end + + it "does not set binmode from false :binmode" do + @io = StringIO.new('', 'w', binmode: false) + @io.external_encoding.to_s.should == "UTF-8" # #binmode? isn't implemented in StringIO + end +end + +# NOTE: Synchronise with core/io/new_spec.rb (core/io/shared/new.rb) +describe "StringIO#initialize when passed keyword arguments and error happens" do + it "raises an error if passed encodings two ways" do + -> { + @io = StringIO.new('', 'w:ISO-8859-1', encoding: 'ISO-8859-1') + }.should raise_error(ArgumentError) + -> { + @io = StringIO.new('', 'w:ISO-8859-1', external_encoding: 'ISO-8859-1') + }.should raise_error(ArgumentError) + -> { + @io = StringIO.new('', 'w:ISO-8859-1:UTF-8', internal_encoding: 'ISO-8859-1') + }.should raise_error(ArgumentError) + end + + it "raises an error if passed matching binary/text mode two ways" do + -> { + @io = StringIO.new('', "wb", binmode: true) + }.should raise_error(ArgumentError) + -> { + @io = StringIO.new('', "wt", textmode: true) + }.should raise_error(ArgumentError) + + -> { + @io = StringIO.new('', "wb", textmode: false) + }.should raise_error(ArgumentError) + -> { + @io = StringIO.new('', "wt", binmode: false) + }.should raise_error(ArgumentError) + end + + it "raises an error if passed conflicting binary/text mode two ways" do + -> { + @io = StringIO.new('', "wb", binmode: false) + }.should raise_error(ArgumentError) + -> { + @io = StringIO.new('', "wt", textmode: false) + }.should raise_error(ArgumentError) + + -> { + @io = StringIO.new('', "wb", textmode: true) + }.should raise_error(ArgumentError) + -> { + @io = StringIO.new('', "wt", binmode: true) + }.should raise_error(ArgumentError) + end + + it "raises an error when trying to set both binmode and textmode" do + -> { + @io = StringIO.new('', "w", textmode: true, binmode: true) + }.should raise_error(ArgumentError) + -> { + @io = StringIO.new('', File::Constants::WRONLY, textmode: true, binmode: true) + }.should raise_error(ArgumentError) + end +end + describe "StringIO#initialize when passed no arguments" do before :each do @io = StringIO.allocate diff --git a/spec/ruby/library/stringio/new_spec.rb b/spec/ruby/library/stringio/new_spec.rb new file mode 100644 index 0000000000..e36d210caa --- /dev/null +++ b/spec/ruby/library/stringio/new_spec.rb @@ -0,0 +1,8 @@ +require_relative '../../spec_helper' +require 'stringio' + +describe "StringIO.new" do + it "warns when called with a block" do + -> { eval("StringIO.new {}") }.should complain(/StringIO::new\(\) does not take block; use StringIO::open\(\) instead/) + end +end
\ No newline at end of file diff --git a/spec/ruby/library/stringio/shared/write.rb b/spec/ruby/library/stringio/shared/write.rb index c5a0f8f513..d9c21028e0 100644 --- a/spec/ruby/library/stringio/shared/write.rb +++ b/spec/ruby/library/stringio/shared/write.rb @@ -66,6 +66,28 @@ describe :stringio_write_string, shared: true do @io.tainted?.should be_false end end + + it "handles writing non-ASCII UTF-8 after seek" do + @io.binmode + @io << "\x80" + @io.pos = 0 + @io << "\x81" + @io.string.should == "\x812345".b + end + + it "handles writing with position < buffer size" do + @io.pos = 2 + @io.write "abc" + @io.string.should == "12abc" + + @io.pos = 2 + @io.write "de" + @io.string.should == "12dec" + + @io.pos = 2 + @io.write "fghi" + @io.string.should == "12fghi" + end end describe :stringio_write_not_writable, shared: true do |