Class: Probatio::Context
- Inherits:
-
Object
show all
- Includes:
- Helpers, Waiters
- Defined in:
- lib/probatio/assertions.rb,
lib/probatio.rb,
lib/probatio/helpers.rb,
lib/probatio/waiters.rb
Overview
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#__group_name ⇒ Object
-
#__test_name ⇒ Object
-
#assert(*as) ⇒ Object
Jack of all trade assert.
-
#assert_any(*as) ⇒ Object
-
#assert_empty(*as) ⇒ Object
-
#assert_end_with(*as) ⇒ Object
-
#assert_equal(*as) ⇒ Object
-
#assert_error(*as, &block) ⇒ Object
-
#assert_false(*as) ⇒ Object
-
#assert_falsey(*as) ⇒ Object
(also: #assert_falsy, #assert_falseish)
-
#assert_hashy(*as) ⇒ Object
-
#assert_include(*as) ⇒ Object
-
#assert_instance_of(*as) ⇒ Object
(also: #assert_is_a, #assert_class)
-
#assert_match(*as) ⇒ Object
-
#assert_nil(*as) ⇒ Object
Beware: ws.any? returns false when ws == [ false ].
-
#assert_not(*as) ⇒ Object
-
#assert_not_error(*as, &block) ⇒ Object
(also: #assert_no_error, #assert_nothing_raised)
-
#assert_not_match(*as) ⇒ Object
-
#assert_not_nil(*as) ⇒ Object
-
#assert_size(*as) ⇒ Object
(also: #assert_count)
-
#assert_start_with(*as) ⇒ Object
-
#assert_true(*as) ⇒ Object
-
#assert_truthy(*as) ⇒ Object
(also: #assert_trueish)
-
#block ⇒ Object
-
#initialize(group, test) ⇒ Context
constructor
A new instance of Context.
-
#method_missing(name, *args, &block) ⇒ Object
Checks whether its "_assert_something", if that's the case, just flags the assertion as :pending an moves on.
-
#refute(*as) ⇒ Object
Jack of all trade refute.
-
#refute_hashy(*as) ⇒ Object
-
#refute_match(*as) ⇒ Object
-
#run(run_opts) ⇒ Object
Methods included from Waiters
#monow, #wait_until
Methods included from Helpers
#beep, #jruby?, #time, #windows?
Constructor Details
#initialize(group, test) ⇒ Context
Returns a new instance of Context.
711
712
713
714
715
716
717
|
# File 'lib/probatio.rb', line 711
def initialize(group, test)
@__group = group
@__test = test
group.context.each { |k, v| instance_variable_set(k, v) }
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Checks whether its "_assert_something", if that's the case,
just flags the assertion as :pending an moves on
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
# File 'lib/probatio/assertions.rb', line 209
def method_missing(name, *args, &block)
n = name.to_s
if n.start_with?('_assert') && self.respond_to?(n[1..-1])
Probatio.despatch(:assertion_pending, self, @__child)
:pending
else
super
end
end
|
Instance Attribute Details
#__group ⇒ Object
Returns the value of attribute __group.
708
709
710
|
# File 'lib/probatio.rb', line 708
def __group
@__group
end
|
#__test ⇒ Object
Returns the value of attribute __test.
709
710
711
|
# File 'lib/probatio.rb', line 709
def __test
@__test
end
|
Instance Method Details
#__group_name ⇒ Object
727
|
# File 'lib/probatio.rb', line 727
def __group_name; @__group.name; end
|
#__test_name ⇒ Object
726
|
# File 'lib/probatio.rb', line 726
def __test_name; @__test.name; end
|
#assert(*as) ⇒ Object
227
|
# File 'lib/probatio/assertions.rb', line 227
def assert(*as); jack(:assert, as); end
|
#assert_any(*as) ⇒ Object
47
48
49
50
|
# File 'lib/probatio/assertions.rb', line 47
def assert_any(*as)
do_assert(as, 'any') { |a| a.respond_to?(:size) && a.size > 0 }
end
|
#assert_empty(*as) ⇒ Object
52
53
54
55
|
# File 'lib/probatio/assertions.rb', line 52
def assert_empty(*as)
do_assert(as, 'empty') { |a| a.respond_to?(:size) && a.size == 0 }
end
|
#assert_end_with(*as) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/probatio/assertions.rb', line 90
def assert_end_with(*as)
fail ArgumentError.new(
"assert_end_with expects strings, not #{x.inspect}") \
if as.find { |a| ! a.is_a?(String) }
sta = as.inject { |s, a| s.length < a.length ? s : a }
do_assert(as.filter { |a| a != sta }, 'ending with') { |a|
a.end_with?(sta) }
end
|
#assert_equal(*as) ⇒ Object
57
58
59
60
|
# File 'lib/probatio/assertions.rb', line 57
def assert_equal(*as)
do_assert(as, 'equal') { |a| a == as[0] }
end
|
#assert_error(*as, &block) ⇒ Object
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
# File 'lib/probatio/assertions.rb', line 155
def assert_error(*as, &block)
block = block || as.find { |a| a.is_a?(Proc) }
as.delete(block)
fail ArgumentError.new("assert_error expects a block or a proc") \
unless block
err = nil;
begin; block.call; rescue => err; end
s = err ? nil : 'no error raised'
as.each do |a|
s ||=
case a
when String
if err.message != a
msg = err.message.sub(/\s*Did you mean\?.+/, '')
"error message #{msg.inspect} is not #{a.inspect}"
else
nil
end
when Regexp
! err.message.match(a) ?
"error message #{err.message} did not match #{a.inspect}" : nil
when Module
! err.is_a?(a) ?
"error is of class #{err.class} not #{a.name}" : nil
else
fail ArgumentError.new("assert_error cannot fathom #{a.inspect}")
end
end
do_assert_(as) { s }
end
|
#assert_false(*as) ⇒ Object
37
38
39
40
|
# File 'lib/probatio/assertions.rb', line 37
def assert_false(*as)
do_assert(as, 'false') { |a| a == false }
end
|
#assert_falsey(*as) ⇒ Object
Also known as:
assert_falsy, assert_falseish
25
26
27
28
|
# File 'lib/probatio/assertions.rb', line 25
def assert_falsey(*as)
do_assert(as, 'falsey') { |a| ! a }
end
|
#assert_hashy(*as) ⇒ Object
135
136
137
138
|
# File 'lib/probatio/assertions.rb', line 135
def assert_hashy(*as)
do_assert(as[0].to_a, 'hashy equal') { |k, v| k == v }
end
|
#assert_include(*as) ⇒ Object
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/probatio/assertions.rb', line 110
def assert_include(*as)
ai =
as.index { |a| a.is_a?(Array) } ||
fail(ArgumentError.new("assert_include found no array"))
arr = as.delete_at(ai)
do_assert(as, 'included') { |e| arr.include?(e) }
end
|
#assert_instance_of(*as) ⇒ Object
Also known as:
assert_is_a, assert_class
145
146
147
148
149
150
151
|
# File 'lib/probatio/assertions.rb', line 145
def assert_instance_of(*as)
moc = as.find { |a| a.is_a?(Module) }
as.delete(moc)
do_assert(as, 'instance of') { |e| e.is_a?(moc) }
end
|
#assert_match(*as) ⇒ Object
62
63
64
65
66
67
68
|
# File 'lib/probatio/assertions.rb', line 62
def assert_match(*as)
strings, others = as.partition { |a| a.is_a?(String) }
rex = others.find { |o| o.is_a?(Regexp) } || strings.pop
do_assert(strings, 'matched') { |s| s.match?(rex) }
end
|
#assert_nil(*as) ⇒ Object
Beware: ws.any? returns false when ws == [ false ]
9
10
11
12
|
# File 'lib/probatio/assertions.rb', line 9
def assert_nil(*as)
do_assert(as, 'nil') { |a| a == nil }
end
|
#assert_not(*as) ⇒ Object
42
43
44
45
|
# File 'lib/probatio/assertions.rb', line 42
def assert_not(*as)
do_assert(as, 'not') { |a| ! a }
end
|
#assert_not_error(*as, &block) ⇒ Object
Also known as:
assert_no_error, assert_nothing_raised
193
194
195
196
197
198
199
200
201
202
|
# File 'lib/probatio/assertions.rb', line 193
def assert_not_error(*as, &block)
block = block || as.find { |a| a.is_a?(Proc) }
err = nil;
begin; block.call; rescue => err; end
do_assert_([]) {
err && "no error expected but returned #{err.class} #{err.message}" }
end
|
#assert_not_match(*as) ⇒ Object
102
103
104
105
106
107
108
|
# File 'lib/probatio/assertions.rb', line 102
def assert_not_match(*as)
strings, others = as.partition { |a| a.is_a?(String) }
rex = others.find { |o| o.is_a?(Regexp) } || strings.pop
do_assert(strings, 'not matched') { |s| ! s.match?(rex) }
end
|
#assert_not_nil(*as) ⇒ Object
14
15
16
17
|
# File 'lib/probatio/assertions.rb', line 14
def assert_not_nil(*as)
do_assert(as, 'not nil') { |a| a != nil }
end
|
#assert_size(*as) ⇒ Object
Also known as:
assert_count
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/probatio/assertions.rb', line 121
def assert_size(*as)
ai =
as.index { |a| a.is_a?(Integer) && a >= 0 } ||
fail(ArgumentError.new("assert_size found no integer >= 0"))
sz = as.delete_at(ai)
as = as.collect { |a| a.respond_to?(:size) ? a.size : a.class }
do_assert(as, "size #{sz}") { |a| a == sz }
end
|
#assert_start_with(*as) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/probatio/assertions.rb', line 78
def assert_start_with(*as)
fail ArgumentError.new(
"assert_start_with expects strings, not #{x.inspect}") \
if as.find { |a| ! a.is_a?(String) }
sta = as.inject { |s, a| s.length < a.length ? s : a }
do_assert(as.filter { |a| a != sta }, 'starting with') { |a|
a.start_with?(sta) }
end
|
#assert_true(*as) ⇒ Object
32
33
34
35
|
# File 'lib/probatio/assertions.rb', line 32
def assert_true(*as)
do_assert(as, 'true') { |a| a == true }
end
|
#assert_truthy(*as) ⇒ Object
Also known as:
assert_trueish
19
20
21
22
|
# File 'lib/probatio/assertions.rb', line 19
def assert_truthy(*as)
do_assert(as, 'truthy') { |a| !! a }
end
|
#block ⇒ Object
719
|
# File 'lib/probatio.rb', line 719
def block; @__block; end
|
#refute(*as) ⇒ Object
231
|
# File 'lib/probatio/assertions.rb', line 231
def refute(*as); jack(:refute, as); end
|
#refute_hashy(*as) ⇒ Object
140
141
142
143
|
# File 'lib/probatio/assertions.rb', line 140
def refute_hashy(*as)
do_assert(as[0].to_a, 'hashy not equal') { |k, v| k != v }
end
|
#refute_match(*as) ⇒ Object
70
71
72
73
74
75
76
|
# File 'lib/probatio/assertions.rb', line 70
def refute_match(*as)
strings, others = as.partition { |a| a.is_a?(String) }
rex = others.find { |o| o.is_a?(Regexp) } || strings.pop
do_assert(strings, 'not matched') { |s| ! s.match?(rex) }
end
|
#run(run_opts) ⇒ Object
721
722
723
724
|
# File 'lib/probatio.rb', line 721
def run(run_opts)
_run(@__group.arounds + [ :do_run ], run_opts)
end
|