Skip to content

Commit

Permalink
[ruby/irb] Improve RubyLex's tests
Browse files Browse the repository at this point in the history
(ruby/irb#484)

* Improve assert_indenting helper

Instead of putting assertions inside the `auto_indent` block, we
can just make `auto_indent` return the calculated space count, and use
it for assertion outside of the `auto_indent` block call.

This simplifies the setup code and makes the intention easier to
understand.

* Introduce assert_row_indenting helper

1. Helper users shouldn't need to write 2 assertions for the current and
   the next line's indentation.
2. With this new approach, we can generate clearer error message for
   both cases:

When the current line's space count doesn't match

```
  Incorrect spaces calculation for line:

  ```
> def each_top_level_statement
  ```

  All lines:

  ```
  def each_top_level_statement
  ```

<0> expected but was
<nil>
```

When the next line's space count doesn't match

```
  Incorrect spaces calculation for line after the current line:

  ```
  def each_top_level_statement
>
  ```

  All lines:

  ```
  def each_top_level_statement
  ```

<3> expected but was
<2>
```

* Replace assert_indenting with assert_row_indenting
  • Loading branch information
st0012 authored and matzbot committed Feb 21, 2023
1 parent 5baef07 commit 50e77b6
Showing 1 changed file with 71 additions and 58 deletions.
129 changes: 71 additions & 58 deletions test/irb/test_ruby_lex.rb
Expand Up @@ -10,14 +10,15 @@ class TestRubyLex < TestCase
Row = Struct.new(:content, :current_line_spaces, :new_line_spaces, :nesting_level)

class MockIO_AutoIndent
def initialize(params, &assertion)
attr_reader :calculated_indent

def initialize(*params)
@params = params
@assertion = assertion
@calculated_indent
end

def auto_indent(&block)
result = block.call(*@params)
@assertion.call(result)
@calculated_indent = block.call(*@params)
end
end

Expand All @@ -29,20 +30,56 @@ def teardown
restore_encodings
end

def assert_indenting(lines, correct_space_count, add_new_line)
def calculate_indenting(lines, add_new_line)
lines = lines + [""] if add_new_line
last_line_index = lines.length - 1
byte_pointer = lines.last.length

context = build_context
context.auto_indent_mode = true

ruby_lex = RubyLex.new(context)
io = MockIO_AutoIndent.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)
mock_io = MockIO_AutoIndent.new(lines, last_line_index, byte_pointer, add_new_line)

ruby_lex.set_input(mock_io)
ruby_lex.set_auto_indent
mock_io.calculated_indent
end

def assert_row_indenting(lines, row)
actual_current_line_spaces = calculate_indenting(lines, false)

error_message = <<~MSG
Incorrect spaces calculation for line:
```
> #{lines.last}
```
All lines:
```
#{lines.join("\n")}
```
MSG
assert_equal(row.current_line_spaces, actual_current_line_spaces, error_message)

error_message = <<~MSG
Incorrect spaces calculation for line after the current line:
```
#{lines.last}
>
```
All lines:
```
#{lines.join("\n")}
```
MSG
actual_next_line_spaces = calculate_indenting(lines, true)
assert_equal(row.new_line_spaces, actual_next_line_spaces, error_message)
end

def assert_nesting_level(lines, expected, local_variables: [])
Expand Down Expand Up @@ -108,8 +145,7 @@ def test_auto_indent
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_row_indenting(lines, row)
end
end

Expand All @@ -124,8 +160,7 @@ def test_braces_on_their_own_line
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_row_indenting(lines, row)
end
end

Expand All @@ -143,8 +178,7 @@ def test_multiple_braces_in_a_line
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_row_indenting(lines, row)
end
end

Expand All @@ -157,8 +191,7 @@ def test_a_closed_brace_and_not_closed_brace_in_a_line
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_row_indenting(lines, row)
end
end

Expand All @@ -177,8 +210,7 @@ def test_symbols
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_row_indenting(lines, row)
end
end

Expand Down Expand Up @@ -253,8 +285,7 @@ def test_incomplete_coding_magic_comment
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_row_indenting(lines, row)
end
end

Expand All @@ -266,8 +297,7 @@ def test_incomplete_encoding_magic_comment
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_row_indenting(lines, row)
end
end

Expand All @@ -279,8 +309,7 @@ def test_incomplete_emacs_coding_magic_comment
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_row_indenting(lines, row)
end
end

Expand All @@ -292,8 +321,7 @@ def test_incomplete_vim_coding_magic_comment
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_row_indenting(lines, row)
end
end

Expand All @@ -319,8 +347,7 @@ def test_mixed_rescue
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_row_indenting(lines, row)
end
end

Expand Down Expand Up @@ -350,8 +377,7 @@ def test_oneliner_method_definition
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_row_indenting(lines, row)
end
end

Expand All @@ -366,8 +392,7 @@ def test_tlambda
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_row_indenting(lines, row)
assert_nesting_level(lines, row.nesting_level)
end
end
Expand All @@ -387,8 +412,7 @@ def test_corresponding_syntax_to_keyword_do_in_class
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_row_indenting(lines, row)
assert_nesting_level(lines, row.nesting_level)
end
end
Expand Down Expand Up @@ -436,8 +460,7 @@ def test_corresponding_syntax_to_keyword_do
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_row_indenting(lines, row)
assert_nesting_level(lines, row.nesting_level)
end
end
Expand All @@ -452,8 +475,7 @@ def test_corresponding_syntax_to_keyword_for
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_row_indenting(lines, row)
assert_nesting_level(lines, row.nesting_level)
end
end
Expand All @@ -468,8 +490,7 @@ def test_corresponding_syntax_to_keyword_for_with_do
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_row_indenting(lines, row)
assert_nesting_level(lines, row.nesting_level)
end
end
Expand All @@ -485,8 +506,7 @@ def test_corresponding_syntax_to_keyword_in
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_row_indenting(lines, row)
assert_nesting_level(lines, row.nesting_level)
end
end
Expand All @@ -501,8 +521,7 @@ def test_bracket_corresponding_to_times
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_row_indenting(lines, row)
assert_nesting_level(lines, row.nesting_level)
end
end
Expand All @@ -517,8 +536,7 @@ def test_do_corresponding_to_times
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_row_indenting(lines, row)
assert_nesting_level(lines, row.nesting_level)
end
end
Expand All @@ -533,8 +551,7 @@ def test_bracket_corresponding_to_loop
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_row_indenting(lines, row)
assert_nesting_level(lines, row.nesting_level)
end
end
Expand All @@ -549,8 +566,7 @@ def test_do_corresponding_to_loop
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_row_indenting(lines, row)
assert_nesting_level(lines, row.nesting_level)
end
end
Expand All @@ -575,8 +591,7 @@ def test_heredoc_with_indent
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_row_indenting(lines, row)
assert_nesting_level(lines, row.nesting_level)
end
end
Expand All @@ -592,8 +607,7 @@ def test_oneliner_def_in_multiple_lines
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_row_indenting(lines, row)
assert_nesting_level(lines, row.nesting_level)
end
end
Expand All @@ -611,8 +625,7 @@ def test_broken_heredoc
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_row_indenting(lines, row)
assert_nesting_level(lines, row.nesting_level)
end
end
Expand Down

0 comments on commit 50e77b6

Please sign in to comment.