oktest 1.3.1 → 1.5.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.
data/test/helper_test.rb CHANGED
@@ -1,10 +1,13 @@
1
+ # -*- coding: utf-8 -*-
2
+ # frozen_string_literal: true
3
+
1
4
  ###
2
- ### $Release: 1.3.1 $
5
+ ### $Release: 1.5.0 $
3
6
  ### $Copyright: copyright(c) 2011-2024 kuwata-lab.com all rights reserved $
4
7
  ### $License: MIT License $
5
8
  ###
6
9
 
7
- require_relative './initialize'
10
+ require_relative './init'
8
11
 
9
12
 
10
13
  class DummyUser
@@ -16,83 +19,85 @@ class DummyUser
16
19
  end
17
20
 
18
21
 
19
- class SpecHelper_TC < TC
20
- include Oktest::SpecHelper
21
-
22
- def setup()
23
- end
22
+ class SpecHelper__Test
23
+ extend NanoTest
24
+ extend Oktest::SpecHelper
24
25
 
25
- def teardown()
26
+ def self.test_subject(desc, &b)
27
+ super
28
+ ensure
26
29
  Oktest::AssertionObject::NOT_YET.clear()
30
+ @__at_end_blocks = nil
27
31
  end
28
32
 
29
- describe '#ok()' do
30
- it "[!3jhg6] creates new assertion object." do
33
+ test_target 'Oktest::SpecHelper#ok()' do
34
+ test_subject "[!3jhg6] creates new assertion object." do
31
35
  o = ok {"foo"}
32
- assert_eq o.class, Oktest::AssertionObject
33
- assert_eq o.actual, "foo"
34
- assert_eq o.bool, true
36
+ test_eq? o.class, Oktest::AssertionObject
37
+ test_eq? o.actual, "foo"
38
+ test_eq? o.bool, true
35
39
  end
36
- it "[!bc3l2] records invoked location." do
40
+ test_subject "[!bc3l2] records invoked location." do
37
41
  lineno = __LINE__ + 1
38
42
  o = ok {"bar"}
39
- assert o.location.to_s.start_with?("#{__FILE__}:#{lineno}:")
43
+ test_ok? o.location.to_s.start_with?("#{__FILE__}:#{lineno}:")
40
44
  end
41
- it "[!mqtdy] not record invoked location when `Config.ok_location == false`." do
45
+ test_subject "[!mqtdy] not record invoked location when `Config.ok_location == false`." do
42
46
  bkup = Oktest::Config.ok_location
43
47
  begin
44
48
  Oktest::Config.ok_location = false
45
49
  o = ok {"bar"}
46
- assert_eq o.location, nil
50
+ test_eq? o.location, nil
47
51
  ensure
48
52
  Oktest::Config.ok_location = bkup
49
53
  end
50
54
  end
51
55
  end
52
56
 
53
- describe '#not_ok()' do
54
- it "[!d332o] creates new assertion object for negative condition." do
57
+ test_target 'Oktest::SpecHelper#not_ok()' do
58
+ test_subject "[!d332o] creates new assertion object for negative condition." do
55
59
  o = not_ok {"abc"}
56
- assert_eq o.class, Oktest::AssertionObject
57
- assert_eq o.actual, "abc"
58
- assert_eq o.bool, false
60
+ test_eq? o.class, Oktest::AssertionObject
61
+ test_eq? o.actual, "abc"
62
+ test_eq? o.bool, false
59
63
  end
60
- it "[!agmx8] records invoked location." do
64
+ test_subject "[!agmx8] records invoked location." do
61
65
  lineno = __LINE__ + 1
62
66
  o = not_ok {"bar"}
63
- assert o.location.to_s.start_with?("#{__FILE__}:#{lineno}:")
67
+ test_ok? o.location.to_s.start_with?("#{__FILE__}:#{lineno}:")
64
68
  end
65
- it "[!a9508] not record invoked location when `Config.ok_location == false`." do
69
+ test_subject "[!a9508] not record invoked location when `Config.ok_location == false`." do
66
70
  bkup = Oktest::Config.ok_location
67
71
  begin
68
72
  Oktest::Config.ok_location = false
69
73
  o = not_ok {"bar"}
70
- assert_eq o.location, nil
74
+ test_eq? o.location, nil
71
75
  ensure
72
76
  Oktest::Config.ok_location = bkup
73
77
  end
74
78
  end
75
79
  end
76
80
 
77
- describe '#skip_when()' do
78
- it "[!3xqf4] raises SkipException if condition is truthy." do
79
- assert_exc(Oktest::SkipException, "..reason..") do
81
+ test_target 'Oktest::SpecHelper#skip_when()' do
82
+ test_subject "[!3xqf4] raises SkipException if condition is truthy." do
83
+ exc = test_exception? Oktest::SkipException do
80
84
  skip_when (1+1 == 2), "..reason.."
81
85
  end
86
+ test_eq? exc.message, "..reason.."
82
87
  end
83
- it "[!r7cxx] not raise nothing if condition is falsy." do
88
+ test_subject "[!r7cxx] not raise nothing if condition is falsy." do
84
89
  begin
85
90
  skip_when (1+1 == 0), "..reason.."
86
91
  rescue Exception => exc
87
- assert false, "nothing should be raised but #{exc.class} raised"
92
+ test_ok? false, "nothing should be raised but #{exc.class} raised"
88
93
  else
89
- assert true, "OK"
94
+ test_ok? true, msg: "OK"
90
95
  end
91
96
  end
92
97
  end
93
98
 
94
- describe '#fixture()' do
95
- it "[!m4ava] calls fixture block and returns result of it." do
99
+ test_target 'Oktest::SpecHelper#fixture()' do
100
+ test_subject "[!m4ava] calls fixture block and returns result of it." do
96
101
  val = nil
97
102
  Oktest.scope() do
98
103
  topic 'Example' do
@@ -102,10 +107,10 @@ class SpecHelper_TC < TC
102
107
  end
103
108
  end
104
109
  end
105
- capture { Oktest.run() }
106
- assert_eq val, "<<foo>>"
110
+ capture_output! { Oktest.run() }
111
+ test_eq? val, "<<foo>>"
107
112
  end
108
- it "[!zgfg9] finds fixture block in current or parent node." do
113
+ test_subject "[!zgfg9] finds fixture block in current or parent node." do
109
114
  val1 = val2 = val3 = nil
110
115
  Oktest.scope() do
111
116
  fixture :foo do "<<foo>>" end
@@ -121,12 +126,12 @@ class SpecHelper_TC < TC
121
126
  end
122
127
  end
123
128
  end
124
- capture { Oktest.run() }
125
- assert_eq val1, "<<baz>>"
126
- assert_eq val2, "<<bar>>"
127
- assert_eq val3, "<<foo>>"
129
+ capture_output! { Oktest.run() }
130
+ test_eq? val1, "<<baz>>"
131
+ test_eq? val2, "<<bar>>"
132
+ test_eq? val3, "<<foo>>"
128
133
  end
129
- it "[!l2mcx] accepts block arguments." do
134
+ test_subject "[!l2mcx] accepts block arguments." do
130
135
  val = nil
131
136
  Oktest.scope() do
132
137
  fixture :foo do |x, y, z: 0|
@@ -138,10 +143,10 @@ class SpecHelper_TC < TC
138
143
  end
139
144
  end
140
145
  end
141
- capture { Oktest.run() }
142
- assert_eq val, {x: 10, y: 20, z: 30}
146
+ capture_output! { Oktest.run() }
147
+ test_eq? val, {x: 10, y: 20, z: 30}
143
148
  end
144
- it "[!wxcsp] raises error when fixture not found." do
149
+ test_subject "[!wxcsp] raises error when fixture not found." do
145
150
  exc = nil
146
151
  Oktest.scope() do
147
152
  fixture :foo do "<<foo>>" end
@@ -154,14 +159,14 @@ class SpecHelper_TC < TC
154
159
  end
155
160
  end
156
161
  end
157
- capture { Oktest.run() }
158
- assert_eq exc.class, Oktest::FixtureNotFoundError
159
- assert_eq exc.message, "`:bar`: fixture not found."
162
+ capture_output! { Oktest.run() }
163
+ test_eq? exc.class, Oktest::FixtureNotFoundError
164
+ test_eq? exc.message, "`:bar`: fixture not found."
160
165
  end
161
166
  end
162
167
 
163
- describe '#at_end()' do
164
- it "[!x58eo] records clean-up block." do
168
+ test_target 'Oktest::SpecHelper#at_end()' do
169
+ test_subject "[!x58eo] records clean-up block." do
165
170
  Oktest.scope() do
166
171
  topic 'Example' do
167
172
  spec 'sample #1' do
@@ -171,173 +176,346 @@ class SpecHelper_TC < TC
171
176
  end
172
177
  end
173
178
  end
174
- sout, serr = capture { Oktest.run() }
179
+ sout, serr = capture_output! { Oktest.run() }
175
180
  expected = <<'END'
176
181
  before at_end()
177
182
  after at_end()
178
183
  in at_end()
179
184
  END
180
- assert sout.include?(expected), "not matched"
185
+ test_ok? sout.include?(expected), msg: "not matched"
181
186
  end
182
187
  end
183
188
 
184
- describe '#capture_sio()' do
185
- it "[!1kbnj] captures $stdio and $stderr." do
186
- sout, serr = capture_sio() do
189
+ test_target 'Oktest::SpecHelper#capture_stdio()' do
190
+ test_subject "[!1kbnj] captures $stdio and $stderr." do
191
+ sout, serr = capture_stdio() do
187
192
  puts "fooo"
188
193
  $stderr.puts "baaa"
189
194
  end
190
- assert_eq sout, "fooo\n"
191
- assert_eq serr, "baaa\n"
195
+ test_eq? sout, "fooo\n"
196
+ test_eq? serr, "baaa\n"
192
197
  end
193
- it "[!53mai] takes $stdin data." do
198
+ test_subject "[!53mai] takes $stdin data." do
194
199
  data = nil
195
- sout, serr = capture_sio("blabla") do
200
+ sout, serr = capture_stdio("blabla") do
196
201
  data = $stdin.read()
197
202
  end
198
203
  data = "blabla"
199
204
  end
200
- it "[!wq8a9] recovers stdio even when exception raised." do
205
+ test_subject "[!wq8a9] recovers stdio even when exception raised." do
201
206
  stdin_, stdout_, stderr_ = $stdin, $stdout, $stderr
202
207
  exception = nil
203
208
  begin
204
- sout, serr = capture_sio() do
209
+ sout, serr = capture_stdio() do
205
210
  puts "fooo"
206
211
  $stderr.puts "baaa"
207
- assert stdin_ != $stdin , "stdin should be replaced"
208
- assert stdout_ != $stdout, "stdout should be replaced"
209
- assert stderr_ != $stderr, "stderr should be replaced"
212
+ test_ok? stdin_ != $stdin , msg: "stdin should be replaced"
213
+ test_ok? stdout_ != $stdout, msg: "stdout should be replaced"
214
+ test_ok? stderr_ != $stderr, msg: "stderr should be replaced"
210
215
  1/0 # ZeroDivisionError
211
216
  end
212
217
  rescue ZeroDivisionError => exc
213
218
  exception = exc
214
219
  end
215
- assert exception != nil, "exception should be raised."
216
- assert exception.is_a?(ZeroDivisionError), "ZeroDivisionError should be raised."
217
- assert stdin_ == $stdin , "stdin should be recovered"
218
- assert stdout_ == $stdout, "stdout should be recovered"
219
- assert stderr_ == $stderr, "stderr should be recovered"
220
- end
221
- it "[!4j494] returns outpouts of stdout and stderr." do
222
- sout, serr = capture_sio() do
220
+ test_ok? exception != nil, msg: "exception should be raised."
221
+ test_ok? exception.is_a?(ZeroDivisionError), msg: "ZeroDivisionError should be raised."
222
+ test_ok? stdin_ == $stdin , msg: "stdin should be recovered"
223
+ test_ok? stdout_ == $stdout, msg: "stdout should be recovered"
224
+ test_ok? stderr_ == $stderr, msg: "stderr should be recovered"
225
+ end
226
+ test_subject "[!4j494] returns outpouts of stdout and stderr." do
227
+ sout, serr = capture_stdio() do
223
228
  puts "foo"
224
229
  $stderr.puts "bar"
225
230
  end
226
- assert_eq sout, "foo\n"
227
- assert_eq serr, "bar\n"
231
+ test_eq? sout, "foo\n"
232
+ test_eq? serr, "bar\n"
233
+ end
234
+ test_subject "[!6ik8b] can simulate tty." do
235
+ sout, serr = capture_stdio() do
236
+ test_eq? $stdin.tty?, false
237
+ test_eq? $stdout.tty?, false
238
+ test_eq? $stderr.tty?, false
239
+ end
240
+ #
241
+ sout, serr = capture_stdio(tty: true) do
242
+ test_eq? $stdin.tty?, true
243
+ test_eq? $stdout.tty?, true
244
+ test_eq? $stderr.tty?, true
245
+ end
246
+ end
247
+ end
248
+
249
+ test_target 'Oktest::SpecHelper#capture_sio()' do
250
+ test_subject "[!qjmaa] 'capture_sio()' is an alias of 'capture_stdio()'." do
251
+ sin = nil
252
+ sout, serr = capture_sio("INPUT", tty: true) do
253
+ sin = $stdin.read()
254
+ puts "OUTPUT"
255
+ $stderr.puts "ERROR"
256
+ test_eq? $stdin.tty?, true
257
+ test_eq? $stdout.tty?, true
258
+ test_eq? $stdout.tty?, true
259
+ end
260
+ test_eq? sin, "INPUT"
261
+ test_eq? sout, "OUTPUT\n"
262
+ test_eq? serr, "ERROR\n"
263
+ end
264
+ end
265
+
266
+ test_target 'Oktest::SpecHelper#capture_stdout()' do
267
+ test_subject "[!4agii] same as `sout, serr = capture_stdio(); ok {serr} == ''`." do
268
+ sin = nil
269
+ sout = capture_stdout("INPUT", tty: true) do
270
+ sin = $stdin.read()
271
+ puts "OUTPUT"
272
+ test_eq? $stdin.tty?, true
273
+ test_eq? $stdout.tty?, true
274
+ test_eq? $stderr.tty?, true
275
+ end
276
+ test_eq? sin, "INPUT"
277
+ test_eq? sout, "OUTPUT\n"
278
+ end
279
+ test_subject "[!may84] fails when stderr is not empty." do
280
+ exc = test_exception? Oktest::AssertionFailed do
281
+ capture_stdout() do
282
+ $stderr.print "ERROR"
283
+ end
284
+ end
285
+ test_eq? exc.message, "Output of $stderr expected to be empty, but got: \"ERROR\""
286
+ end
287
+ test_subject "[!5n04e] returns output of stdout." do
288
+ sout = capture_stdout() do
289
+ print "OUTPUT"
290
+ end
291
+ test_eq? sout, "OUTPUT"
292
+ end
293
+ end
294
+
295
+ test_target 'Oktest::SpecHelper#capture_stderr()' do
296
+ test_subject "[!46tj4] same as `sout, serr = capture_stdio(); ok {sout} == ''`." do
297
+ sin = nil
298
+ serr = capture_stderr("INPUT", tty: true) do
299
+ sin = $stdin.read()
300
+ $stderr.puts "ERROR"
301
+ test_eq? $stdin.tty?, true
302
+ test_eq? $stdout.tty?, true
303
+ test_eq? $stderr.tty?, true
304
+ end
305
+ test_eq? sin, "INPUT"
306
+ test_eq? serr, "ERROR\n"
307
+ end
308
+ test_subject "[!3zh32] fails when stdout is not empty." do
309
+ exc = test_exception? Oktest::AssertionFailed do
310
+ capture_stderr() do
311
+ print "OUTPUT"
312
+ end
313
+ end
314
+ test_eq? exc.message, "Output of $stdout expected to be empty, but got: \"OUTPUT\""
315
+ end
316
+ test_subject "[!5vs64] returns output of stderr." do
317
+ serr = capture_stderr() do
318
+ $stderr.print "ERROR"
319
+ end
320
+ test_eq? serr, "ERROR"
228
321
  end
229
- it "[!6ik8b] can simulate tty." do
230
- sout, serr = capture_sio() do
231
- assert_eq $stdin.tty?, false
232
- assert_eq $stdout.tty?, false
233
- assert_eq $stderr.tty?, false
322
+ end
323
+
324
+ test_target 'Oktest::SpecHelper#capture_command()' do
325
+ test_subject "[!wyp17] executes command with stdin data." do
326
+ sout, serr = capture_command("cat -n", "AAA\nBBB\n")
327
+ test_eq? sout, " 1\tAAA\n 2\tBBB\n"
328
+ test_eq? serr, ""
329
+ end
330
+ test_subject "[!jd63p] raises error if command failed." do
331
+ begin
332
+ capture_command("ls *not*exist*")
333
+ rescue => exc
334
+ test_eq? exc.class, RuntimeError
335
+ test_eq? exc.message, "Command failed with status (1): `ls *not*exist*`"
336
+ else
337
+ test_ok? false, msg: "Exception should be raised but not."
234
338
  end
339
+ end
340
+ test_subject "[!lsmgq] calls error handler block if command failed." do
341
+ called = nil
342
+ sout, serr = capture_command("ls *not*exist*") do |pstat|
343
+ called = pstat
344
+ end
345
+ test_ok? called.is_a?(Process::Status)
346
+ test_ok? called.exitstatus == 1
347
+ test_eq? sout, ""
348
+ test_eq? serr, "ls: *not*exist*: No such file or directory\n"
349
+ end
350
+ test_subject "[!vivq3] doesn't call error handler block if command finished successfully." do
351
+ called = false
352
+ sout, serr = capture_command("cat -n", "AAA\nBBB\n") do
353
+ called = true
354
+ end
355
+ test_eq? called, false
356
+ test_eq? sout, " 1\tAAA\n 2\tBBB\n"
357
+ test_eq? serr, ""
358
+ end
359
+ test_subject "[!nxw59] not raise error if command failed and error handler specified." do
360
+ begin
361
+ sout, serr = capture_command("ls *not*exist*") do end
362
+ rescue => exc
363
+ test_ok? false, msg: "Exception should not raised, but raised #{exc.inspect}"
364
+ end
365
+ test_eq? sout, ""
366
+ test_eq? serr, "ls: *not*exist*: No such file or directory\n"
367
+ end
368
+ test_subject "[!h5994] returns output of stdin and stderr." do
369
+ sout, serr = capture_command("cat -n", "AAA\nBBB\n")
370
+ test_eq? sout, " 1\tAAA\n 2\tBBB\n"
371
+ test_eq? serr, ""
235
372
  #
236
- sout, serr = capture_sio(tty: true) do
237
- assert_eq $stdin.tty?, true
238
- assert_eq $stdout.tty?, true
239
- assert_eq $stderr.tty?, true
373
+ sout, serr = capture_command("echo ERR >&2")
374
+ test_eq? sout, ""
375
+ test_eq? serr, "ERR\n"
376
+ end
377
+ end
378
+
379
+ test_target 'Oktest::SpecHelper#capture_command!()' do
380
+ test_subject "[!vlbpo] executes command with stdin data." do
381
+ sout, serr = capture_command!("cat -n", "AAA\nBBB\n")
382
+ test_eq? sout, " 1\tAAA\n 2\tBBB\n"
383
+ test_eq? serr, ""
384
+ end
385
+ test_subject "[!yfohb] not raise error even if command failed." do
386
+ sout, serr = capture_command!("ls *not*exist*")
387
+ test_eq? sout, ""
388
+ test_eq? serr, "ls: *not*exist*: No such file or directory\n"
389
+ end
390
+ test_subject "[!andyj] calls error handler block if command failed." do
391
+ called = nil
392
+ sout, serr = capture_command!("ls *not*exist*") do |pstat|
393
+ called = pstat
240
394
  end
395
+ test_ok? called.is_a?(Process::Status)
396
+ test_ok? called.exitstatus == 1
397
+ test_eq? sout, ""
398
+ test_eq? serr, "ls: *not*exist*: No such file or directory\n"
399
+ end
400
+ test_subject "[!xnkqc] doesn't call error handler block if command finished successfully." do
401
+ called = false
402
+ sout, serr = capture_command!("cat -n", "AAA\nBBB\n") do
403
+ called = true
404
+ end
405
+ test_eq? called, false
406
+ test_eq? sout, " 1\tAAA\n 2\tBBB\n"
407
+ test_eq? serr, ""
408
+ end
409
+ test_subject "[!3xdgo] returns output of stdin and stderr." do
410
+ sout, serr = capture_command!("cat -n", "AAA\nBBB\n")
411
+ test_eq? sout, " 1\tAAA\n 2\tBBB\n"
412
+ test_eq? serr, ""
413
+ #
414
+ sout, serr = capture_command!("echo ERR >&2")
415
+ test_eq? sout, ""
416
+ test_eq? serr, "ERR\n"
241
417
  end
242
418
  end
243
419
 
244
- describe '#dummy_file()' do
245
- it "[!7e0bo] creates dummy file." do
420
+ test_target 'Oktest::SpecHelper#dummy_file()' do
421
+ test_subject "[!7e0bo] creates dummy file." do
246
422
  tmpfile = "_tmp_3511.txt"
247
423
  File.unlink(tmpfile) if File.exist?(tmpfile)
248
424
  begin
249
425
  dummy_file(tmpfile, "foobar")
250
- assert File.exist?(tmpfile), "tmpfile should be created."
251
- assert_eq @__at_end_blocks.length, 1
426
+ test_ok? File.exist?(tmpfile), msg: "tmpfile should be created."
427
+ test_eq? @__at_end_blocks.length, 1
252
428
  pr = @__at_end_blocks.pop()
253
429
  pr.call()
254
- assert !File.exist?(tmpfile), "tmpfile should be removed."
430
+ test_ok? !File.exist?(tmpfile), msg: "tmpfile should be removed."
255
431
  ensure
256
432
  File.unlink(tmpfile) if File.exist?(tmpfile)
257
433
  end
258
434
  end
259
- it "[!yvfxq] raises error when dummy file already exists." do
435
+ test_subject "[!yvfxq] raises error when dummy file already exists." do
260
436
  tmp = "_tmp_4883.txt"
261
437
  [true, false].each do |flag|
262
438
  begin
263
439
  flag ? File.write(tmp, "") : Dir.mkdir(tmp)
264
- assert_exc(ArgumentError, "dummy_file('#{tmp}'): temporary file already exists.") do
440
+ exc = test_exception? ArgumentError do
265
441
  dummy_file(tmp, "foobar")
266
442
  end
443
+ test_eq? exc.message, "dummy_file('#{tmp}'): temporary file already exists."
267
444
  ensure
268
445
  File.unlink(tmp) if File.file?(tmp)
269
446
  Dir.rmdir(tmp) if File.directory?(tmp)
270
447
  end
271
448
  end
272
449
  end
273
- it "[!nvlkq] returns filename." do
450
+ test_subject "[!nvlkq] returns filename." do
274
451
  tmpfile = "_tmp_4947.txt"
275
452
  begin
276
453
  ret = dummy_file(tmpfile, "foobar")
277
- assert_eq ret, tmpfile
454
+ test_eq? ret, tmpfile
278
455
  ensure
279
456
  File.unlink(tmpfile) if File.exist?(tmpfile)
280
457
  end
281
458
  end
282
- it "[!3mg26] generates temporary filename if 1st arg is nil." do
459
+ test_subject "[!3mg26] generates temporary filename if 1st arg is nil." do
283
460
  begin
284
461
  tmpfile1 = dummy_file(nil, "foobar")
285
462
  tmpfile2 = dummy_file(nil, "foobar")
286
- assert tmpfile1 =~ /^_tmpfile_\d{6}/, "tempoary filename should be generated."
287
- assert tmpfile2 =~ /^_tmpfile_\d{6}/, "tempoary filename should be generated."
288
- assert tmpfile1 != tmpfile2, "tempoary filename should contain random number."
463
+ test_ok? tmpfile1 =~ /^_tmpfile_\d{6}/, msg: "tempoary filename should be generated."
464
+ test_ok? tmpfile2 =~ /^_tmpfile_\d{6}/, msg: "tempoary filename should be generated."
465
+ test_ok? tmpfile1 != tmpfile2, msg: "tempoary filename should contain random number."
289
466
  ensure
290
467
  File.unlink(tmpfile1) if File.exist?(tmpfile1)
291
468
  File.unlink(tmpfile2) if File.exist?(tmpfile2)
292
469
  end
293
470
  end
294
- it "[!ky7nh] can take block argument." do
471
+ test_subject "[!ky7nh] can take block argument." do
295
472
  tmpfile = "_tmp_9080"
296
473
  begin
297
474
  ret = dummy_file(tmpfile) do |filename|
298
- assert_eq filename, tmpfile
299
- assert File.file?(tmpfile), "tmpfile should be created."
475
+ test_eq? filename, tmpfile
476
+ test_ok? File.file?(tmpfile), msg: "tmpfile should be created."
300
477
  1234
301
478
  end
302
- assert !File.file?(tmpfile), "tmpfile should be removed."
303
- assert_eq ret, 1234
304
- assert_eq @__at_end_blocks, nil
479
+ test_ok? !File.file?(tmpfile), msg: "tmpfile should be removed."
480
+ test_eq? ret, 1234
481
+ test_eq? @__at_end_blocks, nil
305
482
  ensure
306
483
  File.unlink(tmpfile) if File.exist?(tmpfile)
307
484
  end
308
485
  end
309
486
  end
310
487
 
311
- describe '#dummy_dir()' do
312
- it "[!l34d5] creates dummy directory." do
488
+ test_target 'Oktest::SpecHelper#dummy_dir()' do
489
+ test_subject "[!l34d5] creates dummy directory." do
313
490
  tmpdir = "_tmpdir_7903"
314
491
  Dir.rmdir(tmpdir) if File.exist?(tmpdir)
315
492
  begin
316
493
  dummy_dir(tmpdir)
317
- assert File.exist?(tmpdir), "tmpdir should be created."
318
- assert_eq @__at_end_blocks.length, 1
494
+ test_ok? File.exist?(tmpdir), msg: "tmpdir should be created."
495
+ test_eq? @__at_end_blocks.length, 1
319
496
  pr = @__at_end_blocks.pop()
320
497
  pr.call()
321
- assert !File.exist?(tmpdir), "tmpdir should be removed."
498
+ test_ok? !File.exist?(tmpdir), msg: "tmpdir should be removed."
322
499
  ensure
323
500
  Dir.rmdir(tmpdir) if File.exist?(tmpdir)
324
501
  end
325
502
  end
326
- it "[!zypj6] raises error when dummy dir already exists." do
503
+ test_subject "[!zypj6] raises error when dummy dir already exists." do
327
504
  tmp = "_tmpdir_1062"
328
505
  [true, false].each do |flag|
329
506
  begin
330
507
  flag ? Dir.mkdir(tmp) : File.write(tmp, "")
331
- assert_exc(ArgumentError, "dummy_dir('#{tmp}'): temporary directory already exists.") do
508
+ exc = test_exception? ArgumentError do
332
509
  dummy_dir(tmp)
333
510
  end
511
+ test_eq? exc.message, "dummy_dir('#{tmp}'): temporary directory already exists."
334
512
  ensure
335
513
  Dir.rmdir(tmp) if File.directory?(tmp)
336
514
  File.unlink(tmp) if File.file?(tmp)
337
515
  end
338
516
  end
339
517
  end
340
- it "[!01gt7] removes dummy directory even if it contains other files." do
518
+ test_subject "[!01gt7] removes dummy directory even if test_subject contains other files." do
341
519
  tmpdir = "_tmpdir_3869"
342
520
  begin
343
521
  dummy_dir(tmpdir)
@@ -345,197 +523,197 @@ END
345
523
  Dir.mkdir("#{tmpdir}/d1")
346
524
  Dir.mkdir("#{tmpdir}/d1/d2")
347
525
  File.write("#{tmpdir}/d1/d2/bar.txt", "barbar", encoding: 'utf-8')
348
- assert File.exist?("#{tmpdir}/foo.txt"), "should exists."
349
- assert File.exist?("#{tmpdir}/d1/d2/bar.txt"), "should exists."
526
+ test_ok? File.exist?("#{tmpdir}/foo.txt"), msg: "should exists."
527
+ test_ok? File.exist?("#{tmpdir}/d1/d2/bar.txt"), msg: "should exists."
350
528
  #
351
529
  pr = @__at_end_blocks.pop()
352
530
  pr.call()
353
- assert !File.exist?(tmpdir), "tmpdir should be removed."
531
+ test_ok? !File.exist?(tmpdir), msg: "tmpdir should be removed."
354
532
  ensure
355
533
  FileUtils.rm_rf(tmpdir) if File.exist?(tmpdir)
356
534
  end
357
535
  end
358
- it "[!jxh30] returns directory name." do
536
+ test_subject "[!jxh30] returns directory name." do
359
537
  tmpdir = "_tmpdir_2546"
360
538
  begin
361
539
  ret = dummy_dir(tmpdir)
362
- assert_eq ret, tmpdir
540
+ test_eq? ret, tmpdir
363
541
  ensure
364
542
  Dir.rmdir(tmpdir) if File.exist?(tmpdir)
365
543
  end
366
544
  end
367
- it "[!r14uy] generates temporary directory name if 1st arg is nil." do
545
+ test_subject "[!r14uy] generates temporary directory name if 1st arg is nil." do
368
546
  begin
369
547
  tmpdir1 = dummy_dir(nil)
370
548
  tmpdir2 = dummy_dir()
371
- assert tmpdir1 =~ /^_tmpdir_\d{6}/, "tempoary directory name should be generated."
372
- assert tmpdir2 =~ /^_tmpdir_\d{6}/, "tempoary directory name should be generated."
373
- assert tmpdir1 != tmpdir2, "tempoary directory name should contain random number."
549
+ test_ok? tmpdir1 =~ /^_tmpdir_\d{6}/, msg: "tempoary directory name should be generated."
550
+ test_ok? tmpdir2 =~ /^_tmpdir_\d{6}/, msg: "tempoary directory name should be generated."
551
+ test_ok? tmpdir1 != tmpdir2, msg: "tempoary directory name should contain random number."
374
552
  ensure
375
553
  Dir.rmdir(tmpdir1) if File.exist?(tmpdir1)
376
554
  Dir.rmdir(tmpdir2) if File.exist?(tmpdir2)
377
555
  end
378
556
  end
379
- it "[!tfsqo] can take block argument." do
557
+ test_subject "[!tfsqo] can take block argument." do
380
558
  tmpdir = "_tmp_5799"
381
559
  begin
382
560
  ret = dummy_dir(tmpdir) do |dirname|
383
- assert_eq dirname, tmpdir
384
- assert File.directory?(tmpdir), "tmpdir should be created."
561
+ test_eq? dirname, tmpdir
562
+ test_ok? File.directory?(tmpdir), msg: "tmpdir should be created."
385
563
  2345
386
564
  end
387
- assert !File.directory?(tmpdir), "tmpdir should be removed."
388
- assert_eq ret, 2345
389
- assert_eq @__at_end_blocks, nil
565
+ test_ok? !File.directory?(tmpdir), msg: "tmpdir should be removed."
566
+ test_eq? ret, 2345
567
+ test_eq? @__at_end_blocks, nil
390
568
  ensure
391
569
  Dir.rmdir(tmpdir) if File.exist?(tmpdir)
392
570
  end
393
571
  end
394
572
  end
395
573
 
396
- describe '#dummy_values()' do
397
- it "[!hgwg2] changes hash value temporarily." do
574
+ test_target 'Oktest::SpecHelper#dummy_values()' do
575
+ test_subject "[!hgwg2] changes hash value temporarily." do
398
576
  hashobj = {:a=>10, 'b'=>20, :c=>30}
399
577
  dummy_values(hashobj, :a=>1000, 'b'=>2000, :x=>9000)
400
- assert_eq hashobj[:a], 1000
401
- assert_eq hashobj['b'], 2000
402
- assert_eq hashobj[:c], 30
403
- assert_eq hashobj[:x], 9000
578
+ test_eq? hashobj[:a], 1000
579
+ test_eq? hashobj['b'], 2000
580
+ test_eq? hashobj[:c], 30
581
+ test_eq? hashobj[:x], 9000
404
582
  end
405
- it "[!jw2kx] recovers hash values." do
583
+ test_subject "[!jw2kx] recovers hash values." do
406
584
  hashobj = {:a=>10, 'b'=>20, :c=>30}
407
585
  dummy_values(hashobj, :a=>1000, 'b'=>2000, :x=>9000)
408
- assert_eq hashobj[:a], 1000
409
- assert_eq hashobj['b'], 2000
410
- assert_eq hashobj[:c], 30
411
- assert_eq hashobj[:x], 9000
412
- assert_eq @__at_end_blocks.length, 1
586
+ test_eq? hashobj[:a], 1000
587
+ test_eq? hashobj['b'], 2000
588
+ test_eq? hashobj[:c], 30
589
+ test_eq? hashobj[:x], 9000
590
+ test_eq? @__at_end_blocks.length, 1
413
591
  pr = @__at_end_blocks.pop()
414
592
  pr.call()
415
- assert_eq hashobj[:a], 10
416
- assert_eq hashobj['b'], 20
417
- assert_eq hashobj[:c], 30
418
- assert !hashobj.key?(:x), "key :x should not exist."
593
+ test_eq? hashobj[:a], 10
594
+ test_eq? hashobj['b'], 20
595
+ test_eq? hashobj[:c], 30
596
+ test_ok? !hashobj.key?(:x), msg: "key :x should not exist."
419
597
  end
420
- it "[!w3r0p] returns keyvals." do
598
+ test_subject "[!w3r0p] returns keyvals." do
421
599
  hashobj = {:a=>10, 'b'=>20, :c=>30}
422
600
  ret = dummy_values(hashobj, :a=>1000, 'b'=>2000, :x=>9000)
423
- assert_eq ret, {:a=>1000, 'b'=>2000, :x=>9000}
601
+ test_eq? ret, {:a=>1000, 'b'=>2000, :x=>9000}
424
602
  end
425
- it "[!pwq6v] can take block argument." do
603
+ test_subject "[!pwq6v] can take block argument." do
426
604
  hashobj = {:a=>10, 'b'=>20, :c=>30}
427
605
  ret = dummy_values(hashobj, :a=>1000, 'b'=>2000, :x=>9000) do |kvs|
428
- assert_eq hashobj[:a], 1000
429
- assert_eq hashobj['b'], 2000
430
- assert_eq hashobj[:c], 30
431
- assert_eq hashobj[:x], 9000
432
- assert_eq kvs, {:a=>1000, 'b'=>2000, :x=>9000}
606
+ test_eq? hashobj[:a], 1000
607
+ test_eq? hashobj['b'], 2000
608
+ test_eq? hashobj[:c], 30
609
+ test_eq? hashobj[:x], 9000
610
+ test_eq? kvs, {:a=>1000, 'b'=>2000, :x=>9000}
433
611
  5678
434
612
  end
435
- assert_eq ret, 5678
436
- assert_eq hashobj[:a], 10
437
- assert_eq hashobj['b'], 20
438
- assert_eq hashobj[:c], 30
439
- assert !hashobj.key?(:x), "key :x should not exist."
440
- assert_eq @__at_end_blocks, nil
613
+ test_eq? ret, 5678
614
+ test_eq? hashobj[:a], 10
615
+ test_eq? hashobj['b'], 20
616
+ test_eq? hashobj[:c], 30
617
+ test_ok? !hashobj.key?(:x), msg: "key :x should not exist."
618
+ test_eq? @__at_end_blocks, nil
441
619
  end
442
620
  end
443
621
 
444
- describe '#dummy_attrs()' do
445
- it "[!4vd73] changes object attributes temporarily." do
622
+ test_target 'Oktest::SpecHelper#dummy_attrs()' do
623
+ test_subject "[!4vd73] changes object attributes temporarily." do
446
624
  obj = DummyUser.new(123, "alice")
447
625
  dummy_attrs(obj, :id=>999, :name=>"bob")
448
- assert_eq obj.id, 999
449
- assert_eq obj.name, "bob"
626
+ test_eq? obj.id, 999
627
+ test_eq? obj.name, "bob"
450
628
  end
451
- it "[!fi0t3] recovers attribute values." do
629
+ test_subject "[!fi0t3] recovers attribute values." do
452
630
  obj = DummyUser.new(123, "alice")
453
631
  dummy_attrs(obj, :id=>999, :name=>"bob")
454
- assert_eq obj.id, 999
455
- assert_eq obj.name, "bob"
632
+ test_eq? obj.id, 999
633
+ test_eq? obj.name, "bob"
456
634
  #
457
- assert_eq @__at_end_blocks.length, 1
635
+ test_eq? @__at_end_blocks.length, 1
458
636
  pr = @__at_end_blocks.pop()
459
637
  pr.call()
460
- assert_eq obj.id, 123
461
- assert_eq obj.name, "alice"
638
+ test_eq? obj.id, 123
639
+ test_eq? obj.name, "alice"
462
640
  end
463
- it "[!27yeh] returns keyvals." do
641
+ test_subject "[!27yeh] returns keyvals." do
464
642
  obj = DummyUser.new(123, "alice")
465
643
  ret = dummy_attrs(obj, :id=>789, :name=>"charlie")
466
- assert_eq ret, {:id=>789, :name=>"charlie"}
644
+ test_eq? ret, {:id=>789, :name=>"charlie"}
467
645
  end
468
- it "[!j7tvp] can take block argument." do
646
+ test_subject "[!j7tvp] can take block argument." do
469
647
  obj = DummyUser.new(123, "alice")
470
648
  ret = dummy_attrs(obj, :id=>888, :name=>"dave") do |kvs|
471
- assert_eq obj.id, 888
472
- assert_eq obj.name, "dave"
473
- assert_eq kvs, {:id=>888, :name=>"dave"}
649
+ test_eq? obj.id, 888
650
+ test_eq? obj.name, "dave"
651
+ test_eq? kvs, {:id=>888, :name=>"dave"}
474
652
  4567
475
653
  end
476
- assert_eq ret, 4567
477
- assert_eq obj.id, 123
478
- assert_eq obj.name, "alice"
479
- assert_eq @__at_end_blocks, nil
654
+ test_eq? ret, 4567
655
+ test_eq? obj.id, 123
656
+ test_eq? obj.name, "alice"
657
+ test_eq? @__at_end_blocks, nil
480
658
  end
481
659
  end
482
660
 
483
- describe '#dummy_ivars()' do
484
- it "[!rnqiv] changes instance variables temporarily." do
661
+ test_target 'Oktest::SpecHelper#dummy_ivars()' do
662
+ test_subject "[!rnqiv] changes instance variables temporarily." do
485
663
  obj = DummyUser.new(123, "alice")
486
664
  dummy_ivars(obj, :id=>999, :name=>"bob")
487
- assert_eq obj.instance_variable_get('@id'), 999
488
- assert_eq obj.instance_variable_get('@name'), "bob"
665
+ test_eq? obj.instance_variable_get('@id'), 999
666
+ test_eq? obj.instance_variable_get('@name'), "bob"
489
667
  end
490
- it "[!8oirn] recovers instance variables." do
668
+ test_subject "[!8oirn] recovers instance variables." do
491
669
  obj = DummyUser.new(123, "alice")
492
670
  dummy_ivars(obj, :id=>999, :name=>"bob")
493
- assert_eq obj.instance_variable_get('@id'), 999
494
- assert_eq obj.instance_variable_get('@name'), "bob"
671
+ test_eq? obj.instance_variable_get('@id'), 999
672
+ test_eq? obj.instance_variable_get('@name'), "bob"
495
673
  #
496
- assert_eq @__at_end_blocks.length, 1
674
+ test_eq? @__at_end_blocks.length, 1
497
675
  pr = @__at_end_blocks.pop()
498
676
  pr.call()
499
- assert_eq obj.instance_variable_get('@id'), 123
500
- assert_eq obj.instance_variable_get('@name'), "alice"
677
+ test_eq? obj.instance_variable_get('@id'), 123
678
+ test_eq? obj.instance_variable_get('@name'), "alice"
501
679
  end
502
- it "[!01dc8] returns keyvals." do
680
+ test_subject "[!01dc8] returns keyvals." do
503
681
  obj = DummyUser.new(123, "alice")
504
682
  ret = dummy_ivars(obj, :id=>789, :name=>"charlie")
505
- assert_eq ret, {:id=>789, :name=>"charlie"}
683
+ test_eq? ret, {:id=>789, :name=>"charlie"}
506
684
  end
507
- it "[!myzk4] can take block argument." do
685
+ test_subject "[!myzk4] can take block argument." do
508
686
  obj = DummyUser.new(123, "alice")
509
687
  ret = dummy_attrs(obj, :id=>888, :name=>"dave") do |kvs|
510
- assert_eq obj.instance_variable_get('@id'), 888
511
- assert_eq obj.instance_variable_get('@name'), "dave"
512
- assert_eq kvs, {:id=>888, :name=>"dave"}
688
+ test_eq? obj.instance_variable_get('@id'), 888
689
+ test_eq? obj.instance_variable_get('@name'), "dave"
690
+ test_eq? kvs, {:id=>888, :name=>"dave"}
513
691
  4567
514
692
  end
515
- assert_eq ret, 4567
516
- assert_eq obj.id, 123
517
- assert_eq obj.name, "alice"
518
- assert_eq @__at_end_blocks, nil
693
+ test_eq? ret, 4567
694
+ test_eq? obj.id, 123
695
+ test_eq? obj.name, "alice"
696
+ test_eq? @__at_end_blocks, nil
519
697
  end
520
698
  end
521
699
 
522
- describe '#recorder()' do
523
- it "[!qwrr8] loads 'benry/recorder' automatically." do
700
+ test_target 'Oktest::SpecHelper#recorder()' do
701
+ test_subject "[!qwrr8] loads 'benry/recorder' automatically." do
524
702
  if defined?(Benry::Recorder)
525
703
  $stderr.puts "** skip because 'benry/recorder' already loaded."
526
704
  else
527
- assert !defined? Benry::Recorder, "should not be loaded."
705
+ test_ok? !defined? Benry::Recorder, msg: "should not be loaded."
528
706
  recorder()
529
- assert defined? Benry::Recorder, "should be loaded."
707
+ test_ok? !!defined? Benry::Recorder, msg: "should be loaded."
530
708
  end
531
709
  end
532
- it "[!glfvx] creates Benry::Recorder object." do
710
+ test_subject "[!glfvx] creates Benry::Recorder object." do
533
711
  rec = recorder()
534
- assert rec.is_a?(Benry::Recorder)
712
+ test_ok? rec.is_a?(Benry::Recorder)
535
713
  o = rec.fake_object(:foo=>123)
536
- assert_eq o.foo(), 123
537
- assert_eq rec[0].name, :foo
538
- assert_eq rec[0].obj, o
714
+ test_eq? o.foo(), 123
715
+ test_eq? rec[0].name, :foo
716
+ test_eq? rec[0].obj, o
539
717
  end
540
718
  end
541
719