Skip to content
Browse files

Finally got all the tests passing

  • Loading branch information...
1 parent 2626d32 commit 39cd63cae5e3770a73b443c588b79c54e8fa2505 @practicingruby practicingruby committed Oct 17, 2011
Showing with 183 additions and 12 deletions.
  1. +3 −0 Gemfile
  2. +1 −0 README.md
  3. +10 −1 bin/rcat
  4. +131 −0 example.rb
  5. +11 −4 lib/rcat/application.rb
  6. +5 −7 lib/rcat/display.rb
  7. +3 −0 lib/rcat/version.rb
  8. +19 −0 rcat.gemspec
View
3 Gemfile
@@ -0,0 +1,3 @@
+source :rubygems
+
+gemspec
View
1 README.md
@@ -0,0 +1 @@
+## A demo application for Practicing Ruby 2.9
View
11 bin/rcat
@@ -3,4 +3,13 @@
require "optparse"
require_relative "../lib/rcat"
-RCat::Application.new(ARGV).run
+begin
+ RCat::Application.new(ARGV).run
+rescue Errno::ENOENT => err
+ STDERR.puts "rcat: #{err.message}"
+ exit(1)
+rescue OptionParser::InvalidOption => err
+ STDERR.puts "rcat: #{err.message}"
+ STDERR.puts "usage: rcat [-bns] [file ...]"
+ exit(1)
+end
View
131 example.rb
@@ -0,0 +1,131 @@
+require "open3"
+
+working_dir = File.dirname(__FILE__)
+gettysburg_file = "#{working_dir}/data/gettysburg.txt"
+spaced_file = "#{working_dir}/data/spaced_out.txt"
+
+############################################################################
+
+cat_output = `cat #{gettysburg_file}`
+rcat_output = `rcat #{gettysburg_file}`
+
+fail "Failed 'cat == rcat'" unless cat_output == rcat_output
+
+############################################################################
+
+cat_output = `cat -n #{gettysburg_file}`
+rcat_output = `rcat -n #{gettysburg_file}`
+
+fail "Failed 'cat -n == rcat -n'" unless cat_output == rcat_output
+
+############################################################################
+
+cat_output = `cat -b #{gettysburg_file}`
+rcat_output = `rcat -b #{gettysburg_file}`
+
+fail "Failed 'cat -b == rcat -b'" unless cat_output == rcat_output
+
+############################################################################
+
+cat_output = `cat -s #{spaced_file}`
+rcat_output = `rcat -s #{spaced_file}`
+
+fail "Failed 'cat -s == rcat -s'" unless cat_output == rcat_output
+
+############################################################################
+
+cat_output = `cat -bs #{spaced_file}`
+rcat_output = `rcat -bs #{spaced_file}`
+
+fail "Failed 'cat -bns == rcat -bns'" unless cat_output == rcat_output
+
+############################################################################
+
+cat_output = `cat -bns #{spaced_file}`
+rcat_output = `rcat -bns #{spaced_file}`
+
+fail "Failed 'cat -bns == rcat -bns'" unless cat_output == rcat_output
+
+############################################################################
+
+cat_output = `cat -nbs #{spaced_file}`
+rcat_output = `rcat -nbs #{spaced_file}`
+
+fail "Failed 'cat -nbs == rcat -nbs'" unless cat_output == rcat_output
+
+############################################################################
+
+cat_output = `cat #{gettysburg_file} #{spaced_file}`
+rcat_output = `rcat #{gettysburg_file} #{spaced_file}`
+
+fail "Failed 'cat [f1 f2] == rcat [f1 f2]'" unless cat_output == rcat_output
+
+############################################################################
+
+cat_output = `cat -ns #{gettysburg_file} #{spaced_file}`
+rcat_output = `rcat -ns #{gettysburg_file} #{spaced_file}`
+
+unless cat_output == rcat_output
+ fail "Failed 'cat -ns [f1 f2] == rcat -ns [f1 f2]'"
+end
+
+############################################################################
+
+cat_output = `cat -bs #{gettysburg_file} #{spaced_file}`
+rcat_output = `rcat -bs #{gettysburg_file} #{spaced_file}`
+
+unless cat_output == rcat_output
+ fail "Failed 'cat -bs [f1 f2] == rcat -bs [f1 f2]'"
+end
+
+############################################################################
+
+cat_output = `cat < #{spaced_file}`
+rcat_output = `rcat < #{spaced_file}`
+
+unless cat_output == rcat_output
+ fail "Failed 'cat < file == rcat < file"
+end
+
+############################################################################
+
+`cat #{gettysburg_file}`
+cat_success = $?
+
+`rcat #{gettysburg_file}`
+rcat_success = $?
+
+unless cat_success.exitstatus == 0 && rcat_success.exitstatus == 0
+ fail "Failed 'cat and rcat success exit codes match"
+end
+
+############################################################################
+
+cat_out, cat_err, cat_process = Open3.capture3("cat some_invalid_file")
+rcat_out, rcat_err, rcat_process = Open3.capture3("rcat some_invalid_file")
+
+unless cat_process.exitstatus == 1 && rcat_process.exitstatus == 1
+ fail "Failed 'cat and rcat exit codes match on bad file"
+end
+
+unless rcat_err == "rcat: No such file or directory - some_invalid_file\n"
+ fail "Failed 'cat and rcat error messages match on bad file'"
+end
+
+############################################################################
+
+
+cat_out, cat_err, cat_proccess = Open3.capture3("cat -x #{gettysburg_file}")
+rcat_out,rcat_err, rcat_process = Open3.capture3("rcat -x #{gettysburg_file}")
+
+unless cat_process.exitstatus == 1 && rcat_process.exitstatus == 1
+ fail "Failed 'cat and rcat exit codes match on bad switch"
+end
+
+unless rcat_err == "rcat: invalid option: -x\nusage: rcat [-bns] [file ...]\n"
+ fail "Failed 'rcat provides usage instructions when given invalid option"
+end
+
+############################################################################
+
+puts "You passed the tests, yay!"
View
15 lib/rcat/application.rb
@@ -2,19 +2,26 @@ module RCat
class Application
def initialize(argv)
@params, @files = parse_options(argv)
+
@display = RCat::Display.new(@params)
end
def run
- @files.each { |f| @display.render_file(f) }
+ if @files.empty?
+ @display.render(STDIN)
+ else
+ @files.each do |filename|
+ File.open(filename) { |f| @display.render(f) }
+ end
+ end
end
def parse_options(argv)
params = {}
files = OptionParser.new do |parser|
- parser.on("-n") { params[:line_numbering] = :all_lines }
- parser.on("-b") { params[:line_numbering] = :significant_lines }
- parser.on("-s") { params[:squeeze] = true }
+ parser.on("-n") { params[:line_numbering] ||= :all_lines }
+ parser.on("-b") { params[:line_numbering] = :significant_lines }
+ parser.on("-s") { params[:squeeze] = true }
end.parse(argv)
[params, files]
View
12 lib/rcat/display.rb
@@ -5,13 +5,11 @@ def initialize(params)
@squeeze = params[:squeeze]
end
- def render_file(filename)
+ def render(data)
@line_number = 1
- File.open(filename) do |f|
- lines = f.lines
- loop { render_line(lines) }
- end
+ lines = data.lines
+ loop { render_line(lines) }
end
def render_line(lines)
@@ -21,13 +19,13 @@ def render_line(lines)
case @line_numbering
when :all_lines
- print "#{@line_number.to_s.rjust(6)} #{line}"
+ print "#{@line_number.to_s.rjust(6)}\t#{line}"
@line_number += 1
when :significant_lines
if current_line_empty
print line
else
- print "#{@line_number.to_s.rjust(6)} #{line}"
+ print "#{@line_number.to_s.rjust(6)}\t#{line}"
@line_number += 1
end
else
View
3 lib/rcat/version.rb
@@ -0,0 +1,3 @@
+module RCat
+ VERSION = "0.0.1"
+end
View
19 rcat.gemspec
@@ -0,0 +1,19 @@
+require "#{File.dirname(__FILE__)}/lib/rcat/version"
+
+Gem::Specification.new do |s|
+ s.name = "rcat"
+ s.version = RCat::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Gregory Brown"]
+ s.email = ["gregory.t.brown@gmail.com"]
+ s.homepage = "http://github.com/sandal/rcat"
+ s.summary = "Demo application for Practicing Ruby 2.9"
+ s.description = "Demo application for Practicing Ruby 2.9"
+ s.files = Dir.glob("{bin,lib,test,examples,doc,data}/**/*") + %w(README.md)
+ s.require_path = 'lib'
+ s.executables = ["rcat"]
+ s.required_ruby_version = ">= 1.9.2"
+ s.required_rubygems_version = ">= 1.3.6"
+ s.rubyforge_project = "rcat"
+end
+

0 comments on commit 39cd63c

Please sign in to comment.
Something went wrong with that request. Please try again.