Skip to content

Commit 55548fd

Browse files
committed
Add test for measure command measuring proc arguments
1 parent 632da0f commit 55548fd

1 file changed

Lines changed: 13 additions & 18 deletions

File tree

test/irb/test_command.rb

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@ def test_irb_info_lang
208208
end
209209

210210
class MeasureTest < CommandTestCase
211+
CUSTOM_MEASURE_PROC = proc { |context, line, line_no, &block|
212+
raise 'Wrong argument' unless IRB::Context === context && String === line && Integer === line_no
213+
time = Time.now
214+
result = block.()
215+
puts 'custom processing time: %fs' % (Time.now - time) if IRB.conf[:MEASURE]
216+
result
217+
}
211218
def test_measure
212219
conf = {
213220
PROMPT: {
@@ -292,12 +299,6 @@ def test_measure_enabled_by_rc
292299
end
293300

294301
def test_measure_enabled_by_rc_with_custom
295-
measuring_proc = proc { |line, line_no, &block|
296-
time = Time.now
297-
result = block.()
298-
puts 'custom processing time: %fs' % (Time.now - time) if IRB.conf[:MEASURE]
299-
result
300-
}
301302
conf = {
302303
PROMPT: {
303304
DEFAULT: {
@@ -308,7 +309,7 @@ def test_measure_enabled_by_rc_with_custom
308309
},
309310
PROMPT_MODE: :DEFAULT,
310311
MEASURE: true,
311-
MEASURE_PROC: { CUSTOM: measuring_proc }
312+
MEASURE_PROC: { CUSTOM: CUSTOM_MEASURE_PROC }
312313
}
313314

314315
out, err = execute_lines(
@@ -322,12 +323,6 @@ def test_measure_enabled_by_rc_with_custom
322323
end
323324

324325
def test_measure_with_custom
325-
measuring_proc = proc { |line, line_no, &block|
326-
time = Time.now
327-
result = block.()
328-
puts 'custom processing time: %fs' % (Time.now - time) if IRB.conf[:MEASURE]
329-
result
330-
}
331326
conf = {
332327
PROMPT: {
333328
DEFAULT: {
@@ -338,7 +333,7 @@ def test_measure_with_custom
338333
},
339334
PROMPT_MODE: :DEFAULT,
340335
MEASURE: false,
341-
MEASURE_PROC: { CUSTOM: measuring_proc }
336+
MEASURE_PROC: { CUSTOM: CUSTOM_MEASURE_PROC }
342337
}
343338
out, err = execute_lines(
344339
"3\n",
@@ -365,12 +360,12 @@ def test_measure_toggle
365360
PROMPT_MODE: :DEFAULT,
366361
MEASURE: false,
367362
MEASURE_PROC: {
368-
FOO: proc { |&block| puts 'foo'; block.call },
369-
BAR: proc { |&block| puts 'bar'; block.call }
363+
FOO: proc { |ctx, line, line_no, arg, &block| puts "foo(#{arg.inspect})"; block.call },
364+
BAR: proc { |ctx, line, line_no, arg, &block| puts "bar(#{arg.inspect})"; block.call }
370365
}
371366
}
372367
out, err = execute_lines(
373-
"measure :foo\n",
368+
"measure :foo, :arg\n",
374369
"1\n",
375370
"measure :on, :bar\n",
376371
"2\n",
@@ -382,7 +377,7 @@ def test_measure_toggle
382377
)
383378

384379
assert_empty err
385-
assert_match(/\AFOO is added\.\nfoo\n=> 1\nBAR is added\.\nbar\nfoo\n=> 2\nbar\n=> 3\n=> 4\n/, out)
380+
assert_match(/\AFOO is added\.\nfoo\(:arg\)\n=> 1\nBAR is added\.\nbar\(nil\)\nfoo\(:arg\)\n=> 2\nbar\(nil\)\n=> 3\n=> 4\n/, out)
386381
end
387382

388383
def test_measure_with_proc_warning

0 commit comments

Comments
 (0)