Exception: Expresenter::Fail

Inherits:
StandardError
  • Object
show all
Includes:
Common
Defined in:
lib/expresenter/fail.rb

Overview

Class responsible for handling and reporting failed test expectations.

The Fail class represents test failures and errors, inheriting from StandardError to support exception handling. It distinguishes between two types of failures:

  • Regular failures: When an assertion fails but no exception occurred

  • Errors: When an unexpected exception was raised during the test

Examples:

Handling a regular test failure

begin
  Expresenter.call(false).with(
    actual: 42,
    definition: 'eq 43',
    error: nil,
    got: false,
    negate: false,
    level: :MUST
  )
rescue Expresenter::Fail => e
  e.failure?     # => true
  e.error?       # => false
  e.to_sym      # => :failure
  e.char        # => "F"
  e.emoji       # => "āŒ"
  e.to_s        # => "Failure: expected 42 to eq 43."
end

Handling a test error

begin
  error = StandardError.new("Unexpected error")
  Expresenter.call(false).with(
    actual: nil,
    definition: 'be_valid',
    error: error,
    got: false,
    negate: false,
    level: :MUST
  )
rescue Expresenter::Fail => e
  e.failure?     # => false
  e.error?       # => true
  e.to_sym      # => :error
  e.char        # => "E"
  e.emoji       # => "šŸ’„"
  e.to_s        # => "StandardError: Unexpected error."
end

Constant Summary collapse

FAILURE_CHAR =

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.

Single character indicator for test failures.

"F"
FAILURE_EMOJI =

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.

Emoji indicator for test failures.

"āŒ"
ERROR_CHAR =

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.

Single character indicator for test errors.

"E"
ERROR_EMOJI =

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.

Emoji indicator for test errors.

"šŸ’„"

Constants included from Common

Common::SPACE

Instance Attribute Summary

Attributes included from Common

#actual, #definition, #error, #got, #level

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common

#colored_char, #colored_string, #error?, #negate?, #passed?, #success?, #summary, #titre, #to_s

Constructor Details

#initialize(actual:, definition:, error:, got:, negate:, level:) ⇒ Fail

Initializes a new Fail instance.

Examples:

Creating a failure result

Fail.new(
  actual: 42,
  definition: 'eq 43',
  error: nil,
  got: false,
  negate: false,
  level: :MUST
)

Parameters:

  • actual (#object_id)

    The actual value returned by the test

  • definition (String)

    Human-readable description of the expectation

  • error (Exception, nil)

    Any exception that was raised during the test

  • got (Boolean, nil)

    Result of comparing actual vs expected values

  • negate (Boolean)

    Whether this is a negative assertion

  • level (:MUST, :SHOULD, :MAY)

    The requirement level of the test



105
106
107
108
109
110
111
112
113
114
# File 'lib/expresenter/fail.rb', line 105

def initialize(actual:, definition:, error:, got:, negate:, level:)
  @actual     = actual
  @definition = definition
  @error      = error
  @got        = got
  @negate     = negate
  @level      = level

  super(to_s)
end

Class Method Details

.with(**details) ⇒ Object

Creates and raises a new Fail instance with the given details.

Examples:

Expresenter::Fail.with(
  actual: 42,
  definition: 'eq 43',
  error: nil,
  got: false,
  negate: false,
  level: :MUST
) # raises Expresenter::Fail

Parameters:

  • details (Hash)

    Test result details (see #initialize for parameters)

Raises:

  • (Fail)

    Always raises a Fail exception with the provided details



83
84
85
# File 'lib/expresenter/fail.rb', line 83

def self.with(**details)
  raise new(**details)
end

Instance Method Details

#charString

Returns a single character representing the failure type.

Returns:

  • (String)

    ā€œFā€ for failures, ā€œEā€ for errors



158
159
160
161
162
163
164
# File 'lib/expresenter/fail.rb', line 158

def char
  if failure?
    FAILURE_CHAR
  else
    ERROR_CHAR
  end
end

#emojiString

Returns an emoji representing the failure type.

Returns:

  • (String)

    ā€œāŒā€ for failures, ā€œšŸ’„ā€ for errors



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

def emoji
  if failure?
    FAILURE_EMOJI
  else
    ERROR_EMOJI
  end
end

#failed?true

Always returns true since this class represents failed tests.

Returns:

  • (true)

    Always returns true



119
120
121
# File 'lib/expresenter/fail.rb', line 119

def failed?
  true
end

#failure?Boolean

Indicates if this is a regular failure (not an error).

Returns:

  • (Boolean)

    true if no error was captured, false otherwise



126
127
128
# File 'lib/expresenter/fail.rb', line 126

def failure?
  !error?
end

#info?false

Fail results are never informational.

Returns:

  • (false)

    Always returns false



133
134
135
# File 'lib/expresenter/fail.rb', line 133

def info?
  false
end

#to_sym:failure, :error

Returns the symbolic representation of the failure type.

Returns:

  • (:failure, :error)

    :failure for regular failures, :error when an exception occurred



147
148
149
150
151
152
153
# File 'lib/expresenter/fail.rb', line 147

def to_sym
  if failure?
    :failure
  else
    :error
  end
end

#warning?false

Fail results are never warnings.

Returns:

  • (false)

    Always returns false



140
141
142
# File 'lib/expresenter/fail.rb', line 140

def warning?
  false
end