summaryrefslogtreecommitdiff
path: root/spec/ruby/core/argf/shared/gets.rb
blob: 160d24c27bb306f805988df5591e6522ad4c81b3 (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
describe :argf_gets, shared: true do
  before :each do
    @file1_name = fixture __FILE__, "file1.txt"
    @file2_name = fixture __FILE__, "file2.txt"
    @stdin_name = fixture __FILE__, "stdin.txt"

    @file1 = File.readlines @file1_name
    @file2 = File.readlines @file2_name
    @stdin = File.read @stdin_name
  end

  it "reads one line of a file" do
    argf [@file1_name] do
      @argf.send(@method).should == @file1.first
    end
  end

  it "reads all lines of a file" do
    argf [@file1_name] do
      lines = []
      @file1.size.times { lines << @argf.send(@method) }
      lines.should == @file1
    end
  end

  it "reads all lines of stdin" do
    total = @stdin.count $/
    stdin = ruby_exe(
      "#{total}.times { print ARGF.send(#{@method.inspect}) }",
      args: "< #{@stdin_name}")
    stdin.should == @stdin
  end

  it "reads all lines of two files" do
    argf [@file1_name, @file2_name] do
      total = @file1.size + @file2.size
      lines = []
      total.times { lines << @argf.send(@method) }
      lines.should == @file1 + @file2
    end
  end

  it "sets $_ global variable with each line read" do
    argf [@file1_name, @file2_name] do
      total = @file1.size + @file2.size
      total.times do
        line = @argf.send(@method)
        $_.should == line
      end
    end
  end
end

describe :argf_gets_inplace_edit, shared: true do
  before :each do
    @file1_name = fixture __FILE__, "file1.txt"
    @file2_name = fixture __FILE__, "file2.txt"

    @tmp1_name  = tmp "file1.txt"
    @tmp2_name  = tmp "file2.txt"

    @tmp1_name_bak = @tmp1_name + ".bak"
    @tmp2_name_bak = @tmp2_name + ".bak"

    cp @file1_name, @tmp1_name
    cp @file2_name, @tmp2_name

    method = "ARGF.send(#{@method.inspect})"
    @code = "begin while line = #{method} do puts 'x' end rescue EOFError; end"
  end

  after :each do
    rm_r @tmp1_name, @tmp2_name, @tmp1_name_bak, @tmp2_name_bak
  end

  # -i with no backup extension is not supported on Windows
  platform_is_not :windows do
    it "modifies the files when in place edit mode is on" do
      ruby_exe(@code,
               options: "-i",
               args: "#{@tmp1_name} #{@tmp2_name}")

      File.read(@tmp1_name).should == "x\nx\n"
      File.read(@tmp2_name).should == "x\nx\n"
    end
  end

  it "modifies and backups two files when in place edit mode is on" do
    ruby_exe(@code,
             options: "-i.bak",
             args: "#{@tmp1_name} #{@tmp2_name}")

    File.read(@tmp1_name).should == "x\nx\n"
    File.read(@tmp2_name).should == "x\nx\n"

    File.read(@tmp1_name_bak).should == "file1.1\nfile1.2\n"
    File.read(@tmp2_name_bak).should == "line2.1\nline2.2\n"
  end
end