Class: Peck::Should
Constant Summary
collapse
- KILL_METHODS_RE =
Kills ==, ===, =~, eql?, equal?, frozen?, instance_of?, is_a?, kind_of?, nil?, respond_to?, tainted?
/\?|^\W+$/
- PREDICATE_METHOD_RE =
/\w[^?]\z/
Instance Method Summary
collapse
Constructor Details
#initialize(this) ⇒ Should
Returns a new instance of Should.
12
13
14
15
16
|
# File 'lib/peck/expectations.rb', line 12
def initialize(this)
@this = this
@negated = false
Thread.current['peck-spec'].expectations << self
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
99
100
101
102
103
104
105
106
107
|
# File 'lib/peck/expectations.rb', line 99
def method_missing(name, *args, &block)
name = "#{name}?" if name.to_s =~ PREDICATE_METHOD_RE
desc = @negated ? "not " : ""
desc << @this.inspect << "." << name.to_s
desc << "(" << args.map{|x|x.inspect}.join(", ") << ") failed"
satisfy(desc) { |x| x.__send__(name, *args, &block) }
end
|
Instance Method Details
#be(*args, &block) ⇒ Object
23
24
25
26
27
28
29
30
|
# File 'lib/peck/expectations.rb', line 23
def be(*args, &block)
if args.empty?
self
else
block = args.shift unless block_given?
satisfy(*args, &block)
end
end
|
#change(expression, change = nil) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/peck/expectations.rb', line 46
def change(expression, change=nil)
if @negated
description = "#{expression} changed"
description << " by #{actual}" if change
else
description = "#{expression} didn't change"
description << " by #{change}" if change
end
satisfy(description) do |x|
difference = change || 1
binding = x.send(:binding)
before = eval(expression, binding)
result = @this.call
after = eval(expression, binding)
after == before + difference
end
end
|
18
19
20
21
|
# File 'lib/peck/expectations.rb', line 18
def not
@negated = !@negated
self
end
|
#raise(exception_class = nil) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/peck/expectations.rb', line 67
def raise(exception_class=nil)
exception = nil
begin
@this.call
rescue Exception => e
exception = e
end
description = if exception_class
if @negated
"expected `#{exception_class}' to not be raised"
else
"expected `#{exception_class}' to be raised, but got a `#{exception.class}'"
end
else
if @negated
"expected nothing to be raised, but got `#{exception.inspect}'"
else
"expected an exception, but nothing was raised"
end
end
satisfy(description) do
if exception_class
exception.kind_of?(exception_class)
else
!exception.nil?
end
end
end
|
#satisfy(*args, &block) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/peck/expectations.rb', line 32
def satisfy(*args, &block)
if args.size == 1 && String === args.first
description = args.shift
else
description = ""
end
result = yield(@this, *args)
unless @negated ^ result
Kernel.raise Peck::Error.new(:failed, description)
end
result
end
|