diff options
author | Stan Lo <[email protected]> | 2022-11-20 04:47:51 +0000 |
---|---|---|
committer | git <[email protected]> | 2022-11-20 04:47:54 +0000 |
commit | 180ed611b238db48db9feb1449c4e3b563d2dce0 (patch) | |
tree | bfd4d7be6b7c0d17773eeead6ea0f6c63186e342 /test/irb/test_cmd.rb | |
parent | 439990318d90a689b2ac067b41c3462ddda60ae5 (diff) |
[ruby/irb] Add edit command (https://github.com/ruby/irb/pull/453)
* Add edit command
* Make find_source a public singleton method
* Add document for the edit command
* Make find_end private
* Remove duplicated private
https://github.com/ruby/irb/commit/4321674aa7
Co-authored-by: Takashi Kokubun <[email protected]>
Diffstat (limited to 'test/irb/test_cmd.rb')
-rw-r--r-- | test/irb/test_cmd.rb | 78 |
1 files changed, 77 insertions, 1 deletions
diff --git a/test/irb/test_cmd.rb b/test/irb/test_cmd.rb index bcfb1d0b86..44f348a724 100644 --- a/test/irb/test_cmd.rb +++ b/test/irb/test_cmd.rb @@ -565,9 +565,84 @@ module TestIRB $bar = nil end + class EditTest < ExtendCommandTest + def setup + @original_editor = ENV["EDITOR"] + # noop the command so nothing gets executed + ENV["EDITOR"] = ": code" + end + + def teardown + ENV["EDITOR"] = @original_editor + end + + def test_edit_without_arg + out, err = execute_lines( + "edit", + irb_path: __FILE__ + ) + + assert_empty err + assert_match("path: #{__FILE__}", out) + assert_match("command: ': code'", out) + end + + def test_edit_with_path + out, err = execute_lines( + "edit #{__FILE__}" + ) + + assert_empty err + assert_match("path: #{__FILE__}", out) + assert_match("command: ': code'", out) + end + + def test_edit_with_non_existing_path + out, err = execute_lines( + "edit foo.rb" + ) + + assert_empty err + assert_match /Can not find file: foo\.rb/, out + end + + def test_edit_with_constant + # const_source_location is supported after Ruby 2.7 + omit if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.7.0') || RUBY_ENGINE == 'truffleruby' + + out, err = execute_lines( + "edit IRB::Irb" + ) + + assert_empty err + assert_match(/path: .*\/lib\/irb\.rb/, out) + assert_match("command: ': code'", out) + end + + def test_edit_with_class_method + out, err = execute_lines( + "edit IRB.start" + ) + + assert_empty err + assert_match(/path: .*\/lib\/irb\.rb/, out) + assert_match("command: ': code'", out) + end + + def test_edit_with_instance_method + out, err = execute_lines( + "edit IRB::Irb#run" + ) + + assert_empty err + assert_match(/path: .*\/lib\/irb\.rb/, out) + assert_match("command: ': code'", out) + end + end + private - def execute_lines(*lines, conf: {}, main: self) + def execute_lines(*lines, conf: {}, main: self, irb_path: nil) IRB.init_config(nil) IRB.conf[:VERBOSE] = false IRB.conf[:PROMPT_MODE] = :SIMPLE @@ -575,6 +650,7 @@ module TestIRB input = TestInputMethod.new(lines) irb = IRB::Irb.new(IRB::WorkSpace.new(main), input) irb.context.return_format = "=> %s\n" + irb.context.irb_path = irb_path if irb_path IRB.conf[:MAIN_CONTEXT] = irb.context capture_output do irb.eval_input |