diff options
author | Benoit Daloze <eregontp@gmail.com> | 2023-02-27 21:02:44 +0100 |
---|---|---|
committer | Benoit Daloze <eregontp@gmail.com> | 2023-02-27 21:02:44 +0100 |
commit | 18b4def471bb901d0baa4a1307185484cb05815f (patch) | |
tree | 779b24566144e9ee06c6f8de35bd2f7fd74ccdb4 /spec/ruby/core/io | |
parent | de60139053fa7c561858c5c5556d61c82f361dd9 (diff) | |
download | ruby-18b4def471bb901d0baa4a1307185484cb05815f.tar.gz |
Update to ruby/spec@e7dc804
Diffstat (limited to 'spec/ruby/core/io')
-rw-r--r-- | spec/ruby/core/io/initialize_spec.rb | 13 | ||||
-rw-r--r-- | spec/ruby/core/io/read_spec.rb | 36 | ||||
-rw-r--r-- | spec/ruby/core/io/readlines_spec.rb | 6 | ||||
-rw-r--r-- | spec/ruby/core/io/shared/binwrite.rb | 15 | ||||
-rw-r--r-- | spec/ruby/core/io/shared/new.rb | 11 | ||||
-rw-r--r-- | spec/ruby/core/io/write_spec.rb | 91 |
6 files changed, 166 insertions, 6 deletions
diff --git a/spec/ruby/core/io/initialize_spec.rb b/spec/ruby/core/io/initialize_spec.rb index ba5bc117b7..28fd7af7ab 100644 --- a/spec/ruby/core/io/initialize_spec.rb +++ b/spec/ruby/core/io/initialize_spec.rb @@ -27,6 +27,19 @@ describe "IO#initialize" do @io.fileno.should == fd end + ruby_version_is "3.0" do + it "accepts options as keyword arguments" do + fd = new_fd @name, "w:utf-8" + + @io.send(:initialize, fd, "w", flags: File::CREAT) + @io.fileno.should == fd + + -> { + @io.send(:initialize, fd, "w", {flags: File::CREAT}) + }.should raise_error(ArgumentError, "wrong number of arguments (given 3, expected 1..2)") + end + end + it "raises a TypeError when passed an IO" do -> { @io.send :initialize, STDOUT, 'w' }.should raise_error(TypeError) end diff --git a/spec/ruby/core/io/read_spec.rb b/spec/ruby/core/io/read_spec.rb index 529afbf0ff..e74012d027 100644 --- a/spec/ruby/core/io/read_spec.rb +++ b/spec/ruby/core/io/read_spec.rb @@ -23,6 +23,17 @@ describe "IO.read" do IO.read(p) end + ruby_version_is "3.0" do + # https://bugs.ruby-lang.org/issues/19354 + it "accepts options as keyword arguments" do + IO.read(@fname, 3, 0, mode: "r+").should == @contents[0, 3] + + -> { + IO.read(@fname, 3, 0, {mode: "r+"}) + }.should raise_error(ArgumentError, /wrong number of arguments/) + end + end + it "accepts an empty options Hash" do IO.read(@fname, **{}).should == @contents end @@ -55,6 +66,31 @@ describe "IO.read" do IO.read(@fname, mode: "a+").should == @contents end + platform_is_not :windows do + it "uses an :open_args option" do + string = IO.read(@fname, nil, 0, open_args: ["r", nil, {encoding: Encoding::US_ASCII}]) + string.encoding.should == Encoding::US_ASCII + + string = IO.read(@fname, nil, 0, open_args: ["r", nil, {}]) + string.encoding.should == Encoding::UTF_8 + end + end + + it "disregards other options if :open_args is given" do + string = IO.read(@fname,mode: "w", encoding: Encoding::UTF_32LE, open_args: ["r", encoding: Encoding::UTF_8]) + string.encoding.should == Encoding::UTF_8 + end + + it "doesn't require mode to be specified in :open_args" do + string = IO.read(@fname, nil, 0, open_args: [{encoding: Encoding::US_ASCII}]) + string.encoding.should == Encoding::US_ASCII + end + + it "doesn't require mode to be specified in :open_args even if flags option passed" do + string = IO.read(@fname, nil, 0, open_args: [{encoding: Encoding::US_ASCII, flags: File::CREAT}]) + string.encoding.should == Encoding::US_ASCII + end + it "treats second nil argument as no length limit" do IO.read(@fname, nil).should == @contents IO.read(@fname, nil, 5).should == IO.read(@fname, @contents.length, 5) diff --git a/spec/ruby/core/io/readlines_spec.rb b/spec/ruby/core/io/readlines_spec.rb index 496003002d..43d0750a72 100644 --- a/spec/ruby/core/io/readlines_spec.rb +++ b/spec/ruby/core/io/readlines_spec.rb @@ -127,6 +127,12 @@ describe "IO#readlines" do end end end + + describe "when passed arbitrary keyword argument" do + it "tolerates it" do + @io.readlines(chomp: true, foo: :bar).should == IOSpecs.lines_without_newline_characters + end + end end describe "IO#readlines" do diff --git a/spec/ruby/core/io/shared/binwrite.rb b/spec/ruby/core/io/shared/binwrite.rb index 3649bb47ff..950a6f51ab 100644 --- a/spec/ruby/core/io/shared/binwrite.rb +++ b/spec/ruby/core/io/shared/binwrite.rb @@ -21,6 +21,16 @@ describe :io_binwrite, shared: true do IO.send(@method, @filename, "abcde").should == 5 end + ruby_version_is "3.0" do + it "accepts options as a keyword argument" do + IO.send(@method, @filename, "hi", 0, flags: File::CREAT).should == 2 + + -> { + IO.send(@method, @filename, "hi", 0, {flags: File::CREAT}) + }.should raise_error(ArgumentError, "wrong number of arguments (given 4, expected 2..3)") + end + end + it "creates a file if missing" do fn = @filename + "xxx" begin @@ -67,6 +77,11 @@ describe :io_binwrite, shared: true do File.read(@filename).should == "\0\0foo" end + it "accepts a :flags option without :mode one" do + IO.send(@method, @filename, "hello, world!", flags: File::CREAT) + File.read(@filename).should == "hello, world!" + end + it "raises an error if readonly mode is specified" do -> { IO.send(@method, @filename, "abcde", mode: "r") }.should raise_error(IOError) end diff --git a/spec/ruby/core/io/shared/new.rb b/spec/ruby/core/io/shared/new.rb index 7677aada6e..da4c0af7a9 100644 --- a/spec/ruby/core/io/shared/new.rb +++ b/spec/ruby/core/io/shared/new.rb @@ -64,6 +64,17 @@ describe :io_new, shared: true do @io.should be_an_instance_of(IO) end + ruby_version_is "3.0" do + it "accepts options as keyword arguments" do + @io = IO.send(@method, @fd, "w", flags: File::CREAT) + @io.write("foo").should == 3 + + -> { + IO.send(@method, @fd, "w", {flags: File::CREAT}) + }.should raise_error(ArgumentError, "wrong number of arguments (given 3, expected 1..2)") + end + end + it "accepts a :mode option" do @io = IO.send(@method, @fd, mode: "w") @io.write("foo").should == 3 diff --git a/spec/ruby/core/io/write_spec.rb b/spec/ruby/core/io/write_spec.rb index bcc0582d9e..6e89c7cd9e 100644 --- a/spec/ruby/core/io/write_spec.rb +++ b/spec/ruby/core/io/write_spec.rb @@ -24,10 +24,6 @@ describe "IO#write on a file" do -> { @readonly_file.write("") }.should_not raise_error end - it "returns a length of 0 when writing a blank string" do - @file.write('').should == 0 - end - before :each do @external = Encoding.default_external @internal = Encoding.default_internal @@ -40,6 +36,18 @@ describe "IO#write on a file" do Encoding.default_internal = @internal end + it "returns a length of 0 when writing a blank string" do + @file.write('').should == 0 + end + + it "returns a length of 0 when writing blank strings" do + @file.write('', '', '').should == 0 + end + + it "returns a length of 0 when passed no arguments" do + @file.write().should == 0 + end + it "returns the number of bytes written" do @file.write("hellø").should == 6 end @@ -54,6 +62,18 @@ describe "IO#write on a file" do File.binread(@filename).bytes.should == [159] end + it "does not modify arguments when passed multiple arguments and external encoding not set" do + a, b = "a".freeze, "b".freeze + + File.open(@filename, "w") do |f| + f.write(a, b) + end + + File.binread(@filename).bytes.should == [97, 98] + a.encoding.should == Encoding::UTF_8 + b.encoding.should == Encoding::UTF_8 + end + it "uses the encoding from the given option for non-ascii encoding" do File.open(@filename, "w", encoding: Encoding::UTF_32LE) do |file| file.write("hi").should == 8 @@ -61,8 +81,25 @@ describe "IO#write on a file" do File.binread(@filename).should == "h\u0000\u0000\u0000i\u0000\u0000\u0000" end - it "uses an :open_args option" do - IO.write(@filename, 'hi', open_args: ["w", nil, {encoding: Encoding::UTF_32LE}]).should == 8 + it "uses the encoding from the given option for non-ascii encoding even if in binary mode" do + File.open(@filename, "w", encoding: Encoding::UTF_32LE, binmode: true) do |file| + file.should.binmode? + file.write("hi").should == 8 + end + File.binread(@filename).should == "h\u0000\u0000\u0000i\u0000\u0000\u0000" + + File.open(@filename, "wb", encoding: Encoding::UTF_32LE) do |file| + file.should.binmode? + file.write("hi").should == 8 + end + File.binread(@filename).should == "h\u0000\u0000\u0000i\u0000\u0000\u0000" + end + + it "uses the encoding from the given option for non-ascii encoding when multiple arguments passes" do + File.open(@filename, "w", encoding: Encoding::UTF_32LE) do |file| + file.write("h", "i").should == 8 + end + File.binread(@filename).should == "h\u0000\u0000\u0000i\u0000\u0000\u0000" end it "raises a invalid byte sequence error if invalid bytes are being written" do @@ -82,6 +119,20 @@ describe "IO#write on a file" do res = "H#{ë}ll#{ö}" File.binread(@filename).should == res.force_encoding(Encoding::BINARY) end + + platform_is_not :windows do + it "writes binary data if no encoding is given and multiple arguments passed" do + File.open(@filename, "w") do |file| + file.write("\x87".b, "ą") # 0x87 isn't a valid UTF-8 binary representation of a character + end + File.binread(@filename).bytes.should == [0x87, 0xC4, 0x85] + + File.open(@filename, "w") do |file| + file.write("\x61".encode("utf-32le"), "ą") + end + File.binread(@filename).bytes.should == [0x61, 0x00, 0x00, 0x00, 0xC4, 0x85] + end + end end describe "IO.write" do @@ -96,10 +147,38 @@ describe "IO.write" do File.read(@filename).should == "\0\0hi" end + it "requires mode to be specified in :open_args" do + -> { + IO.write(@filename, 'hi', open_args: [{encoding: Encoding::UTF_32LE, binmode: true}]) + }.should raise_error(IOError, "not opened for writing") + + IO.write(@filename, 'hi', open_args: ["w", {encoding: Encoding::UTF_32LE, binmode: true}]).should == 8 + IO.write(@filename, 'hi', open_args: [{encoding: Encoding::UTF_32LE, binmode: true, mode: "w"}]).should == 8 + end + + it "requires mode to be specified in :open_args even if flags option passed" do + -> { + IO.write(@filename, 'hi', open_args: [{encoding: Encoding::UTF_32LE, binmode: true, flags: File::CREAT}]) + }.should raise_error(IOError, "not opened for writing") + + IO.write(@filename, 'hi', open_args: ["w", {encoding: Encoding::UTF_32LE, binmode: true, flags: File::CREAT}]).should == 8 + IO.write(@filename, 'hi', open_args: [{encoding: Encoding::UTF_32LE, binmode: true, flags: File::CREAT, mode: "w"}]).should == 8 + end + it "uses the given encoding and returns the number of bytes written" do IO.write(@filename, 'hi', mode: "w", encoding: Encoding::UTF_32LE).should == 8 end + it "raises ArgumentError if encoding is specified in mode parameter and is given as :encoding option" do + -> { + IO.write(@filename, 'hi', mode: "w:UTF-16LE:UTF-16BE", encoding: Encoding::UTF_32LE) + }.should raise_error(ArgumentError, "encoding specified twice") + + -> { + IO.write(@filename, 'hi', mode: "w:UTF-16BE", encoding: Encoding::UTF_32LE) + }.should raise_error(ArgumentError, "encoding specified twice") + end + it "writes the file with the permissions in the :perm parameter" do rm_r @filename IO.write(@filename, 'write :perm spec', mode: "w", perm: 0o755).should == 16 |