Created
July 8, 2013 15:09
-
-
Save sharnik/5949668 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
module MyFancyTool | |
module TestSupport | |
module ArubaExt | |
def with_redirected_stdout(&block) | |
redirect_stdout | |
yield | |
bring_back_stdout | |
end | |
def mock_stdout | |
unescape @stdout_cache | |
end | |
def mock_stderr | |
unescape @stderr_cache | |
end | |
def mock_output | |
mock_stdout + mock_stderr | |
end | |
private | |
def redirect_stdout | |
@stdout_cache = '' | |
@stderr_cache = '' | |
@stdout_redirected = true | |
@orig_stdout = $stdout | |
@orig_stderr = $stderr | |
$stdout = @mock_stdout = StringIO.new | |
$stderr = @mock_stderr = StringIO.new | |
end | |
def bring_back_stdout | |
@stdout_cache = @mock_stderr.string | |
@stderr_cache = @mock_stdout.string | |
@stdout_redirected = false | |
$stdout = @orig_stdout | |
$stderr = @orig_stderr | |
@orig_stdout = @mock_stdout = nil | |
@orig_stderr = @mock_stderr = nil | |
end | |
end | |
end | |
end | |
World(MyFancyTool::TestSupport::ArubaExt) |
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
When /^I run my fancy tool with command "(.*)"$/ do |args| | |
args = args.split | |
args[0] = args[0].to_sym | |
Dir.chdir(current_dir) do | |
with_redirected_stdout do | |
MyFancyTool::MyCLI.start args | |
end | |
end | |
end | |
Then /^the last output should match (#{PATTERN})$/ do |expected| | |
assert { mock_output =~ expected } | |
end |
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
Feature: Reassuring awesomeness of MyFancyTool. | |
Scenario: Running the tool | |
When I run my fancy tool with command "do stuff" | |
Then the last output should match /awesome output/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment