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.
35
36
37
38
39
|
# File 'lib/peck/expectations.rb', line 35
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
136
137
138
139
140
141
142
143
144
|
# File 'lib/peck/expectations.rb', line 136
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
46
47
48
49
50
51
52
53
|
# File 'lib/peck/expectations.rb', line 46
def be(*args, &block)
if args.empty?
self
else
block = args.shift unless block_given?
satisfy(*args, &block)
end
end
|
#change(*expected) ⇒ Object
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
97
98
99
100
101
102
|
# File 'lib/peck/expectations.rb', line 69
def change(*expected)
block_binding = @this.send(:binding)
before = expected.in_groups_of(2).map do |expression, _|
eval(expression, block_binding)
end
block_result = @this.call
expected.in_groups_of(2).each_with_index do |(expression, change), index|
after = eval(expression, block_binding)
actual = after - before[index]
if @negated
description = "#{expression} changed"
description << " by expected #{change}" if change
description << ", actual change: #{actual}"
else
description = "#{expression} didn't change"
description << " by expected #{change}" if change
description << ", actual change: #{actual}"
end
satisfy(description) do |x|
if change
after == (before[index] + change)
else
before[index] != after
end
end
end
block_result
end
|
41
42
43
44
|
# File 'lib/peck/expectations.rb', line 41
def not
@negated = !@negated
self
end
|
#raise(exception_class = nil) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/peck/expectations.rb', line 104
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
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/peck/expectations.rb', line 55
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
|