summaryrefslogtreecommitdiff
path: root/spec/ruby/shared/file/size.rb
blob: bb95190fc04126b873897c586824765ec7142225 (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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