Class: Cog::SpecHelpers::Matchers::MatchMaker

Inherits:
Object
  • Object
show all
Defined in:
lib/cog/spec_helpers/matchers/match_maker.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorObject (readonly)

A list of lines read from STDERR after executing the Invocation



13
14
15
# File 'lib/cog/spec_helpers/matchers/match_maker.rb', line 13

def error
  @error
end

#linesObject (readonly)

A list of lines read from STDOUT after executing the Invocation



10
11
12
# File 'lib/cog/spec_helpers/matchers/match_maker.rb', line 10

def lines
  @lines
end

Instance Method Details

#before { ... } ⇒ nil

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.

Yields:

  • called before the invocation. Save context as instance variables

Returns:

  • (nil)


44
45
46
47
# File 'lib/cog/spec_helpers/matchers/match_maker.rb', line 44

def before(&block)
  @before_block = block
  nil
end

#failure_messageString

Returns positive interpretation of the #message block result.

Returns:



75
76
77
# File 'lib/cog/spec_helpers/matchers/match_maker.rb', line 75

def failure_message
  _failure_message '\1'
end

#failure_message_when_negatedString

Returns negative interpretation of the #message block result.

Returns:



81
82
83
# File 'lib/cog/spec_helpers/matchers/match_maker.rb', line 81

def failure_message_when_negated
  _failure_message '\2'
end

#matches?(invocation) ⇒ Boolean

Returns result of the #test block.

Parameters:

  • invocation (Invocation)

    cog executable and arguments bundled up

Returns:

  • (Boolean)

    result of the #test block



63
64
65
66
67
68
69
70
71
# 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 { ... } ⇒ nil

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.

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.

Examples:

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

Yields:

  • called after the invocation. #lines and #error can be used

Returns:

  • (nil)


32
33
34
35
# File 'lib/cog/spec_helpers/matchers/match_maker.rb', line 32

def message(&block)
  @msg_block = block
  nil
end

#test { ... } ⇒ nil

Define the test which runs after the Invocation

This can make use of instance variables set during #before.

Yields:

  • called after the invocation

Returns:

  • (nil)


55
56
57
58
# File 'lib/cog/spec_helpers/matchers/match_maker.rb', line 55

def test(&block)
  @test_block = block
  nil
end

#traceString

Returns STDOUT and STDERR.

Returns:

  • (String)

    STDOUT and STDERR



87
88
89
# File 'lib/cog/spec_helpers/matchers/match_maker.rb', line 87

def trace
  "STDOUT:\n#{@lines.join "\n"}\nSTDERR:\n#{@error.join "\n"}"
end