Created
June 24, 2013 17:01
-
-
Save uxp/5851635 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
require 'optparse' | |
require 'minitest/autorun' | |
class Chatty | |
attr_reader :args | |
def initialize(args) | |
@args = args | |
end | |
def say_something | |
puts "Mary had a little lamb" | |
end | |
def be_loud | |
warn "Blow your house down!" | |
end | |
def option_parser | |
@option_parser ||= OptionParser.new do |opt| | |
opt.banner = "Usage: #{File.basename($0)} [option]" | |
opt.on('-V', '--version', "Display the version number.") do | |
puts "MiniTest example v0.0.1" | |
exit | |
end | |
opt.on('-h', '--help', "Display this message.") do | |
puts opt | |
exit | |
end | |
end | |
end | |
def parse_options | |
if args.empty? | |
warn "I don't know what to do" | |
warn option_parser | |
exit | |
end | |
option_parser.parse!(args) | |
end | |
end | |
class ChattyIntegrationTest < MiniTest::Unit::TestCase | |
# This works as advertized | |
def test_output_of_stdout | |
chat = Chatty.new [] | |
out, _ = capture_io do | |
chat.say_something | |
end | |
assert /mary had a little lamb/i =~ out | |
end | |
# This works as well | |
def test_output_of_stderr | |
chat = Chatty.new [] | |
_, err = capture_io do | |
chat.be_loud | |
end | |
assert /blow your house down/i =~ err | |
end | |
# This causes all tests to disappear | |
def test_output_from_optparse_no_args | |
# skip | |
chat = Chatty.new [] | |
_, err = capture_io do | |
chat.parse_options | |
end | |
assert /usage:/ =~ err | |
end | |
# This doesn't work at all | |
def test_output_from_optparse_help | |
# skip | |
chat = Chatty.new ['-h'] | |
out, _ = capture_io do | |
chat.parse_options | |
end | |
assert /usage:/ =~ out | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment