Skip to content

Commit 815d91b

Browse files
satoryuclaude
andcommitted
Cover the empty-match guard in Paragraph#substitute
A mutation check showed no spec failed when the guard against empty matches was removed, although without it a pattern like /x*/ loops forever. Pin the existing behavior, under a timeout so a regression fails instead of hanging. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 81ab611 commit 815d91b

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

‎spec/docx/containers/paragraph_spec.rb‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require 'spec_helper'
44
require 'docx'
5+
require 'timeout'
56

67
describe Docx::Elements::Containers::Paragraph do
78
def paragraph(xml, document_properties = {}, document = nil)
@@ -142,6 +143,19 @@ def paragraph(xml, document_properties = {}, document = nil)
142143
expect(p.text).to eq('{{a}}{{a}}')
143144
end
144145

146+
# Without the guard against empty matches this would loop forever, so it
147+
# runs under a timeout to fail instead of hanging.
148+
it 'ignores a pattern that only matches the empty string' do
149+
p = paragraph('<w:p><w:r><w:t>abc</w:t></w:r></w:p>')
150+
151+
Timeout.timeout(5) do
152+
p.substitute(/x*/, '-')
153+
p.substitute(/x*/, '')
154+
end
155+
156+
expect(p.text).to eq('abc')
157+
end
158+
145159
it 'leaves the paragraph untouched when nothing matches' do
146160
expect(split.substitute('{{missing}}', 'x').text).to eq('Hello {{first_name}}!')
147161
end

0 commit comments

Comments
 (0)