Exception: Assay::Assertion

Inherits:
Exception
  • Object
show all
Defined in:
lib/assay/assertion.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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, *arguments, &block) ⇒ Assertion

Returns a new instance of Assertion.



79
80
81
82
83
84
85
86
87
88
# File 'lib/assay/assertion.rb', line 79

def initialize(message=nil, *arguments, &block)
  message ? super(message % arguments) : super()

  @arguments = arguments
  @block     = block

  #set_arguments options[:arguments] if options[:arguments]
  #set_negative  options[:negated]   if options[:negated]
  #set_backtrace options[:backtrace] if options[:backtrace]
end

Class Method Details

.assert(*args, &blk) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/assay/assertion.rb', line 25

def self.assert(*args, &blk)
  opts = Hash === args.last ? args.pop : {}

  backtrace = opts[:backtrace] || caller
  message   = opts[:message]

  err = new(message, *args, &blk)
  err.set_backtrace(backtrace)
  err.assert

  #chk  = check(*args, &blk)
  #msg  = fail_message(*args, &blk)
  #if !chk
  #  msg = opts[:message]
  #  btr = opts[:backtrace] || caller
  #  err = new(msg, *args)
  #  err.set_backtrace(btr)
  #  fail err
  #end
end

.assertable_methodObject



20
21
22
# File 'lib/assay/assertion.rb', line 20

def self.assertable_method
  "assert_#{assertion_name}"
end

.fail?(*args, &blk) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/assay/assertion.rb', line 74

def self.fail?(*args, &blk)
  ! pass?(*args, &blk)
end

.pass?(*args, &blk) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


69
70
71
# File 'lib/assay/assertion.rb', line 69

def self.pass?(*args, &blk)
  raise NotImplementedError
end

.refute(*args, &blk) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/assay/assertion.rb', line 47

def self.refute(*args, &blk)
  opts = Hash === args.last ? args.pop : {}

  backtrace = opts[:backtrace] || caller
  message   = opts[:message]

  err = new(message, *args, &blk)
  err.set_backtrace(backtrace)
  err.refute

  #opts = Hash === args.last ? args.pop : {}
  #chk  = check!(*args, &blk)
  ##msg  = fail_message!(*args, &blk)
  #if !chk
  #  msg = opts[:message]
  #  btr = opts[:backtrace] || caller
  #  err = new(msg, :backtrace=>btr, :arguments=>args)
  #  fail err
  #end
end

.to_matcher(*args, &blk) ⇒ Object

Returns Matcher for the failure class.



15
16
17
# File 'lib/assay/assertion.rb', line 15

def self.to_matcher(*args, &blk)
  Matcher.new(self, *args, &blk)
end

Instance Method Details

#assertObject

Raises:

  • (self)


109
110
111
112
# File 'lib/assay/assertion.rb', line 109

def assert
  #@negative = false
  raise self unless pass?
end

#assertion?Boolean

Failure is always a type of assertion.

This method allows Assay’s classes to work in any test framework that supports this interface.

Returns:

  • (Boolean)


94
95
96
# File 'lib/assay/assertion.rb', line 94

def assertion?
  true  # @assertion = true if @assertion.nil?
end

#fail?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/assay/assertion.rb', line 104

def fail?
  not pass?
end

#negative?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/assay/assertion.rb', line 121

def negative?
  @negative
end

#pass?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/assay/assertion.rb', line 99

def pass?
  self.class.pass?(*@arguments, &@block) #^ @negative
end

#refuteObject

Raises:

  • (self)


115
116
117
118
# File 'lib/assay/assertion.rb', line 115

def refute
  #@negative = true
  raise self unless fail?
end

#set_arguments(arguments) ⇒ Object

Set arguments used to make assertion.



132
133
134
135
# File 'lib/assay/assertion.rb', line 132

def set_arguments(arguments)
  @arguments = arguments
  #@block    = block
end

#set_negative(negative) ⇒ Object

Set whether this failure was the inverse of it’s normal meaning. For example, ‘!=` rather than `==`.



127
128
129
# File 'lib/assay/assertion.rb', line 127

def set_negative(negative)
  @negative = !!negative
end

#to_sObject



138
139
140
141
142
143
144
# File 'lib/assay/assertion.rb', line 138

def to_s
  if @negative
    "NOT " + super()
  else
    super()
  end
end