Skip to content

Commit ef881a8

Browse files
committed
Deprecate measure :on and navigate to use measure on
1 parent e2d0fad commit ef881a8

File tree

4 files changed

+66
-37
lines changed

4 files changed

+66
-37
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ Debugging
149149
150150
Misc
151151
edit Open a file or source location.
152-
measure `measure` enables the mode to measure processing time. `measure :off` disables it.
152+
measure `measure` enables the mode to measure processing time. `measure off` disables it.
153153
154154
Context
155155
show_doc Enter the mode to look up RI documents.

lib/irb/command/measure.rb

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ module IRB
33

44
module Command
55
class Measure < Base
6-
include RubyArgsExtractor
7-
86
category "Misc"
9-
description "`measure` enables the mode to measure processing time. `measure :off` disables it."
7+
description "`measure` enables the mode to measure processing time. `measure off` disables it."
108

119
def initialize(*args)
1210
super(*args)
@@ -17,28 +15,43 @@ def execute(arg)
1715
warn 'Configure IRB.conf[:MEASURE_PROC] to add custom measure methods.'
1816
return
1917
end
20-
args, kwargs = ruby_args(arg)
21-
execute_internal(*args, **kwargs)
18+
19+
if arg.empty?
20+
execute_internal(nil, nil)
21+
elsif arg.start_with? ':'
22+
# Legacy style `measure :sym`, `measure :sym, :sym`
23+
type, arg_val = arg.split(/,\s*/, 2).map { |v| v.sub(/\A:/, '') }
24+
warn "`measure #{arg}` is deprecated. Please use `measure #{[type, arg_val].compact.join(' ')}` instead."
25+
execute_internal(type.to_sym, arg_val)
26+
else
27+
type, arg_val = arg.split(/\s+/, 2)
28+
execute_internal(type&.to_sym, arg_val)
29+
end
2230
end
2331

24-
def execute_internal(type = nil, arg = nil)
32+
def execute_internal(type, arg)
2533
# Please check IRB.init_config in lib/irb/init.rb that sets
2634
# IRB.conf[:MEASURE_PROC] to register default "measure" methods,
27-
# "measure :time" (abbreviated as "measure") and "measure :stackprof".
35+
# "measure time" (abbreviated as "measure") and "measure stackprof".
2836

2937
case type
3038
when :off
31-
IRB.unset_measure_callback(arg)
39+
IRB.unset_measure_callback(arg&.to_sym)
3240
when :list
3341
IRB.conf[:MEASURE_CALLBACKS].each do |type_name, _, arg_val|
3442
puts "- #{type_name}" + (arg_val ? "(#{arg_val.inspect})" : '')
3543
end
36-
when :on
37-
added = IRB.set_measure_callback(arg)
38-
puts "#{added[0]} is added." if added
3944
else
40-
added = IRB.set_measure_callback(type, arg)
41-
puts "#{added[0]} is added." if added
45+
type, arg = arg&.to_sym, nil if type == :on
46+
47+
measure_methods = IRB.conf[:MEASURE_PROC].keys.map(&:downcase)
48+
if type && !measure_methods.include?(type)
49+
puts "Measure method `#{type}` not found."
50+
puts "Available measure methods: %w[#{measure_methods.join(' ')}]."
51+
else
52+
added = IRB.set_measure_callback(type&.to_sym, arg)
53+
puts "#{added[0]} is added." if added
54+
end
4255
end
4356
nil
4457
end

lib/irb/init.rb

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -204,19 +204,9 @@ def IRB.set_measure_callback(type = nil, arg = nil, &block)
204204
added = [type_sym, IRB.conf[:MEASURE_PROC][type_sym], arg]
205205
end
206206
elsif IRB.conf[:MEASURE_PROC][:CUSTOM]
207-
added = [:CUSTOM, IRB.conf[:MEASURE_PROC][:CUSTOM], arg]
208-
elsif block_given?
209-
added = [:BLOCK, block, arg]
210-
found = IRB.conf[:MEASURE_CALLBACKS].find{ |m| m[0] == added[0] && m[2] == added[2] }
211-
if found
212-
found[1] = block
213-
return added
214-
else
215-
IRB.conf[:MEASURE_CALLBACKS] << added
216-
return added
217-
end
207+
added = [:CUSTOM, IRB.conf[:MEASURE_PROC][:CUSTOM], nil]
218208
else
219-
added = [:TIME, IRB.conf[:MEASURE_PROC][:TIME], arg]
209+
added = [:TIME, IRB.conf[:MEASURE_PROC][:TIME], nil]
220210
end
221211
if added
222212
IRB.conf[:MEASURE] = true

test/irb/test_command.rb

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,11 @@ def test_measure
235235
out, err = execute_lines(
236236
"measure\n",
237237
"3\n",
238-
"measure :off\n",
238+
"measure off\n",
239239
"3\n",
240-
"measure :on\n",
240+
"measure on\n",
241241
"3\n",
242-
"measure :off\n",
242+
"measure off\n",
243243
"3\n",
244244
conf: conf,
245245
main: c
@@ -292,7 +292,7 @@ def test_measure_enabled_by_rc
292292

293293
out, err = execute_lines(
294294
"3\n",
295-
"measure :off\n",
295+
"measure off\n",
296296
"3\n",
297297
conf: conf,
298298
)
@@ -317,7 +317,7 @@ def test_measure_enabled_by_rc_with_custom
317317

318318
out, err = execute_lines(
319319
"3\n",
320-
"measure :off\n",
320+
"measure off\n",
321321
"3\n",
322322
conf: conf,
323323
)
@@ -342,7 +342,7 @@ def test_measure_with_custom
342342
"3\n",
343343
"measure\n",
344344
"3\n",
345-
"measure :off\n",
345+
"measure off\n",
346346
"3\n",
347347
conf: conf
348348
)
@@ -368,19 +368,19 @@ def test_measure_toggle
368368
}
369369
}
370370
out, err = execute_lines(
371-
"measure :foo, :arg\n",
371+
"measure foo arg\n",
372372
"1\n",
373-
"measure :on, :bar\n",
373+
"measure on bar\n",
374374
"2\n",
375-
"measure :off, :foo\n",
375+
"measure off foo\n",
376376
"3\n",
377-
"measure :off, :bar\n",
377+
"measure off bar\n",
378378
"4\n",
379379
conf: conf
380380
)
381381

382382
assert_empty err
383-
assert_match(/\AFOO is added\.\n=> nil\nfoo\(:arg\)\n=> 1\nBAR is added\.\n=> nil\nbar\(nil\)\nfoo\(:arg\)\n=> 2\n=> nil\nbar\(nil\)\n=> 3\n=> nil\n=> 4\n/, out)
383+
assert_match(/\AFOO is added\.\n=> nil\nfoo\("arg"\)\n=> 1\nBAR is added\.\n=> nil\nbar\(nil\)\nfoo\("arg"\)\n=> 2\n=> nil\nbar\(nil\)\n=> 3\n=> nil\n=> 4\n/, out)
384384
end
385385

386386
def test_measure_with_proc_warning
@@ -408,6 +408,32 @@ def test_measure_with_proc_warning
408408
assert_match(/\A=> 3\n=> nil\n=> 3\n/, out)
409409
assert_empty(c.class_variables)
410410
end
411+
412+
def test_legacy_measure_warning
413+
conf = {
414+
MEASURE_PROC: {
415+
FOO: proc {},
416+
BAR: proc {},
417+
}
418+
}
419+
out, err = execute_lines(
420+
"measure :foo\n",
421+
"measure :on, :bar\n",
422+
conf: conf
423+
)
424+
assert_match(/FOO is added/, out)
425+
assert_match(/BAR is added/, out)
426+
assert_match(/`measure :foo` is deprecated. Please use `measure foo`/, err)
427+
assert_match(/`measure :on, :bar` is deprecated. Please use `measure on bar`/, err)
428+
end
429+
430+
def test_unknown_method
431+
out, err = execute_lines("measure foo\n", "measure on bar\n")
432+
assert_empty(err)
433+
assert_match(/`foo` not found/, out)
434+
assert_match(/`bar` not found/, out)
435+
assert_match(/time stackprof/, out)
436+
end
411437
end
412438

413439
class IrbSourceTest < CommandTestCase

0 commit comments

Comments
 (0)