mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
parent
e1e61e256b
commit
5704b5fe5e
4 changed files with 49 additions and 7 deletions
|
@ -8,7 +8,7 @@ module IRB
|
||||||
super(*args)
|
super(*args)
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute(type = nil, arg = nil)
|
def execute(type = nil, arg = nil, &block)
|
||||||
case type
|
case type
|
||||||
when :off
|
when :off
|
||||||
IRB.conf[:MEASURE] = nil
|
IRB.conf[:MEASURE] = nil
|
||||||
|
@ -21,11 +21,17 @@ module IRB
|
||||||
IRB.conf[:MEASURE] = true
|
IRB.conf[:MEASURE] = true
|
||||||
added = IRB.set_measure_callback(type, arg)
|
added = IRB.set_measure_callback(type, arg)
|
||||||
puts "#{added[0]} is added." if added
|
puts "#{added[0]} is added." if added
|
||||||
|
else
|
||||||
|
if block_given?
|
||||||
|
IRB.conf[:MEASURE] = true
|
||||||
|
added = IRB.set_measure_callback(&block)
|
||||||
|
puts "#{added[0]} is added." if added
|
||||||
else
|
else
|
||||||
IRB.conf[:MEASURE] = true
|
IRB.conf[:MEASURE] = true
|
||||||
added = IRB.set_measure_callback(type, arg)
|
added = IRB.set_measure_callback(type, arg)
|
||||||
puts "#{added[0]} is added." if added
|
puts "#{added[0]} is added." if added
|
||||||
end
|
end
|
||||||
|
end
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,9 +15,9 @@ module IRB
|
||||||
class Nop
|
class Nop
|
||||||
|
|
||||||
|
|
||||||
def self.execute(conf, *opts)
|
def self.execute(conf, *opts, &block)
|
||||||
command = new(conf)
|
command = new(conf)
|
||||||
command.execute(*opts)
|
command.execute(*opts, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(conf)
|
def initialize(conf)
|
||||||
|
|
|
@ -146,7 +146,7 @@ module IRB # :nodoc:
|
||||||
@CONF[:AT_EXIT] = []
|
@CONF[:AT_EXIT] = []
|
||||||
end
|
end
|
||||||
|
|
||||||
def IRB.set_measure_callback(type = nil, arg = nil)
|
def IRB.set_measure_callback(type = nil, arg = nil, &block)
|
||||||
added = nil
|
added = nil
|
||||||
if type
|
if type
|
||||||
type_sym = type.upcase.to_sym
|
type_sym = type.upcase.to_sym
|
||||||
|
@ -155,6 +155,8 @@ module IRB # :nodoc:
|
||||||
end
|
end
|
||||||
elsif IRB.conf[:MEASURE_PROC][:CUSTOM]
|
elsif IRB.conf[:MEASURE_PROC][:CUSTOM]
|
||||||
added = [:CUSTOM, IRB.conf[:MEASURE_PROC][:CUSTOM], arg]
|
added = [:CUSTOM, IRB.conf[:MEASURE_PROC][:CUSTOM], arg]
|
||||||
|
elsif block_given?
|
||||||
|
added = [:BLOCK, block, arg]
|
||||||
else
|
else
|
||||||
added = [:TIME, IRB.conf[:MEASURE_PROC][:TIME], arg]
|
added = [:TIME, IRB.conf[:MEASURE_PROC][:TIME], arg]
|
||||||
end
|
end
|
||||||
|
|
|
@ -276,6 +276,40 @@ module TestIRB
|
||||||
assert_match(/\A=> 3\nCUSTOM is added\.\n=> nil\ncustom processing time: .+\n=> 3\n=> nil\n=> 3\n/, out)
|
assert_match(/\A=> 3\nCUSTOM is added\.\n=> nil\ncustom processing time: .+\n=> 3\n=> nil\n=> 3\n/, out)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_measure_with_proc
|
||||||
|
IRB.init_config(nil)
|
||||||
|
IRB.conf[:PROMPT] = {
|
||||||
|
DEFAULT: {
|
||||||
|
PROMPT_I: '> ',
|
||||||
|
PROMPT_S: '> ',
|
||||||
|
PROMPT_C: '> ',
|
||||||
|
PROMPT_N: '> '
|
||||||
|
}
|
||||||
|
}
|
||||||
|
IRB.conf[:VERBOSE] = false
|
||||||
|
IRB.conf[:PROMPT_MODE] = :DEFAULT
|
||||||
|
IRB.conf[:MEASURE] = false
|
||||||
|
input = TestInputMethod.new([
|
||||||
|
"3\n",
|
||||||
|
"measure { |context, code, line_no, &block|\n",
|
||||||
|
" result = block.()\n",
|
||||||
|
" puts 'aaa' if IRB.conf[:MEASURE]\n",
|
||||||
|
"}\n",
|
||||||
|
"3\n",
|
||||||
|
"measure :off\n",
|
||||||
|
"3\n",
|
||||||
|
])
|
||||||
|
c = Class.new(Object)
|
||||||
|
irb = IRB::Irb.new(IRB::WorkSpace.new(c.new), input)
|
||||||
|
irb.context.return_format = "=> %s\n"
|
||||||
|
out, err = capture_output do
|
||||||
|
irb.eval_input
|
||||||
|
end
|
||||||
|
assert_empty err
|
||||||
|
assert_match(/\A=> 3\nBLOCK is added\.\n=> nil\naaa\n=> 3\n=> nil\n=> 3\n/, out)
|
||||||
|
assert_empty(c.class_variables)
|
||||||
|
end
|
||||||
|
|
||||||
def test_irb_source
|
def test_irb_source
|
||||||
IRB.init_config(nil)
|
IRB.init_config(nil)
|
||||||
File.write("#{@tmpdir}/a.rb", "a = 'hi'\n")
|
File.write("#{@tmpdir}/a.rb", "a = 'hi'\n")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue