class Cog::SpecHelpers::Matchers::MatchMaker

Within {Matchers#match_maker} blocks, self is set to an instance of this class

Attributes

error[R]

A list of lines read from STDERR after executing the Invocation

lines[R]

A list of lines read from STDOUT after executing the Invocation

Public Instance Methods

before(&block) click to toggle source

Define a block which runs before the Invocation.

This is not required, but can be used to save context that is used the in post invocation test.

@yield called before the invocation. Save context as instance variables @return [nil]

# File lib/cog/spec_helpers/matchers/match_maker.rb, line 44
def before(&block)
  @before_block = block
  nil
end
failure_message() click to toggle source

@api developer @return [String] positive interpretation of the {#message} block result

# File lib/cog/spec_helpers/matchers/match_maker.rb, line 75
def failure_message
  _failure_message '\1'
end
failure_message_when_negated() click to toggle source

@api developer @return [String] negative interpretation of the {#message} block result

# File lib/cog/spec_helpers/matchers/match_maker.rb, line 81
def failure_message_when_negated
  _failure_message '\2'
end
matches?(invocation) click to toggle source

@api developer @param invocation [Invocation] cog executable and arguments bundled up @return [Boolean] result of the {#test} block

# File lib/cog/spec_helpers/matchers/match_maker.rb, line 63
def matches?(invocation)
  @invocation = invocation
  instance_eval &@before_block unless @before_block.nil?
  @invocation.exec do |input, output, error|
    @lines = output.readlines
    @error = error.readlines
  end
  instance_eval &@test_block
end
message(&block) click to toggle source

Define a block which runs after a test fails and should return a failure message template.

The template is used for both positive and negative failures. Substrings which look like this "[positive|negative]“ will be replaced with the appropriate section.

@example

message { "to [show|not show] the default help text" }

would read “expected cog to show the default help text” for a positive failure and “expected cog to not show the default help text” for a negative failure. The “expected cog” part is inserted automatically.

@yield called after the invocation. {#lines} and {#error} can be used @return [nil]

# File lib/cog/spec_helpers/matchers/match_maker.rb, line 32
def message(&block)
  @msg_block = block
  nil
end
test(&block) click to toggle source

Define the test which runs after the Invocation

This can make use of instance variables set during before.

@yield called after the invocation @return [nil]

# File lib/cog/spec_helpers/matchers/match_maker.rb, line 55
def test(&block)
  @test_block = block
  nil
end
trace() click to toggle source

@api developer @return [String] STDOUT and STDERR

# File lib/cog/spec_helpers/matchers/match_maker.rb, line 87
def trace
  "STDOUT:\n#{@lines.join "\n"}\nSTDERR:\n#{@error.join "\n"}"
end

Private Instance Methods

_failure_message(repl) click to toggle source
# File lib/cog/spec_helpers/matchers/match_maker.rb, line 93
def _failure_message(repl)
  msg = instance_eval &@msg_block
  msg = msg.gsub /\[([^\|\]]*)(?:\|([^\]]*)\])?/, repl
  "expected #{@invocation} #{msg}\n#{trace}"
end