diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
commit | a3736e97a6ca517c2cd7d3d93a8f2ef86e39e5b5 (patch) | |
tree | 9eef7f720314ebaff56845a74e203770e62284e4 /spec/rubyspec/optional/capi/file_spec.rb | |
parent | 52df1d0d3370919711c0577aaa42d1a864709885 (diff) | |
download | ruby-a3736e97a6ca517c2cd7d3d93a8f2ef86e39e5b5.tar.gz |
Add in-tree mspec and ruby/spec
* For easier modifications of ruby/spec by MRI developers.
* .gitignore: track changes under spec.
* spec/mspec, spec/rubyspec: add in-tree mspec and ruby/spec.
These files can therefore be updated like any other file in MRI.
Instructions are provided in spec/README.
[Feature #13156] [ruby-core:79246]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58595 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/rubyspec/optional/capi/file_spec.rb')
-rw-r--r-- | spec/rubyspec/optional/capi/file_spec.rb | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/spec/rubyspec/optional/capi/file_spec.rb b/spec/rubyspec/optional/capi/file_spec.rb new file mode 100644 index 0000000000..2459dba979 --- /dev/null +++ b/spec/rubyspec/optional/capi/file_spec.rb @@ -0,0 +1,89 @@ +require File.expand_path('../spec_helper', __FILE__) + +load_extension('file') + +describe :rb_file_open, shared: true do + it "raises an ArgumentError if passed an empty mode string" do + touch @name + lambda { @s.rb_file_open(@name, "") }.should raise_error(ArgumentError) + end + + it "opens a file in read-only mode with 'r'" do + touch(@name) { |f| f.puts "readable" } + @file = @s.send(@method, @name, "r") + @file.should be_an_instance_of(File) + @file.read.chomp.should == "readable" + end + + it "creates and opens a non-existent file with 'w'" do + @file = @s.send(@method, @name, "w") + @file.write "writable" + @file.flush + File.read(@name).should == "writable" + end + + it "truncates an existing file with 'w'" do + touch(@name) { |f| f.puts "existing" } + @file = @s.send(@method, @name, "w") + File.read(@name).should == "" + end +end + +describe "C-API File function" do + before :each do + @s = CApiFileSpecs.new + @name = tmp("rb_file_open") + end + + after :each do + @file.close if @file and !@file.closed? + rm_r @name + end + + describe "rb_file_open" do + it_behaves_like :rb_file_open, :rb_file_open + end + + describe "rb_file_open_str" do + it_behaves_like :rb_file_open, :rb_file_open_str + end + + describe "rb_file_open_str" do + it "calls #to_path to convert on object to a path" do + path = mock("rb_file_open_str to_path") + path.should_receive(:to_path).and_return(@name) + @file = @s.rb_file_open_str(path, "w") + end + + it "calls #to_str to convert an object to a path if #to_path isn't defined" do + path = mock("rb_file_open_str to_str") + path.should_receive(:to_str).and_return(@name) + @file = @s.rb_file_open_str(path, "w") + end + end + + describe "FilePathValue" do + it "returns a String argument unchanged" do + obj = "path" + @s.FilePathValue(obj).should eql(obj) + end + + it "does not call #to_str on a String" do + obj = "path" + obj.should_not_receive(:to_str) + @s.FilePathValue(obj).should eql(obj) + end + + it "calls #to_path to convert an object to a String" do + obj = mock("FilePathValue to_path") + obj.should_receive(:to_path).and_return("path") + @s.FilePathValue(obj).should == "path" + end + + it "calls #to_str to convert an object to a String if #to_path isn't defined" do + obj = mock("FilePathValue to_str") + obj.should_receive(:to_str).and_return("path") + @s.FilePathValue(obj).should == "path" + end + end +end |