Class: Wukong::SpecHelpers::IntegrationTestMatcher
- Inherits:
-
Object
- Object
- Wukong::SpecHelpers::IntegrationTestMatcher
- Defined in:
- lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb
Overview
A class for running commands and capturing their STDOUT, STDERR, and exit code. This class is designed to work with the matchers defined in IntegrationTestMatchers.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#expectations ⇒ Object
An array of expectations about the output of the runner.
-
#failed_expectation ⇒ Object
The expectation which caused failure.
-
#runner ⇒ Object
The runner used to run the actual commands.
Instance Method Summary collapse
-
#failure_message ⇒ Object
:nodoc:.
-
#formatted_command ⇒ Object
:nodoc:.
-
#formatted_env ⇒ Object
:nodoc:.
-
#formatted_error_output ⇒ Object
:nodoc:.
-
#formatted_output ⇒ Object
:nodoc:.
-
#initialize(*expectations) ⇒ IntegrationTestMatcher
constructor
Create a matcher on the given expectations.
-
#match_function(expectation) ⇒ Object
:nodoc:.
-
#match_type ⇒ Object
:nodoc:.
-
#matches?(runner) ⇒ true, false
Return whether or not the given command's output matches expectations.
-
#negative_failure_message ⇒ Object
:nodoc:.
Constructor Details
#initialize(*expectations) ⇒ IntegrationTestMatcher
Create a matcher on the given expectations. Each expectation can be either a String or a Regexp. Strings will be tested for inclusion in the output, Regexps will be tested for a match against the output.
44 45 46 |
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 44 def initialize *expectations self.expectations = expectations end |
Instance Attribute Details
#expectations ⇒ Object
An array of expectations about the output of the runner.
13 14 15 |
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 13 def expectations @expectations end |
#failed_expectation ⇒ Object
The expectation which caused failure.
16 17 18 |
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 16 def failed_expectation @failed_expectation end |
#runner ⇒ Object
The runner used to run the actual commands.
10 11 12 |
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 10 def runner @runner end |
Instance Method Details
#failure_message ⇒ Object
:nodoc:
49 50 51 |
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 49 def "From within #{runner.cwd} ran\n\n#{formatted_env}\n#{formatted_command}\n\nand expected #{output_description}\n\n#{formatted_output}\n\nto #{match_type}\n\n #{failed_expectation}#{formatted_error_output}" end |
#formatted_command ⇒ Object
:nodoc:
69 70 71 |
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 69 def formatted_command " $ #{runner.cmd}" end |
#formatted_env ⇒ Object
:nodoc:
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 74 def formatted_env [' {'].tap do |lines| runner.env.each_pair do |key, value| if key =~ /^(BUNDLE_GEMFILE|PATH|RUBYLIB)$/ lines << " #{key} => #{value}," end end lines << ' }' end.join("\n") end |
#formatted_error_output ⇒ Object
:nodoc:
64 65 66 |
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 64 def formatted_error_output output_description.to_s =~ /stderr/ ? "\n\nSTDOUT was\n\n#{runner.stdout}" : "\n\nSTDERR was\n\n#{runner.stderr}" end |
#formatted_output ⇒ Object
:nodoc:
59 60 61 |
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 59 def formatted_output output.split("\n").map { |line| ' ' + line }.join("\n") end |
#match_function(expectation) ⇒ Object
:nodoc:
86 87 88 |
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 86 def match_function expectation expectation.is_a?(Regexp) ? :match : :include? end |
#match_type ⇒ Object
:nodoc:
91 92 93 |
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 91 def match_type failed_expectation.is_a?(Regexp) ? 'match' : 'include' end |
#matches?(runner) ⇒ true, false
Return whether or not the given command's output matches expectations.
If an expectation failes to match, the failed_expectation
attribute will be set accordingly.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 26 def matches?(runner) self.runner = runner runner.run! expectations.each do |expectation| unless output.send(match_function(expectation), expectation) self.failed_expectation = expectation return false end end true end |
#negative_failure_message ⇒ Object
:nodoc:
54 55 56 |
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 54 def "Expected #{output_description} of #{runner.cmd}\n\n#{output}\n\nto NOT #{match_type}\n\n#{self.failed_expectation}." end |