diff options
author | Kevin Newton <kddnewton@gmail.com> | 2024-03-06 07:04:13 -0500 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2024-03-06 16:43:03 +0000 |
commit | 294fe8490b475febc2e562d12bf4441d1ac4659e (patch) | |
tree | 37362766957b22adecf401e3743ea2defe2e1d60 | |
parent | 2574e783f4452a57ac03a1ca4a70ec49815f0774 (diff) |
[ruby/prism] Simplify ripper tests in ripper translation
https://github.com/ruby/prism/commit/a66d066162
-rw-r--r-- | lib/prism/translation/ripper.rb | 49 | ||||
-rw-r--r-- | test/prism/ripper_test.rb | 73 |
2 files changed, 54 insertions, 68 deletions
diff --git a/lib/prism/translation/ripper.rb b/lib/prism/translation/ripper.rb index 05795ffae7..6ce5f18411 100644 --- a/lib/prism/translation/ripper.rb +++ b/lib/prism/translation/ripper.rb @@ -2494,10 +2494,10 @@ module Prism parts.each do |part| if part.is_a?(StringNode) - if dedent_next + if dedent_next && !(content = part.content).chomp.empty? common_whitespace = [ common_whitespace || Float::INFINITY, - part.content[/\A\s*/].each_char.inject(0) do |part_whitespace, char| + content[/\A\s*/].each_char.inject(0) do |part_whitespace, char| char == "\t" ? ((part_whitespace / 8 + 1) * 8) : (part_whitespace + 1) end ].min @@ -2509,7 +2509,7 @@ module Prism end end - common_whitespace + common_whitespace || 0 end # Take the content of a string and return the index of the first character @@ -2539,44 +2539,49 @@ module Prism if common_whitespace == 0 bounds(parts.first.location) - previous_string = [] - previous_result = base + string = [] + result = base parts.each do |part| if part.is_a?(StringNode) - if previous_string.empty? - previous_string = [part] + if string.empty? + string = [part] else - previous_string << part + string << part end else - unless previous_string.empty? - bounds(previous_string[0].location) - previous_result = yield(previous_result, on_tstring_content(previous_string.map(&:content).join)) - previous_string = [] + unless string.empty? + bounds(string[0].location) + result = yield(result, on_tstring_content(string.map(&:content).join)) + string = [] end - previous_result = yield(previous_result, visit(part)) + result = yield(result, visit(part)) end end - unless previous_string.empty? - bounds(previous_string[0].location) - previous_result = yield(previous_result, on_tstring_content(previous_string.map(&:content).join)) + unless string.empty? + bounds(string[0].location) + result = yield(result, on_tstring_content(string.map(&:content).join)) end - previous_result + result else bounds(parts.first.location) - parts.inject(base) do |string_content, part| + parts.each.with_index(parts.length).inject(base) do |string_content, (part, index)| yield( string_content, if part.is_a?(StringNode) - content = part.content - trimmed_whitespace = heredoc_trimmed_whitespace(content, common_whitespace) + if index % 2 == 1 + content = part.content + trimmed_whitespace = heredoc_trimmed_whitespace(content, common_whitespace) - bounds(part.content_loc.copy(start_offset: part.content_loc.start_offset + trimmed_whitespace)) - on_tstring_content(part.content[trimmed_whitespace..]) + bounds(part.content_loc.copy(start_offset: part.content_loc.start_offset + trimmed_whitespace)) + on_tstring_content(content[trimmed_whitespace..]) + else + bounds(part.content_loc) + on_tstring_content(part.content) + end else visit(part) end diff --git a/test/prism/ripper_test.rb b/test/prism/ripper_test.rb index 69d226e43f..59a116c593 100644 --- a/test/prism/ripper_test.rb +++ b/test/prism/ripper_test.rb @@ -2,9 +2,6 @@ require_relative "test_helper" -File.delete("passing.txt") if File.exist?("passing.txt") -File.delete("failing.txt") if File.exist?("failing.txt") - module Prism class RipperTest < TestCase base = File.join(__dir__, "fixtures") @@ -29,43 +26,36 @@ module Prism "whitequark/lvar_injecting_match.txt" ] - heredocs = %w[ - dos_endings.txt - heredocs_with_ignored_newlines.txt - seattlerb/heredoc__backslash_dos_format.txt - seattlerb/heredoc_backslash_nl.txt - seattlerb/heredoc_nested.txt - seattlerb/heredoc_squiggly.txt - seattlerb/heredoc_squiggly_blank_line_plus_interpolation.txt - seattlerb/heredoc_squiggly_blank_lines.txt - seattlerb/heredoc_squiggly_interp.txt - seattlerb/heredoc_squiggly_no_indent.txt - seattlerb/heredoc_squiggly_tabs.txt - seattlerb/heredoc_squiggly_tabs_extra.txt - seattlerb/heredoc_squiggly_visually_blank_lines.txt - spanning_heredoc.txt - tilde_heredocs.txt - unparser/corpus/semantic/dstr.txt - whitequark/dedenting_heredoc.txt - whitequark/dedenting_interpolating_heredoc_fake_line_continuation.txt - whitequark/dedenting_non_interpolating_heredoc_line_continuation.txt - whitequark/parser_bug_640.txt - whitequark/parser_drops_truncated_parts_of_squiggly_heredoc.txt - whitequark/parser_slash_slash_n_escaping_in_literals.txt - whitequark/slash_newline_in_heredocs.txt - ] - - skips = incorrect | heredocs | %w[ - seattlerb/block_call_dot_op2_brace_block.txt - seattlerb/block_command_operation_colon.txt - seattlerb/block_command_operation_dot.txt - whitequark/send_block_chain_cmd.txt + omitted = [ + "dos_endings.txt", + "heredocs_with_ignored_newlines.txt", + "seattlerb/heredoc__backslash_dos_format.txt", + "seattlerb/heredoc_backslash_nl.txt", + "seattlerb/heredoc_nested.txt", + "seattlerb/heredoc_squiggly_blank_line_plus_interpolation.txt", + "seattlerb/heredoc_squiggly_no_indent.txt", + "spanning_heredoc.txt", + "tilde_heredocs.txt", + "unparser/corpus/semantic/dstr.txt", + "whitequark/dedenting_heredoc.txt", + "whitequark/parser_bug_640.txt", + "whitequark/parser_drops_truncated_parts_of_squiggly_heredoc.txt", + "whitequark/parser_slash_slash_n_escaping_in_literals.txt", + "whitequark/slash_newline_in_heredocs.txt", + + "seattlerb/block_call_dot_op2_brace_block.txt", + "seattlerb/block_command_operation_colon.txt", + "seattlerb/block_command_operation_dot.txt", + "whitequark/send_block_chain_cmd.txt" ] relatives.each do |relative| + # Skip the tests that Ripper is reporting the wrong results for. + next if incorrect.include?(relative) filepath = File.join(__dir__, "fixtures", relative) define_method "test_ripper_#{relative}" do + omit("Not yet implemented") if omitted.include?(relative) source = File.read(filepath, binmode: true, external_encoding: Encoding::UTF_8) case relative @@ -78,23 +68,14 @@ module Prism source = "def __invalid_yield__\n#{source}\nend" end - assert_ripper(source, filepath, skips.include?(relative)) + assert_ripper(source) end end private - def assert_ripper(source, filepath, allowed_failure) - expected = Ripper.sexp_raw(source) - - begin - assert_equal expected, Prism::Translation::Ripper.sexp_raw(source) - rescue Exception, NoMethodError - File.open("failing.txt", "a") { |f| f.puts filepath } - raise unless allowed_failure - else - File.open("passing.txt", "a") { |f| f.puts filepath } if allowed_failure - end + def assert_ripper(source) + assert_equal Ripper.sexp_raw(source), Prism::Translation::Ripper.sexp_raw(source) end end end |