Class: Peck::Should
Defined Under Namespace
Classes: Proxy, Specification
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.
32
33
34
35
36
|
# File 'lib/peck/expectations.rb', line 32
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
119
120
121
122
123
124
125
126
127
|
# File 'lib/peck/expectations.rb', line 119
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
43
44
45
46
47
48
49
50
|
# File 'lib/peck/expectations.rb', line 43
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/peck/expectations.rb', line 66
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
|
38
39
40
41
|
# File 'lib/peck/expectations.rb', line 38
def not
@negated = !@negated
self
end
|
#raise(exception_class = nil) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/peck/expectations.rb', line 87
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
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/peck/expectations.rb', line 52
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
|