8000 Add tests for RubyLex · ruby/irb@752d559 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 752d559

Browse files
committed
Add tests for RubyLex
The set_auto_indent method calculates the correct number of spaces for indenting a line. We think there might be a few bugs in this method so we are testing the current functionality to make sure nothing breaks when we address those bugs. Example test failure: ``` 1) Failure: TestIRB::TestRubyLex#test_auto_indent [/Users/Ben/Projects/irb/test/irb/test_ruby_lex.rb:75]: Calculated the wrong number of spaces for: def each_top_level_statement initialize_input catch(:TERM_INPUT) do loop do begin prompt unless l = lex throw :TERM_INPUT if @line == '' else . <10> expected but was <12>. ```
1 parent aa43b69 commit 752d559

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

test/irb/test_ruby_lex.rb

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

Comments
 (0)
0