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"


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

def actual
  @actual
end

#definition ⇒ String (readonly)

The human-readable description of the expectation.

Examples:

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


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

def definition
  @definition
end

#error ⇒ Exception? (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


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


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

See Also:



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

def level
  @level
end

Instance Method Details

#colored_char ⇒ String

Returns the result indicator with ANSI color codes.

Examples:

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


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

def colored_char
  color(char)
end

#colored_string ⇒ String

Returns the full result message with ANSI color codes.

Examples:

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


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


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


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


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


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

def success?
  got.equal?(true)
end

#summary ⇒ String

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"


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

#titre ⇒ String

Returns the title for the result type.

Examples:

Normal result

result.titre # => "Success"

Error result

result.titre # => "StandardError"


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_s ⇒ String

Returns the complete result message.

Examples:

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


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

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