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 File.expand_path('../../../spec_helper', __FILE__)
describe "ARGF.read" do
before :each do
@file1_name = fixture __FILE__, "file1.txt"
@file2_name = fixture __FILE__, "file2.txt"
@stdin_name = fixture __FILE__, "stdin.txt"
@file1 = File.read @file1_name
@file2 = File.read @file2_name
@stdin = File.read @stdin_name
end
after :each do
ARGF.close unless ARGF.closed?
end
it "reads the contents of a file" do
argv [@file1_name] do
ARGF.read().should == @file1
end
end
it "treats first nil argument as no length limit" do
argv [@file1_name] do
ARGF.read(nil).should == @file1
end
end
it "treats second nil argument as no output buffer" do
argv [@file1_name] do
ARGF.read(nil, nil).should == @file1
end
end
it "treats second argument as an output buffer" do
argv [@file1_name] do
buffer = ""
ARGF.read(nil, buffer)
buffer.should == @file1
end
end
it "reads a number of bytes from the first file" do
argv [@file1_name] do
ARGF.read(5).should == @file1[0,5]
end
end
it "reads from a single file consecutively" do
argv [@file1_name] do
ARGF.read(1).should == @file1[0,1]
ARGF.read(2).should == @file1[1,2]
ARGF.read(3).should == @file1[3,3]
end
end
it "reads the contents of two files" do
argv [@file1_name, @file2_name] do
ARGF.read.should == @file1 + @file2
end
end
it "reads the contents of one file and some characters from the second" do
argv [@file1_name, @file2_name] do
len = @file1.size + (@file2.size / 2)
ARGF.read(len).should == (@file1 + @file2)[0,len]
end
end
it "reads across two files consecutively" do
argv [@file1_name, @file2_name] do
ARGF.read(@file1.size - 2).should == @file1[0..-3]
ARGF.read(2+5).should == @file1[-2..-1] + @file2[0,5]
end
end
it "reads the contents of stdin" do
stdin = ruby_exe("print ARGF.read", :args => "< #{@stdin_name}")
stdin.should == @stdin
end
it "reads a number of bytes from stdin" do
stdin = ruby_exe("print ARGF.read(10)", :args => "< #{@stdin_name}")
stdin.should == @stdin[0,10]
end
it "reads the contents of one file and stdin" do
stdin = ruby_exe("print ARGF.read", :args => "#{@file1_name} - < #{@stdin_name}")
stdin.should == @file1 + @stdin
end
it "reads the contents of the same file twice" do
argv [@file1_name, @file1_name] do
ARGF.read.should == @file1 + @file1
end
end
platform_is_not :windows do
it "reads the contents of a special device file" do
argv ['/dev/zero'] do
ARGF.read(100).should == "\000" * 100
end
end
end
end
|