Skip to content

Commit a66d066

Browse files
committed
Simplify ripper tests in ripper translation
1 parent 3f59d07 commit a66d066

File tree

2 files changed

+54
-68
lines changed

2 files changed

+54
-68
lines changed

lib/prism/translation/ripper.rb

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2494,10 +2494,10 @@ def visit_string_node(node)
24942494

24952495
parts.each do |part|
24962496
if part.is_a?(StringNode)
2497-
if dedent_next
2497+
if dedent_next && !(content = part.content).chomp.empty?
24982498
common_whitespace = [
24992499
common_whitespace || Float::INFINITY,
2500-
part.content[/\A\s*/].each_char.inject(0) do |part_whitespace, char|
2500+
content[/\A\s*/].each_char.inject(0) do |part_whitespace, char|
25012501
char == "\t" ? ((part_whitespace / 8 + 1) * 8) : (part_whitespace + 1)
25022502
end
25032503
].min
@@ -2509,7 +2509,7 @@ def visit_string_node(node)
25092509
end
25102510
end
25112511

2512-
common_whitespace
2512+
common_whitespace || 0
25132513
end
25142514

25152515
# Take the content of a string and return the index of the first character
@@ -2539,44 +2539,49 @@ def visit_string_node(node)
25392539
if common_whitespace == 0
25402540
bounds(parts.first.location)
25412541

2542-
previous_string = []
2543-
previous_result = base
2542+
string = []
2543+
result = base
25442544

25452545
parts.each do |part|
25462546
if part.is_a?(StringNode)
2547-
if previous_string.empty?
2548-
previous_string = [part]
2547+
if string.empty?
2548+
string = [part]
25492549
else
2550-
previous_string << part
2550+
string << part
25512551
end
25522552
else
2553-
unless previous_string.empty?
2554-
bounds(previous_string[0].location)
2555-
previous_result = yield(previous_result, on_tstring_content(previous_string.map(&:content).join))
2556-
previous_string = []
2553+
unless string.empty?
2554+
bounds(string[0].location)
2555+
result = yield(result, on_tstring_content(string.map(&:content).join))
2556+
string = []
25572557
end
25582558

2559-
previous_result = yield(previous_result, visit(part))
2559+
result = yield(result, visit(part))
25602560
end
25612561
end
25622562

2563-
unless previous_string.empty?
2564-
bounds(previous_string[0].location)
2565-
previous_result = yield(previous_result, on_tstring_content(previous_string.map(&:content).join))
2563+
unless string.empty?
2564+
bounds(string[0].location)
2565+
result = yield(result, on_tstring_content(string.map(&:content).join))
25662566
end
25672567

2568-
previous_result
2568+
result
25692569
else
25702570
bounds(parts.first.location)
2571-
parts.inject(base) do |string_content, part|
2571+
parts.each.with_index(parts.length).inject(base) do |string_content, (part, index)|
25722572
yield(
25732573
string_content,
25742574
if part.is_a?(StringNode)
2575-
content = part.content
2576-
trimmed_whitespace = heredoc_trimmed_whitespace(content, common_whitespace)
2575+
if index % 2 == 1
2576+
content = part.content
2577+
trimmed_whitespace = heredoc_trimmed_whitespace(content, common_whitespace)
25772578

2578-
bounds(part.content_loc.copy(start_offset: part.content_loc.start_offset + trimmed_whitespace))
2579-
on_tstring_content(part.content[trimmed_whitespace..])
2579+
bounds(part.content_loc.copy(start_offset: part.content_loc.start_offset + trimmed_whitespace))
2580+
on_tstring_content(content[trimmed_whitespace..])
2581+
else
2582+
bounds(part.content_loc)
2583+
on_tstring_content(part.content)
2584+
end
25802585
else
25812586
visit(part)
25822587
end

test/prism/ripper_test.rb

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
require_relative "test_helper"
44

5-
File.delete("passing.txt") if File.exist?("passing.txt")
6-
File.delete("failing.txt") if File.exist?("failing.txt")
7-
85
module Prism
96
class RipperTest < TestCase
107
base = File.join(__dir__, "fixtures")
@@ -29,43 +26,36 @@ class RipperTest < TestCase
2926
"whitequark/lvar_injecting_match.txt"
3027
]
3128

32-
heredocs = %w[
33-
dos_endings.txt
34-
heredocs_with_ignored_newlines.txt
35-
seattlerb/heredoc__backslash_dos_format.txt
36-
seattlerb/heredoc_backslash_nl.txt
37-
seattlerb/heredoc_nested.txt
38-
seattlerb/heredoc_squiggly.txt
39-
seattlerb/heredoc_squiggly_blank_line_plus_interpolation.txt
40-
seattlerb/heredoc_squiggly_blank_lines.txt
41-
seattlerb/heredoc_squiggly_interp.txt
42-
seattlerb/heredoc_squiggly_no_indent.txt
43-
seattlerb/heredoc_squiggly_tabs.txt
44-
seattlerb/heredoc_squiggly_tabs_extra.txt
45-
seattlerb/heredoc_squiggly_visually_blank_lines.txt
46-
spanning_heredoc.txt
47-
tilde_heredocs.txt
48-
unparser/corpus/semantic/dstr.txt
49-
whitequark/dedenting_heredoc.txt
50-
whitequark/dedenting_interpolating_heredoc_fake_line_continuation.txt
51-
whitequark/dedenting_non_interpolating_heredoc_line_continuation.txt
52-
whitequark/parser_bug_640.txt
53-
whitequark/parser_drops_truncated_parts_of_squiggly_heredoc.txt
54-
whitequark/parser_slash_slash_n_escaping_in_literals.txt
55-
whitequark/slash_newline_in_heredocs.txt
56-
]
57-
58-
skips = incorrect | heredocs | %w[
59-
seattlerb/block_call_dot_op2_brace_block.txt
60-
seattlerb/block_command_operation_colon.txt
61-
seattlerb/block_command_operation_dot.txt
62-
whitequark/send_block_chain_cmd.txt
29+
omitted = [
30+
"dos_endings.txt",
31+
"heredocs_with_ignored_newlines.txt",
32+
"seattlerb/heredoc__backslash_dos_format.txt",
33+
"seattlerb/heredoc_backslash_nl.txt",
34+
"seattlerb/heredoc_nested.txt",
35+
"seattlerb/heredoc_squiggly_blank_line_plus_interpolation.txt",
36+
"seattlerb/heredoc_squiggly_no_indent.txt",
37+
"spanning_heredoc.txt",
38+
"tilde_heredocs.txt",
39+
"unparser/corpus/semantic/dstr.txt",
40+
"whitequark/dedenting_heredoc.txt",
41+
"whitequark/parser_bug_640.txt",
42+
"whitequark/parser_drops_truncated_parts_of_squiggly_heredoc.txt",
43+
"whitequark/parser_slash_slash_n_escaping_in_literals.txt",
44+
"whitequark/slash_newline_in_heredocs.txt",
45+
46+
"seattlerb/block_call_dot_op2_brace_block.txt",
47+
"seattlerb/block_command_operation_colon.txt",
48+
"seattlerb/block_command_operation_dot.txt",
49+
"whitequark/send_block_chain_cmd.txt"
6350
]
6451

6552
relatives.each do |relative|
53+
# Skip the tests that Ripper is reporting the wrong results for.
54+
next if incorrect.include?(relative)
6655
filepath = File.join(__dir__, "fixtures", relative)
6756

6857
define_method "test_ripper_#{relative}" do
58+
omit("Not yet implemented") if omitted.include?(relative)
6959
source = File.read(filepath, binmode: true, external_encoding: Encoding::UTF_8)
7060

7161
case relative
@@ -78,23 +68,14 @@ class RipperTest < TestCase
7868
source = "def __invalid_yield__\n#{source}\nend"
7969
end
8070

81-
assert_ripper(source, filepath, skips.include?(relative))
71+
assert_ripper(source)
8272
end
8373
end
8474

8575
private
8676

87-
def assert_ripper(source, filepath, allowed_failure)
88-
expected = Ripper.sexp_raw(source)
89-
90-
begin
91-
assert_equal expected, Prism::Translation::Ripper.sexp_raw(source)
92-
rescue Exception, NoMethodError
93-
File.open("failing.txt", "a") { |f| f.puts filepath }
94-
raise unless allowed_failure
95-
else
96-
File.open("passing.txt", "a") { |f| f.puts filepath } if allowed_failure
97-
end
77+
def assert_ripper(source)
78+
assert_equal Ripper.sexp_raw(source), Prism::Translation::Ripper.sexp_raw(source)
9879
end
9980
end
10081
end

0 commit comments

Comments
 (0)