Skip to content

Commit

Permalink
Converted shared specs to RSpec-style shared 'describe' blocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Ford committed Jul 17, 2008
1 parent 41dac50 commit 63dc9eb
Show file tree
Hide file tree
Showing 352 changed files with 7,093 additions and 7,447 deletions.
24 changes: 13 additions & 11 deletions 1.8/core/argf/fixtures/classes.rb
@@ -1,31 +1,31 @@
module ARGFSpecs
# create a temporary fixture file in tmp directory based on

# create a temporary fixture file in tmp directory based on
# the fixture file passed as an argument
def self.fixture_file(*filename)
fixture_file_path = File.join(File.dirname(__FILE__), filename)
tmp_fixture_file_path = tmp(filename)
File.open(tmp_fixture_file_path, 'w') { |fh| fh.write(File.read(fixture_file_path))}
tmp_fixture_file_path
end

def self.fixture_file_delete(*filenames)
filenames.each { |fn| File.delete(fn) if File.exists?(fn) }
end

def self.file_args(*args)
files = args.collect do |filename|
# if STDIN or abslute path then return as is
# else append the fixture path to the file
if filename == '-' || filename[0..0] == '/'
if filename == '-' || filename[0..0] == '/'
filename
else
self.fixture_file(filename)
end
end
ARGV.concat(files)
end

def self.ruby(cmd_args)
ruby = self.rubybin
code_file = tmp('spec_code.rb')
Expand All @@ -37,11 +37,14 @@ def self.ruby(cmd_args)
f.close unless !f || f.closed?
#File.delete(code_file) if File.exists?(code_file)
end


# TODO: put this into a helper in MSpec and require it in
# spec_helper.rb for RSpec.
#
# got it from test/ruby/envutil.rb from Ruby 1.9.x trunk
def self.rubybin
unless ENV["RUBYOPT"]

end
if ruby = ENV["RUBY"]
return ruby
Expand All @@ -60,12 +63,11 @@ def self.rubybin
begin
require "rbconfig"
File.join(
RbConfig::CONFIG["bindir"],
RbConfig::CONFIG["ruby_install_name"] + RbConfig::CONFIG["EXEEXT"]
RbConfig::CONFIG["bindir"], RbConfig::CONFIG["ruby_install_name"] + RbConfig::CONFIG["EXEEXT"]
)
rescue LoadError
"ruby"
end
end

end
65 changes: 33 additions & 32 deletions 1.8/core/argf/gets_spec.rb
Expand Up @@ -17,12 +17,12 @@
ARGF.close rescue nil
ARGFSpecs.fixture_file_delete(@file1,@file2,@stdin)
end

it "reads one line of a file" do
ARGFSpecs.file_args('file1.txt')
ARGF.gets().should == @contents_file1.split($/).first+$/
end

it "reads all lines of a file" do
ARGFSpecs.file_args('file1.txt')
number_of_lines = @contents_file1.split($/).size
Expand All @@ -32,7 +32,7 @@
end
all_lines.should == @contents_file1.split($/).collect { |l| l+$/ }
end

it "reads all lines of stdin" do
ARGFSpecs.file_args('-')
number_of_lines = @contents_stdin.split($/).size
Expand All @@ -43,7 +43,7 @@
end
all_lines.should == @contents_stdin.split($/).collect { |l| l+$/ }
end

it "reads all lines of two files" do
ARGFSpecs.file_args('file1.txt', 'file2.txt')
number_of_lines = @contents_file1.split($/).size + @contents_file2.split($/).size
Expand All @@ -53,7 +53,7 @@
end
all_lines.should == (@contents_file1+ @contents_file2).split($/).collect { |l| l+$/ }
end

it "returns nil when reaching end of files" do
ARGFSpecs.file_args('file1.txt', 'file2.txt')
number_of_lines = @contents_file1.split($/).size + @contents_file2.split($/).size
Expand All @@ -62,42 +62,43 @@
end
ARGF.gets.should == nil
end

it "sets $_ global variable with each line read" do
ARGFSpecs.file_args('file1.txt', 'file2.txt')
while line = ARGF.gets
$_.should == line
end
end

# Note: this test will only work on platforms that is capable of doing
# safe rename. Unfortunately there is no method in 1.8.X to test that.
# This has been corrected in Ruby 1.9.x
it "modifies the files when in place edit mode is on" do

ARGFSpecs.ruby(:options =>['-i'], :code => <<-SRC, :args => [@file1, @file2]) do |f|
while line = ARGF.gets
puts line.chomp+'.new'
end
SRC
File.read(@file1).should == @contents_file1.split($/).collect { |l| l+'.new'+$/}.join
File.read(@file2).should == @contents_file2.split($/).collect { |l| l+'.new'+$/}.join
ruby_version_is "1.9" do
it "modifies the files when in place edit mode is on" do

ARGFSpecs.ruby(:options =>['-i'], :code => <<-SRC, :args => [@file1, @file2]) do |f|
while line = ARGF.gets
puts line.chomp+'.new'
end
SRC
File.read(@file1).should == @contents_file1.split($/).collect { |l| l+'.new'+$/}.join
File.read(@file2).should == @contents_file2.split($/).collect { |l| l+'.new'+$/}.join
end

end

end

it "modifies and backups two files when in place edit mode is on" do

ARGFSpecs.ruby(:options =>['-i.bak'], :code => <<-SRC, :args => [@file1, @file2]) do |f|
while line = ARGF.gets
puts line.chomp+'.new'
end
SRC
File.read(@file1).should == @contents_file1.split($/).collect { |l| l+'.new'+$/}.join
File.read(@file2).should == @contents_file2.split($/).collect { |l| l+'.new'+$/}.join
File.read(@file1+'.bak').should == @contents_file1
File.read(@file2+'.bak').should == @contents_file2

it "modifies and backups two files when in place edit mode is on" do

ARGFSpecs.ruby(:options =>['-i.bak'], :code => <<-SRC, :args => [@file1, @file2]) do |f|
while line = ARGF.gets
puts line.chomp+'.new'
end
SRC
File.read(@file1).should == @contents_file1.split($/).collect { |l| l+'.new'+$/}.join
File.read(@file2).should == @contents_file2.split($/).collect { |l| l+'.new'+$/}.join
File.read(@file1+'.bak').should == @contents_file1
File.read(@file2+'.bak').should == @contents_file2
end
end
end

end
end
61 changes: 31 additions & 30 deletions 1.8/core/argf/readline_spec.rb
Expand Up @@ -17,12 +17,12 @@
ARGF.close unless ARGF.closed?
ARGFSpecs.fixture_file_delete(@file1,@file2,@stdin)
end

it "reads one line of a file" do
ARGFSpecs.file_args('file1.txt')
ARGF.readline().should == @contents_file1.split($/).first+$/
end

it "reads all lines of a file" do
ARGFSpecs.file_args('file1.txt')
number_of_lines = @contents_file1.split($/).size
Expand All @@ -32,7 +32,7 @@
end
all_lines.should == @contents_file1.split($/).collect { |l| l+$/ }
end

it "reads all lines of stdin" do
ARGFSpecs.file_args('-')
number_of_lines = @contents_stdin.split($/).size
Expand All @@ -43,7 +43,7 @@
end
all_lines.should == @contents_stdin.split($/).collect { |l| l+$/ }
end

it "reads all lines of two files" do
ARGFSpecs.file_args('file1.txt', 'file2.txt')
number_of_lines = @contents_file1.split($/).size + @contents_file2.split($/).size
Expand All @@ -53,46 +53,47 @@
end
all_lines.should == (@contents_file1+ @contents_file2).split($/).collect { |l| l+$/ }
end

it "raises an EOFError when reaching end of files" do
ARGFSpecs.file_args('file1.txt', 'file2.txt')
lambda { while line = ARGF.readline;end }.should raise_error(EOFError)
end

# Note: this test will only work on platforms that is capable of doing
# safe rename. Unfortunately there is no method in 1.8.X to test that.
# This has been corrected in Ruby 1.9.x
it "modifies the files when in place edit mode is on" do

ARGFSpecs.ruby(:options =>['-i'], :code => <<-SRC, :args => [@file1, @file2]) do |f|
begin
while line = ARGF.readline
puts line.chomp+'.new'
end
rescue EOFError
end
SRC
File.read(@file1).should == @contents_file1.split($/).collect { |l| l+'.new'+$/}.join
File.read(@file2).should == @contents_file2.split($/).collect { |l| l+'.new'+$/}.join
end

end

it "modifies and backups two files when in place edit mode is on" do

ARGFSpecs.ruby(:options =>['-i.bak'], :code => <<-SRC, :args => [@file1, @file2]) do |f|
ruby_version_is "1.9" do
it "modifies the files when in place edit mode is on" do

ARGFSpecs.ruby(:options =>['-i'], :code => <<-SRC, :args => [@file1, @file2]) do |f|
begin
while line = ARGF.readline
puts line.chomp+'.new'
end
rescue EOFError
end
SRC
File.read(@file1).should == @contents_file1.split($/).collect { |l| l+'.new'+$/}.join
File.read(@file2).should == @contents_file2.split($/).collect { |l| l+'.new'+$/}.join
File.read(@file1+'.bak').should == @contents_file1
File.read(@file2+'.bak').should == @contents_file2
File.read(@file1).should == @contents_file1.split($/).collect { |l| l+'.new'+$/}.join
File.read(@file2).should == @contents_file2.split($/).collect { |l| l+'.new'+$/}.join
end

end

it "modifies and backups two files when in place edit mode is on" do

ARGFSpecs.ruby(:options =>['-i.bak'], :code => <<-SRC, :args => [@file1, @file2]) do |f|
begin
while line = ARGF.readline
puts line.chomp+'.new'
end
rescue EOFError
end
SRC
File.read(@file1).should == @contents_file1.split($/).collect { |l| l+'.new'+$/}.join
File.read(@file2).should == @contents_file2.split($/).collect { |l| l+'.new'+$/}.join
File.read(@file1+'.bak').should == @contents_file1
File.read(@file2+'.bak').should == @contents_file2
end
end
end

end
48 changes: 23 additions & 25 deletions 1.8/core/argf/shared/each_line.rb
@@ -1,27 +1,25 @@
shared :argf_each_line do |cmd|
describe "ARGF.#{cmd}" do
before :each do
ARGV.clear
@file1 = ARGFSpecs.fixture_file('file1.txt')
@file2 = ARGFSpecs.fixture_file('file2.txt')
@stdin = ARGFSpecs.fixture_file('stdin.txt')
@contents_file1 = File.read(@file1)
@contents_file2 = File.read(@file2)
@contents_stdin = File.read(@stdin)
end
describe :argf_each_line, :shared => true do
before :each do
ARGV.clear
@file1 = ARGFSpecs.fixture_file('file1.txt')
@file2 = ARGFSpecs.fixture_file('file2.txt')
@stdin = ARGFSpecs.fixture_file('stdin.txt')
@contents_file1 = File.read(@file1)
@contents_file2 = File.read(@file2)
@contents_stdin = File.read(@stdin)
end

after :each do
# Close any open file (catch exception if already closed)
ARGF.close rescue nil
ARGFSpecs.fixture_file_delete(@file1,@file2,@stdin)
end

after :each do
# Close any open file (catch exception if already closed)
ARGF.close rescue nil
ARGFSpecs.fixture_file_delete(@file1,@file2,@stdin)
end

it "reads each line of files" do
ARGFSpecs.file_args('file1.txt', 'file2.txt', '-')
STDIN.reopen(@stdin)
line_list = []
ARGF.send(cmd) { |b| line_list << b }
line_list.should == (@contents_file1 + @contents_file2 + @contents_stdin).split($/).collect { |l| l+$/} # ord
end
it "reads each line of files" do
ARGFSpecs.file_args('file1.txt', 'file2.txt', '-')
STDIN.reopen(@stdin)
line_list = []
ARGF.send(@method) { |b| line_list << b }
line_list.should == (@contents_file1 + @contents_file2 + @contents_stdin).split($/).collect { |l| l+$/} # ord
end
end
end

0 comments on commit 63dc9eb

Please sign in to comment.