Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions core/argf/each_byte_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,60 @@
require_relative '../../spec_helper'
require_relative 'shared/each_byte'

describe "ARGF.each_byte" do
it_behaves_like :argf_each_byte, :each_byte
before :each do
@file1_name = fixture __FILE__, "file1.txt"
@file2_name = fixture __FILE__, "file2.txt"

@bytes = []
File.read(@file1_name).each_byte { |b| @bytes << b }
File.read(@file2_name).each_byte { |b| @bytes << b }
end

it "yields each byte of all streams to the passed block" do
argf [@file1_name, @file2_name] do
bytes = []
@argf.each_byte { |b| bytes << b }
bytes.should == @bytes
end
end

it "returns self when passed a block" do
argf [@file1_name, @file2_name] do
@argf.each_byte {}.should.equal?(@argf)
end
end

it "returns an Enumerator when passed no block" do
argf [@file1_name, @file2_name] do
enum = @argf.each_byte
enum.should.instance_of?(Enumerator)

bytes = []
enum.each { |b| bytes << b }
bytes.should == @bytes
end
end

describe "when no block is given" do
it "returns an Enumerator" do
argf [@file1_name, @file2_name] do
enum = @argf.each_byte
enum.should.instance_of?(Enumerator)

bytes = []
enum.each { |b| bytes << b }
bytes.should == @bytes
end
end

describe "returned Enumerator" do
describe "size" do
it "should return nil" do
argf [@file1_name, @file2_name] do
@argf.each_byte.size.should == nil
end
end
end
end
end
end
58 changes: 56 additions & 2 deletions core/argf/each_char_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,60 @@
require_relative '../../spec_helper'
require_relative 'shared/each_char'

describe "ARGF.each_char" do
it_behaves_like :argf_each_char, :each_char
before :each do
@file1_name = fixture __FILE__, "file1.txt"
@file2_name = fixture __FILE__, "file2.txt"

@chars = []
File.read(@file1_name).each_char { |c| @chars << c }
File.read(@file2_name).each_char { |c| @chars << c }
end

it "yields each char of all streams to the passed block" do
argf [@file1_name, @file2_name] do
chars = []
@argf.each_char { |c| chars << c }
chars.should == @chars
end
end

it "returns self when passed a block" do
argf [@file1_name, @file2_name] do
@argf.each_char {}.should.equal?(@argf)
end
end

it "returns an Enumerator when passed no block" do
argf [@file1_name, @file2_name] do
enum = @argf.each_char
enum.should.instance_of?(Enumerator)

chars = []
enum.each { |c| chars << c }
chars.should == @chars
end
end

describe "when no block is given" do
it "returns an Enumerator" do
argf [@file1_name, @file2_name] do
enum = @argf.each_char
enum.should.instance_of?(Enumerator)

chars = []
enum.each { |c| chars << c }
chars.should == @chars
end
end

describe "returned Enumerator" do
describe "size" do
it "should return nil" do
argf [@file1_name, @file2_name] do
@argf.each_char.size.should == nil
end
end
end
end
end
end
58 changes: 56 additions & 2 deletions core/argf/each_codepoint_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,60 @@
require_relative '../../spec_helper'
require_relative 'shared/each_codepoint'

describe "ARGF.each_codepoint" do
it_behaves_like :argf_each_codepoint, :each_codepoint
before :each do
file1_name = fixture __FILE__, "file1.txt"
file2_name = fixture __FILE__, "file2.txt"
@filenames = [file1_name, file2_name]

@codepoints = File.read(file1_name).codepoints
@codepoints.concat File.read(file2_name).codepoints
end

it "is a public method" do
argf @filenames do
@argf.public_methods(false).should.include?(:each_codepoint)
end
end

it "does not require arguments" do
argf @filenames do
@argf.method(:each_codepoint).arity.should == 0
end
end

it "returns self when passed a block" do
argf @filenames do
@argf.each_codepoint {}.should.equal?(@argf)
end
end

it "returns an Enumerator when passed no block" do
argf @filenames do
@argf.each_codepoint.should.instance_of?(Enumerator)
end
end

it "yields each codepoint of all streams" do
argf @filenames do
@argf.each_codepoint.to_a.should == @codepoints
end
end

describe "when no block is given" do
it "returns an Enumerator" do
argf @filenames do
@argf.each_codepoint.should.instance_of?(Enumerator)
end
end

describe "returned Enumerator" do
describe "size" do
it "should return nil" do
argf @filenames do
@argf.each_codepoint.size.should == nil
end
end
end
end
end
end
62 changes: 60 additions & 2 deletions core/argf/each_line_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,64 @@
require_relative '../../spec_helper'
require_relative 'shared/each_line'

describe "ARGF.each_line" do
it_behaves_like :argf_each_line, :each_line
before :each do
@file1_name = fixture __FILE__, "file1.txt"
@file2_name = fixture __FILE__, "file2.txt"

@lines = File.readlines @file1_name
@lines += File.readlines @file2_name
end

it "is a public method" do
argf [@file1_name, @file2_name] do
@argf.public_methods(false).should.include?(:each_line)
end
end

it "requires multiple arguments" do
argf [@file1_name, @file2_name] do
@argf.method(:each_line).arity.should < 0
end
end

it "reads each line of files" do
argf [@file1_name, @file2_name] do
lines = []
@argf.each_line { |b| lines << b }
lines.should == @lines
end
end

it "returns self when passed a block" do
argf [@file1_name, @file2_name] do
@argf.each_line {}.should.equal?(@argf)
end
end

describe "with a separator" do
it "yields each separated section of all streams" do
argf [@file1_name, @file2_name] do
@argf.send(:each_line, '.').to_a.should ==
(File.readlines(@file1_name, '.') + File.readlines(@file2_name, '.'))
end
end
end

describe "when no block is given" do
it "returns an Enumerator" do
argf [@file1_name, @file2_name] do
@argf.each_line.should.instance_of?(Enumerator)
end
end

describe "returned Enumerator" do
describe "size" do
it "should return nil" do
argf [@file1_name, @file2_name] do
@argf.each_line.size.should == nil
end
end
end
end
end
end
5 changes: 3 additions & 2 deletions core/argf/each_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require_relative '../../spec_helper'
require_relative 'shared/each_line'

describe "ARGF.each" do
it_behaves_like :argf_each_line, :each
it "is an alias of ARGF.each_line" do
ARGF.method(:each).should == ARGF.method(:each_line)
end
end
28 changes: 25 additions & 3 deletions core/argf/eof_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
require_relative '../../spec_helper'
require_relative 'shared/eof'

describe "ARGF.eof" do
it_behaves_like :argf_eof, :eof
it "is an alias of ARGF.eof?" do
ARGF.method(:eof).should == ARGF.method(:eof?)
end
end

describe "ARGF.eof?" do
it_behaves_like :argf_eof, :eof?
before :each do
@file1 = fixture __FILE__, "file1.txt"
@file2 = fixture __FILE__, "file2.txt"
end

# NOTE: this test assumes that fixtures files have two lines each
it "returns true when reaching the end of a file" do
argf [@file1, @file2] do
result = []
while @argf.gets
result << @argf.eof?
end
result.should == [false, true, false, true]
end
end

it "raises IOError when called on a closed stream" do
argf [@file1] do
@argf.read
-> { @argf.eof? }.should.raise(IOError)
end
end
end
28 changes: 26 additions & 2 deletions core/argf/filename_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
require_relative '../../spec_helper'
require_relative 'shared/filename'

describe "ARGF.filename" do
it_behaves_like :argf_filename, :filename
before :each do
@file1 = fixture __FILE__, "file1.txt"
@file2 = fixture __FILE__, "file2.txt"
end

# NOTE: this test assumes that fixtures files have two lines each
it "returns the current file name on each file" do
argf [@file1, @file2] do
result = []
# returns first current file even when not yet open
result << @argf.filename
result << @argf.filename while @argf.gets
# returns last current file even when closed
result << @argf.filename

result.map! { |f| File.expand_path(f) }
result.should == [@file1, @file1, @file1, @file2, @file2, @file2]
end
end

# NOTE: this test assumes that fixtures files have two lines each
it "sets the $FILENAME global variable with the current file name on each file" do
script = fixture __FILE__, "filename.rb"
out = ruby_exe(script, args: [@file1, @file2])
out.should == "#{@file1}\n#{@file1}\n#{@file2}\n#{@file2}\n#{@file2}\n"
end
end
24 changes: 22 additions & 2 deletions core/argf/fileno_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
require_relative '../../spec_helper'
require_relative 'shared/fileno'

describe "ARGF.fileno" do
it_behaves_like :argf_fileno, :fileno
before :each do
@file1 = fixture __FILE__, "file1.txt"
@file2 = fixture __FILE__, "file2.txt"
end

# NOTE: this test assumes that fixtures files have two lines each
it "returns the current file number on each file" do
argf [@file1, @file2] do
result = []
# returns first current file even when not yet open
result << @argf.fileno while @argf.gets
# returns last current file even when closed
result.map { |d| d.class }.should == [Integer, Integer, Integer, Integer]
end
end

it "raises an ArgumentError when called on a closed stream" do
argf [@file1] do
@argf.read
-> { @argf.fileno }.should.raise(ArgumentError)
end
end
end
7 changes: 7 additions & 0 deletions core/argf/inspect_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require_relative '../../spec_helper'

describe "ARGF.inspect" do
it "is an alias of ARGF.to_s" do
ARGF.method(:inspect).should == ARGF.method(:to_s)
end
end
5 changes: 3 additions & 2 deletions core/argf/path_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require_relative '../../spec_helper'
require_relative 'shared/filename'

describe "ARGF.path" do
it_behaves_like :argf_filename, :path
it "is an alias of ARGF.filename" do
ARGF.method(:path).should == ARGF.method(:filename)
end
end
Loading
Loading