Class: Crspec::Matchers::OutputMatcher

Inherits:
BaseMatcher show all
Defined in:
lib/crspec/matchers.rb

Instance Attribute Summary

Attributes inherited from BaseMatcher

#actual, #expected

Instance Method Summary collapse

Methods inherited from BaseMatcher

#and, #failure_message_when_negated, #or

Constructor Details

#initialize(expected = nil) ⇒ OutputMatcher

Returns a new instance of OutputMatcher.



586
587
588
589
# File 'lib/crspec/matchers.rb', line 586

def initialize(expected = nil)
  super
  @stream = :stdout
end

Instance Method Details

#failure_messageObject



614
615
616
# File 'lib/crspec/matchers.rb', line 614

def failure_message
  "Expected block to output #{@expected ? @expected.inspect : "something"} to #{@stream}, got #{@captured.inspect}"
end

#matches?(block) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


601
602
603
604
605
606
607
608
609
610
611
612
# File 'lib/crspec/matchers.rb', line 601

def matches?(block)
  raise ArgumentError, "output matcher requires a block" unless block.respond_to?(:call)

  @captured = capture(block)
  if @expected.nil?
    !@captured.empty?
  elsif @expected.is_a?(Regexp)
    @expected.match?(@captured)
  else
    @captured.include?(@expected)
  end
end

#to_stderrObject



596
597
598
599
# File 'lib/crspec/matchers.rb', line 596

def to_stderr
  @stream = :stderr
  self
end

#to_stdoutObject



591
592
593
594
# File 'lib/crspec/matchers.rb', line 591

def to_stdout
  @stream = :stdout
  self
end