|
| 1 | +require 'test/unit' |
| 2 | + |
| 3 | +module TestIRB |
| 4 | + class TestRubyLex < Test::Unit::TestCase |
| 5 | + Row = Struct.new(:content, :current_line_spaces, :new_line_spaces) |
| 6 | + |
| 7 | + class MockIO |
| 8 | + def initialize(params, &assertion) |
| 9 | + @params = params |
| 10 | + @assertion = assertion |
| 11 | + end |
| 12 | + |
| 13 | + def auto_indent(&block) |
| 14 | + result = block.call(*@params) |
| 15 | + @assertion.call(result) |
| 16 | + end |
| 17 | + end |
| 18 | + |
| 19 | + def assert_indenting(lines, correct_space_count, add_new_line) |
| 20 | + lines = lines + [""] if add_new_line |
| 21 | + last_line_index = lines.length - 1 |
| 22 | + byte_pointer = lines.last.length |
| 23 | + |
| 24 | + ruby_lex = RubyLex.new() |
| 25 | + io = MockIO.new([lines, last_line_index, byte_pointer, add_new_line]) do |auto_indent| |
| 26 | + error_message = "Calculated the wrong number of spaces for:\n #{lines.join("\n")}" |
| 27 | + assert_equal(correct_space_count, auto_indent, error_message) |
| 28 | + end |
| 29 | + ruby_lex.set_input(io) |
| 30 | + context = OpenStruct.new(auto_indent_mode: true) |
| 31 | + ruby_lex.set_auto_indent(context) |
| 32 | + end |
| 33 | + |
| 34 | + def test_auto_indent |
| 35 | + input_with_correct_indents = [ |
| 36 | + Row.new(%q(def each_top_level_statement), nil, 2), |
| 37 | + Row.new(%q( initialize_input), nil, 2), |
| 38 | + Row.new(%q( catch(:TERM_INPUT) do), nil, 4), |
| 39 | + Row.new(%q( loop do), nil, 6), |
| 40 | + Row.new(%q( begin), nil, 8), |
| 41 | + Row.new(%q( prompt), nil, 8), |
| 42 | + Row.new(%q( unless l = lex), nil, 10), |
| 43 | + Row.new(%q( throw :TERM_INPUT if @line == ''), nil, 10), |
| 44 | + Row.new(%q( else), 8, 10), |
| 45 | + Row.new(%q( @line_no += l.count("\n")), nil, 10), |
| 46 | + Row.new(%q( next if l == "\n"), nil, 10), |
| 47 | + Row.new(%q( @line.concat l), nil, 10), |
| 48 | + Row.new(%q( if @code_block_open or @ltype or @continue or @indent > 0), nil, 12), |
| 49 | + Row.new(%q( next), nil, 12), |
| 50 | + Row.new(%q( end), 10, 10), |
| 51 | + Row.new(%q( end), 8, 8), |
| 52 | + Row.new(%q( if @line != "\n"), nil, 10), |
| 53 | + Row.new(%q( @line.force_encoding(@io.encoding)), nil, 10), |
| 54 | + Row.new(%q( yield @line, @exp_line_no), nil, 10), |
| 55 | + Row.new(%q( end), 8, 8), |
| 56 | + Row.new(%q( break if @io.eof?), nil, 8), |
| 57 | + Row.new(%q( @line = ''), nil, 8), |
| 58 | + Row.new(%q( @exp_line_no = @line_no), nil, 8), |
| 59 | + Row.new(%q( ), nil, 8), |
| 60 | + Row.new(%q( @indent = 0), nil, 8), |
| 61 | + Row.new(%q( rescue TerminateLineInput), 6, 8), |
| 62 | + Row.new(%q( initialize_input), nil, 8), |
| 63 | + Row.new(%q( prompt), nil, 8), |
| 64 | + Row.new(%q( end), 6, 6), |
| 65 | + Row.new(%q( end), 4, 4), |
| 66 | + Row.new(%q( end), 2, 2), |
| 67 | + Row.new(%q(end), 0, 0), |
| 68 | + ] |
| 69 | + |
| 70 | + lines = [] |
| 71 | + input_with_correct_indents.each do |row| |
| 72 | + lines << row.content |
| 73 | + |
| 74 | + assert_indenting(lines, row.current_line_spaces, false) |
| 75 | + assert_indenting(lines, row.new_line_spaces, true) |
| 76 | + end |
| 77 | + end |
| 78 | + end |
| 79 | +end |
0 commit comments