Module: Expresenter::Common

Included in:
Fail, Pass
Defined in:
lib/expresenter/common.rb

Overview

Common collection of methods shared between Pass and Fail result classes.

This module provides the core functionality for presenting test results, including:

  • Access to test result data (actual value, definition, error state)

  • Test state queries (passed?, error?, success?)

  • Result formatting and presentation (summary, colored output)

  • Support for requirement levels (MUST/SHOULD/MAY)

Examples:

Using common methods in a test result

result = Expresenter.call(true).new(
  actual: "foo",
  definition: 'eq "foo"',
  error: nil,
  got: true,
  negate: false,
  level: :MUST
)

result.passed?    # => true
result.success?   # => true
result.summary    # => 'expected "foo" to eq "foo"'
result.to_s      # => 'Success: expected "foo" to eq "foo".'

Constant Summary collapse

SPACE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

String constant used for joining message parts.

" "

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#actual#object_id (readonly)

The actual value returned by the test subject.

Examples:

result.actual # => "foo"

Returns:

  • (#object_id)

    The value being tested against the expectation.



38
39
40
# File 'lib/expresenter/common.rb', line 38

def actual
  @actual
end

#definitionString (readonly)

The human-readable description of the expectation.

Examples:

result.definition # => 'eq "foo"'

Returns:

  • (String)

    Description combining the matcher and expected values.



45
46
47
# File 'lib/expresenter/common.rb', line 45

def definition
  @definition
end

#errorException? (readonly)

Any exception that was raised during the test.

Examples:

begin
  raise StandardError, "Oops"
rescue => e
  result = Expresenter.call(false).new(error: e, ...)
  result.error # => #<StandardError: Oops>
end

Returns:

  • (Exception, nil)

    The error object if an exception occurred, nil otherwise.



57
58
59
# File 'lib/expresenter/common.rb', line 57

def error
  @error
end

#got#object_id (readonly)

The boolean result of comparing the actual value against the expectation.

Examples:

result.got # => true

Returns:

  • (#object_id)

    Usually true/false, but can be any object that responds to equal?



64
65
66
# File 'lib/expresenter/common.rb', line 64

def got
  @got
end

#level:MUST, ... (readonly)

The requirement level of the expectation.

Examples:

result.level # => :MUST

Returns:

  • (:MUST, :SHOULD, :MAY)

    The RFC 2119 requirement level.

See Also:



72
73
74
# File 'lib/expresenter/common.rb', line 72

def level
  @level
end

Instance Method Details

#colored_charString

Returns the result indicator with ANSI color codes.

Examples:

result.colored_char # => "\e[32m.\e[0m"

Returns:

  • (String)

    A colored single character indicating the result type.



139
140
141
# File 'lib/expresenter/common.rb', line 139

def colored_char
  color(char)
end

#colored_stringString

Returns the full result message with ANSI color codes.

Examples:

result.colored_string # => "\e[32mSuccess: expected 1 to eq 1.\e[0m"

Returns:

  • (String)

    A colored string with the complete test result.



148
149
150
# File 'lib/expresenter/common.rb', line 148

def colored_string
  color(to_bold_s)
end

#error?Boolean

Checks if an error occurred during the test.

Examples:

begin
  raise "Oops"
rescue => e
  result = Expresenter.call(false).new(error: e, ...)
  result.error? # => true
end

Returns:

  • (Boolean)

    true if an error was captured, false otherwise.



103
104
105
# File 'lib/expresenter/common.rb', line 103

def error?
  !error.nil?
end

#negate?Boolean

Checks if this is a negative assertion.

Examples:

result = Expresenter.call(true).new(negate: true, ...)
result.negate? # => true

Returns:

  • (Boolean)

    true if this is a negative assertion (using not), false otherwise.



89
90
91
# File 'lib/expresenter/common.rb', line 89

def negate?
  @negate
end

#passed?Boolean

Checks if the test passed.

Examples:

result.passed? # => true

Returns:

  • (Boolean)

    true if the test passed, false otherwise.



79
80
81
# File 'lib/expresenter/common.rb', line 79

def passed?
  !failed?
end

#success?Boolean

Checks if the test was successful.

Examples:

result = Expresenter.call(true).new(got: true, ...)
result.success? # => true

Returns:

  • (Boolean)

    true if the got value equals true, false otherwise.



113
114
115
# File 'lib/expresenter/common.rb', line 113

def success?
  got.equal?(true)
end

#summaryString

Generates a human-readable summary of the test result.

Examples:

With regular value

result.summary # => 'expected "foo" to eq "bar"'

With error

result.summary # => "Unexpected error occurred"

Returns:

  • (String)

    A description of what was expected vs what was received.



124
125
126
127
128
129
130
131
132
# File 'lib/expresenter/common.rb', line 124

def summary
  if error?
    error.message
  elsif actual.is_a?(::Exception)
    actual.message
  else
    ["expected", actual.inspect, negation, "to", definition].compact.join(SPACE)
  end
end

#titreString

Returns the title for the result type.

Examples:

Normal result

result.titre # => "Success"

Error result

result.titre # => "StandardError"

Returns:

  • (String)

    Either the error class name or capitalized result type.



168
169
170
171
172
173
174
# File 'lib/expresenter/common.rb', line 168

def titre
  if error?
    error.class.name
  else
    to_sym.to_s.capitalize
  end
end

#to_sString

Returns the complete result message.

Examples:

result.to_s # => "Success: expected 1 to eq 1."

Returns:

  • (String)

    A string containing the result type and summary.



157
158
159
# File 'lib/expresenter/common.rb', line 157

def to_s
  "#{titre}: #{summary}."
end