Skip to content

Commit 59465d7

Browse files
committed
sturdier html tests
1 parent d0170aa commit 59465d7

2 files changed

Lines changed: 29 additions & 7 deletions

File tree

spec/docx/document_spec.rb

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
describe Docx::Document do
66
before(:all) do
77
@fixtures_path = "spec/fixtures"
8-
@formatting_line_count = 11 # number of lines the formatting.docx file has
8+
@formatting_line_count = 12 # number of lines the formatting.docx file has
99
end
1010

1111
describe 'reading' do
@@ -311,25 +311,29 @@
311311
before do
312312
@doc = Docx::Document.open(@fixtures_path + '/formatting.docx')
313313
@formatted_line = @doc.paragraphs[5]
314+
@p_regex = /(^\<p).+((?<=\>)\w+)(\<\/p>$)/
315+
@span_regex = /(\<span).+((?<=\>)\w+)(<\/span>)/
316+
@em_regex = /(\<em).+((?<=\>)\w+)(\<\/em\>)/
317+
@strong_regex = /(\<strong).+((?<=\>)\w+)(\<\/strong\>)/
314318
end
315319

316320
it 'should wrap pragraphs in a p tag' do
317-
scan = @doc.paragraphs[0].to_html.scan(/(^\<p).+((?<=\>)\w+)(\<\/p>$)/).flatten
321+
scan = @doc.paragraphs[0].to_html.scan(@p_regex).flatten
318322
scan.first.should eq '<p'
319323
scan.last.should eq '</p>'
320324
scan[1].should eq 'Normal'
321325
end
322326

323327
it 'should emphasize italicized text' do
324-
scan = @doc.paragraphs[1].to_html.scan(/(\<em\>)(\w+)(\<\/em\>)/).flatten
325-
scan.first.should eq '<em>'
328+
scan = @doc.paragraphs[1].to_html.scan(@em_regex).flatten
329+
scan.first.should eq '<em'
326330
scan.last.should eq '</em>'
327331
scan[1].should eq 'Italic'
328332
end
329333

330334
it 'should strong bolded text' do
331-
scan = @doc.paragraphs[2].to_html.scan(/(\<strong\>)(\w+)(\<\/strong\>)/).flatten
332-
scan.first.should eq '<strong>'
335+
scan = @doc.paragraphs[2].to_html.scan(@strong_regex).flatten
336+
scan.first.should eq '<strong'
333337
scan.last.should eq '</strong>'
334338
scan[1].should eq 'Bold'
335339
end
@@ -362,7 +366,25 @@
362366
scan[1].split(';').include?('font-size:16pt').should be_true
363367
end
364368

365-
it 'should output an entire document as html' do
369+
it 'should properly highlight different text in different places in a sentence' do
370+
paragraph = @doc.paragraphs[11]
371+
scan = paragraph.to_html.scan(@em_regex).flatten
372+
scan.first.should eq '<em'
373+
scan.last.should eq '</em>'
374+
scan[1].should eq 'sentence'
375+
scan = paragraph.to_html.scan(@strong_regex).flatten
376+
scan.first.should eq '<strong'
377+
scan.last.should eq '</strong>'
378+
scan[1].should eq 'formatting'
379+
scan = paragraph.to_html.scan(@span_regex).flatten
380+
scan.first.should eq '<span'
381+
scan.last.should eq '</span>'
382+
scan[1].should eq 'different'
383+
scan = paragraph.to_html.scan(/\<span\s+([^\>]+)/).flatten
384+
scan.first.should eq 'style="text-decoration:underline;"'
385+
end
386+
387+
it 'should output an entire document as html fragment' do
366388
@doc.to_html.scan(/(\<p)/).flatten.size.should eq @formatting_line_count
367389
end
368390

spec/fixtures/formatting.docx

1.92 KB
Binary file not shown.

0 commit comments

Comments
 (0)