123456789_123456789_123456789_123456789_123456789_

Class: RSpec::Matchers::BuiltIn::Output Private

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, BaseMatcher
Instance Chain:
Inherits: RSpec::Matchers::BuiltIn::BaseMatcher
Defined in: rspec-expectations/lib/rspec/matchers/built_in/output.rb

Overview

Provides the implementation for RSpec::Matchers#output. Not intended to be instantiated directly.

Constant Summary

BaseMatcher - Inherited

UNDEFINED

Class Method Summary

BaseMatcher - Inherited

.matcher_name, .new,
.underscore

Borrowed from ActiveSupport.

Instance Attribute Summary

BaseMatcher - Inherited

#actual,
#diffable?

::RSpec::Matchers are not diffable by default.

#expected, #expects_call_stack_jump?, #matcher_name, #matcher_name=, #rescued_exception,
#supports_block_expectations?

Most matchers are value matchers (i.e. meant to work with ‘expect(value)`) rather than block matchers (i.e. meant to work with `expect { }`), so this defaults to false.

#supports_value_expectations?

BaseMatcher::StringEncodingFormatting - Included

Instance Method Summary

BaseMatcher - Inherited

#actual_formatted,
#description

Generates a description using ::RSpec::Matchers::EnglishPhrasing.

#expected_formatted,
#match_unless_raises

Used to wrap a block of code that will indicate failure by raising one of the named exceptions.

#matches?

Indicates if the match is successful.

BaseMatcher::DefaultFailureMessages - Included

#failure_message

Provides a good generic failure message.

#failure_message_when_negated

Provides a good generic negative failure message.

BaseMatcher::StringEncodingFormatting - Included

#format_encoding

Formats a String’s encoding as a human readable string :nocov:

BaseMatcher::HashFormatting - Included

#improve_hash_formatting

‘{ :a => 5, :b => 2 }.inspect` produces:

::RSpec::Matchers::Composable - Included

#&

Alias for Composable#and.

#===

Delegates to #matches?.

#and

Creates a compound and expectation.

#description_of

Returns the description of the given object in a way that is aware of composed matchers.

#or

Creates a compound or expectation.

#values_match?

This provides a generic way to fuzzy-match an expected value against an actual value.

#|

Alias for Composable#or.

#should_enumerate?

We should enumerate arrays as long as they are not recursive.

#surface_descriptions_in

Transforms the given data structure (typically a hash or array) into a new data structure that, when #inspect is called on it, will provide descriptions of any contained matchers rather than the normal #inspect output.

#unreadable_io?,
#with_matchers_cloned

Historically, a single matcher instance was only checked against a single value.

Instance Attribute Details

#captured?Boolean (readonly, private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/output.rb', line 127

def captured?
  @actual.length > 0
end

#diffable?Boolean (readonly)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/output.rb', line 107

def diffable?
  true
end

#supports_block_expectations?True (readonly)

Indicates this matcher matches against a block.

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/output.rb', line 114

def supports_block_expectations?
  true
end

#supports_value_expectations?False (readonly)

Indicates this matcher matches against a block only.

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/output.rb', line 121

def supports_value_expectations?
  false
end

Instance Method Details

#actual_output_description (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/output.rb', line 142

def actual_output_description
  return "nothing" unless captured?
  actual_formatted
end

#as_not_tty

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).

Raises:

  • (ArgumentError)
[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/output.rb', line 76

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

#as_tty

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

Raises:

  • (ArgumentError)
[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/output.rb', line 65

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

#descriptionString

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/output.rb', line 97

def description
  if @expected
    "output #{description_of @expected} to #{@stream_capturer.name}"
  else
    "output to #{@stream_capturer.name}"
  end
end

#does_not_match?(block) ⇒ Boolean

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/output.rb', line 24

def does_not_match?(block)
  !matches?(block) && Proc === block
end

#failure_messageString

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/output.rb', line 85

def failure_message
  "expected block to #{description}, but #{positive_failure_reason}"
end

#failure_message_when_negatedString

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/output.rb', line 91

def failure_message_when_negated
  "expected block to not #{description}, but #{negative_failure_reason}"
end

#matches?(block) ⇒ Boolean

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/output.rb', line 17

def matches?(block)
  @block = block
  return false unless Proc === block
  @actual = @stream_capturer.capture(block)
  @expected ? values_match?(@expected, @actual) : captured?
end

#negative_failure_reason (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/output.rb', line 137

def negative_failure_reason
  return "was not a block" unless Proc === @block
  "output #{actual_output_description}"
end

#positive_failure_reason (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/output.rb', line 131

def positive_failure_reason
  return "was not a block" unless Proc === @block
  return "output #{actual_output_description}" if @expected
  "did not"
end

#to_stderr

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

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/output.rb', line 39

def to_stderr
  @stream_capturer = CaptureStderr.new
  self
end

#to_stderr_from_any_process

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

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/output.rb', line 57

def to_stderr_from_any_process
  @stream_capturer = CaptureStreamToTempfile.new("stderr", $stderr)
  self
end

#to_stdout

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

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/output.rb', line 31

def to_stdout
  @stream_capturer = CaptureStdout.new
  self
end

#to_stdout_from_any_process

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

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/output.rb', line 48

def to_stdout_from_any_process
  @stream_capturer = CaptureStreamToTempfile.new("stdout", $stdout)
  self
end