From 8016358fc9d5d02932015589b57bb0c11cd9e0fe Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Thu, 7 Dec 2023 17:46:09 +0000 Subject: [PATCH] Add dedicated tests for input recognition logic --- test/irb/test_irb.rb | 52 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/test/irb/test_irb.rb b/test/irb/test_irb.rb index fb8b5c2bf..b03e76f3a 100644 --- a/test/irb/test_irb.rb +++ b/test/irb/test_irb.rb @@ -738,4 +738,56 @@ def build_irb IRB::Irb.new(workspace, TestInputMethod.new) end end + + class InputCategorisationTest < TestCase + def test_irb_distinguihes_commands_and_non_commands_correctly + irb = build_irb + + [ + "show_source Foo#bar", + "show_source Foo#bar -s", + "show_source Foo.bar", + "show_source Foo.bar -s", + "show_source == -s", + "show_source =~ -s", + "ls foo.bar -g baz", + "ls -g foo", + "bt 4", + "bt" + ].each do |input| + statement = irb.build_statement(input) + assert_equal(IRB::Statement::Command, statement.class, "Expected #{input} to be a command") + end + + [ + "info + 1", + "info - 1", + "info = 1", + "info = -1", + "info = /regex/", + "info = a", + "info += a", + "info -= a", + "info &&= a", + "info ||= a", + ].each do |input| + statement = irb.build_statement(input) + assert_equal(IRB::Statement::Expression, statement.class, "Expected #{input} to not be a command") + end + end + + private + + def build_binding + Object.new.instance_eval { binding } + end + + def build_irb + IRB.init_config(nil) + workspace = IRB::WorkSpace.new(build_binding) + + IRB.conf[:VERBOSE] = false + IRB::Irb.new(workspace, TestInputMethod.new) + end + end end