class RSpec::Matchers::BuiltIn::Output

@api private Provides the implementation for ‘output`. Not intended to be instantiated directly.

Public Class Methods

# File rspec-expectations/lib/rspec/matchers/built_in/output.rb, line 12
def initialize(expected)
  @expected        = expected
  @actual          = ""
  @block           = nil
  @stream_capturer = NullCapture
end

Public Instance Methods

@api public Tells the matcher to simulate the output stream not being a TTY. Note that that’s the default behaviour if you don’t call ‘as_tty` (since `StringIO` is not a TTY).

# File rspec-expectations/lib/rspec/matchers/built_in/output.rb, line 77
def as_not_tty
  raise ArgumentError, '`as_not_tty` can only be used after `to_stdout` or `to_stderr`' unless @stream_capturer.respond_to?(:as_tty=)

  @stream_capturer.as_tty = false
  self
end

@api public Tells the matcher to simulate the output stream being a TTY. This is useful to test code like ‘puts ’…‘ if $stdout.tty?`.

# File rspec-expectations/lib/rspec/matchers/built_in/output.rb, line 66
def as_tty
  raise ArgumentError, '`as_tty` can only be used after `to_stdout` or `to_stderr`' unless @stream_capturer.respond_to?(:as_tty=)

  @stream_capturer.as_tty = true
  self
end

@api private @return [String]

# File rspec-expectations/lib/rspec/matchers/built_in/output.rb, line 98
def description
  if @expected
    "output #{description_of @expected} to #{@stream_capturer.name}"
  else
    "output to #{@stream_capturer.name}"
  end
end

@api private @return [Boolean]

# File rspec-expectations/lib/rspec/matchers/built_in/output.rb, line 108
def diffable?
  true
end
# File rspec-expectations/lib/rspec/matchers/built_in/output.rb, line 25
def does_not_match?(block)
  !matches?(block)
end

@api private @return [String]

# File rspec-expectations/lib/rspec/matchers/built_in/output.rb, line 86
def failure_message
  "expected block to #{description}, but #{positive_failure_reason}"
end

@api private @return [String]

# File rspec-expectations/lib/rspec/matchers/built_in/output.rb, line 92
def failure_message_when_negated
  "expected block to not #{description}, but #{negative_failure_reason}"
end
# File rspec-expectations/lib/rspec/matchers/built_in/output.rb, line 19
def matches?(block)
  @block = block
  @actual = @stream_capturer.capture(block)
  @expected ? values_match?(@expected, @actual) : captured?
end

@api private Indicates this matcher matches against a block. @return [True]

# File rspec-expectations/lib/rspec/matchers/built_in/output.rb, line 115
def supports_block_expectations?
  true
end

@api private Indicates this matcher matches against a block only. @return [False]

# File rspec-expectations/lib/rspec/matchers/built_in/output.rb, line 122
def supports_value_expectations?
  false
end

@api public Tells the matcher to match against stderr. Works only when the main Ruby process prints to stderr

# File rspec-expectations/lib/rspec/matchers/built_in/output.rb, line 40
def to_stderr
  @stream_capturer = CaptureStderr.new
  self
end

@api public Tells the matcher to match against stderr. Works when subprocesses print to stderr as well. This is significantly (~30x) slower than ‘to_stderr`

# File rspec-expectations/lib/rspec/matchers/built_in/output.rb, line 58
def to_stderr_from_any_process
  @stream_capturer = CaptureStreamToTempfile.new("stderr", $stderr)
  self
end

@api public Tells the matcher to match against stdout. Works only when the main Ruby process prints to stdout

# File rspec-expectations/lib/rspec/matchers/built_in/output.rb, line 32
def to_stdout
  @stream_capturer = CaptureStdout.new
  self
end

@api public Tells the matcher to match against stdout. Works when subprocesses print to stdout as well. This is significantly (~30x) slower than ‘to_stdout`

# File rspec-expectations/lib/rspec/matchers/built_in/output.rb, line 49
def to_stdout_from_any_process
  @stream_capturer = CaptureStreamToTempfile.new("stdout", $stdout)
  self
end