Skip to content

Commit

Permalink
Merge pull request #546 from cucumber/improve-have_output-output
Browse files Browse the repository at this point in the history
Improve failure messages from output matchers
  • Loading branch information
mvz authored Mar 5, 2018
2 parents 3fd419c + 435aa11 commit b7dc0d7
Show file tree
Hide file tree
Showing 23 changed files with 139 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ Feature: All output of commands which were executed
When I run `cucumber`
Then the features should all pass

Scenario: Failed detection of one-line output
Given an executable named "bin/aruba-test-cli" with:
"""bash
#!/usr/bin/env bash
echo 'hello world'
"""
And a file named "features/output.feature" with:
"""cucumber
Feature: Run command
Scenario: Run command
When I run `aruba-test-cli`
Then the output should contain "goodbye world"
"""
When I run `cucumber`
Then the features should not all pass with:
"""
expected `aruba-test-cli` to have output string includes: "goodbye world"
but was:
hello world (RSpec::Expectations::ExpectationNotMetError)
"""

Scenario: Detect subset of multiline output
Given an executable named "bin/aruba-test-cli" with:
"""bash
Expand Down Expand Up @@ -179,22 +201,32 @@ Feature: All output of commands which were executed
When I run `cucumber`
Then the features should all pass

Scenario: Detect subset of one-line output
Scenario: Failed detection of exact multi-line output
Given an executable named "bin/aruba-test-cli" with:
"""bash
#!/usr/bin/env bash
echo 'hello world'
echo -e "goodbye\nworld"
"""
And a file named "features/output.feature" with:
"""cucumber
Feature: Run command
Scenario: Run command
When I run `aruba-test-cli`
Then the output should contain "hello world"
Then the output should contain exactly:
\"\"\"
hello
world
\"\"\"
"""
When I run `cucumber`
Then the features should all pass
Then the features should not all pass with:
"""
expected `aruba-test-cli` to have output output string is eq: "hello\nworld"
but was:
goodbye
world (RSpec::Expectations::ExpectationNotMetError)
"""

Scenario: Detect subset of one-line output with regex
Given an executable named "bin/aruba-test-cli" with:
Expand Down

This file was deleted.

19 changes: 19 additions & 0 deletions lib/aruba/matchers/base/message_indenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Aruba
module Matchers
module Base
# Provide #indent_multiline_message helper method.
#
# @api private
module MessageIndenter
module_function

def indent_multiline_message(message)
message = message.sub(/\n+\z/, '')
message.lines.map do |line|
line =~ /\S/ ? ' ' + line : line
end.join
end
end
end
end
end
14 changes: 5 additions & 9 deletions lib/aruba/matchers/collection/include_an_object.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'aruba/matchers/base/base_matcher'
require 'aruba/matchers/base/message_indenter'

# Aruba
module Aruba
Expand All @@ -8,6 +9,7 @@ module Matchers
# Provides the implementation for `include_an_object`.
# Not intended to be instantiated directly.
class IncludeAnObject < BaseMatcher
include Base::MessageIndenter
protected

# @private
Expand All @@ -19,7 +21,7 @@ class IncludeAnObject < BaseMatcher

def initialize(matcher)
@matcher = matcher
@failed_objects = {}
@failed_objects = []
@any_succeeded_object = false
end

Expand All @@ -30,8 +32,9 @@ def failure_message
return "#{improve_hash_formatting(super)}, but was not iterable"
end

return failed_objects.first if failed_objects.size == 1
all_messages = [improve_hash_formatting(super)]
failed_objects.each do |index, matcher_failure_message|
failed_objects.each_with_index do |matcher_failure_message, index|
all_messages << failure_message_for_item(index, matcher_failure_message)
end
all_messages.join("\n\n")
Expand Down Expand Up @@ -92,13 +95,6 @@ def failure_message_for_item(index, failure_message)
def add_new_line_if_needed(message)
message.start_with?("\n") ? message : "\n#{message}"
end

def indent_multiline_message(message)
message = message.sub(/\n+\z/, '')
message.lines.map do |line|
line =~ /\S/ ? ' ' + line : line
end.join
end
end
end
end
Expand Down
4 changes: 1 addition & 3 deletions lib/aruba/matchers/command/be_successfully_executed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,4 @@
end
end

if RSpec::Expectations::Version::STRING >= '3.1'
RSpec::Matchers.define_negated_matcher :have_failed_running, :be_successfully_executed
end
RSpec::Matchers.define_negated_matcher :have_failed_running, :be_successfully_executed
4 changes: 1 addition & 3 deletions lib/aruba/matchers/command/have_finished_in_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,4 @@
end
end

if RSpec::Expectations::Version::STRING >= '3.1'
RSpec::Matchers.define_negated_matcher :run_too_long, :have_finished_in_time
end
RSpec::Matchers.define_negated_matcher :run_too_long, :have_finished_in_time
11 changes: 8 additions & 3 deletions lib/aruba/matchers/command/have_output.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'aruba/matchers/base/message_indenter'

# @!method have_output
# This matchers checks if <command> has created output
#
Expand Down Expand Up @@ -29,8 +31,11 @@
diffable

description { "have output: #{description_of expected}" }
end

if RSpec::Expectations::Version::STRING >= '3.0'
RSpec::Matchers.alias_matcher :a_command_having_output, :have_output
failure_message do |actual|
"expected `#{@old_actual.commandline}` to have output #{description_of expected}\n" \
"but was:\n#{Aruba::Matchers::Base::MessageIndenter.indent_multiline_message @actual}"
end
end

RSpec::Matchers.alias_matcher :a_command_having_output, :have_output
4 changes: 1 addition & 3 deletions lib/aruba/matchers/directory/be_an_existing_directory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,4 @@
end
end

if RSpec::Expectations::Version::STRING >= '3.0'
RSpec::Matchers.alias_matcher :an_existing_directory, :be_an_existing_directory
end
RSpec::Matchers.alias_matcher :an_existing_directory, :be_an_existing_directory
4 changes: 1 addition & 3 deletions lib/aruba/matchers/directory/have_sub_directory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,4 @@
end
end

if RSpec::Expectations::Version::STRING >= '3.0'
RSpec::Matchers.alias_matcher :a_directory_having_sub_directory, :have_sub_directory
end
RSpec::Matchers.alias_matcher :a_directory_having_sub_directory, :have_sub_directory
4 changes: 1 addition & 3 deletions lib/aruba/matchers/file/be_a_command_found_in_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,4 @@
end
end

if RSpec::Expectations::Version::STRING >= '3.0'
RSpec::Matchers.alias_matcher :a_command_found_in_path, :be_a_command_found_in_path
end
RSpec::Matchers.alias_matcher :a_command_found_in_path, :be_a_command_found_in_path
4 changes: 1 addition & 3 deletions lib/aruba/matchers/file/be_an_existing_executable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,4 @@
end
end

if RSpec::Expectations::Version::STRING >= '3.0'
RSpec::Matchers.alias_matcher :an_existing_executable, :be_an_existing_executable
end
RSpec::Matchers.alias_matcher :an_existing_executable, :be_an_existing_executable
4 changes: 1 addition & 3 deletions lib/aruba/matchers/file/be_an_existing_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,4 @@
end
end

if RSpec::Expectations::Version::STRING >= '3.0'
RSpec::Matchers.alias_matcher :an_existing_file, :be_an_existing_file
end
RSpec::Matchers.alias_matcher :an_existing_file, :be_an_existing_file
4 changes: 1 addition & 3 deletions lib/aruba/matchers/file/have_file_content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,4 @@
description { "have file content: #{description_of expected}" }
end

if RSpec::Expectations::Version::STRING >= '3.0'
RSpec::Matchers.alias_matcher :a_file_having_content, :have_file_content
end
RSpec::Matchers.alias_matcher :a_file_having_content, :have_file_content
4 changes: 1 addition & 3 deletions lib/aruba/matchers/file/have_file_size.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,4 @@
end
end

if RSpec::Expectations::Version::STRING >= '3.0'
RSpec::Matchers.alias_matcher :a_file_of_size, :have_file_size
end
RSpec::Matchers.alias_matcher :a_file_of_size, :have_file_size
4 changes: 1 addition & 3 deletions lib/aruba/matchers/file/have_same_file_content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,4 @@
end
end

if RSpec::Expectations::Version::STRING >= '3.0'
RSpec::Matchers.alias_matcher :a_file_with_same_content_like, :have_same_file_content_like
end
RSpec::Matchers.alias_matcher :a_file_with_same_content_like, :have_same_file_content_like
4 changes: 1 addition & 3 deletions lib/aruba/matchers/path/a_path_matching_pattern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,4 @@
# RSpec.describe do
# it { expect(files).to include a_path_matching_pattern /asdf/ }
# end
if RSpec::Expectations::Version::STRING >= '3.0'
RSpec::Matchers.alias_matcher :a_path_matching_pattern, :match
end
RSpec::Matchers.alias_matcher :a_path_matching_pattern, :match
4 changes: 1 addition & 3 deletions lib/aruba/matchers/path/be_an_absolute_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,4 @@
end
end

if RSpec::Expectations::Version::STRING >= '3.0'
RSpec::Matchers.alias_matcher :an_absolute_path, :be_an_absolute_path
end
RSpec::Matchers.alias_matcher :an_absolute_path, :be_an_absolute_path
4 changes: 1 addition & 3 deletions lib/aruba/matchers/path/be_an_existing_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,4 @@
end
end

if RSpec::Expectations::Version::STRING >= '3.0'
RSpec::Matchers.alias_matcher :an_existing_path, :be_an_existing_path
end
RSpec::Matchers.alias_matcher :an_existing_path, :be_an_existing_path
4 changes: 1 addition & 3 deletions lib/aruba/matchers/path/have_permissions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,4 @@ def permissions(file)
end
end

if RSpec::Expectations::Version::STRING >= '3.0'
RSpec::Matchers.alias_matcher :a_path_having_permissions, :have_permissions
end
RSpec::Matchers.alias_matcher :a_path_having_permissions, :have_permissions
6 changes: 2 additions & 4 deletions lib/aruba/matchers/string/include_output_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,5 @@
description { "string includes: #{description_of expected}" }
end

if RSpec::Expectations::Version::STRING >= '3.0'
RSpec::Matchers.alias_matcher :an_output_string_including, :include_output_string
RSpec::Matchers.alias_matcher :file_content_including, :include_output_string
end
RSpec::Matchers.alias_matcher :an_output_string_including, :include_output_string
RSpec::Matchers.alias_matcher :file_content_including, :include_output_string
8 changes: 3 additions & 5 deletions lib/aruba/matchers/string/match_output_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
description { "output string matches: #{description_of expected}" }
end

if RSpec::Expectations::Version::STRING >= '3.0'
RSpec::Matchers.alias_matcher :an_output_string_matching, :match_output_string
RSpec::Matchers.alias_matcher :a_file_name_matching, :match_output_string
RSpec::Matchers.alias_matcher :file_content_matching, :match_output_string
end
RSpec::Matchers.alias_matcher :an_output_string_matching, :match_output_string
RSpec::Matchers.alias_matcher :a_file_name_matching, :match_output_string
RSpec::Matchers.alias_matcher :file_content_matching, :match_output_string
4 changes: 1 addition & 3 deletions lib/aruba/matchers/string/output_string_eq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,4 @@
description { "output string is eq: #{description_of expected}" }
end

if RSpec::Expectations::Version::STRING >= '3.0'
RSpec::Matchers.alias_matcher :an_output_string_being_eq, :output_string_eq
end
RSpec::Matchers.alias_matcher :an_output_string_being_eq, :output_string_eq
Loading

0 comments on commit b7dc0d7

Please sign in to comment.