Module: Expresenter::Base

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

Overview

Common collection of methods for Result’s classes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#actual#object_id (readonly)

Returned value by the challenged subject.

Returns:

  • (#object_id)

    Returned value by the challenged subject.



7
8
9
# File 'lib/expresenter/base.rb', line 7

def actual
  @actual
end

#errorException? (readonly)

Returns Any possible raised exception.

Returns:

  • (Exception, nil)

    Any possible raised exception.



10
11
12
# File 'lib/expresenter/base.rb', line 10

def error
  @error
end

#expected#object_id (readonly)

Returns The expected value.

Returns:

  • (#object_id)

    The expected value.



13
14
15
# File 'lib/expresenter/base.rb', line 13

def expected
  @expected
end

#got#object_id (readonly)

Returns The result of the boolean comparison between the actual value and the expected value through the matcher.

Returns:

  • (#object_id)

    The result of the boolean comparison between the actual value and the expected value through the matcher.



17
18
19
# File 'lib/expresenter/base.rb', line 17

def got
  @got
end

#level:MUST, ... (readonly)

Returns The requirement level of the expectation.

Returns:

  • (:MUST, :SHOULD, :MAY)

    The requirement level of the expectation.



23
24
25
# File 'lib/expresenter/base.rb', line 23

def level
  @level
end

#matcher#object_id (readonly)

Returns The matcher.

Returns:

  • (#object_id)

    The matcher.



20
21
22
# File 'lib/expresenter/base.rb', line 20

def matcher
  @matcher
end

Instance Method Details

#colored_charString

Express the result with one colored char.

Returns:

  • (String)

    The colored char that identify the result.



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

def colored_char
  color(char)
end

#colored_stringString

The colored string representation of the result.

Returns:

  • (String)

    A string representing the result.



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

def colored_string
  color(to_s)
end

#definitionString

The readable definition.

Returns:

  • (String)

    A readable string of the definition.



104
105
106
# File 'lib/expresenter/base.rb', line 104

def definition
  [matcher, expected&.inspect].compact.join(" ")
end

#error?Boolean

The state of error.

Returns:

  • (Boolean)

    The test raised an error?



68
69
70
# File 'lib/expresenter/base.rb', line 68

def error?
  !error.nil?
end

#initialize(actual:, error:, expected:, got:, negate:, valid:, matcher:, level:) ⇒ Object

Common initialize method.

Parameters:

  • actual (#object_id)

    Returned value by the challenged subject.

  • error (Exception, nil)

    Any possible raised exception.

  • expected (#object_id)

    The expected value.

  • got (Boolean, nil)

    The result of the boolean comparison between the actual value and the expected value through the matcher.

  • negate (Boolean)

    Evaluated to a negative assertion?

  • valid (Boolean)

    Report if the test was true or false?

  • matcher (Symbol)

    The matcher.

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

    The requirement level.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/expresenter/base.rb', line 36

def initialize(actual:, error:, expected:, got:, negate:, valid:,
               matcher:, level:)

  @actual   = actual
  @error    = error
  @expected = expected
  @got      = got
  @negate   = negate
  @valid    = valid
  @matcher  = matcher
  @level    = level

  super(to_s) if failed?
end

#inspectString

A string containing a human-readable representation of the result.

Returns:

  • (String)

    The human-readable representation of the result.



90
91
92
93
94
95
96
97
98
99
# File 'lib/expresenter/base.rb', line 90

def inspect
  "#{self.class}(actual: #{actual.inspect}, "     \
                "error: #{error.inspect}, "       \
                "expected: #{expected.inspect}, " \
                "got: #{got.inspect}, "           \
                "matcher: #{matcher.inspect}, "   \
                "negate: #{negate?.inspect}, "    \
                "level: #{level.inspect}, "       \
                "valid: #{valid?.inspect})"       \
end

#maybe_negateString

The negation, if any.

Returns:

  • (String)

    The negation, or an empty string.



111
112
113
# File 'lib/expresenter/base.rb', line 111

def maybe_negate
  negate? ? " not" : ""
end

#negate?Boolean

The value of the negate instance variable.

Returns:

  • (Boolean)

    Evaluated to a negative assertion?



61
62
63
# File 'lib/expresenter/base.rb', line 61

def negate?
  @negate
end

#passed?Boolean

Did the test pass?

Returns:

  • (Boolean)

    The spec passed or failed?



54
55
56
# File 'lib/expresenter/base.rb', line 54

def passed?
  !failed?
end

#success?Boolean

The state of success.

Returns:

  • (Boolean)

    The test was a success?



75
76
77
# File 'lib/expresenter/base.rb', line 75

def success?
  got.equal?(true)
end

#summaryString

The summary of the result.

Returns:

  • (String)

    A string representing the summary of the result.



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/expresenter/base.rb', line 118

def summary
  if error?
    error.message
  elsif actual.is_a?(::Exception)
    actual.message
  elsif actual == expected
    "expected#{maybe_negate} to #{definition}"
  else
    "expected #{actual.inspect}#{maybe_negate} to #{definition}"
  end
end

#titreString

Titre for the result.

Returns:

  • (String)

    A string representing the titre.



154
155
156
157
158
159
160
# File 'lib/expresenter/base.rb', line 154

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

#to_sString

The representation of the result.

Returns:

  • (String)

    A string representing the result.



147
148
149
# File 'lib/expresenter/base.rb', line 147

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

#valid?Boolean

The value of the boolean comparison between the actual value and the expected value.

Returns:

  • (Boolean)

    The test was true or false?



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

def valid?
  @valid
end