summaryrefslogtreecommitdiff
path: root/test/irb
diff options
context:
space:
mode:
authorStan Lo <stan001212@gmail.com>2023-02-22 03:38:03 +0800
committergit <svn-admin@ruby-lang.org>2023-02-21 19:38:09 +0000
commit50e77b6a9c0ebbb1c5f2a9b075b7afb2fbe19fb4 (patch)
tree620d6540b150bb0dcc2474f840ddf3fbf6f59a09 /test/irb
parent5baef075067cff551c8b3b00c86714b8c98acc65 (diff)
[ruby/irb] Improve RubyLex's tests
(https://github.com/ruby/irb/pull/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
Diffstat (limited to 'test/irb')
-rw-r--r--test/irb/test_ruby_lex.rb129
1 files changed, 71 insertions, 58 deletions
diff --git a/test/irb/test_ruby_lex.rb b/test/irb/test_ruby_lex.rb
index e4dea3d662..d9b1418cbe 100644
--- a/test/irb/test_ruby_lex.rb
+++ b/test/irb/test_ruby_lex.rb
@@ -10,14 +10,15 @@ module TestIRB
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
@@ -29,20 +30,56 @@ module TestIRB
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: [])
@@ -108,8 +145,7 @@ module TestIRB
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
@@ -124,8 +160,7 @@ module TestIRB
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
@@ -143,8 +178,7 @@ module TestIRB
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
@@ -157,8 +191,7 @@ module TestIRB
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
@@ -177,8 +210,7 @@ module TestIRB
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
@@ -253,8 +285,7 @@ module TestIRB
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
@@ -266,8 +297,7 @@ module TestIRB
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
@@ -279,8 +309,7 @@ module TestIRB
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
@@ -292,8 +321,7 @@ module TestIRB
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
@@ -319,8 +347,7 @@ module TestIRB
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
@@ -350,8 +377,7 @@ module TestIRB
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
@@ -366,8 +392,7 @@ module TestIRB
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
@@ -387,8 +412,7 @@ module TestIRB
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
@@ -436,8 +460,7 @@ module TestIRB
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
@@ -452,8 +475,7 @@ module TestIRB
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
@@ -468,8 +490,7 @@ module TestIRB
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
@@ -485,8 +506,7 @@ module TestIRB
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
@@ -501,8 +521,7 @@ module TestIRB
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
@@ -517,8 +536,7 @@ module TestIRB
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
@@ -533,8 +551,7 @@ module TestIRB
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
@@ -549,8 +566,7 @@ module TestIRB
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
@@ -575,8 +591,7 @@ module TestIRB
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
@@ -592,8 +607,7 @@ module TestIRB
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
@@ -611,8 +625,7 @@ module TestIRB
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