summaryrefslogtreecommitdiff
path: root/spec/ruby/core/dir/each_child_spec.rb
blob: 520186e79ef31063c870205b4f8c7f5ec091e44c (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
require_relative '../../spec_helper'
require_relative 'fixtures/common'

describe "Dir.each_child" do
  before :all do
    DirSpecs.create_mock_dirs
  end

  after :all do
    DirSpecs.delete_mock_dirs
  end

  it "accepts an encoding keyword for the encoding of the entries" do
    dirs = Dir.each_child("#{DirSpecs.mock_dir}/deeply/nested", encoding: "utf-8").to_a.sort
    dirs.each {|dir| dir.encoding.should == Encoding::UTF_8}
  end

  it "yields all names in an existing directory to the provided block" do
    a, b = [], []

    Dir.each_child(DirSpecs.mock_dir) {|f| a << f}
    Dir.each_child("#{DirSpecs.mock_dir}/deeply/nested") {|f| b << f}

    a.sort.should == DirSpecs.expected_paths -  %w[. ..]
    b.sort.should == %w|.dotfile.ext directory|
  end

  it "returns nil when successful" do
    Dir.each_child(DirSpecs.mock_dir) {|f| f}.should == nil
  end

  it "calls #to_path on non-String arguments" do
    p = mock('path')
    p.should_receive(:to_path).and_return(DirSpecs.mock_dir)
    Dir.each_child(p).to_a
  end

  it "raises a SystemCallError if passed a nonexistent directory" do
    -> { Dir.each_child(DirSpecs.nonexistent) {} }.should raise_error(SystemCallError)
  end

  describe "when no block is given" do
    it "returns an Enumerator" do
      Dir.each_child(DirSpecs.mock_dir).should be_an_instance_of(Enumerator)
      Dir.each_child(DirSpecs.mock_dir).to_a.sort.should == DirSpecs.expected_paths - %w[. ..]
    end

    describe "returned Enumerator" do
      describe "size" do
        it "should return nil" do
          Dir.each_child(DirSpecs.mock_dir).size.should == nil
        end
      end
    end
  end
end

describe "Dir#each_child" do
  before :all do
    DirSpecs.create_mock_dirs
  end

  after :all do
    DirSpecs.delete_mock_dirs
  end

  after :each do
    @dir.close if @dir
  end

  it "yields all names in an existing directory to the provided block" do
    a, b = [], []
    @dir = Dir.new(DirSpecs.mock_dir)
    @dir2 = Dir.new("#{DirSpecs.mock_dir}/deeply/nested")

    @dir.each_child { |f| a << f }
    @dir2.each_child { |f| b << f }
    @dir2.close

    a.sort.should == DirSpecs.expected_paths - %w|. ..|
    b.sort.should == %w|.dotfile.ext directory|
  end

  it "returns self when successful" do
    @dir = Dir.new(DirSpecs.mock_dir)
    @dir.each_child { |f| f }.should == @dir
  end

  describe "when no block is given" do
    it "returns an Enumerator" do
      @dir = Dir.new(DirSpecs.mock_dir)

      @dir.each_child.should be_an_instance_of(Enumerator)
      @dir.each_child.to_a.sort.should == DirSpecs.expected_paths - %w|. ..|
    end

    describe "returned Enumerator" do
      describe "size" do
        it "should return nil" do
          @dir = Dir.new(DirSpecs.mock_dir)
          @dir.each_child.size.should == nil
        end
      end
    end
  end
end