Skip to content

Commit

Permalink
Remove spaces in command input parse
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng committed Apr 1, 2024
1 parent 9cfb4fd commit 59b52a7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ def build_statement(code)
COMMAND_LIKE_ASSIGN_REGEXP = /\A[a-z_]\w* #{Regexp.union(ASSIGN_OPERATORS)}( |$)/

def parse_command(code)
command_name, arg = code.strip.split(/\s/, 2)
command_name, arg = code.strip.split(/\s+/, 2)
return unless code.lines.size == 1 && command_name
return if COMMAND_LIKE_ASSIGN_REGEXP.match?(code)

Expand Down
66 changes: 56 additions & 10 deletions test/irb/test_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,23 +210,14 @@ def test_irb_info_lang
end
end

class ExtendCommandBundleCompatibilityTest < CommandTestCase
class FooBarCommand < IRB::Command::Base
category 'FooBarCategory'
description 'foobar_description'
def execute(_arg)
puts "FooBar executed"
end
end

class CustomCommandTestCase < CommandTestCase
def setup
super
execute_lines("show_cmds\n") # To ensure command initialization is done
@EXTEND_COMMANDS_backup = IRB::ExtendCommandBundle.instance_variable_get(:@EXTEND_COMMANDS).dup
@cvars_backup = IRB::ExtendCommandBundle.class_variables.to_h do |cvar|
[cvar, IRB::ExtendCommandBundle.class_variable_get(cvar)]
end
IRB::Command.const_set :FooBarCommand, FooBarCommand
end

def teardown
Expand All @@ -235,6 +226,61 @@ def teardown
@cvars_backup.each do |cvar, value|
IRB::ExtendCommandBundle.class_variable_set(cvar, value)
end
end
end

class CommandArgTest < CustomCommandTestCase
class PrintArgCommand < IRB::Command::Base
category 'CommandTest'
description 'print_command_arg'
def execute(arg)
puts "arg=#{arg.inspect}"
end
end

def test_arg
IRB::Command.const_set :PrintArgCommand, PrintArgCommand
IRB::ExtendCommandBundle.def_extend_command(:print_arg, :PrintArgCommand, nil, [:pa, IRB::ExtendCommandBundle::OVERRIDE_ALL])
out, err = execute_lines("print_arg\n")
assert_empty err
assert_include(out, 'arg=""')

out, err = execute_lines("print_arg \n")
assert_empty err
assert_include(out, 'arg=""')

out, err = execute_lines("print_arg a r g\n")
assert_empty err
assert_include(out, 'arg="a r g"')

out, err = execute_lines("print_arg a r g \n")
assert_empty err
assert_include(out, 'arg="a r g"')

out, err = execute_lines("pa a r g \n")
assert_empty err
assert_include(out, 'arg="a r g"')
ensure
IRB::Command.send(:remove_const, :PrintArgCommand)
end
end

class ExtendCommandBundleCompatibilityTest < CustomCommandTestCase
class FooBarCommand < IRB::Command::Base
category 'FooBarCategory'
description 'foobar_description'
def execute(_arg)
puts "FooBar executed"
end
end

def setup
super
IRB::Command.const_set :FooBarCommand, FooBarCommand
end

def teardown
super
IRB::Command.send(:remove_const, :FooBarCommand)
end

Expand Down

0 comments on commit 59b52a7

Please sign in to comment.