diff options
Diffstat (limited to 'spec/ruby/library/stringio/initialize_spec.rb')
-rw-r--r-- | spec/ruby/library/stringio/initialize_spec.rb | 85 |
1 files changed, 85 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 |