minitest-ok 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0954d6aa6e1f266702561e5d1bc754e240939d62
4
+ data.tar.gz: 6356034dae10336b4b53a39b70e9fb2d35a42029
5
+ SHA512:
6
+ metadata.gz: c5b547b6b93ec8261f4eb38a4e00aed087bcc721f594312b685eef0a7780a7eb0fda544c891ed6a2c297d7f58890aed7ecd7284dd4c3acf9948c2eb0888e9e28
7
+ data.tar.gz: 95dcfac0362e4040c9512c24878b36a83038d2201080297950c05982715163605655b2b3b526abb05a57a9451bd836f1d48fa3f22975c75f387b977174df4b24
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ $Copyright: copyright(c) 2015 kuwata-lab.com all rights reserved $
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,143 @@
1
+ # Minitest::Ok
2
+
3
+ Minitest::Ok is a pretty good helper library for Minitest.
4
+ For example:
5
+
6
+ ```ruby
7
+ ok {1+1} == 2 # same as assert_equal 2, 1+1
8
+ ok {1+1} > 1 # same as assert_operator 1+1, :>, 1
9
+ ok {'123'} =~ /\d+/ # same as assert_match /\d+/, '123'
10
+ ok {[]}.kind_of?(Array) # same as assert_kind_of Array, []
11
+ ok {1..9}.include?(1) # same as assert_includes 1, 1..9
12
+ ok {1}.in?(1..9) # same as assert_includes 1, 1..9
13
+ ok {0}.NOT.in?(1..9) # same as refute_includes 0, 1..9
14
+ ok {""}.truthy? # same as assert true, !!""
15
+ ok {128}.even? # same as assert_predicate 128, :even?
16
+ ok {obj}.attrs(x: 1, y: 2) # same as assert obj.x == 1; assert obj.y == 2
17
+ ok {obj}.items(x: 1, y: 2) # same as assert obj[:x] == 1; assert obj[:y] == 2
18
+ ```
19
+
20
+
21
+ ## Example
22
+
23
+ ```ruby
24
+ # -*- coding: utf-8 -*-
25
+
26
+ require 'minitest/spec'
27
+ require 'minitest/autorun'
28
+
29
+ require 'minitest/ok'
30
+
31
+
32
+ describe 'Minitest::Ok' do
33
+
34
+ it "helps to write assertions" do
35
+
36
+ ## operators
37
+ ok {1+1} == 2 # same as assert_equal 2, 1+1
38
+ ok {1+1} != 1 # same as refute_equal 2, 1+1
39
+ ok {1+1} > 1 # same as assert_operator 1+1, :>, 1
40
+ ok {1+1} >= 2 # same as assert_operator 1+1, :>=, 2
41
+ ok {1+1} < 3 # same as assert_operator 1+1, :<, 3
42
+ ok {1+1} <= 2 # same as assert_operator 1+1, :<=, 2
43
+ ok {Array} === [1, 2] # same as assert_operator Array, :===, [1, 2]
44
+ ok {/\d+/} === '123' # same as assert_operator /\d+/, :===, '123'
45
+
46
+ ## pattern matching
47
+ ok {'123'} =~ /\d+/ # same as assert_match /\d+/, '123'
48
+ ok {'abc'} !~ /\d+/ # same as refute_match /\d+/, 'abc'
49
+
50
+ ## type
51
+ arr = [1, 2, 3]
52
+ ok {arr}.kind_of?(Array) # same as assert_kind_of Array, [1,2,3]
53
+ ok {arr}.is_a?(Array) # 'is_a?' is an alias of 'kind_of?'
54
+ ok {arr}.NOT.kind_of?(Hash) # same as refute_kind_of Hash, [1,2,3]
55
+
56
+ ok {1}.instance_of?(Fixnum) # same as assert_instance_of Fixnum, 1
57
+ ok {1}.NOT.instance_of?(Integer) # same as refute_instance_of Integer, 1
58
+
59
+ ## identity
60
+ arr = []
61
+ ok {arr}.same?(arr) # same as assert_same arr, arr
62
+ ok {arr}.NOT.same?([]) # same as refute_same arr, []
63
+
64
+ ## numeric difference
65
+ ok {3.14159}.in_delta?(3.14, 0.01) # same as assert_in_delta 3.14, 3.14159, 0.001
66
+ ok {3.14159}.NOT.in_delta?(3.14, 0.001) # same as refute_in_delta 3.14, 3.14159, 0.0001
67
+
68
+ ok {3.14159}.in_epsilon?(3.14, 0.001) # same as assert_in_epsilon 3.14, 3.14159, 0.001
69
+ ok {3.14159}.NOT.in_epsilon?(3.14, 0.0001) # same as refute_in_epsilon 3.14, 3.14159, 0.0001
70
+
71
+ ## exception
72
+ ex = ok {proc { 1/0 }}.raise?(ZeroDivisionError) # same as assert_raise(...)
73
+ ex = ok {proc { 1/0 }}.raise?(ZeroDivisionError, "divided by zero")
74
+ ex = ok {proc { 1/0 }}.raise?(ZeroDivisionError, /^divided by zero$/)
75
+ p ex #=> #<ZeroDivisionError: divided by 0>
76
+ ok {proc { 1/1 }}.NOT.raise?(Exception)
77
+
78
+ ok {proc {throw :exit}}.throw?(:exit) # same as assert_throws(:exit) {...}
79
+
80
+ ## stdout and stderr
81
+ ok {proc {puts 'X'}}.output?("X\n") # same as assert_output {...}
82
+ ok {proc {nil}}.silent? # same as assert_silent {...}
83
+
84
+ ## boolean
85
+ ok {123}.truthy? # similar to assert 123
86
+ ok {0}.truthy? # similar to assert 0
87
+ ok {""}.truthy? # similar to assert ""
88
+ ok {false}.falthy? # similar to refute false
89
+ ok {nil}.falthy? # similar to refute nil
90
+
91
+ ## predicates
92
+ ok {""}.empty? # same as assert_empty? ""
93
+ ok {[]}.empty? # same as assert_empty? []
94
+ ok {[1,2,3]}.NOT.empty? # same as refute_empty? [1,2,3]
95
+ ok {1..9}.respond_to?(:each) # same as assert_respond_to "foo", :each
96
+ ok {1..9}.include?(1) # same as assert_includes 1..9, 1
97
+ ok {1..9}.NOT.include?(0) # same as refute_includes 1..9, 0
98
+ ok {1}.in?(1..9) # same as assert_includes 1..9, 1
99
+ ok {0}.NOT.in?(1..9) # same as refute_includes 1..9, 0
100
+
101
+ ok {'foo'.freeze}.frozen? # same as assert 'foo'.freeze.frozen?
102
+ ok {'foo'.taint}.tainted? # same as assert 'foo'.taint.tainted?
103
+ obj = Object.new
104
+ obj.instance_variable_set('@x', 10)
105
+ ok {obj}.instance_variable_defined?('@x')
106
+
107
+ ok {'logo.png'}.end_with?('.png') # same as assert 'logon.png'.start_with?('.png')
108
+ ok {[1,2,3]}.any? {|x| x % 2 == 0} # same as assert [1,2,3].any? {...}
109
+ ok {[1,2,3]}.NOT.all? {|x| x < 0 } # same as refute [1,2,3].all? {...}
110
+
111
+ ## attribute
112
+ time = Time.new(2015, 2, 14)
113
+ ok {time}.attr(:year, 2015).attr(:month, 2).attr(:day, 14)
114
+ ok {time}.attrs(year: 2015, month: 2, day: 14)
115
+
116
+ ## key and value
117
+ dict = {:name=>"Haruhi", :age=>16}
118
+ ok {dict}.item(:name, "Haruhi").item(:age, 16)
119
+ ok {dict}.items(name: "Haruhi", age: 16)
120
+
121
+ ## file and directory
122
+ ok {__FILE__}.file_exist? # whether file exists or not
123
+ ok {Dir.pwd }.dir_exist? # whether directory exists or not
124
+ ok {'/XXXX' }.not_exist? # whether nothing exits or not
125
+
126
+ end
127
+
128
+ end
129
+ ```
130
+
131
+
132
+ ## Copyright and License
133
+
134
+ $Copyright: copyright(c) 2015 kuwata-lab.com all rights reserved $
135
+
136
+ $License: MIT License $
137
+
138
+
139
+ ## History
140
+
141
+ ### 2015-11-22: Release 0.1.0
142
+
143
+ * First release
data/Rakefile ADDED
@@ -0,0 +1,57 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
11
+
12
+
13
+ desc "show how to release"
14
+ task :help do
15
+ puts <<END
16
+ How to release:
17
+
18
+ $ git checkout dev
19
+ $ git diff
20
+ $ which ruby
21
+ $ rake test # for confirmation
22
+ $ git checkout -b rel-1.0 # or git checkout rel-1.0
23
+ $ rake edit rel=1.0.0
24
+ $ git diff
25
+ $ git commit -a -m "release preparation for 1.0.0"
26
+ $ rake build # for confirmation
27
+ $ rake install # for confirmation
28
+ $ #rake release
29
+ $ gem push pkg/minitest-ok-1.0.0.gem
30
+ $ git tag v1.0.0
31
+ $ git push -u origin rel-1.0
32
+ $ git push --tags
33
+ END
34
+
35
+ end
36
+
37
+
38
+ desc "edit files (for release preparation)"
39
+ task :edit do
40
+ rel = ENV['rel'] or
41
+ raise "ERROR: 'rel' environment variable expected."
42
+ filenames = Dir[*%w[lib/**/*.rb test/**/*_test.rb test/test_helper.rb *.gemspec]]
43
+ filenames.each do |fname|
44
+ File.open(fname, 'r+', encoding: 'utf-8') do |f|
45
+ content = f.read()
46
+ x = content.gsub!(/\$Release:.*?\$/, "$Release: #{rel} $")
47
+ if x.nil?
48
+ puts "[_] #{fname}"
49
+ else
50
+ puts "[C] #{fname}"
51
+ f.rewind()
52
+ f.truncate(0)
53
+ f.write(content)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,676 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ###
4
+ ### $Release: 0.1.0 $
5
+ ### $Copyright: copyright(c) 2015 kuwata-lab.com all rights reserved $
6
+ ### $License: MIT License $
7
+ ###
8
+
9
+ require 'minitest/unit'
10
+
11
+
12
+ ## for Ruby 1.9
13
+ if ! defined?(Minitest) && defined?(MiniTest)
14
+ Minitest = MiniTest
15
+ end
16
+
17
+
18
+ module Minitest
19
+
20
+
21
+ module Assertions
22
+
23
+ ##
24
+ ## Helper method to call assertion methods.
25
+ ## See AssertionObject class for details.
26
+ ##
27
+ ## ok {1+1} == 2 # same as assert_equal 2, 1+1
28
+ ## ok {1+1} != 1 # same as refute_equal 1, 1+1
29
+ ## ok {1+1} > 1 # same as assert_operator 1+1, :>, 1
30
+ ## ok {1+1} <= 2 # same as assert_operator 1+1, :<=, 2
31
+ ## ok {'123'} =~ /\d+/ # same as assert_match /\d+/, '123'
32
+ ## ok {[]}.kind_of?(Array) # same as assert_kind_of Array, []
33
+ ## ok {[]}.NOT.kind_of?(Hash) # same as refute_kind_of Hash, []
34
+ ## ok {1..9}.include?(5) # same as assert_includes 5, 1..9
35
+ ## ok {1..9}.NOT.include?(0) # same as refute_includes 0, 1..9
36
+ ## ok {""}.truthy? # same as assert true, !!""
37
+ ## ok {nil}.falthy? # same as assert false, !!""
38
+ ##
39
+ ## ex = ok { proc { 1 / 0 } }.raise?(ZeroDivisionError, "divided by 0")
40
+ ## p ex #=> #<ZeroDivisionError: divided by 0>
41
+ ##
42
+ def ok
43
+ actual = yield
44
+ Ok::AssertionObject.new(actual, self, caller(1, 1).first)
45
+ end
46
+
47
+ ## for Ruby 1.9
48
+ begin
49
+ caller(1, 1)
50
+ rescue ArgumentError
51
+ def ok
52
+ actual = yield
53
+ Ok::AssertionObject.new(actual, self, caller(1).first)
54
+ end
55
+ end
56
+
57
+ end
58
+
59
+
60
+ module Ok
61
+
62
+ VERSION = '$Release: 0.1.0 $'.split()[1]
63
+
64
+
65
+ class AssertionObject
66
+
67
+ ## Don't create this object directly. Use <tt>ok {value}</tt> instead.
68
+ def initialize(actual, context, location)
69
+ @actual = actual
70
+ @context = context
71
+ @not = false
72
+ @tested = tested = [false]
73
+ ObjectSpace.define_finalizer(self, proc {
74
+ unless tested[0]
75
+ loc = location.to_s.split(/:in /).first
76
+ $stderr.puts "** WARNING: ok() called but no assertion invoked (#{loc})"
77
+ end
78
+ })
79
+ end
80
+
81
+ def _mark_as_tested # :nodoc:
82
+ @tested[0] = true
83
+ self
84
+ end
85
+
86
+ ##
87
+ ## Make logical condition reversed.
88
+ ##
89
+ ## ok {[1,2,3]}.empty? # Fail
90
+ ## ok {[1,2,3]}.NOT.empty? # Pass
91
+ ##
92
+ def NOT()
93
+ @not = ! @not
94
+ self
95
+ end
96
+
97
+ ##
98
+ ## Same as <tt>assert_equal()</tt>.
99
+ ##
100
+ ## ok {1+1} == 2 # Pass
101
+ ## ok {1+1} == 1 # Fail
102
+ ##
103
+ def ==(expected)
104
+ _mark_as_tested()
105
+ @context.assert_equal expected, @actual unless @not
106
+ @context.refute_equal expected, @actual if @not
107
+ self
108
+ end
109
+
110
+ ##
111
+ ## Same as <tt>refute_equal()</tt>.
112
+ ##
113
+ ## ok {1+1} != 1 # Pass
114
+ ## ok {1+1} != 2 # Fail
115
+ ##
116
+ def !=(expected)
117
+ _mark_as_tested()
118
+ @context.refute_equal expected, @actual unless @not
119
+ @context.assert_equal expected, @actual if @not
120
+ self
121
+ end
122
+
123
+ ##
124
+ ## Tests <tt>actual > expected</tt>.
125
+ ##
126
+ ## ok {1+1} > 1 # Pass
127
+ ## ok {1+1} > 2 # Fail
128
+ ## ok {1+1} > 3 # Fail
129
+ ##
130
+ def >(expected)
131
+ _mark_as_tested()
132
+ @context.assert_operator @actual, :'>', expected unless @not
133
+ @context.refute_operator @actual, :'>', expected if @not
134
+ self
135
+ end
136
+
137
+ ##
138
+ ## Tests <tt>actual >= expected</tt>.
139
+ ##
140
+ ## ok {1+1} >= 1 # Pass
141
+ ## ok {1+1} >= 2 # Pass
142
+ ## ok {1+1} >= 3 # Fail
143
+ ##
144
+ def >=(expected)
145
+ _mark_as_tested()
146
+ @context.assert_operator @actual, :'>=', expected unless @not
147
+ @context.refute_operator @actual, :'>=', expected if @not
148
+ self
149
+ end
150
+
151
+ ##
152
+ ## Tests <tt>actual < expected</tt>.
153
+ ##
154
+ ## ok {1+1} < 3 # Pass
155
+ ## ok {1+1} < 2 # Fail
156
+ ## ok {1+1} < 1 # Fail
157
+ ##
158
+ def <(expected)
159
+ _mark_as_tested()
160
+ @context.assert_operator @actual, :'<', expected unless @not
161
+ @context.refute_operator @actual, :'<', expected if @not
162
+ self
163
+ end
164
+
165
+ ##
166
+ ## Tests <tt>actual <= expected</tt>.
167
+ ##
168
+ ## ok {1+1} <= 3 # Pass
169
+ ## ok {1+1} <= 2 # Pass
170
+ ## ok {1+1} <= 1 # Fail
171
+ ##
172
+ def <=(expected)
173
+ _mark_as_tested()
174
+ @context.assert_operator @actual, :'<=', expected unless @not
175
+ @context.refute_operator @actual, :'<=', expected if @not
176
+ self
177
+ end
178
+
179
+ ##
180
+ ## Tests <tt>actual === expected</tt>.
181
+ ##
182
+ ## ok {String} === 'foo' # Pass
183
+ ## ok {/\d+/} === '123' # Pass
184
+ ##
185
+ def ===(expected)
186
+ _mark_as_tested()
187
+ @context.assert_operator @actual, :'===', expected unless @not
188
+ @context.refute_operator @actual, :'===', expected if @not
189
+ self
190
+ end
191
+
192
+ ##
193
+ ## Same as <tt>assert_match()</tt>.
194
+ ##
195
+ ## ok {"abc"} =~ /\w+/ # Pass
196
+ ## ok {"abc"} =~ /\d+/ # Fail
197
+ ##
198
+ def =~(expected)
199
+ _mark_as_tested()
200
+ @context.assert_match expected, @actual unless @not
201
+ @context.refute_match expected, @actual if @not
202
+ self
203
+ end
204
+
205
+ ##
206
+ ## Same as <tt>refute_match()</tt>.
207
+ ##
208
+ ## ok {"abc"} !~ /\d+/ # Pass
209
+ ## ok {"abc"} !~ /\w+/ # Fail
210
+ ##
211
+ def !~(expected)
212
+ _mark_as_tested()
213
+ @context.refute_match expected, @actual unless @not
214
+ @context.assert_match expected, @actual if @not
215
+ self
216
+ end
217
+
218
+ ##
219
+ ## Same as <tt>assert_kind_of()</tt>.
220
+ ##
221
+ ## ok {123}.kind_of?(Fixnum) # Pass
222
+ ## ok {123}.kind_af?(Integer) # Pass
223
+ ## ok {123}.kind_of?(Float) # Fail
224
+ ##
225
+ def kind_of?(expected)
226
+ _mark_as_tested()
227
+ @context.assert_kind_of expected, @actual unless @not
228
+ @context.refute_kind_of expected, @actual if @not
229
+ self
230
+ end
231
+
232
+ ##
233
+ ## Same as <tt>assert_kind_of()</tt>.
234
+ ##
235
+ ## ok {123}.is_a?(Fixnum) # Pass
236
+ ## ok {123}.is_a?(Integer) # Pass
237
+ ## ok {123}.is_a?(Float) # Fail
238
+ ##
239
+ alias is_a? kind_of?
240
+
241
+ ##
242
+ ## Same as <tt>assert_instance_of()</tt>.
243
+ ##
244
+ ## ok {123}.instance_of?(Fixnum) # Pass
245
+ ## ok {123}.instance_of?(Integer) # Fail
246
+ ##
247
+ def instance_of?(expected)
248
+ _mark_as_tested()
249
+ @context.assert_instance_of expected, @actual unless @not
250
+ @context.refute_instance_of expected, @actual if @not
251
+ self
252
+ end
253
+
254
+ ##
255
+ ## Same as <tt>assert_same()</tt>.
256
+ ##
257
+ ## arr = [1]
258
+ ## ok {arr}.same?(arr) # Pass
259
+ ## ok {arr}.same?([1]) # Fail
260
+ ## ok {arr}.NOT.same?([1]) # Pass
261
+ ##
262
+ def same?(expected)
263
+ _mark_as_tested()
264
+ @context.assert_same expected, @actual unless @not
265
+ @context.refute_same expected, @actual if @not
266
+ self
267
+ end
268
+
269
+ ##
270
+ ## Same as <tt>assert_empty()</tt>.
271
+ ##
272
+ ## ok {""}.empty? # Pass
273
+ ## ok {[]}.empty? # Pass
274
+ ## ok {"X"}.empty? # Fail
275
+ ## ok {"X"}.NOT.empty? # Pass
276
+ ##
277
+ def empty?
278
+ _mark_as_tested()
279
+ @context.assert_empty @actual unless @not
280
+ @context.refute_empty @actual if @not
281
+ self
282
+ end
283
+
284
+ ##
285
+ ## Same as <tt>assert_in_delta()</tt>.
286
+ ##
287
+ ## ok {3.14159}.in_delta?(3.14, 0.01) # Pass
288
+ ## ok {3.14159}.in_delta?(3.14, 0.001) # Fail
289
+ ##
290
+ def in_delta?(expected, delta)
291
+ _mark_as_tested()
292
+ @context.assert_in_delta(expected, @actual, delta) unless @not
293
+ @context.refute_in_delta(expected, @actual, delta) if @not
294
+ self
295
+ end
296
+
297
+ ##
298
+ ## Same as <tt>assert_in_epsilon()</tt>.
299
+ ##
300
+ ## ok {3.14159}.in_epsilon?(3.14, 0.001) # Pass
301
+ ## ok {3.14159}.in_epsilon?(3.14, 0.0001) # Fail
302
+ ##
303
+ def in_epsilon?(expected, epsilon)
304
+ _mark_as_tested()
305
+ @context.assert_in_epsilon(expected, @actual, epsilon) unless @not
306
+ @context.refute_in_epsilon(expected, @actual, epsilon) if @not
307
+ self
308
+ end
309
+
310
+ ##
311
+ ## Same as <tt>assert_raises()</tt>.
312
+ ##
313
+ ## ex = ok { proc { 1/0 } }.raise?(ZeroDivisionError) # Pass
314
+ ## p ex.class #=> ZeroDivisionError
315
+ ## ex = ok { proc { 1/0 } }.raise?(ZeroDivisionError, "divided by zero")
316
+ ## ex = ok { proc { 1/0 } }.raise?(ZeroDivisionError, /^divided by zero$/)
317
+ ##
318
+ ## ok { proc { 1 / 1 } }.NOT.raise?(Exception) # Pass
319
+ ##
320
+ def raise?(exception_class, message=nil)
321
+ _mark_as_tested()
322
+ ex = nil
323
+ unless @not
324
+ ex = @context.assert_raises(exception_class) { @actual.call }
325
+ else
326
+ begin
327
+ @actual.call
328
+ rescue exception_class => ex
329
+ @context.assert false, "Exception #{ex.class} raised unexpectedly."
330
+ else
331
+ @context.assert true
332
+ end
333
+ end
334
+ return ex # not self!
335
+ end
336
+
337
+ ##
338
+ ## Same as <tt>assert_throws()</tt>.
339
+ ##
340
+ ## ok {proc {throw :exit}}.throw?(:exit) # Pass
341
+ ## ok {proc {nil}}.NOT.throw?(:exit) # NOT AVAILABLE
342
+ ##
343
+ def throw?(sym)
344
+ _mark_as_tested()
345
+ ! @not or
346
+ raise "NOT.throw? is unsupported because refute_throws() is not defined in Minitest."
347
+ @context.assert_throws(sym) { @actual.call }
348
+ self
349
+ end
350
+
351
+ ##
352
+ ## Same as <tt>assert_respond_to()</tt>.
353
+ ##
354
+ ## ok {"X"}.respond_to?(:each) # Pass
355
+ ## ok {123}.respond_to?(:each) # Fail
356
+ ##
357
+ def respond_to?(expected)
358
+ _mark_as_tested()
359
+ @context.assert_respond_to @actual, expected unless @not
360
+ @context.refute_respond_to @actual, expected if @not
361
+ self
362
+ end
363
+
364
+ ##
365
+ ## Same as <tt>assert_includes()</tt>.
366
+ ##
367
+ ## ok {[1, 2, 3]}.include?(2) # Pass
368
+ ## ok {[1, 2, 3]}.include?(0) # Fail
369
+ ## ok {[1, 2, 3]}.NOT.include?(0) # Pass
370
+ ##
371
+ def include?(expected)
372
+ _mark_as_tested()
373
+ @context.assert_includes @actual, expected unless @not
374
+ @context.refute_includes @actual, expected if @not
375
+ self
376
+ end
377
+
378
+ ##
379
+ ## Same as <tt>assert_includes()</tt>.
380
+ ##
381
+ ## ok {1}.in?(1..9) # Pass
382
+ ## ok {0}.in?(1..9) # Fail
383
+ ## ok {0}.NOT.in?(1..9) # Pass
384
+ ##
385
+ def in?(expected)
386
+ _mark_as_tested()
387
+ @context.assert_includes expected, @actual unless @not
388
+ @context.refute_includes expected, @actual if @not
389
+ self
390
+ end
391
+
392
+ ##
393
+ ## Same as <tt>assert_output()</tt>.
394
+ ##
395
+ ## ok {proc { puts 'X' }}.output?("X\n") # Pass
396
+ ## ok {proc { x = 123 }}.NOT.output? # NOT AVAILABLE
397
+ ##
398
+ def output?(stdout=nil, stderr=nil)
399
+ _mark_as_tested()
400
+ ! @not or
401
+ raise "use ok().silent? instead of ok().NOT.output?."
402
+ @context.assert_output(stdout, stderr, &@actual)
403
+ self
404
+ end
405
+
406
+ ##
407
+ ## Same as <tt>assert_silent()</tt>.
408
+ ##
409
+ ## ok {proc { x = 1234 }}.silent? # Pass
410
+ ## ok {proc { puts 'X' }}.NOT.silent? # NOT AVAILABLE
411
+ ##
412
+ def silent?
413
+ _mark_as_tested()
414
+ ! @not or
415
+ raise "use ok().output? instead of ok().NOT.silent?."
416
+ @context.assert_silent(&@actual)
417
+ self
418
+ end
419
+
420
+ ## for predicates
421
+
422
+ ##
423
+ ## Tests whether object is frozen or not.
424
+ ##
425
+ ## ok {"foo".freeze}.frozen? # Pass
426
+ ## ok {"foo"}.NOT.frozen? # Pass
427
+ ##
428
+ def frozen?
429
+ _mark_as_tested()
430
+ @context.assert_predicate @actual, :frozen? unless @not
431
+ @context.refute_predicate @actual, :frozen? if @not
432
+ self
433
+ end
434
+
435
+ ##
436
+ ## Tests whether object is tainted or not.
437
+ ##
438
+ ## ok {"foo".tainted}.tainted? # Pass
439
+ ## ok {"foo"}.NOT.tainted? # Pass
440
+ ##
441
+ def tainted?
442
+ _mark_as_tested()
443
+ @context.assert_predicate @actual, :tainted? unless @not
444
+ @context.refute_predicate @actual, :tainted? if @not
445
+ self
446
+ end
447
+
448
+ ##
449
+ ## Tests whether object has instance variable or not.
450
+ ##
451
+ ## class User
452
+ ## def initialize(name); @name = name; end
453
+ ## end
454
+ ## ok {User.new('Haruhi')}.instance_variable_defined?('@name') # Pass
455
+ ##
456
+ def instance_variable_defined?(varname)
457
+ _mark_as_tested()
458
+ result = @actual.instance_variable_defined?(varname)
459
+ unless @not
460
+ @context.assert result, "Expected #{@actual.inspect} to have instance variable #{varname}, but not."
461
+ else
462
+ @context.refute result, "Expected #{@actual.inspect} not to have instance variable #{varname}, but has it."
463
+ end
464
+ self
465
+ end
466
+
467
+ ##
468
+ ## When <tt>ok {actual}.xxx?</tt> called, tries <tt>assert_xxx(actual)</tt> at first.
469
+ ## If it is not defined, tries <tt>assert actual.xxx?</tt>.
470
+ ##
471
+ ## ok {'logo.jpg'}.end_with?('.jpg') # Pass
472
+ ## ok {[1, 2, 3]}.all? {|x| x <= 3 } # Pass
473
+ ##
474
+ def method_missing(symbol, *args, &block)
475
+ unless symbol.to_s =~ /\?\z/
476
+ return super
477
+ end
478
+ _mark_as_tested()
479
+ unless @not
480
+ ## Try assert_xxxx() at first.
481
+ ## If not defined, try assert @actual.xxxx?.
482
+ begin
483
+ @context.__send__("assert_#{symbol.to_s[0..-2]}", @actual, *args, &block)
484
+ rescue NoMethodError
485
+ result = @actual.__send__(symbol, *args, &block)
486
+ msg = result ? nil : "Expected #{@actual.inspect}.#{symbol}(#{args.inspect[1..-2]}) but failed."
487
+ @context.assert result, msg
488
+ end
489
+ else
490
+ ## Try refute_xxxx() at first.
491
+ ## If not defined, try refute @actual.xxxx?.
492
+ begin
493
+ @context.__send__("refute_#{symbol.to_s[0..-2]}", @actual, *args, &block)
494
+ rescue NoMethodError
495
+ result = @actual.__send__(symbol, *args, &block)
496
+ msg = ! result ? nil : "Expected #{@actual.inspect}.#{symbol}(#{args.inspect[1..-2]}) to fail but succeeded."
497
+ @context.refute result, msg
498
+ end
499
+ end
500
+ self
501
+ end
502
+
503
+ ## other helpers
504
+
505
+ ##
506
+ ## Tests whether actual is regarded as true-like value.
507
+ ##
508
+ ## ok {true}.truthy? # Pass
509
+ ## ok {0}.truthy? # Pass
510
+ ## ok {""}.truthy? # Pass
511
+ ## ok {[]}.truthy? # Pass
512
+ ## ok {nil}.truthy? # Fail
513
+ ## ok {false}.truthy? # Fail
514
+ ##
515
+ def truthy?
516
+ _mark_as_tested()
517
+ unless @not
518
+ msg = @actual ? nil : "Expected (!! #{@actual.inspect}) == true, but not."
519
+ @context.assert @actual, msg
520
+ else
521
+ msg = ! @actual ? nil : "Expected (!! #{@actual.inspect}) == false, but not."
522
+ @context.refute @actual, msg
523
+ end
524
+ self
525
+ end
526
+
527
+ ##
528
+ ## Tests whether actual is false or nil.
529
+ ##
530
+ ## ok {nil}.falthy? # Pass
531
+ ## ok {false}.falthy? # Pass
532
+ ## ok {true}.falthy? # Fail
533
+ ## ok {0}.falthy? # Fail
534
+ ## ok {""}.falthy? # Fail
535
+ ## ok {[]}.falthy? # Fail
536
+ ##
537
+ def falthy?
538
+ _mark_as_tested()
539
+ unless @not
540
+ msg = ! @actual ? nil : "Expected (!! #{@actual.inspect}) == false, but not."
541
+ @context.refute @actual, msg
542
+ else
543
+ msg = @actual ? nil : "Expected (!! #{@actual.inspect}) == true, but not."
544
+ @context.assert @actual, msg
545
+ end
546
+ self
547
+ end
548
+
549
+ ##
550
+ ## Tests attribute value.
551
+ ##
552
+ ## User = Struct.new(:user, :age) # define User class quickly
553
+ ## user = User.new('Haruhi', 16)
554
+ ## ok {user}.attr(:name, 'Haruhi').attr(:age, 16) # Pass
555
+ ##
556
+ def attr(name, expected)
557
+ _mark_as_tested()
558
+ object = @actual
559
+ actual = object.__send__(name)
560
+ pr = proc {|op|
561
+ "Expected <object>.#{name} #{op} <exected>, but failed.\n" +
562
+ " (object: #{object.inspect})"
563
+ }
564
+ result = expected == actual
565
+ @context.assert_equal expected, actual, (result ? nil : pr.call('==')) unless @not
566
+ @context.refute_equal expected, actual, (result ? pr.call('!=') : nil) if @not
567
+ self
568
+ end
569
+
570
+ ##
571
+ ## Tests attribute values.
572
+ ##
573
+ ## User = Struct.new(:user, :age) # define User class quickly
574
+ ## user = User.new('Haruhi', 16)
575
+ ## ok {user}.attrs(name: 'Haruhi', age: 16) # Pass
576
+ ##
577
+ def attrs(expecteds={})
578
+ _mark_as_tested()
579
+ expecteds.each do |name, expected|
580
+ attr(name, expected)
581
+ end
582
+ self
583
+ end
584
+
585
+ ##
586
+ ## Tests key and value.
587
+ ##
588
+ ## user = {:name=>'Haruhi', :age=>16}
589
+ ## ok {user}.item(:name, 'Haruhi').item(:age, 16) # Pass
590
+ ##
591
+ def item(key, expected)
592
+ _mark_as_tested()
593
+ object = @actual
594
+ actual = object[key]
595
+ pr = proc {|op|
596
+ "Expected <object>[#{key.inspect}] #{op} <exected>, but failed.\n" +
597
+ " (object: #{object.inspect})"
598
+ }
599
+ result = expected == actual
600
+ @context.assert_equal expected, actual, (result ? nil : pr.call('==')) unless @not
601
+ @context.refute_equal expected, actual, (result ? pr.call('!=') : nil) if @not
602
+ self
603
+ end
604
+
605
+ ##
606
+ ## Tests keys and values.
607
+ ##
608
+ ## user = {:name=>'Haruhi', :age=>16}
609
+ ## ok {user}.items(name: 'Haruhi', age: 16) # Pass
610
+ ##
611
+ def items(expecteds={})
612
+ _mark_as_tested()
613
+ expecteds.each do |key, expected|
614
+ item(key, expected)
615
+ end
616
+ self
617
+ end
618
+
619
+ ##
620
+ ## Tests whether file exists or not.
621
+ ##
622
+ ## ok {__FILE__}.file_exist? # Pass
623
+ ## ok {'/some/where'}.file_exist? # Fail
624
+ ## ok {Dir.pwd}.file_exist? # Fail
625
+ ##
626
+ def file_exist?
627
+ _mark_as_tested()
628
+ fpath = @actual
629
+ unless @not
630
+ @context.assert File.exist?(fpath), "File '#{fpath}' doesn't exist."
631
+ @context.assert File.file?(fpath), "'#{fpath}' is not a file."
632
+ else
633
+ @context.refute File.exist?(fpath), "File '#{fpath}' exists unexpectedly."
634
+ end
635
+ end
636
+
637
+ ##
638
+ ## Tests whether directory exists or not.
639
+ ##
640
+ ## ok {Dir.pwd}.dir_exist? # Pass
641
+ ## ok {'/some/where'}.dir_exist? # Fail
642
+ ## ok {__FILE__}.dir_exist? # Fail
643
+ ##
644
+ def dir_exist?
645
+ _mark_as_tested()
646
+ fpath = @actual
647
+ unless @not
648
+ @context.assert File.exist?(fpath), "Directory '#{fpath}' doesn't exist."
649
+ @context.assert File.directory?(fpath), "'#{fpath}' is not a directory."
650
+ else
651
+ @context.refute File.exist?(fpath), "Directory '#{fpath}' exists unexpectedly."
652
+ end
653
+ end
654
+
655
+ ##
656
+ ## Tests whether file or directory not exist.
657
+ ##
658
+ ## ok {'/some/where'}.not_exist? # Pass
659
+ ## ok {__FILE__}.not_exist? # Fail
660
+ ## ok {Dir.pwd}.not_exist? # Fail
661
+ ##
662
+ def not_exist?
663
+ _mark_as_tested()
664
+ fpath = @actual
665
+ @context.assert ! File.exist?(fpath), "'#{fpath}' exists unexpectedly." unless @not
666
+ @context.refute ! File.exist?(fpath), "'#{fpath}' doesn't exist." if @not
667
+ self
668
+ end
669
+
670
+ end
671
+
672
+
673
+ end
674
+
675
+
676
+ end