diff options
author | aycabta <aycabta@gmail.com> | 2020-09-16 11:19:17 +0900 |
---|---|---|
committer | nagachika <nagachika@ruby-lang.org> | 2020-09-16 21:08:31 +0900 |
commit | 2159798f4c0f71007db733cddd82b99186c8e424 (patch) | |
tree | 4e79977d4c6cbc84221d06adad3acfb4234cc911 /test | |
parent | 3bb503e0e8f92c039ce50f430b14649a36c03feb (diff) |
Merge IRB 1.2.6
Diffstat (limited to 'test')
-rw-r--r-- | test/irb/test_cmd.rb | 127 | ||||
-rw-r--r-- | test/irb/test_context.rb | 176 | ||||
-rw-r--r-- | test/irb/test_history.rb | 153 | ||||
-rw-r--r-- | test/irb/test_ruby_lex.rb | 136 | ||||
-rw-r--r-- | test/irb/test_workspace.rb | 1 |
5 files changed, 588 insertions, 5 deletions
diff --git a/test/irb/test_cmd.rb b/test/irb/test_cmd.rb new file mode 100644 index 0000000000..bb33f535b6 --- /dev/null +++ b/test/irb/test_cmd.rb @@ -0,0 +1,127 @@ +# frozen_string_literal: false +require "test/unit" +require "irb" +require "irb/extend-command" + +module TestIRB + class ExtendCommand < Test::Unit::TestCase + def setup + @pwd = Dir.pwd + @tmpdir = File.join(Dir.tmpdir, "test_reline_config_#{$$}") + begin + Dir.mkdir(@tmpdir) + rescue Errno::EEXIST + FileUtils.rm_rf(@tmpdir) + Dir.mkdir(@tmpdir) + end + Dir.chdir(@tmpdir) + @home_backup = ENV["HOME"] + ENV["HOME"] = @tmpdir + @default_encoding = [Encoding.default_external, Encoding.default_internal] + @stdio_encodings = [STDIN, STDOUT, STDERR].map {|io| [io.external_encoding, io.internal_encoding] } + IRB.instance_variable_get(:@CONF).clear + end + + def teardown + ENV["HOME"] = @home_backup + Dir.chdir(@pwd) + FileUtils.rm_rf(@tmpdir) + EnvUtil.suppress_warning { + Encoding.default_external, Encoding.default_internal = *@default_encoding + [STDIN, STDOUT, STDERR].zip(@stdio_encodings) do |io, encs| + io.set_encoding(*encs) + end + } + end + + def test_irb_info_multiline + FileUtils.touch("#{@tmpdir}/.inputrc") + FileUtils.touch("#{@tmpdir}/.irbrc") + IRB.setup(__FILE__, argv: []) + IRB.conf[:USE_MULTILINE] = true + IRB.conf[:USE_SINGLELINE] = false + IRB.conf[:VERBOSE] = false + workspace = IRB::WorkSpace.new(self) + irb = IRB::Irb.new(workspace) + IRB.conf[:MAIN_CONTEXT] = irb.context + expected = %r{ + Ruby\sversion: .+\n + IRB\sversion:\sirb .+\n + InputMethod:\sReidlineInputMethod\swith\sReline .+ and .+\n + \.irbrc\spath: .+ + }x + assert_match expected, irb.context.main.irb_info.to_s + end + + def test_irb_info_singleline + FileUtils.touch("#{@tmpdir}/.inputrc") + FileUtils.touch("#{@tmpdir}/.irbrc") + IRB.setup(__FILE__, argv: []) + IRB.conf[:USE_MULTILINE] = false + IRB.conf[:USE_SINGLELINE] = true + IRB.conf[:VERBOSE] = false + workspace = IRB::WorkSpace.new(self) + irb = IRB::Irb.new(workspace) + IRB.conf[:MAIN_CONTEXT] = irb.context + expected = %r{ + Ruby\sversion: .+\n + IRB\sversion:\sirb .+\n + InputMethod:\sReadlineInputMethod\swith .+ and .+\n + \.irbrc\spath: .+ + }x + assert_match expected, irb.context.main.irb_info.to_s + end + + def test_irb_info_multiline_without_rc_files + inputrc_backup = ENV["INPUTRC"] + ENV["INPUTRC"] = "unknown_inpurc" + ext_backup = IRB::IRBRC_EXT + IRB.__send__(:remove_const, :IRBRC_EXT) + IRB.const_set(:IRBRC_EXT, "unknown_ext") + IRB.setup(__FILE__, argv: []) + IRB.conf[:USE_MULTILINE] = true + IRB.conf[:USE_SINGLELINE] = false + IRB.conf[:VERBOSE] = false + workspace = IRB::WorkSpace.new(self) + irb = IRB::Irb.new(workspace) + IRB.conf[:MAIN_CONTEXT] = irb.context + expected = %r{ + Ruby\sversion: .+\n + IRB\sversion:\sirb .+\n + InputMethod:\sReidlineInputMethod\swith\sReline\s[^ ]+(?!\sand\s.+)\n + \z + }x + assert_match expected, irb.context.main.irb_info.to_s + ensure + ENV["INPUTRC"] = inputrc_backup + IRB.__send__(:remove_const, :IRBRC_EXT) + IRB.const_set(:IRBRC_EXT, ext_backup) + end + + def test_irb_info_singleline_without_rc_files + inputrc_backup = ENV["INPUTRC"] + ENV["INPUTRC"] = "unknown_inpurc" + ext_backup = IRB::IRBRC_EXT + IRB.__send__(:remove_const, :IRBRC_EXT) + IRB.const_set(:IRBRC_EXT, "unknown_ext") + IRB.setup(__FILE__, argv: []) + IRB.conf[:USE_MULTILINE] = false + IRB.conf[:USE_SINGLELINE] = true + IRB.conf[:VERBOSE] = false + workspace = IRB::WorkSpace.new(self) + irb = IRB::Irb.new(workspace) + IRB.conf[:MAIN_CONTEXT] = irb.context + expected = %r{ + Ruby\sversion: .+\n + IRB\sversion:\sirb .+\n + InputMethod:\sReadlineInputMethod\swith\s(?~.*\sand\s.+)\n + \z + }x + assert_match expected, irb.context.main.irb_info.to_s + ensure + ENV["INPUTRC"] = inputrc_backup + IRB.__send__(:remove_const, :IRBRC_EXT) + IRB.const_set(:IRBRC_EXT, ext_backup) + end + end +end diff --git a/test/irb/test_context.rb b/test/irb/test_context.rb index d03cc30c78..fa628bba46 100644 --- a/test/irb/test_context.rb +++ b/test/irb/test_context.rb @@ -30,6 +30,10 @@ module TestIRB def reset @line_no = 0 end + + def winsize + [10, 20] + end end def setup @@ -98,6 +102,21 @@ module TestIRB $VERBOSE = verbose end + def test_eval_object_without_inspect_method + verbose, $VERBOSE = $VERBOSE, nil + input = TestInputMethod.new([ + "BasicObject.new\n", + ]) + irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input) + out, err = capture_output do + irb.eval_input + end + assert_empty err + assert(/\(Object doesn't support #inspect\)\n(=> )?\n/, out) + ensure + $VERBOSE = verbose + end + def test_default_config assert_equal(true, @context.use_colorize?) end @@ -198,6 +217,151 @@ module TestIRB assert_equal("", out) end + def test_omit_on_assignment + input = TestInputMethod.new([ + "a = [1] * 100\n", + "a\n", + ]) + value = [1] * 100 + irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input) + irb.context.return_format = "=> %s\n" + + irb.context.echo = true + irb.context.echo_on_assignment = false + irb.context.omit_on_assignment = true + out, err = capture_io do + irb.eval_input + end + assert_empty err + assert_equal("=> #{value.inspect}\n", out) + + input.reset + irb.context.echo = true + irb.context.echo_on_assignment = true + irb.context.omit_on_assignment = true + out, err = capture_io do + irb.eval_input + end + assert_empty err + assert_equal("=> #{value.inspect[0..(input.winsize.last - 9)]}...\e[0m\n=> #{value.inspect}\n", out) + + input.reset + irb.context.echo = true + irb.context.echo_on_assignment = true + irb.context.omit_on_assignment = false + out, err = capture_io do + irb.eval_input + end + assert_empty err + assert_equal("=> #{value.inspect}\n=> #{value.inspect}\n", out) + + input.reset + irb.context.echo = false + irb.context.echo_on_assignment = false + irb.context.omit_on_assignment = true + out, err = capture_io do + irb.eval_input + end + assert_empty err + assert_equal("", out) + + input.reset + irb.context.echo = false + irb.context.echo_on_assignment = true + irb.context.omit_on_assignment = true + out, err = capture_io do + irb.eval_input + end + assert_empty err + assert_equal("", out) + + input.reset + irb.context.echo = false + irb.context.echo_on_assignment = true + irb.context.omit_on_assignment = false + out, err = capture_io do + irb.eval_input + end + assert_empty err + assert_equal("", out) + end + + def test_omit_multiline_on_assignment + input = TestInputMethod.new([ + "class A; def inspect; ([?* * 1000] * 3).join(%{\\n}); end; end; a = A.new\n", + "a\n" + ]) + value = ([?* * 1000] * 3).join(%{\n}) + value_first_line = (?* * 1000).to_s + irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input) + irb.context.return_format = "=> %s\n" + + irb.context.echo = true + irb.context.echo_on_assignment = false + irb.context.omit_on_assignment = true + out, err = capture_io do + irb.eval_input + end + assert_empty err + assert_equal("=> \n#{value}\n", out) + irb.context.evaluate('A.remove_method(:inspect)', 0) + + input.reset + irb.context.echo = true + irb.context.echo_on_assignment = true + irb.context.omit_on_assignment = true + out, err = capture_io do + irb.eval_input + end + assert_empty err + assert_equal("=> #{value_first_line[0..(input.winsize.last - 9)]}...\e[0m\n=> \n#{value}\n", out) + irb.context.evaluate('A.remove_method(:inspect)', 0) + + input.reset + irb.context.echo = true + irb.context.echo_on_assignment = true + irb.context.omit_on_assignment = false + out, err = capture_io do + irb.eval_input + end + assert_empty err + assert_equal("=> \n#{value}\n=> \n#{value}\n", out) + irb.context.evaluate('A.remove_method(:inspect)', 0) + + input.reset + irb.context.echo = false + irb.context.echo_on_assignment = false + irb.context.omit_on_assignment = true + out, err = capture_io do + irb.eval_input + end + assert_empty err + assert_equal("", out) + irb.context.evaluate('A.remove_method(:inspect)', 0) + + input.reset + irb.context.echo = false + irb.context.echo_on_assignment = true + irb.context.omit_on_assignment = true + out, err = capture_io do + irb.eval_input + end + assert_empty err + assert_equal("", out) + irb.context.evaluate('A.remove_method(:inspect)', 0) + + input.reset + irb.context.echo = false + irb.context.echo_on_assignment = true + irb.context.omit_on_assignment = false + out, err = capture_io do + irb.eval_input + end + assert_empty err + assert_equal("", out) + irb.context.evaluate('A.remove_method(:inspect)', 0) + end + def test_echo_on_assignment_conf # Default IRB.conf[:ECHO] = nil @@ -206,22 +370,26 @@ module TestIRB irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input) assert(irb.context.echo?, "echo? should be true by default") - refute(irb.context.echo_on_assignment?, "echo_on_assignment? should be false by default") + assert(irb.context.echo_on_assignment?, "echo_on_assignment? should be true by default") + assert(irb.context.omit_on_assignment?, "omit_on_assignment? should be true by default") # Explicitly set :ECHO to false IRB.conf[:ECHO] = false irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input) refute(irb.context.echo?, "echo? should be false when IRB.conf[:ECHO] is set to false") - refute(irb.context.echo_on_assignment?, "echo_on_assignment? should be false by default") + assert(irb.context.echo_on_assignment?, "echo_on_assignment? should be true by default") + assert(irb.context.omit_on_assignment?, "omit_on_assignment? should be true by default") # Explicitly set :ECHO_ON_ASSIGNMENT to true IRB.conf[:ECHO] = nil - IRB.conf[:ECHO_ON_ASSIGNMENT] = true + IRB.conf[:ECHO_ON_ASSIGNMENT] = false + IRB.conf[:OMIT_ON_ASSIGNMENT] = false irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input) assert(irb.context.echo?, "echo? should be true by default") - assert(irb.context.echo_on_assignment?, "echo_on_assignment? should be true when IRB.conf[:ECHO_ON_ASSIGNMENT] is set to true") + refute(irb.context.echo_on_assignment?, "echo_on_assignment? should be false when IRB.conf[:ECHO_ON_ASSIGNMENT] is set to false") + refute(irb.context.omit_on_assignment?, "omit_on_assignment? should be false when IRB.conf[:OMIT_ON_ASSIGNMENT] is set to false") end def test_multiline_output_on_default_inspector diff --git a/test/irb/test_history.rb b/test/irb/test_history.rb new file mode 100644 index 0000000000..3591f88f59 --- /dev/null +++ b/test/irb/test_history.rb @@ -0,0 +1,153 @@ +# frozen_string_literal: false +require 'test/unit' +require 'irb' +require 'readline' + +module TestIRB + class TestHistory < Test::Unit::TestCase + def setup + IRB.conf[:RC_NAME_GENERATOR] = nil + end + + def teardown + IRB.conf[:RC_NAME_GENERATOR] = nil + end + + def test_history_save_1 + omit "Skip Editline" if /EditLine/n.match(Readline::VERSION) + _result_output, result_history_file = launch_irb_with_irbrc_and_irb_history(<<~IRBRC, <<~IRB_HISTORY) do |stdin| + IRB.conf[:USE_READLINE] = true + IRB.conf[:SAVE_HISTORY] = 1 + IRB.conf[:USE_READLINE] = true + IRBRC + 1 + 2 + 3 + 4 + IRB_HISTORY + stdin.write("5\nexit\n") + end + + assert_equal(<<~HISTORY_FILE, result_history_file) + exit + HISTORY_FILE + end + + def test_history_save_100 + omit "Skip Editline" if /EditLine/n.match(Readline::VERSION) + _result_output, result_history_file = launch_irb_with_irbrc_and_irb_history(<<~IRBRC, <<~IRB_HISTORY) do |stdin| + IRB.conf[:USE_READLINE] = true + IRB.conf[:SAVE_HISTORY] = 100 + IRB.conf[:USE_READLINE] = true + IRBRC + 1 + 2 + 3 + 4 + IRB_HISTORY + stdin.write("5\nexit\n") + end + + assert_equal(<<~HISTORY_FILE, result_history_file) + 1 + 2 + 3 + 4 + 5 + exit + HISTORY_FILE + end + + def test_history_save_bignum + omit "Skip Editline" if /EditLine/n.match(Readline::VERSION) + _result_output, result_history_file = launch_irb_with_irbrc_and_irb_history(<<~IRBRC, <<~IRB_HISTORY) do |stdin| + IRB.conf[:USE_READLINE] = true + IRB.conf[:SAVE_HISTORY] = 10 ** 19 + IRB.conf[:USE_READLINE] = true + IRBRC + 1 + 2 + 3 + 4 + IRB_HISTORY + stdin.write("5\nexit\n") + end + + assert_equal(<<~HISTORY_FILE, result_history_file) + 1 + 2 + 3 + 4 + 5 + exit + HISTORY_FILE + end + + def test_history_save_minus_as_infinity + omit "Skip Editline" if /EditLine/n.match(Readline::VERSION) + _result_output, result_history_file = launch_irb_with_irbrc_and_irb_history(<<~IRBRC, <<~IRB_HISTORY) do |stdin| + IRB.conf[:USE_READLINE] = true + IRB.conf[:SAVE_HISTORY] = -1 # infinity + IRB.conf[:USE_READLINE] = true + IRBRC + 1 + 2 + 3 + 4 + IRB_HISTORY + stdin.write("5\nexit\n") + end + + assert_equal(<<~HISTORY_FILE, result_history_file) + 1 + 2 + 3 + 4 + 5 + exit + HISTORY_FILE + end + + private + + def launch_irb_with_irbrc_and_irb_history(irbrc, irb_history) + result = nil + result_history = nil + backup_irbrc = ENV.delete("IRBRC") + backup_home = ENV["HOME"] + Dir.mktmpdir("test_irb_history_#{$$}") do |tmpdir| + ENV["HOME"] = tmpdir + open(IRB.rc_file, "w") do |f| + f.write(irbrc) + end + open(IRB.rc_file("_history"), "w") do |f| + f.write(irb_history) + end + + with_temp_stdio do |stdin, stdout| + yield(stdin, stdout) + stdin.close + stdout.flush + system('ruby', '-Ilib', '-Itest', '-W0', '-rirb', '-e', 'IRB.start(__FILE__)', in: stdin.path, out: stdout.path) + result = stdout.read + stdout.close + end + open(IRB.rc_file("_history"), "r") do |f| + result_history = f.read + end + end + [result, result_history] + ensure + ENV["HOME"] = backup_home + ENV["IRBRC"] = backup_irbrc + end + + def with_temp_stdio + Tempfile.create("test_readline_stdin") do |stdin| + Tempfile.create("test_readline_stdout") do |stdout| + yield stdin, stdout + end + end + end + end +end if not RUBY_PLATFORM.match?(/solaris|mswin|mingw/i) diff --git a/test/irb/test_ruby_lex.rb b/test/irb/test_ruby_lex.rb index dd5a1f7ca5..14b43f5718 100644 --- a/test/irb/test_ruby_lex.rb +++ b/test/irb/test_ruby_lex.rb @@ -5,7 +5,7 @@ require 'ostruct' module TestIRB class TestRubyLex < Test::Unit::TestCase - Row = Struct.new(:content, :current_line_spaces, :new_line_spaces) + Row = Struct.new(:content, :current_line_spaces, :new_line_spaces, :nesting_level) class MockIO def initialize(params, &assertion) @@ -34,6 +34,15 @@ module TestIRB ruby_lex.set_auto_indent(context) end + def assert_nesting_level(lines, expected) + ruby_lex = RubyLex.new() + io = proc{ lines.join("\n") } + ruby_lex.set_input(io, io) + ruby_lex.lex + error_message = "Calculated the wrong number of nesting level for:\n #{lines.join("\n")}" + assert_equal(expected, ruby_lex.instance_variable_get(:@indent), error_message) + end + def test_auto_indent input_with_correct_indents = [ Row.new(%q(def each_top_level_statement), nil, 2), @@ -126,5 +135,130 @@ module TestIRB assert_indenting(lines, row.new_line_spaces, true) end end + + def test_incomplete_coding_magic_comment + input_with_correct_indents = [ + Row.new(%q(#coding:u), nil, 0), + ] + + lines = [] + input_with_correct_indents.each do |row| + lines << row.content + assert_indenting(lines, row.current_line_spaces, false) + assert_indenting(lines, row.new_line_spaces, true) + end + end + + def test_incomplete_encoding_magic_comment + input_with_correct_indents = [ + Row.new(%q(#encoding:u), nil, 0), + ] + + lines = [] + input_with_correct_indents.each do |row| + lines << row.content + assert_indenting(lines, row.current_line_spaces, false) + assert_indenting(lines, row.new_line_spaces, true) + end + end + + def test_incomplete_emacs_coding_magic_comment + input_with_correct_indents = [ + Row.new(%q(# -*- coding: u), nil, 0), + ] + + lines = [] + input_with_correct_indents.each do |row| + lines << row.content + assert_indenting(lines, row.current_line_spaces, false) + assert_indenting(lines, row.new_line_spaces, true) + end + end + + def test_incomplete_vim_coding_magic_comment + input_with_correct_indents = [ + Row.new(%q(# vim:set fileencoding=u), nil, 0), + ] + + lines = [] + input_with_correct_indents.each do |row| + lines << row.content + assert_indenting(lines, row.current_line_spaces, false) + assert_indenting(lines, row.new_line_spaces, true) + end + end + + def test_mixed_rescue + input_with_correct_indents = [ + Row.new(%q(def m), nil, 2), + Row.new(%q( begin), nil, 4), + Row.new(%q( begin), nil, 6), + Row.new(%q( x = a rescue 4), nil, 6), + Row.new(%q( y = [(a rescue 5)]), nil, 6), + Row.new(%q( [x, y]), nil, 6), + Row.new(%q( rescue => e), 4, 6), + Row.new(%q( raise e rescue 8), nil, 6), + Row.new(%q( end), 4, 4), + Row.new(%q( rescue), 2, 4), + Row.new(%q( raise rescue 11), nil, 4), + Row.new(%q( end), 2, 2), + Row.new(%q(rescue => e), 0, 2), + Row.new(%q( raise e rescue 14), nil, 2), + Row.new(%q(end), 0, 0), + ] + + lines = [] + input_with_correct_indents.each do |row| + lines << row.content + assert_indenting(lines, row.current_line_spaces, false) + assert_indenting(lines, row.new_line_spaces, true) + end + end + + def test_oneliner_method_definition + input_with_correct_indents = [ + Row.new(%q(class A), nil, 2), + Row.new(%q( def foo0), nil, 4), + Row.new(%q( 3), nil, 4), + Row.new(%q( end), 2, 2), + Row.new(%q( def foo1()), nil, 4), + Row.new(%q( 3), nil, 4), + Row.new(%q( end), 2, 2), + Row.new(%q( def foo2(a, b)), nil, 4), + Row.new(%q( a + b), nil, 4), + Row.new(%q( end), 2, 2), + Row.new(%q( def foo3 a, b), nil, 4), + Row.new(%q( a + b), nil, 4), + Row.new(%q( end), 2, 2), + Row.new(%q( def bar0() = 3), nil, 2), + Row.new(%q( def bar1(a) = a), nil, 2), + Row.new(%q( def bar2(a, b) = a + b), nil, 2), + Row.new(%q(end), 0, 0), + ] + + lines = [] + input_with_correct_indents.each do |row| + lines << row.content + assert_indenting(lines, row.current_line_spaces, false) + assert_indenting(lines, row.new_line_spaces, true) + end + end + + def test_tlambda + input_with_correct_indents = [ + Row.new(%q(if true), nil, 2, 1), + Row.new(%q( -> {), nil, 4, 2), + Row.new(%q( }), 2, 2, 1), + Row.new(%q(end), 0, 0, 0), + ] + + lines = [] + input_with_correct_indents.each do |row| + lines << row.content + assert_indenting(lines, row.current_line_spaces, false) + assert_indenting(lines, row.new_line_spaces, true) + assert_nesting_level(lines, row.nesting_level) + end + end end end diff --git a/test/irb/test_workspace.rb b/test/irb/test_workspace.rb index 992cdd4516..15c77315a8 100644 --- a/test/irb/test_workspace.rb +++ b/test/irb/test_workspace.rb @@ -1,6 +1,7 @@ # frozen_string_literal: false require 'test/unit' require 'tempfile' +require 'rubygems' require 'irb' require 'irb/workspace' require 'irb/color' |