diff options
author | Takashi Kokubun <takashikkbn@gmail.com> | 2019-04-26 18:28:54 +0900 |
---|---|---|
committer | Takashi Kokubun <takashikkbn@gmail.com> | 2019-04-26 18:28:57 +0900 |
commit | 52cfb17086998b9434c9c786bfcf827197216c9a (patch) | |
tree | a94e98569e51a9b4cccc8b6ba481b3f7080061ab /test | |
parent | a6805771ec202a8b8586d6624b05342029cace0d (diff) | |
download | ruby-52cfb17086998b9434c9c786bfcf827197216c9a.tar.gz |
make sync-default-gems GEM=irb
from https://github.com/ruby/irb/commit/e6739d8c66dc78562930adb0b96935c9b38acf74
Diffstat (limited to 'test')
-rw-r--r-- | test/irb/test_color.rb | 18 | ||||
-rw-r--r-- | test/irb/test_completion.rb | 22 | ||||
-rw-r--r-- | test/irb/test_context.rb | 3 | ||||
-rw-r--r-- | test/irb/test_raise_no_backtrace_exception.rb | 2 | ||||
-rw-r--r-- | test/irb/test_ruby-lex.rb | 108 | ||||
-rw-r--r-- | test/irb/test_workspace.rb | 6 |
6 files changed, 151 insertions, 8 deletions
diff --git a/test/irb/test_color.rb b/test/irb/test_color.rb index 4b1878545e..8439fd54f5 100644 --- a/test/irb/test_color.rb +++ b/test/irb/test_color.rb @@ -15,6 +15,11 @@ module TestIRB CYAN = "\e[36m" def test_colorize_code + if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.5.0') + assert_equal({}, IRB::Color::TOKEN_SEQ_EXPRS) + skip "this Ripper version is not supported" + end + { "1" => "#{BLUE}#{BOLD}1#{CLEAR}", "2.3" => "#{MAGENTA}#{BOLD}2.3#{CLEAR}", @@ -23,11 +28,22 @@ module TestIRB "def self.foo; bar; end" => "#{GREEN}def#{CLEAR} #{CYAN}#{BOLD}self#{CLEAR}.#{BLUE}#{BOLD}foo#{CLEAR}; bar; #{GREEN}end#{CLEAR}", 'ERB.new("a#{nil}b", trim_mode: "-")' => "#{BLUE}#{BOLD}#{UNDERLINE}ERB#{CLEAR}.new(#{RED}\"#{CLEAR}#{RED}a#{CLEAR}#{RED}\#{#{CLEAR}#{CYAN}#{BOLD}nil#{CLEAR}#{RED}}#{CLEAR}#{RED}b#{CLEAR}#{RED}\"#{CLEAR}, #{MAGENTA}trim_mode:#{CLEAR} #{RED}\"#{CLEAR}#{RED}-#{CLEAR}#{RED}\"#{CLEAR})", "# comment" => "#{BLUE}#{BOLD}# comment#{CLEAR}", - '/r#{e}g/' => "#{RED}#{BOLD}/#{CLEAR}#{RED}r#{CLEAR}#{RED}\#{#{CLEAR}e}#{RED}g#{CLEAR}#{RED}#{BOLD}/#{CLEAR}", "yield(hello)" => "#{GREEN}yield#{CLEAR}(hello)", }.each do |code, result| assert_equal(result, with_term { IRB::Color.colorize_code(code) }) end + + if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.6.0') + { + '/r#{e}g/' => "#{RED}#{BOLD}/#{CLEAR}#{RED}r#{CLEAR}#{RED}\#{#{CLEAR}e}#{RED}g#{CLEAR}#{RED}#{BOLD}/#{CLEAR}", + } + else + { + '/r#{e}g/' => "#{RED}#{BOLD}/#{CLEAR}#{RED}r#{CLEAR}#{RED}\#{#{CLEAR}e#{RED}}#{CLEAR}#{RED}g#{CLEAR}#{RED}#{BOLD}/#{CLEAR}", + } + end.each do |code, result| + assert_equal(result, with_term { IRB::Color.colorize_code(code) }) + end end def test_inspect_colorable diff --git a/test/irb/test_completion.rb b/test/irb/test_completion.rb new file mode 100644 index 0000000000..608c41bad9 --- /dev/null +++ b/test/irb/test_completion.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: false +require 'test/unit' + +module TestIRB + class TestCompletion < Test::Unit::TestCase + def test_nonstring_module_name + begin + require "irb/completion" + bug5938 = '[ruby-core:42244]' + cmds = %W[-rirb -rirb/completion -e IRB.setup(__FILE__) + -e IRB.conf[:MAIN_CONTEXT]=IRB::Irb.new.context + -e module\sFoo;def\sself.name;//;end;end + -e IRB::InputCompletor::CompletionProc.call("[1].first.") + -- -f --] + status = assert_in_out_err(cmds, "", //, [], bug5938) + assert(status.success?, bug5938) + rescue LoadError + skip "cannot load irb/completion" + end + end + end +end diff --git a/test/irb/test_context.rb b/test/irb/test_context.rb index 469599c11b..fa2432b3f3 100644 --- a/test/irb/test_context.rb +++ b/test/irb/test_context.rb @@ -55,7 +55,6 @@ module TestIRB end def test_eval_input - verbose, $VERBOSE = $VERBOSE, nil input = TestInputMethod.new([ "raise 'Foo'\n", "_\n", @@ -72,8 +71,6 @@ module TestIRB :*, /0$/, :*, /0$/, /\s*/], out) - ensure - $VERBOSE = verbose end end end diff --git a/test/irb/test_raise_no_backtrace_exception.rb b/test/irb/test_raise_no_backtrace_exception.rb index 72e82bf7f7..d3882a427c 100644 --- a/test/irb/test_raise_no_backtrace_exception.rb +++ b/test/irb/test_raise_no_backtrace_exception.rb @@ -4,7 +4,7 @@ require 'test/unit' module TestIRB class TestRaiseNoBacktraceException < Test::Unit::TestCase def test_raise_exception - assert_in_out_err(%w[-rirb -W1 -e IRB.start(__FILE__) -- -f --], <<-IRB, /Exception: foo/, [], success: true) + status = assert_in_out_err(%w[-rirb -e IRB.start(__FILE__) -- -f --], <<-IRB, /Exception: foo/, []) e = Exception.new("foo") def e.backtrace; nil; end raise e diff --git a/test/irb/test_ruby-lex.rb b/test/irb/test_ruby-lex.rb new file mode 100644 index 0000000000..b07b4a2eb6 --- /dev/null +++ b/test/irb/test_ruby-lex.rb @@ -0,0 +1,108 @@ +# frozen_string_literal: false +require 'test/unit' +require 'irb/ruby-lex' +require 'stringio' + +module TestIRB + class TestRubyLex < Test::Unit::TestCase + def setup + @scanner = RubyLex.new + end + + def teardown + RubyLex.debug_level = 0 + end + + def test_set_input_proc + called = false + @scanner.set_input(nil) {called = true; nil} + @scanner.each_top_level_statement {} + assert(called) + end + + def test_comment + assert_equal([["#\n", 1]], top_level_statement("#\n")) + end + + def test_top_level_statement + result = top_level_statement("#{<<-"begin;"}#{<<~"end;"}") + begin; + begin + end + begin + end + end; + assert_equal([ + ["begin\n""end\n", 1], + ["begin\n""end\n", 3], + ], + result) + end + + def test_immature_statement + src = "if false\n" + assert_equal([[src, 1]], top_level_statement(src)) + end + + def test_prompt + prompts = [] + @scanner.set_prompt {|*a| + a << @scanner.instance_variable_get(:@lex_state) + unless prompts.last == a + prompts << a + end + } + src, lineno = "#{<<-"begin;"}#{<<~'end;'}", __LINE__+1 + begin; + # #;# LTYPE:INDENT:CONTINUE + x #;# -:0:- + x( #;# -:0:- + ) #;# -:1:* + a \ #;# -:0:- + #;# -:0:* + a; #;# -:0:- + a #;# -:0:- + #;# -:0:- + a #;# -:0:- + a = #;# -:0:- + ' #;# -:0:* + ' #;# ':0:* + if false or #;# -:0:- + true #;# -:1:* + a #;# -:1:- + " #;# -:1:- + " #;# ":1:- + begin #;# -:1:- + a #;# -:2:- + a #;# -:2:- + end #;# -:2:- + else #;# -:1:- + nil #;# -:1:- + end #;# -:1:- + end; + top_level_statement(src.gsub(/[ \t]*#;#.*/, '')) + src.each_line.with_index(1) do |line, i| + p = prompts.shift + next unless /#;#\s*(?:-|(?<ltype>\S)):(?<indent>\d+):(?:(?<cont>\*)|-)(?:.*FIXME:(?<fixme>.*))?/ =~ line + indent = indent.to_i + cont = (fixme && /`continue'/.match?(fixme)) ^ cont + assert_equal([ltype, indent, cont, i], p[0..3], "#{lineno+i}:#{p[4]}: #{line}") + end + end + + def top_level_statement(lines) + input = InputLines.new(lines, "r") + scanned = [] + @scanner.set_input(input) + @scanner.each_top_level_statement {|*e| + scanned << e + yield(*e) if defined?(yield) + } + scanned + end + + class InputLines < StringIO + alias encoding external_encoding + end + end +end diff --git a/test/irb/test_workspace.rb b/test/irb/test_workspace.rb index 9c87468cf7..fe63c3c225 100644 --- a/test/irb/test_workspace.rb +++ b/test/irb/test_workspace.rb @@ -7,7 +7,7 @@ require 'irb/color' module TestIRB class TestWorkSpace < Test::Unit::TestCase def test_code_around_binding - Tempfile.create do |f| + Tempfile.create('irb') do |f| code = <<~RUBY # 1 # 2 @@ -37,7 +37,7 @@ module TestIRB skip 'chmod cannot make file unreadable on windows' if windows? skip 'skipped in root privilege' if Process.uid == 0 - Tempfile.create do |f| + Tempfile.create('irb') do |f| code = "IRB::WorkSpace.new(binding)\n" f.print(code) f.close @@ -51,7 +51,7 @@ module TestIRB def test_code_around_binding_with_script_lines__ with_script_lines do |script_lines| - Tempfile.create do |f| + Tempfile.create('irb') do |f| code = "IRB::WorkSpace.new(binding)\n" script_lines[f.path] = code.split(/^/) |