Module: Assay::Assertable

Included in:
Assertion
Defined in:
lib/assay/assertable.rb

Constant Summary collapse

SIZE_LIMIT =

When displaying errors, use this as a rule of thumb for determining when the inspected object will be too big for a single line message.

13

Instance Method Summary collapse

Instance Method Details

#[](*criteria, &block) ⇒ Object

Alias for ‘#assertor`.



56
57
58
# File 'lib/assay/assertable.rb', line 56

def [](*criteria, &block)
  assertor(*criteria, &block)
end

#assert!(*arguments, &block) ⇒ Object

Test the assertion, raising the exception if failing.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/assay/assertable.rb', line 77

def assert!(*arguments, &block)
  options = (Hash === arguments.last ? arguments.pop : {})

  backtrace = options[:backtrace] || caller
  message   = options[:message]   || assert_message(*arguments, &block)

  pass = pass?(*arguments, &block)

  assert pass, self, message, backtrace

  #if pass?(*arguments, &block)
  #  increment(:pass)
  #else
  #  increment(:fail)
  #  fail self, message, backtrace
  #end
end

#assert_message(*arguments, &block) ⇒ Object



128
129
130
# File 'lib/assay/assertable.rb', line 128

def assert_message(*arguments, &block)
  standard_message(*arguments, &block)
end

#assertive_nameObject

The assertive name is used for the construction of assertive nomenclatures such as ‘assert_equal`.



36
37
38
39
40
41
42
43
44
# File 'lib/assay/assertable.rb', line 36

def assertive_name
  @assertive_name ||= (
    if operator.to_s.end_with?('?')
      operator.to_s.chomp('?').to_sym
    else
      name.split('::').last.chomp('Assay').downcase.to_sym
    end
  )
end

#assertor(*criteria, &block) ⇒ Object

Create an assertor for the assay class, given criteria.



49
50
51
# File 'lib/assay/assertable.rb', line 49

def assertor(*criteria, &block)
  Assertor.new(self, *criteria, &block)
end

#fail?(subject, *criteria, &block) ⇒ Boolean

Check the assertion, return ‘true` if failing, `false` otherwise.

Returns:

  • (Boolean)


70
71
72
# File 'lib/assay/assertable.rb', line 70

def fail?(subject, *criteria, &block)
  ! pass?(subject, *criteria, &block)
end

#operatorObject

If the assertion coresponds to a regular method, particular a symbolic operator (hence the name of this method) then it should be specified via this interface. Otherwise, it should be given a fitting “make believe” method name and specified here. If not overridden it will be assumed to be the same as the ‘assertion_name` appended by `?`.



28
29
30
# File 'lib/assay/assertable.rb', line 28

def operator
  @operator ||= (name.split('::').last.chomp('Assay').downcase + '?').to_sym
end

#pass?(subject, *criteria, &block) ⇒ Boolean

Check the assertion, return ‘true` if passing, `false` otherwise.

Returns:

  • (Boolean)


63
64
65
# File 'lib/assay/assertable.rb', line 63

def pass?(subject, *criteria, &block)
  subject 
end

#refute!(*arguments, &block) ⇒ Object

Test the refutation of the assertion.

Test the inverse assertion, raising the exception if not failing.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/assay/assertable.rb', line 100

def refute!(*arguments, &block)
  options = (Hash === arguments.last ? arguments.pop : {})

  backtrace = options[:backtrace] || caller
  message   = options[:message]   || refute_message(*arguments, &block)

  fail = fail?(*arguments, &block)

  assert fail, self, message, backtrace

  #if fail?(*arguments, &block)
  #  increment(:pass)
  #else
  #  increment(:fail)
  #  fail self, message, backtrace
  #end
end

#refute_message(*arguments, &block) ⇒ Object



135
136
137
# File 'lib/assay/assertable.rb', line 135

def refute_message(*arguments, &block)
  "! " + assert_message(*arguments, &block)
end