Permalink
Browse files

Add initial specs for Main, way to hook IO

  • Loading branch information...
1 parent 7657a43 commit c11aa42d86998c229580575d74ce63ec5b511115 @paulcbetts committed Jun 15, 2008
Showing with 47 additions and 9 deletions.
  1. +7 −5 lib/main.rb
  2. +40 −4 spec/main_spec.rb
View
@@ -96,7 +96,7 @@ def parse(args)
opts.on_tail("-?", "--help", _("Show this message") ) do
puts opts
- exit
+ return nil
end
opts.on('-d', "--debug", _("Run in debug mode (Extra messages)")) do |x|
@@ -109,7 +109,7 @@ def parse(args)
opts.on_tail("--version", _("Show version") ) do
puts OptionParser::Version.join('.')
- exit
+ return nil
end
end
@@ -127,12 +127,14 @@ def run(args)
results = parse(args)
rescue OptionParser::MissingArgument
puts _('Missing parameter; see --help for more info')
- exit
+ return -1
rescue OptionParser::InvalidOption
puts _('Invalid option; see --help for more info')
- exit
+ return -1
end
+ return -1 unless results
+
# Reset our logging level because option parsing changed it
self.level = $logging_level
@@ -234,5 +236,5 @@ def logger
if __FILE__ == $0
$the_app = Yikes.instance
- $the_app.run(ARGV)
+ exit ($the_app.run(ARGV))
end
View
@@ -2,15 +2,51 @@
require 'helper'
require 'lib/main'
+require 'stringio'
+
+
+def hook_stdio(in_str = "")
+ # Hook stdio for the call
+ in_p, out_p, err_p = [$stdin, $stdout, $stderr]
+ in_s, out_s, err_s = [StringIO.new(in_str, 'r'), StringIO.new('', 'w'), StringIO.new('', 'w')]
+ $stdin, $stdout, $stderr = [in_s, out_s, err_s]
+
+ begin
+ yield
+ ensure
+ $stdin, $stdout, $stderr = [in_p, out_p, err_p]
+ end
+ return [in_s, out_s, err_s]
+end
+
+describe "Test Helpers" do
+ it "should hook stdio properly" do
+ i,o,e = hook_stdio do
+ puts "Foo!"
+ end
+ o.string.should == "Foo!\n"
+
+ i,o,e = hook_stdio("Bar\n") do
+ gets().should == "Bar\n"
+ end
+ end
+end
describe Yikes do
- it "should display help options" do
+ it "should run properly without crashing" do
output = `ruby #{TestDir}/../lib/main.rb --help`
# This test is stupid, but is a smoke test
output.index("Usage:").should == 0
- output.index("options:").should >= 0
- output.index("library").should >= 0
- output.index("target").should >= 0
+ end
+
+ it "should display help options" do
+ i,o,e = hook_stdio do
+ Yikes.instance.run(%w{--help})
+ end
+
+ o.string.index("options:").should >= 0
+ o.string.index("library").should >= 0
+ o.string.index("target").should >= 0
end
end

0 comments on commit c11aa42

Please sign in to comment.