diff options
Diffstat (limited to 'spec/ruby/core/file/new_spec.rb')
| -rw-r--r-- | spec/ruby/core/file/new_spec.rb | 74 |
1 files changed, 39 insertions, 35 deletions
diff --git a/spec/ruby/core/file/new_spec.rb b/spec/ruby/core/file/new_spec.rb index a1ca46979e..4cd2cb5dcb 100644 --- a/spec/ruby/core/file/new_spec.rb +++ b/spec/ruby/core/file/new_spec.rb @@ -16,13 +16,13 @@ describe "File.new" do it "returns a new File with mode string" do @fh = File.new(@file, 'w') - @fh.should be_kind_of(File) + @fh.should.is_a?(File) File.should.exist?(@file) end it "returns a new File with mode num" do @fh = File.new(@file, @flags) - @fh.should be_kind_of(File) + @fh.should.is_a?(File) File.should.exist?(@file) end @@ -30,7 +30,7 @@ describe "File.new" do rm_r @file File.umask(0011) @fh = File.new(@file, @flags, 0755) - @fh.should be_kind_of(File) + @fh.should.is_a?(File) platform_is_not :windows do File.stat(@file).mode.to_s(8).should == "100744" end @@ -44,7 +44,7 @@ describe "File.new" do rm_r @file begin f = File.new(@file, "w", 0444) - -> { f.puts("test") }.should_not raise_error(IOError) + -> { f.puts("test") }.should_not.raise(IOError) ensure f.close end @@ -74,14 +74,14 @@ describe "File.new" do @fh = File.new(@file) fh_copy = File.new(@fh.fileno) fh_copy.autoclose = false - fh_copy.should be_kind_of(File) + fh_copy.should.is_a?(File) File.should.exist?(@file) end it "returns a new read-only File when mode is not specified" do @fh = File.new(@file) - -> { @fh.puts("test") }.should raise_error(IOError) + -> { @fh.puts("test") }.should.raise(IOError) @fh.read.should == "" File.should.exist?(@file) end @@ -89,68 +89,68 @@ describe "File.new" do it "returns a new read-only File when mode is not specified but flags option is present" do @fh = File.new(@file, flags: File::CREAT) - -> { @fh.puts("test") }.should raise_error(IOError) + -> { @fh.puts("test") }.should.raise(IOError) @fh.read.should == "" File.should.exist?(@file) end it "creates a new file when use File::EXCL mode" do @fh = File.new(@file, File::EXCL) - @fh.should be_kind_of(File) + @fh.should.is_a?(File) File.should.exist?(@file) end - it "raises an Errorno::EEXIST if the file exists when create a new file with File::CREAT|File::EXCL" do - -> { @fh = File.new(@file, File::CREAT|File::EXCL) }.should raise_error(Errno::EEXIST) + it "raises an Errno::EEXIST if the file exists when create a new file with File::CREAT|File::EXCL" do + -> { @fh = File.new(@file, File::CREAT|File::EXCL) }.should.raise(Errno::EEXIST) end it "creates a new file when use File::WRONLY|File::APPEND mode" do @fh = File.new(@file, File::WRONLY|File::APPEND) - @fh.should be_kind_of(File) + @fh.should.is_a?(File) File.should.exist?(@file) end it "returns a new File when use File::APPEND mode" do @fh = File.new(@file, File::APPEND) - @fh.should be_kind_of(File) + @fh.should.is_a?(File) File.should.exist?(@file) end it "returns a new File when use File::RDONLY|File::APPEND mode" do @fh = File.new(@file, File::RDONLY|File::APPEND) - @fh.should be_kind_of(File) + @fh.should.is_a?(File) File.should.exist?(@file) end it "returns a new File when use File::RDONLY|File::WRONLY mode" do @fh = File.new(@file, File::RDONLY|File::WRONLY) - @fh.should be_kind_of(File) + @fh.should.is_a?(File) File.should.exist?(@file) end it "creates a new file when use File::WRONLY|File::TRUNC mode" do @fh = File.new(@file, File::WRONLY|File::TRUNC) - @fh.should be_kind_of(File) + @fh.should.is_a?(File) File.should.exist?(@file) end it "returns a new read-only File when use File::RDONLY|File::CREAT mode" do @fh = File.new(@file, File::RDONLY|File::CREAT) - @fh.should be_kind_of(File) + @fh.should.is_a?(File) File.should.exist?(@file) # it's read-only - -> { @fh.puts("test") }.should raise_error(IOError) + -> { @fh.puts("test") }.should.raise(IOError) @fh.read.should == "" end it "returns a new read-only File when use File::CREAT mode" do @fh = File.new(@file, File::CREAT) - @fh.should be_kind_of(File) + @fh.should.is_a?(File) File.should.exist?(@file) # it's read-only - -> { @fh.puts("test") }.should raise_error(IOError) + -> { @fh.puts("test") }.should.raise(IOError) @fh.read.should == "" end @@ -168,39 +168,43 @@ describe "File.new" do File.should.exist?(@file) end - ruby_version_is "3.0" do - it "accepts options as a keyword argument" do - @fh = File.new(@file, 'w', 0755, flags: @flags) - @fh.should be_kind_of(File) - @fh.close + it "accepts options as a keyword argument" do + @fh = File.new(@file, 'w', 0755, flags: @flags) + @fh.should.is_a?(File) + @fh.close - -> { - @fh = File.new(@file, 'w', 0755, {flags: @flags}) - }.should raise_error(ArgumentError, "wrong number of arguments (given 4, expected 1..3)") - end + -> { + @fh = File.new(@file, 'w', 0755, {flags: @flags}) + }.should.raise(ArgumentError, "wrong number of arguments (given 4, expected 1..3)") end it "bitwise-ORs mode and flags option" do -> { @fh = File.new(@file, 'w', flags: File::EXCL) - }.should raise_error(Errno::EEXIST, /File exists/) + }.should.raise(Errno::EEXIST, /File exists/) -> { @fh = File.new(@file, mode: 'w', flags: File::EXCL) - }.should raise_error(Errno::EEXIST, /File exists/) + }.should.raise(Errno::EEXIST, /File exists/) + end + + it "does not use the given block and warns to use File::open" do + -> { + @fh = File.new(@file) { raise } + }.should complain(/warning: File::new\(\) does not take block; use File::open\(\) instead/) end it "raises a TypeError if the first parameter can't be coerced to a string" do - -> { File.new(true) }.should raise_error(TypeError) - -> { File.new(false) }.should raise_error(TypeError) + -> { File.new(true) }.should.raise(TypeError) + -> { File.new(false) }.should.raise(TypeError) end it "raises a TypeError if the first parameter is nil" do - -> { File.new(nil) }.should raise_error(TypeError) + -> { File.new(nil) }.should.raise(TypeError) end it "raises an Errno::EBADF if the first parameter is an invalid file descriptor" do - -> { File.new(-1) }.should raise_error(Errno::EBADF) + -> { File.new(-1) }.should.raise(Errno::EBADF) end platform_is_not :windows do @@ -209,7 +213,7 @@ describe "File.new" do -> { f = File.new(@fh.fileno, @flags) f.autoclose = false - }.should raise_error(Errno::EINVAL) + }.should.raise(Errno::EINVAL) end end |
