|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'benchmark' |
| 4 | + |
| 5 | +RSpec.describe 'Dynamic string unparsing edge cases' do |
| 6 | + # Integration tests to verify roundtripping of various dynamic string patterns |
| 7 | + |
| 8 | + describe 'strings ending without newline' do |
| 9 | + it 'correctly unpars' do |
| 10 | + code = '"\n\n #{x}"' |
| 11 | + ast = Unparser.parse(code) |
| 12 | + unparsed = Unparser.unparse(ast) |
| 13 | + reparsed = Unparser.parse(unparsed) |
| 14 | + |
| 15 | + expect(ast).to eq(reparsed) |
| 16 | + end |
| 17 | + end |
| 18 | + |
| 19 | + describe 'strings with interpolation not at end' do |
| 20 | + it 'correctly unpars when interpolation is last' do |
| 21 | + code = '"foo\n#{x}"' |
| 22 | + ast = Unparser.parse(code) |
| 23 | + unparsed = Unparser.unparse(ast) |
| 24 | + reparsed = Unparser.parse(unparsed) |
| 25 | + |
| 26 | + expect(ast).to eq(reparsed) |
| 27 | + end |
| 28 | + |
| 29 | + it 'correctly unpars when str is last but no newline' do |
| 30 | + code = '"#{x}bar"' |
| 31 | + ast = Unparser.parse(code) |
| 32 | + unparsed = Unparser.unparse(ast) |
| 33 | + reparsed = Unparser.parse(unparsed) |
| 34 | + |
| 35 | + expect(ast).to eq(reparsed) |
| 36 | + end |
| 37 | + |
| 38 | + it 'correctly unpars when str is last with newline' do |
| 39 | + code = '"#{x}bar\n"' |
| 40 | + ast = Unparser.parse(code) |
| 41 | + unparsed = Unparser.unparse(ast) |
| 42 | + reparsed = Unparser.parse(unparsed) |
| 43 | + |
| 44 | + expect(ast).to eq(reparsed) |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + describe 'complex and large dynamic strings' do |
| 49 | + it 'handles patterns with many interpolations' do |
| 50 | + # Create a complex pattern with many interpolations |
| 51 | + code = '"a" "#{a}" "b" "#{b}" "c" "#{c}" "d" "#{d}" "e"' |
| 52 | + ast = Unparser.parse(code) |
| 53 | + unparsed = Unparser.unparse(ast) |
| 54 | + reparsed = Unparser.parse(unparsed) |
| 55 | + |
| 56 | + expect(ast).to eq(reparsed) |
| 57 | + end |
| 58 | + |
| 59 | + it 'handles very large dynamic strings without performance issues' do |
| 60 | + # Create a large dynamic string with many segments |
| 61 | + # This ensures exhaustive search is efficient in practice |
| 62 | + parts = [] |
| 63 | + 20.times do |i| |
| 64 | + parts << '"str' + i.to_s + '"' |
| 65 | + parts << '"#{var' + i.to_s + '}"' |
| 66 | + end |
| 67 | + code = parts.join(' ') |
| 68 | + |
| 69 | + ast = Unparser.parse(code) |
| 70 | + |
| 71 | + # Should complete quickly despite exponential search space |
| 72 | + # Early termination on first valid segmentation makes this efficient |
| 73 | + unparsed = nil |
| 74 | + elapsed = Benchmark.realtime { unparsed = Unparser.unparse(ast) } |
| 75 | + |
| 76 | + expect(elapsed).to be < 5.0 # Should complete in under 5 seconds |
| 77 | + |
| 78 | + reparsed = Unparser.parse(unparsed) |
| 79 | + expect(ast).to eq(reparsed) |
| 80 | + end |
| 81 | + |
| 82 | + it 'handles deeply nested string concatenation' do |
| 83 | + # Test case that exercises the recursive segmentation search |
| 84 | + code = '"a" "b" "c" "#{x}" "d" "e" "f" "#{y}" "g" "h"' |
| 85 | + ast = Unparser.parse(code) |
| 86 | + unparsed = Unparser.unparse(ast) |
| 87 | + reparsed = Unparser.parse(unparsed) |
| 88 | + |
| 89 | + expect(ast).to eq(reparsed) |
| 90 | + end |
| 91 | + end |
| 92 | +end |
0 commit comments