From bb9f758038da86a589c0c6a1dddf42f75ad5bcdc Mon Sep 17 00:00:00 2001 From: Pavel Argentov Date: Mon, 27 Feb 2012 15:27:25 +0400 Subject: [PATCH] Added to DSL: negative output matching + Added Aruba::Api#assert_not_matching_output + Added the corresponding pair of standard cucumber steps Reason why: I have faced a situation where a stakeholder wanted to clearly state the negative match of the output. --- features/output.feature | 19 +++++++++++++++++++ lib/aruba/api.rb | 4 ++++ lib/aruba/cucumber.rb | 11 ++++++++++- spec/aruba/api_spec.rb | 15 +++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/features/output.feature b/features/output.feature index 8af06ca23..35b931d1b 100644 --- a/features/output.feature +++ b/features/output.feature @@ -95,6 +95,25 @@ Feature: Output important line """ + @announce + Scenario: Negative matching of one-line output with regex + When I run `ruby --version` + Then the output should contain "ruby" + And the output should not match /ruby is a better perl$/ + + @announce + @posix + Scenario: Negative matching of multiline output with regex + When I run `ruby -e 'puts "hello\nworld\nextra line1\nextra line2\nimportant line"'` + Then the output should not match: + """ + ruby + is + a + .* + perl + """ + @announce @posix Scenario: Match passing exit status and partial output diff --git a/lib/aruba/api.rb b/lib/aruba/api.rb index 5058375df..a0eb2f13f 100644 --- a/lib/aruba/api.rb +++ b/lib/aruba/api.rb @@ -187,6 +187,10 @@ def assert_matching_output(expected, actual) unescape(actual).should =~ /#{unescape(expected)}/m end + def assert_not_matching_output(expected, actual) + unescape(actual).should_not =~ /#{unescape(expected)}/m + end + def assert_no_partial_output(unexpected, actual) if Regexp === unexpected unescape(actual).should_not =~ unexpected diff --git a/lib/aruba/cucumber.rb b/lib/aruba/cucumber.rb index 8072fb75b..f27fe2c1a 100644 --- a/lib/aruba/cucumber.rb +++ b/lib/aruba/cucumber.rb @@ -118,11 +118,20 @@ Then /^the output should match \/([^\/]*)\/$/ do |expected| assert_matching_output(expected, all_output) end - + Then /^the output should match:$/ do |expected| assert_matching_output(expected, all_output) end +# The previous two steps antagonists +Then /^the output should not match \/([^\/]*)\/$/ do |expected| + assert_not_matching_output(expected, all_output) +end + +Then /^the output should not match:$/ do |expected| + assert_not_matching_output(expected, all_output) +end + Then /^the exit status should be (\d+)$/ do |exit_status| assert_exit_status(exit_status.to_i) end diff --git a/spec/aruba/api_spec.rb b/spec/aruba/api_spec.rb index 2ee50210f..6c9c373d3 100644 --- a/spec/aruba/api_spec.rb +++ b/spec/aruba/api_spec.rb @@ -103,4 +103,19 @@ def announce_or_puts(*args) end #with_file_content + describe "#assert_not_matching_output" do + before :each do + @aruba.run_simple("echo foo", false) + end + + it "passes when the output doesn't match a regexp" do + @aruba.assert_not_matching_output "bar", @aruba.all_output + end + it "fails when the output does match a regexp" do + expect do + @aruba.assert_not_matching_output "foo", @aruba.all_output + end . to raise_error RSpec::Expectations::ExpectationNotMetError + end + end + end # Aruba::Api