1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
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
@name.should have_data("writable")
end
it "truncates an existing file with 'w'" do
touch(@name) { |f| f.puts "existing" }
@file = @s.send(@method, @name, "w")
@name.should have_data("")
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
ruby_version_is "1.9" do
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)
@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)
@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
end
|