section9-unittest 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +21 -0
- data/README.md +166 -0
- data/lib/section9/unittest.rb +455 -0
- data/section9-unittest.gemspec +42 -0
- data/setup.rb +1585 -0
- data/test/unittest_test.rb +772 -0
- metadata +75 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2011 Your Name all rights reserved
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
data/README.md
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
# section9/unittest.rb README
|
2
|
+
|
3
|
+
Release: 0.0.1
|
4
|
+
|
5
|
+
|
6
|
+
## Overview
|
7
|
+
|
8
|
+
section9/unittest.rb is an extension library for Test::Unit, supporting both 1.8 and 1.9.
|
9
|
+
|
10
|
+
* describe() and it() are available.
|
11
|
+
** before(), after(), subject() are NOT supported.
|
12
|
+
* verify_() helps to write assertion easily.
|
13
|
+
|
14
|
+
## Examples:
|
15
|
+
|
16
|
+
Example:
|
17
|
+
|
18
|
+
require 'test/unit'
|
19
|
+
require 'section9/unittest'
|
20
|
+
|
21
|
+
class ExampleTest < TC # or Section9::UnitTest::TestCase
|
22
|
+
describe "Array" do
|
23
|
+
describe "#collect()" do
|
24
|
+
it "collects values for each item" do
|
25
|
+
actual = [10, 20, 30].collect {|x| x + 1}
|
26
|
+
verify_(actual) == [11, 21, 31]
|
27
|
+
## or assert_xxx() are available
|
28
|
+
assert_equal([11, 21, 31], actual)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
Assertions:
|
36
|
+
|
37
|
+
verify_(actual) == expected # same as assert_equal(expected, actual)
|
38
|
+
verify_(actual) != expected # same as assert_not_equal(expected, actual) # for Ruby1.9
|
39
|
+
verify_(actual) > expected # same as assert_operator(actual, :>, expected)
|
40
|
+
verify_(actual) >= expected # same as assert_operator(actual, :>=, expected)
|
41
|
+
verify_(actual) < expected # same as assert_operator(actual, :<, expected)
|
42
|
+
verify_(actual) <= expected # same as assert_operator(actual, :>= expected)
|
43
|
+
verify_(actual) =~ expected # same as assert_match(expected, actual)
|
44
|
+
verify_(actual) !~ expected # same as assert_no_match(expected, actual) # for Ruby 1.9
|
45
|
+
verify_(act).in_delta?(exp, delta) # same as assert_in_delta(exp, act, delta)
|
46
|
+
verify_(actual).nil? # same as assert_nil(actual)
|
47
|
+
verify_(actual).same?(expected) # same as assert_same(expected, actual)
|
48
|
+
verify_(actual).kind_of?(klass) # same as assert_kind_of(klass, actual)
|
49
|
+
verify_(actual).instance_of?(klass) # same as assert_instance_of(klass, actual)
|
50
|
+
|
51
|
+
verify_(proc {...}).raise?(klass, errmsg)
|
52
|
+
# same as ex = assert_raise(klass) {...};
|
53
|
+
# assert_equal(errmsg, ex.msg) if errmsg.is_a?(String)
|
54
|
+
# assert_match(errmsg, ex.msg) if errmsg.is_a?(Regexp)
|
55
|
+
|
56
|
+
|
57
|
+
Any boolean method can be assertion:
|
58
|
+
|
59
|
+
verify_(actual).empty? # same as assert actual.empty? == true
|
60
|
+
verify_(actual).blank? # same as assert actual.blank? == true
|
61
|
+
verify_(actual).end_with?(arg) # same as assert actual.end_with?(arg) == true
|
62
|
+
|
63
|
+
|
64
|
+
Negative assertion:
|
65
|
+
|
66
|
+
## you should use '.NOT ==' and '.NOT =~instead of '!=' and '!~' on Ruby 1.8,
|
67
|
+
## because 1.8 doesn't allow to override '!=' nor '!~' operators.
|
68
|
+
verify_(1+1).NOT == 3 # same as assert_not_equal(3, 1+1)
|
69
|
+
verify_("SOS").NOT =~ /\d+/ # same as assert_no_match(/\d+/, "SOS")
|
70
|
+
verify_(proc {...}).NOT.raise?
|
71
|
+
# same as assert_nothing_raised {...}
|
72
|
+
|
73
|
+
|
74
|
+
Original assertion:
|
75
|
+
|
76
|
+
verify_(path).file_exist? # same as assert File.file?(path)
|
77
|
+
verify_(path).dir_exist? # same as assert File.dir?(path)
|
78
|
+
verify_(path).NOT.exist? # same as assert ! File.exist?(path)
|
79
|
+
verify_(recorder).called?(...) # see the next section
|
80
|
+
|
81
|
+
|
82
|
+
Helper method 'at_exit()' registers block which is called in teardown() method:
|
83
|
+
|
84
|
+
class Example_TC < TC
|
85
|
+
it "is an example to remove temporary file automatically" do
|
86
|
+
File.open("tmp.txt", 'w') {|f| f.write("SOS") }
|
87
|
+
at_exit { File.unlink("tmp.txt") } # register block
|
88
|
+
verify_(File.read("tmp.txt")) == "SOS"
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
Helper method 'tmp()' returns Section::Tmp object (see the next section):
|
94
|
+
|
95
|
+
class Example_TC < TC
|
96
|
+
it "is an example to capture $stdout" do
|
97
|
+
## create new StringIO object and set it to $stdout
|
98
|
+
sout = tmp.stdout()
|
99
|
+
## print something
|
100
|
+
puts "SOS"
|
101
|
+
## do test
|
102
|
+
verify_(sout.string) == "SOS\n"
|
103
|
+
## $stdout is recovered automatically in teardown() method.
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
Helper method 'recorder()' returns Section::Recorder object (see the next section):
|
109
|
+
|
110
|
+
class Calc
|
111
|
+
def total(*nums)
|
112
|
+
return nums.sum
|
113
|
+
end
|
114
|
+
def average(*nums)
|
115
|
+
return total(nums).to_f / nums.length
|
116
|
+
end
|
117
|
+
end
|
118
|
+
class Example_TC < TC
|
119
|
+
it "is an example to record method calls" do
|
120
|
+
rc = recorder() # create recorder object
|
121
|
+
calc = Calc()
|
122
|
+
rc.record(calc, :total, :average) # register methods to record
|
123
|
+
n = calc.average(10, 20, 9)
|
124
|
+
verify_(n) == 13
|
125
|
+
## check method call
|
126
|
+
verify_(rc[0]).called?(:obj=>calc, :name=>:average, :args=>[10, 20, 9], :ret=>13)
|
127
|
+
verify_(rc[1]).called?(:obj=>calc, :name=>:total, :args=>[10, 20, 9], :ret=>39)
|
128
|
+
## or
|
129
|
+
verify_(rc[0]).called?([calc, :average, [10, 20, 9], 13])
|
130
|
+
verify_(rc[1]).called?([calc, :total, [10, 20, 9], 39])
|
131
|
+
## or
|
132
|
+
verify_(rc[0].obj) == calc
|
133
|
+
verify_(rc[0].name) == :average
|
134
|
+
verify_(rc[0].args) == [10, 20, 9]
|
135
|
+
verify_(rc[0].ret) == 13
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
|
140
|
+
These helper methods are defined in TC (== Section9::UnitTest::TestCase) class,
|
141
|
+
therefore you can use them in 'test_xxx()' methods:
|
142
|
+
|
143
|
+
class Example_TC < TC
|
144
|
+
def test_1
|
145
|
+
verify_(1+1) == 2
|
146
|
+
at_exit { ... }
|
147
|
+
sout = tmp.stdout()
|
148
|
+
rc = record()
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
|
153
|
+
## History
|
154
|
+
|
155
|
+
* 0.1.0
|
156
|
+
** public release
|
157
|
+
|
158
|
+
|
159
|
+
## License
|
160
|
+
|
161
|
+
$License: MIT License $
|
162
|
+
|
163
|
+
|
164
|
+
## Copyright
|
165
|
+
|
166
|
+
$Copyright: copyright(c) 2011 kuwata-lab.com all rights reserved $
|
@@ -0,0 +1,455 @@
|
|
1
|
+
###
|
2
|
+
### $Release: 0.0.1 $
|
3
|
+
### $Copyright: copyright(c) 2011 kuwata-lab.com all rights reserved $
|
4
|
+
### $License: MIT License $
|
5
|
+
###
|
6
|
+
|
7
|
+
require 'test/unit'
|
8
|
+
require 'section9/tmp'
|
9
|
+
|
10
|
+
|
11
|
+
module Section9
|
12
|
+
module UnitTest
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
module Section9::UnitTest
|
18
|
+
|
19
|
+
|
20
|
+
ASSERTION = defined?(MiniTest) ? MiniTest::Assertion : Test::Unit::AssertionFailedError # :nodoc:
|
21
|
+
|
22
|
+
|
23
|
+
class TestCase < Test::Unit::TestCase
|
24
|
+
|
25
|
+
##
|
26
|
+
## 'describe' and 'it'
|
27
|
+
##
|
28
|
+
def self.describe(obj, &block)
|
29
|
+
(@__describes ||= []) << obj
|
30
|
+
yield
|
31
|
+
ensure
|
32
|
+
@__describes.pop
|
33
|
+
end unless self.respond_to?(:describe)
|
34
|
+
|
35
|
+
def self.it(desc, &block)
|
36
|
+
@__count ||= 0
|
37
|
+
@__count += 1
|
38
|
+
arr = @__describes
|
39
|
+
desc = "[#{arr.join(' > ')}] #{desc}" if arr && ! arr.empty?
|
40
|
+
method_name = "test_%03d: %s" % [@__count, desc]
|
41
|
+
define_method(method_name, block)
|
42
|
+
end unless self.respond_to?(:it)
|
43
|
+
|
44
|
+
##
|
45
|
+
## shortcut for assertion methods
|
46
|
+
##
|
47
|
+
def verify_(actual=nil, &block)
|
48
|
+
location = caller(1).first
|
49
|
+
vo = VerifyObject.new(self, actual, location, &block)
|
50
|
+
__register(vo)
|
51
|
+
vo
|
52
|
+
end
|
53
|
+
|
54
|
+
def __register(vo) #:nodoc:
|
55
|
+
(@__not_yet ||= {})[vo.__id__] = vo
|
56
|
+
end
|
57
|
+
|
58
|
+
def __unregister(vo) #:nodoc:
|
59
|
+
(@__not_yet ||= {}).delete(vo.__id__)
|
60
|
+
end
|
61
|
+
|
62
|
+
def __clear_registered #:nodoc:
|
63
|
+
@__not_yet.clear if @__not_yet
|
64
|
+
end
|
65
|
+
|
66
|
+
def __report_registered #:nodoc:
|
67
|
+
return unless @__not_yet
|
68
|
+
@__not_yet.each_value do |vo|
|
69
|
+
$stderr.write "** warning: verify() is called but not tested yet (at #{vo.location})\n"
|
70
|
+
end
|
71
|
+
@__not_yet.clear
|
72
|
+
@__not_yet = nil
|
73
|
+
end
|
74
|
+
private :__report_registered
|
75
|
+
|
76
|
+
def teardown
|
77
|
+
super
|
78
|
+
__report_registered()
|
79
|
+
end
|
80
|
+
|
81
|
+
##
|
82
|
+
## assertions
|
83
|
+
##
|
84
|
+
def assert_block(*msgs) # re-define for Ruby <= 1.9.1
|
85
|
+
assert yield, *msgs
|
86
|
+
end if defined?(MiniTest)
|
87
|
+
|
88
|
+
def assert_not_kind_of(klass, obj, msg="")
|
89
|
+
#errmsg = build_message("<?>\nexpected not to be kind_of\\?\n<?> but was\n<?>.", obj, klass, obj.class)
|
90
|
+
errmsg = "<#{obj.inspect}>\nexpected not to be kind_of?\n<#{klass}> but was\n<#{obj.class}>."
|
91
|
+
assert_block(errmsg) { ! obj.kind_of?(klass) }
|
92
|
+
end
|
93
|
+
|
94
|
+
def assert_not_instance_of(klass, obj, msg="")
|
95
|
+
#errmsg = build_message(msg, "<?>\nexpected not to be instance_of\\?\n<?> but was.", obj, klass)
|
96
|
+
errmsg = "<#{obj.inspect}>\nexpected not to be instance_of?\n<#{klass}> but was."
|
97
|
+
assert_block(errmsg) { ! obj.instance_of?(klass) }
|
98
|
+
end
|
99
|
+
|
100
|
+
def assert_not_in_delta(expected, actual, delta, msg="")
|
101
|
+
errmsg = "<#{expected.inspect}> and\n<#{actual.inspect}> expected to be without\n<0.0001> of each other."
|
102
|
+
assert_block(errmsg) { (expected - actual).abs > delta }
|
103
|
+
end
|
104
|
+
|
105
|
+
def assert_not_respond_to(actual, method, msg="")
|
106
|
+
#errmsg = build_message(msg, "Expected ? not to respond to ?.", actual, method.to_s.intern)
|
107
|
+
errmsg = "Expected #{actual.inspect} not to respond to #{method.inspect}."
|
108
|
+
assert_block(errmsg) { ! actual.respond_to?(method) }
|
109
|
+
end
|
110
|
+
|
111
|
+
def assert_nothing_thrown(msg="")
|
112
|
+
errcls = RUBY_VERSION >= "1.9" ? ArgumentError : NameError
|
113
|
+
yield
|
114
|
+
assert_block("Expected nothing to be thrown") { true }
|
115
|
+
rescue errcls => ex
|
116
|
+
#errmsg = build_message(msg, "nothing should be thrown but #{ex.message}")
|
117
|
+
errmsg = "nothing should be thrown but #{ex.message}"
|
118
|
+
assert_block(errmsg) { false }
|
119
|
+
end
|
120
|
+
|
121
|
+
## ignore unnecessary warning
|
122
|
+
def default_test
|
123
|
+
super if self.class.name != 'Section9::UnitTest::TestCase'
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
127
|
+
|
128
|
+
|
129
|
+
class VerifyBaseObject
|
130
|
+
|
131
|
+
DEPTH = defined?(MiniTest) ? 3 : 2 #:nodoc:
|
132
|
+
|
133
|
+
private
|
134
|
+
|
135
|
+
def _wrap
|
136
|
+
@testcase.__unregister(self)
|
137
|
+
begin
|
138
|
+
yield
|
139
|
+
rescue ASSERTION => ex
|
140
|
+
ex.set_backtrace(caller(DEPTH))
|
141
|
+
raise ex
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
def _method_missing(expected, method_name, *args)
|
146
|
+
method_name.to_s =~ /\?$/ or
|
147
|
+
raise NoMethodError.new("undefined method `#{method_name}' for verify()#{expected ? '' : '.NOT'} object.")
|
148
|
+
begin
|
149
|
+
result = @actual.__send__(method_name, *args)
|
150
|
+
rescue NoMethodError => ex
|
151
|
+
ex.set_backtrace(caller(2))
|
152
|
+
raise ex
|
153
|
+
end
|
154
|
+
if result == expected
|
155
|
+
@testcase.assert true
|
156
|
+
elsif result == ! expected
|
157
|
+
msg = "$actual.#{method_name}() == #{expected}: failed.\n"\
|
158
|
+
" actual: #{@actual.inspect}"
|
159
|
+
msg << "\n args: #{args.inspect[1..-2]}" unless args.empty?
|
160
|
+
@testcase.flunk msg
|
161
|
+
else
|
162
|
+
raise ArgumentError.new("#{@actual.class.name}##{method_name}(): expected to return true or false, but returned #{result.inspect}.")
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
undef_method *(instance_methods.grep(/\?\z/) - [:equal?, "equal?"])
|
167
|
+
|
168
|
+
end
|
169
|
+
|
170
|
+
|
171
|
+
class VerifyObject < VerifyBaseObject
|
172
|
+
|
173
|
+
def initialize(testcase, actual=nil, location=nil, &block)
|
174
|
+
@testcase = testcase
|
175
|
+
@actual = actual
|
176
|
+
@block = block
|
177
|
+
@location = location
|
178
|
+
end
|
179
|
+
attr_accessor :location
|
180
|
+
|
181
|
+
def NOT
|
182
|
+
@testcase.__unregister(self)
|
183
|
+
location = caller(1).first
|
184
|
+
vo = VerifyNotObject.new(@testcase, @actual, location, &@block)
|
185
|
+
@testcase.__register(vo)
|
186
|
+
return vo
|
187
|
+
end
|
188
|
+
|
189
|
+
def _diff_context(expected, actual, header)
|
190
|
+
act, exp = "_tmp.#{rand()}", "_tmp.#{rand()}"
|
191
|
+
File.open(act, 'w') {|f| f.write(@actual) }
|
192
|
+
File.open(exp, 'w') {|f| f.write(expected) }
|
193
|
+
begin
|
194
|
+
diff = `diff -u #{exp} #{act}`
|
195
|
+
ensure
|
196
|
+
File.unlink(act)
|
197
|
+
File.unlink(exp)
|
198
|
+
end
|
199
|
+
diff.sub!(/^---.*\n\+\+\+.*\n/, header)
|
200
|
+
return diff
|
201
|
+
end
|
202
|
+
private :_diff_context
|
203
|
+
|
204
|
+
#def ==(expected); _wrap { @testcase.assert_equal(expected, @actual) } end
|
205
|
+
def ==(expected)
|
206
|
+
_wrap {
|
207
|
+
if @actual != expected && @actual.is_a?(String) && expected.is_a?(String) && expected =~ /\n/
|
208
|
+
diff = _diff_context(expected, @actual, "--- expected\n+++ actual\n")
|
209
|
+
begin
|
210
|
+
@testcase.assert_equal(expected, @actual)
|
211
|
+
rescue Exception => ex
|
212
|
+
ex.message << "\n" << diff.chomp
|
213
|
+
raise ex
|
214
|
+
end
|
215
|
+
else
|
216
|
+
@testcase.assert_equal(expected, @actual)
|
217
|
+
end
|
218
|
+
}
|
219
|
+
end
|
220
|
+
def < (expected); _wrap { @testcase.assert_operator(@actual, :<, expected) } end
|
221
|
+
def <=(expected); _wrap { @testcase.assert_operator(@actual, :<=, expected) } end
|
222
|
+
def > (expected); _wrap { @testcase.assert_operator(@actual, :>, expected) } end
|
223
|
+
def >=(expected); _wrap { @testcase.assert_operator(@actual, :>=, expected) } end
|
224
|
+
def =~(expected); _wrap { @testcase.assert_match(expected, @actual) } end
|
225
|
+
|
226
|
+
eval <<-END, binding, __FILE__, __LINE__+1 if RUBY_VERSION >= "1.9"
|
227
|
+
def !=(expected); _wrap { @testcase.assert_not_equal(expected, @actual) } end
|
228
|
+
def !~(expected); _wrap { @testcase.assert_no_match(expected, @actual) } end
|
229
|
+
END
|
230
|
+
|
231
|
+
def nil? (); _wrap { @testcase.assert_nil(@actual) } end
|
232
|
+
def same? (expected); _wrap { @testcase.assert_same(expected, @actual) } end
|
233
|
+
#def is_a? (expected); _wrap { @testcase.assert_kind_of(expected, @actual) } end
|
234
|
+
def kind_of? (expected); _wrap { @testcase.assert_kind_of(expected, @actual) } end
|
235
|
+
def instance_of?(expected); _wrap { @testcase.assert_instance_of(expected, @actual) } end
|
236
|
+
def in_delta? (expected, delta); _wrap { @testcase.assert_in_delta(expected, @actual, delta) } end
|
237
|
+
def respond_to? (expected); _wrap { @testcase.assert_respond_to(@actual, expected) } end
|
238
|
+
|
239
|
+
def raise?(errcls, errmsg=nil)
|
240
|
+
#if block_given?
|
241
|
+
# ex = @testcase.assert_raise(errcls) { yield }
|
242
|
+
# @testcase.assert_equal(errmsg, ex.message) if errmsg
|
243
|
+
#else
|
244
|
+
_wrap {
|
245
|
+
proc_obj = @block || @actual
|
246
|
+
ex = @testcase.assert_raise(errcls) { proc_obj.call }
|
247
|
+
if errmsg
|
248
|
+
errmsg.is_a?(Regexp) ? @testcase.assert_match(errmsg, ex.message) \
|
249
|
+
: @testcase.assert_equal(errmsg, ex.message)
|
250
|
+
end
|
251
|
+
class << proc_obj
|
252
|
+
def exception; @_exception; end
|
253
|
+
end
|
254
|
+
proc_obj.instance_variable_set(:@_exception, ex)
|
255
|
+
ex
|
256
|
+
}
|
257
|
+
#end
|
258
|
+
end
|
259
|
+
|
260
|
+
def throw?(expected)
|
261
|
+
_wrap {
|
262
|
+
proc_obj = @block || @actual
|
263
|
+
@testcase.assert_throws(expected) { proc_obj.call }
|
264
|
+
}
|
265
|
+
end
|
266
|
+
|
267
|
+
def file_exist?
|
268
|
+
_wrap {
|
269
|
+
File.file?(@actual) or
|
270
|
+
@testcase.flunk "File '#{@actual}' not exist."
|
271
|
+
}
|
272
|
+
end
|
273
|
+
|
274
|
+
def dir_exist?
|
275
|
+
_wrap {
|
276
|
+
File.directory?(@actual) or
|
277
|
+
@testcase.flunk "Directory '#{@actual}' not exist."
|
278
|
+
}
|
279
|
+
end
|
280
|
+
|
281
|
+
def exist?
|
282
|
+
_wrap {
|
283
|
+
raise NoMethodError.new("'exist?()' is available only with '.NOT'. please use 'file_exist?' or 'dir_exist?' instead.")
|
284
|
+
}
|
285
|
+
end
|
286
|
+
|
287
|
+
def called?(expected)
|
288
|
+
_wrap {
|
289
|
+
defined?(Section9::Recorder) or
|
290
|
+
raise ArgumentError.new("Recorder class is not defined: please require 'section9/recorder'.")
|
291
|
+
@actual.is_a?(Section9::Recorder::Called) or
|
292
|
+
raise ArgumentError.new("called?(): actual should be Recorder::Called object, but #{expected.inspect}.")
|
293
|
+
c = @actual
|
294
|
+
case expected
|
295
|
+
when Array
|
296
|
+
arr = expected
|
297
|
+
arr[0] == c.obj or @testcase.assert_equal arr[0], c.obj, "receiver object: "
|
298
|
+
arr[1] == c.name or @testcase.assert_equal arr[1], c.name, "method name: "
|
299
|
+
arr[2] == c.args or @testcase.assert_equal arr[2], c.args, "arguments: "
|
300
|
+
arr[3] == c.ret or @testcase.assert_equal arr[3], c.ret, "return value: "
|
301
|
+
@testcase.assert_equal arr, c.to_a
|
302
|
+
when Hash
|
303
|
+
d = expected.dup()
|
304
|
+
[:obj, :name, :args, :ret].each {|key| d.delete(key) }
|
305
|
+
d.each {|k, v| raise ArgumentError.new("#{k.inspect}: unexpected key for verify.called?().") }
|
306
|
+
d = expected
|
307
|
+
! d.key?(:obj) || d[:obj] == c.obj or @testcase.assert_equal d[:obj], c.obj, "receiver object: "
|
308
|
+
! d.key?(:name) || d[:name] == c.name or @testcase.assert_equal d[:name], c.name, "method name: "
|
309
|
+
! d.key?(:args) || d[:args] == c.args or @testcase.assert_equal d[:args], c.args, "arguments: "
|
310
|
+
! d.key?(:ret) || d[:ret] == c.ret or @testcase.assert_equal d[:ret], c.ret, "return value: "
|
311
|
+
else
|
312
|
+
msg = "verify.called?(): argument should be Array or Hash, but got #{d.inspect}."
|
313
|
+
raise ArgumentError.new(msg)
|
314
|
+
end
|
315
|
+
}
|
316
|
+
end
|
317
|
+
|
318
|
+
def method_missing(method_name, *args)
|
319
|
+
_wrap {
|
320
|
+
_method_missing(true, method_name, *args)
|
321
|
+
}
|
322
|
+
end
|
323
|
+
|
324
|
+
end
|
325
|
+
|
326
|
+
|
327
|
+
class VerifyNotObject < VerifyBaseObject
|
328
|
+
|
329
|
+
def initialize(testcase, actual=nil, location=nil, &block)
|
330
|
+
@testcase = testcase
|
331
|
+
@actual = actual
|
332
|
+
@block = block
|
333
|
+
@location = location
|
334
|
+
end
|
335
|
+
attr_accessor :location
|
336
|
+
|
337
|
+
def < (expected); _wrap { @testcase.assert_operator(@actual, :>=, expected) } end
|
338
|
+
def <=(expected); _wrap { @testcase.assert_operator(@actual, :>, expected) } end
|
339
|
+
def > (expected); _wrap { @testcase.assert_operator(@actual, :<=, expected) } end
|
340
|
+
def >=(expected); _wrap { @testcase.assert_operator(@actual, :<, expected) } end
|
341
|
+
|
342
|
+
def ==(expected); _wrap { @testcase.assert_not_equal(expected, @actual) } end
|
343
|
+
def =~(expected); _wrap { @testcase.assert_no_match(expected, @actual) } end
|
344
|
+
|
345
|
+
eval <<-END, binding, __FILE__, __LINE__+1 if RUBY_VERSION >= "1.9"
|
346
|
+
def !=(expected); _wrap { @testcase.assert_equal(expected, @actual) } end
|
347
|
+
def !~(expected); _wrap { @testcase.assert_match(expected, @actual) } end
|
348
|
+
END
|
349
|
+
|
350
|
+
def nil? () ; _wrap { @testcase.assert_not_nil(@actual) } end
|
351
|
+
def same? (expected); _wrap { @testcase.assert_not_same(expected, @actual) } end
|
352
|
+
#def is_a? (expected); _wrap { @testcase.assert_not_kind_of(expected, @actual) } end
|
353
|
+
def kind_of? (expected); _wrap { @testcase.assert_not_kind_of(expected, @actual) } end
|
354
|
+
def instance_of?(expected); _wrap { @testcase.assert_not_instance_of(expected, @actual) } end
|
355
|
+
def in_delta? (expected, delta); _wrap { @testcase.assert_not_in_delta(expected, @actual, delta) } end
|
356
|
+
def respond_to? (expected); _wrap { @testcase.assert_not_respond_to(@actual, expected) } end
|
357
|
+
|
358
|
+
def raise?
|
359
|
+
_wrap {
|
360
|
+
#if block_given?
|
361
|
+
# @testcase.assert_nothing_raised { yield }
|
362
|
+
#else
|
363
|
+
proc_obj = @block || @actual
|
364
|
+
@testcase.assert_nothing_raised { proc_obj.call }
|
365
|
+
#end
|
366
|
+
}
|
367
|
+
end
|
368
|
+
|
369
|
+
def throw?
|
370
|
+
_wrap {
|
371
|
+
proc_obj = @block || @actual
|
372
|
+
@testcase.assert_nothing_thrown { proc_obj.call }
|
373
|
+
}
|
374
|
+
end
|
375
|
+
|
376
|
+
def file_exist?
|
377
|
+
_wrap {
|
378
|
+
raise NoMethodError.new("'file_exist?()' is not available with '.NOT'. please use '.NOT.exist?' instead.")
|
379
|
+
}
|
380
|
+
end
|
381
|
+
|
382
|
+
def dir_exist?
|
383
|
+
_wrap {
|
384
|
+
raise NoMethodError.new("'dir_exist?()' is not available with '.NOT'. please use '.NOT.exist?' instead.")
|
385
|
+
}
|
386
|
+
end
|
387
|
+
|
388
|
+
def exist?
|
389
|
+
_wrap {
|
390
|
+
! File.exist?(@actual) or
|
391
|
+
@testcase.flunk "#{File.directory?(@actual) ? 'Directory' : 'File'} '#{@actual}' exists unexpectedly."
|
392
|
+
}
|
393
|
+
end
|
394
|
+
|
395
|
+
def called?(arg)
|
396
|
+
_wrap {
|
397
|
+
raise NoMethodError.new("'called?()' is not available with '.NOT'.")
|
398
|
+
}
|
399
|
+
end
|
400
|
+
|
401
|
+
def method_missing(method_name, *args)
|
402
|
+
_wrap {
|
403
|
+
_method_missing(false, method_name, *args)
|
404
|
+
}
|
405
|
+
end
|
406
|
+
|
407
|
+
end
|
408
|
+
|
409
|
+
|
410
|
+
##
|
411
|
+
## testcase helper
|
412
|
+
##
|
413
|
+
module TestCaseHelper
|
414
|
+
|
415
|
+
def teardown
|
416
|
+
super
|
417
|
+
@_tmp.revert if @_tmp
|
418
|
+
@_at_end_callbacks.each {|block| block.call }.clear if @_at_end_callbacks
|
419
|
+
end
|
420
|
+
|
421
|
+
def tmp
|
422
|
+
return @_tmp ||= Section9::Tmp.new
|
423
|
+
end
|
424
|
+
|
425
|
+
def at_end(&block)
|
426
|
+
(@_at_end_callbacks ||= []) << block
|
427
|
+
end
|
428
|
+
|
429
|
+
def recorder
|
430
|
+
require 'section9/recorder' unless defined?(Section9::Recorder)
|
431
|
+
return Section9::Recorder.new
|
432
|
+
end
|
433
|
+
|
434
|
+
end
|
435
|
+
TestCase.class_eval { include TestCaseHelper }
|
436
|
+
|
437
|
+
|
438
|
+
end
|
439
|
+
|
440
|
+
|
441
|
+
TC = Section9::UnitTest::TestCase
|
442
|
+
|
443
|
+
|
444
|
+
## Fix a bug on MiniTest
|
445
|
+
if defined?(MiniTest)
|
446
|
+
|
447
|
+
Test::Unit::Assertions.class_eval do
|
448
|
+
def build_message(head, template=nil, *arguments) #:nodoc:
|
449
|
+
template &&= template.chomp
|
450
|
+
#template.gsub(/\?/) { mu_pp(arguments.shift) }
|
451
|
+
template.gsub(/(\\)?\?/) { $1 ? '?' : mu_pp(arguments.shift) }
|
452
|
+
end
|
453
|
+
end
|
454
|
+
|
455
|
+
end
|