summaryrefslogtreecommitdiff
path: root/test/irb
diff options
context:
space:
mode:
Diffstat (limited to 'test/irb')
-rw-r--r--test/irb/test_color.rb2
-rw-r--r--test/irb/test_completion.rb22
-rw-r--r--test/irb/test_context.rb38
-rw-r--r--test/irb/test_ruby_lex.rb130
4 files changed, 191 insertions, 1 deletions
diff --git a/test/irb/test_color.rb b/test/irb/test_color.rb
index 3ced640004..cb90d29c9d 100644
--- a/test/irb/test_color.rb
+++ b/test/irb/test_color.rb
@@ -29,7 +29,7 @@ module TestIRB
"def self.foo; bar; end" => "#{GREEN}def#{CLEAR} #{CYAN}#{BOLD}self#{CLEAR}.#{BLUE}#{BOLD}foo#{CLEAR}; bar; #{GREEN}end#{CLEAR}",
'erb = ERB.new("a#{nil}b", trim_mode: "-")' => "erb = #{BLUE}#{BOLD}#{UNDERLINE}ERB#{CLEAR}.new(#{RED}#{BOLD}\"#{CLEAR}#{RED}a#{CLEAR}#{RED}\#{#{CLEAR}#{CYAN}#{BOLD}nil#{CLEAR}#{RED}}#{CLEAR}#{RED}b#{CLEAR}#{RED}#{BOLD}\"#{CLEAR}, #{MAGENTA}trim_mode:#{CLEAR} #{RED}#{BOLD}\"#{CLEAR}#{RED}-#{CLEAR}#{RED}#{BOLD}\"#{CLEAR})",
"# comment" => "#{BLUE}#{BOLD}# comment#{CLEAR}",
- "yield(hello)" => "#{GREEN}yield#{CLEAR}(hello)",
+ "def f;yield(hello);end" => "#{GREEN}def#{CLEAR} #{BLUE}#{BOLD}f#{CLEAR};#{GREEN}yield#{CLEAR}(hello);#{GREEN}end#{CLEAR}",
'"##@var]"' => "#{RED}#{BOLD}\"#{CLEAR}#{RED}\##{CLEAR}#{RED}\##{CLEAR}@var#{RED}]#{CLEAR}#{RED}#{BOLD}\"#{CLEAR}",
'"foo#{a} #{b}"' => "#{RED}#{BOLD}\"#{CLEAR}#{RED}foo#{CLEAR}#{RED}\#{#{CLEAR}a#{RED}}#{CLEAR}#{RED} #{CLEAR}#{RED}\#{#{CLEAR}b#{RED}}#{CLEAR}#{RED}#{BOLD}\"#{CLEAR}",
'/r#{e}g/' => "#{RED}#{BOLD}/#{CLEAR}#{RED}r#{CLEAR}#{RED}\#{#{CLEAR}e#{RED}}#{CLEAR}#{RED}g#{CLEAR}#{RED}#{BOLD}/#{CLEAR}",
diff --git a/test/irb/test_completion.rb b/test/irb/test_completion.rb
index 52bbc7b2cc..a765bbf3a5 100644
--- a/test/irb/test_completion.rb
+++ b/test/irb/test_completion.rb
@@ -25,5 +25,27 @@ module TestIRB
assert_include(IRB::InputCompletor.retrieve_completion_data("1r.positi", bind: binding), "1r.positive?")
assert_empty(IRB::InputCompletor.retrieve_completion_data("1i.positi", bind: binding))
end
+
+ def test_complete_symbol
+ _ = :aiueo
+ assert_include(IRB::InputCompletor.retrieve_completion_data(":a", bind: binding), ":aiueo")
+ assert_empty(IRB::InputCompletor.retrieve_completion_data(":irb_unknown_symbol_abcdefg", bind: binding))
+ end
+
+ def test_complete_symbol_failure
+ assert_nil(IRB::InputCompletor::PerfectMatchedProc.(":aiueo", bind: binding))
+ end
+
+ def test_complete_reserved_words
+ candidates = IRB::InputCompletor.retrieve_completion_data("de", bind: binding)
+ %w[def defined?].each do |word|
+ assert_include candidates, word
+ end
+
+ candidates = IRB::InputCompletor.retrieve_completion_data("__", bind: binding)
+ %w[__ENCODING__ __LINE__ __FILE__].each do |word|
+ assert_include candidates, word
+ end
+ end
end
end
diff --git a/test/irb/test_context.rb b/test/irb/test_context.rb
index 693ebbeaea..d03cc30c78 100644
--- a/test/irb/test_context.rb
+++ b/test/irb/test_context.rb
@@ -63,6 +63,13 @@ module TestIRB
assert_not_match(/rescue _\.class/, e.message)
end
+ def test_evaluate_with_encoding_error_without_lineno
+ assert_raise_with_message(EncodingError, /invalid symbol/) {
+ @context.evaluate(%q[{"\xAE": 1}], 1)
+ # The backtrace of this invalid encoding hash doesn't contain lineno.
+ }
+ end
+
def test_evaluate_with_onigmo_warning
assert_warning("(irb):1: warning: character class has duplicated range: /[aa]/\n") do
@context.evaluate('/[aa]/', 1)
@@ -216,5 +223,36 @@ module TestIRB
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")
end
+
+ def test_multiline_output_on_default_inspector
+ main = Object.new
+ def main.inspect
+ "abc\ndef"
+ end
+ input = TestInputMethod.new([
+ "self"
+ ])
+ irb = IRB::Irb.new(IRB::WorkSpace.new(main), input)
+ irb.context.return_format = "=> %s\n"
+
+ # The default
+ irb.context.newline_before_multiline_output = true
+ out, err = capture_io do
+ irb.eval_input
+ end
+ assert_empty err
+ assert_equal("=> \nabc\ndef\n",
+ out)
+
+ # No newline before multiline output
+ input.reset
+ irb.context.newline_before_multiline_output = false
+ out, err = capture_io do
+ irb.eval_input
+ end
+ assert_empty err
+ assert_equal("=> abc\ndef\n",
+ out)
+ end
end
end
diff --git a/test/irb/test_ruby_lex.rb b/test/irb/test_ruby_lex.rb
new file mode 100644
index 0000000000..dd5a1f7ca5
--- /dev/null
+++ b/test/irb/test_ruby_lex.rb
@@ -0,0 +1,130 @@
+$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
+require 'irb/ruby-lex'
+require 'test/unit'
+require 'ostruct'
+
+module TestIRB
+ class TestRubyLex < Test::Unit::TestCase
+ Row = Struct.new(:content, :current_line_spaces, :new_line_spaces)
+
+ class MockIO
+ def initialize(params, &assertion)
+ @params = params
+ @assertion = assertion
+ end
+
+ def auto_indent(&block)
+ result = block.call(*@params)
+ @assertion.call(result)
+ end
+ end
+
+ def assert_indenting(lines, correct_space_count, add_new_line)
+ lines = lines + [""] if add_new_line
+ last_line_index = lines.length - 1
+ byte_pointer = lines.last.length
+
+ ruby_lex = RubyLex.new()
+ io = MockIO.new([lines, last_line_index, byte_pointer, add_new_line]) do |auto_indent|
+ error_message = "Calculated the wrong number of spaces for:\n #{lines.join("\n")}"
+ assert_equal(correct_space_count, auto_indent, error_message)
+ end
+ ruby_lex.set_input(io)
+ context = OpenStruct.new(auto_indent_mode: true)
+ ruby_lex.set_auto_indent(context)
+ end
+
+ def test_auto_indent
+ input_with_correct_indents = [
+ Row.new(%q(def each_top_level_statement), nil, 2),
+ Row.new(%q( initialize_input), nil, 2),
+ Row.new(%q( catch(:TERM_INPUT) do), nil, 4),
+ Row.new(%q( loop do), nil, 6),
+ Row.new(%q( begin), nil, 8),
+ Row.new(%q( prompt), nil, 8),
+ Row.new(%q( unless l = lex), nil, 10),
+ Row.new(%q( throw :TERM_INPUT if @line == ''), nil, 10),
+ Row.new(%q( else), 8, 10),
+ Row.new(%q( @line_no += l.count("\n")), nil, 10),
+ Row.new(%q( next if l == "\n"), nil, 10),
+ Row.new(%q( @line.concat l), nil, 10),
+ Row.new(%q( if @code_block_open or @ltype or @continue or @indent > 0), nil, 12),
+ Row.new(%q( next), nil, 12),
+ Row.new(%q( end), 10, 10),
+ Row.new(%q( end), 8, 8),
+ Row.new(%q( if @line != "\n"), nil, 10),
+ Row.new(%q( @line.force_encoding(@io.encoding)), nil, 10),
+ Row.new(%q( yield @line, @exp_line_no), nil, 10),
+ Row.new(%q( end), 8, 8),
+ Row.new(%q( break if @io.eof?), nil, 8),
+ Row.new(%q( @line = ''), nil, 8),
+ Row.new(%q( @exp_line_no = @line_no), nil, 8),
+ Row.new(%q( ), nil, 8),
+ Row.new(%q( @indent = 0), nil, 8),
+ Row.new(%q( rescue TerminateLineInput), 6, 8),
+ Row.new(%q( initialize_input), nil, 8),
+ Row.new(%q( prompt), nil, 8),
+ Row.new(%q( end), 6, 6),
+ Row.new(%q( end), 4, 4),
+ Row.new(%q( end), 2, 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_braces_on_their_own_line
+ input_with_correct_indents = [
+ Row.new(%q(if true), nil, 2),
+ Row.new(%q( [), nil, 4),
+ Row.new(%q( ]), 2, 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_multiple_braces_in_a_line
+ input_with_correct_indents = [
+ Row.new(%q([[[), nil, 6),
+ Row.new(%q( ]), 4, 4),
+ Row.new(%q( ]), 2, 2),
+ Row.new(%q(]), 0, 0),
+ Row.new(%q([<<FOO]), nil, 0),
+ Row.new(%q(hello), nil, 0),
+ Row.new(%q(FOO), 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_a_closed_brace_and_not_closed_brace_in_a_line
+ input_with_correct_indents = [
+ Row.new(%q(p() {), nil, 2),
+ Row.new(%q(}), 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
+ end
+end