Class: Pork::Expect

Inherits:
BasicObject
Defined in:
lib/pork/expect.rb

Instance Method Summary collapse

Constructor Details

#initialize(stat, object = nil, message = nil, message_lazy = nil, &checker) ⇒ Expect

Returns a new instance of Expect.



7
8
9
10
11
# File 'lib/pork/expect.rb', line 7

def initialize stat, object=nil, message=nil, message_lazy=nil, &checker
  @stat, @object, @negate = stat, object, false
  @message, @message_lazy = message, message_lazy
  satisfy(&checker) if checker
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(msg, *args, &block) ⇒ Object



13
14
15
16
17
# File 'lib/pork/expect.rb', line 13

def method_missing msg, *args, &block
  satisfy(nil, Inspect.with(@object, msg, args, @negate)) do
    @object.public_send(msg, *args, &block)
  end
end

Instance Method Details

#approx(rhs, precision = 10) ⇒ Object



43
44
45
# File 'lib/pork/expect.rb', line 43

def approx rhs, precision=10
  round(precision) == rhs.round(precision)
end

#eq(rhs) ⇒ Object



37
# File 'lib/pork/expect.rb', line 37

def eq  rhs; self == rhs; end

#gt(rhs) ⇒ Object



39
# File 'lib/pork/expect.rb', line 39

def gt  rhs; self >  rhs; end

#gte(rhs) ⇒ Object



41
# File 'lib/pork/expect.rb', line 41

def gte rhs; self >= rhs; end

#lt(rhs) ⇒ Object



38
# File 'lib/pork/expect.rb', line 38

def lt  rhs; self <  rhs; end

#lte(rhs) ⇒ Object



40
# File 'lib/pork/expect.rb', line 40

def lte rhs; self <= rhs; end

#not(&checker) ⇒ Object



31
32
33
34
35
# File 'lib/pork/expect.rb', line 31

def not &checker
  @negate = !@negate
  satisfy(&checker) if checker
  self
end

#raise(exception = ::RuntimeError) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/pork/expect.rb', line 47

def raise exception=::RuntimeError
  satisfy("#{__not__}raising #{exception}") do
    begin
      if ::Kernel.block_given? then yield else @object.call end
    rescue exception => e
      e
    else
      false
    end
  end
end

#satisfy(desc = @object, desc_lazy = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pork/expect.rb', line 19

def satisfy desc=@object, desc_lazy=nil
  result = yield(@object)
  if !!result == @negate
    d =     desc_lazy &&     desc_lazy.call || desc
    m = @message_lazy && @message_lazy.call || @message
    ::Kernel.raise Failure.new("Expect #{d}\n#{m}".chomp)
  else
    @stat.incr_assertions
  end
  result
end

#throw(msg) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/pork/expect.rb', line 59

def throw msg
  satisfy("#{__not__}throwing #{msg}") do
    flag = true
    data = ::Kernel.catch(msg) do
      if ::Kernel.block_given? then yield else @object.call end
      flag = false
    end
    flag && [msg, data]
  end
end