Class: Test::Spec::Should
- Inherits:
-
Object
show all
- Includes:
- Unit::Assertions
- Defined in:
- lib/test/spec/should-output.rb,
lib/test/spec.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(object, message = nil) ⇒ Should
Returns a new instance of Should.
44
45
46
47
|
# File 'lib/test/spec.rb', line 44
def initialize(object, message=nil)
@object = object
@message = message
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
180
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/test/spec.rb', line 180
def method_missing(name, *args)
return _raise(*args) if name == :raise
if @object.respond_to?("#{name}?")
assert @object.__send__("#{name}?", *args),
"#{name}? expected to be true. #{@message}"
else
super
end
end
|
Class Method Details
.deprecated_alias(to, from) ⇒ Object
37
38
39
40
41
42
|
# File 'lib/test/spec.rb', line 37
def self.deprecated_alias(to, from) define_method(to) { |*args|
warn "Test::Spec::Should##{to} is deprecated and will be removed in future versions."
__send__ from, *args
}
end
|
Instance Method Details
159
160
161
|
# File 'lib/test/spec.rb', line 159
def <(value)
assert_operator @object, :<, value, @message
end
|
#<=(value) ⇒ Object
163
164
165
|
# File 'lib/test/spec.rb', line 163
def <=(value)
assert_operator @object, :<=, value, @message
end
|
#===(value) ⇒ Object
167
168
169
|
# File 'lib/test/spec.rb', line 167
def ===(value)
assert_operator @object, :===, value, @message
end
|
151
152
153
|
# File 'lib/test/spec.rb', line 151
def >(value)
assert_operator @object, :>, value, @message
end
|
#>=(value) ⇒ Object
155
156
157
|
# File 'lib/test/spec.rb', line 155
def >=(value)
assert_operator @object, :>=, value, @message
end
|
#_raise(*args) ⇒ Object
130
131
132
133
|
# File 'lib/test/spec.rb', line 130
def _raise(*args)
args = [RuntimeError] if args.empty?
assert_raise(*(args + [@message]), &@object)
end
|
59
60
61
|
# File 'lib/test/spec.rb', line 59
def a
self
end
|
#add_assertion ⇒ Object
50
51
52
|
# File 'lib/test/spec.rb', line 50
def add_assertion
$TEST_SPEC_TESTCASE && $TEST_SPEC_TESTCASE.__send__(:add_assertion)
end
|
55
56
57
|
# File 'lib/test/spec.rb', line 55
def an
self
end
|
#be(*value) ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/test/spec.rb', line 96
def be(*value)
case value.size
when 0
self
when 1
if CustomShould === value.first
pass value.first
else
assert_same value.first, @object, @message
end
else
raise ArgumentError, "should.be needs zero or one argument"
end
end
|
#close(value, delta) ⇒ Object
91
92
93
|
# File 'lib/test/spec.rb', line 91
def close(value, delta)
assert_in_delta value, @object, delta, @message
end
|
#equal(value) ⇒ Object
Also known as:
==
86
87
88
|
# File 'lib/test/spec.rb', line 86
def equal(value)
assert_equal value, @object, @message
end
|
#include(value) ⇒ Object
145
146
147
148
149
|
# File 'lib/test/spec.rb', line 145
def include(value)
msg = build_message(@message, "<?> expected to include ?, but it didn't.",
@object, value)
assert_block(msg) { @object.include?(value) }
end
|
#instance_of(klass) ⇒ Object
116
117
118
|
# File 'lib/test/spec.rb', line 116
def instance_of(klass)
assert_instance_of klass, @object, @message
end
|
#kind_of(klass) ⇒ Object
121
122
123
|
# File 'lib/test/spec.rb', line 121
def kind_of(klass)
assert_kind_of klass, @object, @message
end
|
#match(value) ⇒ Object
Also known as:
=~
111
112
113
|
# File 'lib/test/spec.rb', line 111
def match(value)
assert_match value, @object, @message
end
|
#messaging(message) ⇒ Object
Also known as:
blaming
74
75
76
77
|
# File 'lib/test/spec.rb', line 74
def messaging(message)
@message = message.to_s
self
end
|
139
140
141
|
# File 'lib/test/spec.rb', line 139
def nil
assert_nil @object, @message
end
|
#not(*args) ⇒ Object
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/test/spec.rb', line 63
def not(*args)
case args.size
when 0
ShouldNot.new(@object, @message)
when 1
ShouldNot.new(@object, @message).pass(args.first)
else
raise ArgumentError, "#not takes zero or one argument(s)."
end
end
|
#output(expected, to = STDOUT) ⇒ Object
Captures output from the IO given as the second argument (STDIN by default) and matches it against a String or Regexp given as the first argument.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/test/spec/should-output.rb', line 10
def output(expected, to = STDOUT)
old_to = to.dup
to.reopen File.open(File.join(Dir.tmpdir, "should_output_#{$$}"), "w+")
@object.call
out = to.dup
to.reopen old_to
out.rewind
output = out.read
case expected
when Regexp
output.should.match expected
else
output.should.equal expected
end
ensure
out.close
begin
to.seek 0, IO::SEEK_END
rescue Errno::ESPIPE
rescue Errno::EPIPE
end
FileUtils.rm_f out.path
end
|
#pass(custom) ⇒ Object
171
172
173
174
175
176
177
178
|
# File 'lib/test/spec.rb', line 171
def pass(custom)
_wrap_assertion {
assert_nothing_raised(Test::Unit::AssertionFailedError,
@message || custom.failure_message) {
assert custom.matches?(@object), @message || custom.failure_message
}
}
end
|
#respond_to(method) ⇒ Object
126
127
128
|
# File 'lib/test/spec.rb', line 126
def respond_to(method)
assert_respond_to @object, method, @message
end
|
#satisfy(&block) ⇒ Object
80
81
82
83
84
|
# File 'lib/test/spec.rb', line 80
def satisfy(&block)
assert_block(@message || "satisfy block failed.") {
yield @object
}
end
|
#throw(*args) ⇒ Object
135
136
137
|
# File 'lib/test/spec.rb', line 135
def throw(*args)
assert_throws(*(args + [@message]), &@object)
end
|