aboutsummaryrefslogtreecommitdiffstats
path: root/spec/rubyspec/shared/file/size.rb
diff options
context:
space:
mode:
authoreregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-05-07 12:04:49 +0000
committereregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-05-07 12:04:49 +0000
commita3736e97a6ca517c2cd7d3d93a8f2ef86e39e5b5 (patch)
tree9eef7f720314ebaff56845a74e203770e62284e4 /spec/rubyspec/shared/file/size.rb
parent52df1d0d3370919711c0577aaa42d1a864709885 (diff)
downloadruby-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/shared/file/size.rb')
-rw-r--r--spec/rubyspec/shared/file/size.rb124
1 files changed, 124 insertions, 0 deletions
diff --git a/spec/rubyspec/shared/file/size.rb b/spec/rubyspec/shared/file/size.rb
new file mode 100644
index 0000000000..bb95190fc0
--- /dev/null
+++ b/spec/rubyspec/shared/file/size.rb
@@ -0,0 +1,124 @@
+describe :file_size, shared: true do
+ before :each do
+ @exists = tmp('i_exist')
+ touch(@exists) { |f| f.write 'rubinius' }
+ end
+
+ after :each do
+ rm_r @exists
+ end
+
+ it "returns the size of the file if it exists and is not empty" do
+ @object.send(@method, @exists).should == 8
+ end
+
+ it "accepts a String-like (to_str) parameter" do
+ obj = mock("file")
+ obj.should_receive(:to_str).and_return(@exists)
+
+ @object.send(@method, obj).should == 8
+ end
+
+ it "accepts an object that has a #to_path method" do
+ @object.send(@method, mock_to_path(@exists)).should == 8
+ end
+end
+
+describe :file_size_to_io, shared: true do
+ before :each do
+ @exists = tmp('i_exist')
+ touch(@exists) { |f| f.write 'rubinius' }
+ @file = File.open(@exists, 'r')
+ end
+
+ after :each do
+ @file.close unless @file.closed?
+ rm_r @exists
+ end
+
+ it "calls #to_io to convert the argument to an IO" do
+ obj = mock("io like")
+ obj.should_receive(:to_io).and_return(@file)
+
+ @object.send(@method, obj).should == 8
+ end
+end
+
+describe :file_size_raise_when_missing, shared: true do
+ before :each do
+ # TODO: missing_file
+ @missing = tmp("i_dont_exist")
+ rm_r @missing
+ end
+
+ after :each do
+ rm_r @missing
+ end
+
+ it "raises an error if file_name doesn't exist" do
+ lambda {@object.send(@method, @missing)}.should raise_error(Errno::ENOENT)
+ end
+end
+
+describe :file_size_nil_when_missing, shared: true do
+ before :each do
+ # TODO: missing_file
+ @missing = tmp("i_dont_exist")
+ rm_r @missing
+ end
+
+ after :each do
+ rm_r @missing
+ end
+
+ it "returns nil if file_name doesn't exist or has 0 size" do
+ @object.send(@method, @missing).should == nil
+ end
+end
+
+describe :file_size_0_when_empty, shared: true do
+ before :each do
+ @empty = tmp("i_am_empty")
+ touch @empty
+ end
+
+ after :each do
+ rm_r @empty
+ end
+
+ it "returns 0 if the file is empty" do
+ @object.send(@method, @empty).should == 0
+ end
+end
+
+describe :file_size_nil_when_empty, shared: true do
+ before :each do
+ @empty = tmp("i_am_empt")
+ touch @empty
+ end
+
+ after :each do
+ rm_r @empty
+ end
+
+ it "returns nil if file_name is empty" do
+ @object.send(@method, @empty).should == nil
+ end
+end
+
+describe :file_size_with_file_argument, shared: true do
+ before :each do
+ @exists = tmp('i_exist')
+ touch(@exists) { |f| f.write 'rubinius' }
+ end
+
+ after :each do
+ rm_r @exists
+ end
+
+ it "accepts a File argument" do
+ File.open(@exists) do |f|
+ @object.send(@method, f).should == 8
+ end
+ end
+end