Created
June 18, 2010 20:59
-
-
Save blakesmith/444225 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
def keep_stdout(&block) | |
begin | |
orig_stream, $stdout = $stdout, StringIO.new | |
block.call($stdout) | |
ensure | |
s, $stdout = $stdout.string, orig_stream | |
s | |
end | |
end | |
# Used to test exact STDOUT output | |
def assert_output(expected, &block) | |
keep_stdout do |stdout| | |
block.call | |
if expected.is_a?(Regexp) | |
assert_match expected, stdout.string | |
else | |
assert_equal expected.to_s, stdout.string | |
end | |
end | |
end | |
# Used to test if STDOUT contains a certain substring | |
def assert_output_include?(expected, &block) | |
keep_stdout do |stdout| | |
block.call | |
assert_equal true, stdout.string.include?(expected.to_s) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment