|
1 | | -# coding: utf-8 |
2 | 1 | require 'docx' |
3 | 2 | require 'tempfile' |
4 | 3 |
|
5 | 4 | describe Docx::Document do |
6 | 5 | before(:all) do |
7 | | - @fixtures_path = "spec/fixtures" |
| 6 | + @fixtures_path = 'spec/fixtures' |
8 | 7 | @formatting_line_count = 12 # number of lines the formatting.docx file has |
9 | 8 | end |
10 | 9 |
|
11 | | - describe 'reading' do |
12 | | - before do |
13 | | - @doc = Docx::Document.open(@fixtures_path + '/basic.docx') |
14 | | - end |
15 | | - |
| 10 | + shared_examples_for 'reading' do |
16 | 11 | it 'should read the document' do |
17 | 12 | expect(@doc.paragraphs.size).to eq(2) |
18 | 13 | expect(@doc.paragraphs.first.text).to eq('hello') |
|
53 | 48 | end |
54 | 49 | end |
55 | 50 |
|
| 51 | + shared_examples_for 'saving to file' do |
| 52 | + it 'should save to a normal file path' do |
| 53 | + @new_doc_path = @fixtures_path + '/new_save.docx' |
| 54 | + @doc.save(@new_doc_path) |
| 55 | + @new_doc = Docx::Document.open(@new_doc_path) |
| 56 | + expect(@new_doc.paragraphs.size).to eq(@doc.paragraphs.size) |
| 57 | + end |
| 58 | + |
| 59 | + it 'should save to a tempfile' do |
| 60 | + temp_file = Tempfile.new(['docx_gem', '.docx']) |
| 61 | + @new_doc_path = temp_file.path |
| 62 | + @doc.save(@new_doc_path) |
| 63 | + @new_doc = Docx::Document.open(@new_doc_path) |
| 64 | + expect(@new_doc.paragraphs.size).to eq(@doc.paragraphs.size) |
| 65 | + |
| 66 | + temp_file.close |
| 67 | + temp_file.unlink |
| 68 | + # ensure temp file has been removed |
| 69 | + expect(File.exist?(@new_doc_path)).to eq(false) |
| 70 | + end |
| 71 | + |
| 72 | + after do |
| 73 | + File.delete(@new_doc_path) if File.exist?(@new_doc_path) |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + describe 'reading' do |
| 78 | + context 'using normal file' do |
| 79 | + before do |
| 80 | + @doc = Docx::Document.open(@fixtures_path + '/basic.docx') |
| 81 | + end |
| 82 | + |
| 83 | + it_behaves_like 'reading' |
| 84 | + end |
| 85 | + |
| 86 | + context 'using stream' do |
| 87 | + before do |
| 88 | + stream = File.binread(@fixtures_path + '/basic.docx') |
| 89 | + @doc = Docx::Document.open(stream) |
| 90 | + end |
| 91 | + |
| 92 | + it_behaves_like 'reading' |
| 93 | + end |
| 94 | + end |
| 95 | + |
56 | 96 | describe 'read tables' do |
57 | 97 | before do |
58 | 98 | @doc = Docx::Document.open(@fixtures_path + '/tables.docx') |
59 | 99 | end |
60 | 100 |
|
61 | | - it "should have tables with rows and cells" do |
| 101 | + it 'should have tables with rows and cells' do |
62 | 102 | expect(@doc.tables.count).to eq 2 |
63 | 103 | @doc.tables.each do |table| |
64 | 104 | expect(table).to be_an_instance_of(Docx::Elements::Containers::Table) |
|
71 | 111 | end |
72 | 112 | end |
73 | 113 |
|
74 | | - it "should have tables with columns and cells" do |
| 114 | + it 'should have tables with columns and cells' do |
75 | 115 | @doc.tables.each do |table| |
76 | 116 | table.columns.each do |column| |
77 | 117 | expect(column).to be_an_instance_of(Docx::Elements::Containers::TableColumn) |
|
82 | 122 | end |
83 | 123 | end |
84 | 124 |
|
85 | | - it "should have proper count" do |
| 125 | + it 'should have proper count' do |
86 | 126 | expect(@doc.tables[0].row_count).to eq 171 |
87 | 127 | expect(@doc.tables[1].row_count).to eq 2 |
88 | 128 | expect(@doc.tables[0].column_count).to eq 2 |
89 | 129 | expect(@doc.tables[1].column_count).to eq 2 |
90 | 130 | end |
91 | 131 |
|
92 | | - it "should have tables with proper text" do |
93 | | - expect(@doc.tables[0].rows[0].cells[0].text).to eq "ENGLISH" |
94 | | - expect(@doc.tables[0].rows[0].cells[1].text).to eq "FRANÇAIS" |
95 | | - expect(@doc.tables[1].rows[0].cells[0].text).to eq "Second table" |
96 | | - expect(@doc.tables[1].rows[0].cells[1].text).to eq "Second tableau" |
97 | | - expect(@doc.tables[0].columns[0].cells[5].text).to eq "aphids" |
98 | | - expect(@doc.tables[0].columns[1].cells[5].text).to eq "puceron" |
| 132 | + it 'should have tables with proper text' do |
| 133 | + expect(@doc.tables[0].rows[0].cells[0].text).to eq 'ENGLISH' |
| 134 | + expect(@doc.tables[0].rows[0].cells[1].text).to eq 'FRANÇAIS' |
| 135 | + expect(@doc.tables[1].rows[0].cells[0].text).to eq 'Second table' |
| 136 | + expect(@doc.tables[1].rows[0].cells[1].text).to eq 'Second tableau' |
| 137 | + expect(@doc.tables[0].columns[0].cells[5].text).to eq 'aphids' |
| 138 | + expect(@doc.tables[0].columns[1].cells[5].text).to eq 'puceron' |
99 | 139 | end |
100 | 140 |
|
101 | | - it "should read embedded links" do |
| 141 | + it 'should read embedded links' do |
102 | 142 | expect(@doc.tables[0].columns[1].cells[1].text).to match(/^Directive/) |
103 | 143 | end |
104 | 144 |
|
|
109 | 149 | end |
110 | 150 | end |
111 | 151 |
|
112 | | - describe 'editing' do |
| 152 | + describe 'editing' do |
113 | 153 | before do |
114 | 154 | @doc = Docx::Document.open(@fixtures_path + '/editing.docx') |
115 | 155 | end |
|
173 | 213 | end |
174 | 214 |
|
175 | 215 | it 'should allow content deletion' do |
176 | | - expect{@doc.paragraphs.first.remove!}.to change{@doc.paragraphs.size}.by(-1) |
177 | | - |
| 216 | + expect { @doc.paragraphs.first.remove! }.to change { @doc.paragraphs.size }.by(-1) |
178 | 217 | end |
179 | 218 | end |
180 | 219 |
|
|
223 | 262 | end |
224 | 263 |
|
225 | 264 | it 'should contain a paragraph with multiple text runs' do |
226 | | - |
227 | 265 | end |
228 | 266 |
|
229 | 267 | it 'should detect normal formatting' do |
|
316 | 354 | end |
317 | 355 |
|
318 | 356 | describe 'saving' do |
319 | | - before do |
320 | | - @doc = Docx::Document.open(@fixtures_path + '/saving.docx') |
321 | | - end |
322 | | - |
323 | | - it 'should save to a normal file path' do |
324 | | - @new_doc_path = @fixtures_path + '/new_save.docx' |
325 | | - @doc.save(@new_doc_path) |
326 | | - @new_doc = Docx::Document.open(@new_doc_path) |
327 | | - expect(@new_doc.paragraphs.size).to eq(@doc.paragraphs.size) |
328 | | - end |
329 | | - |
330 | | - it 'should save to a tempfile' do |
331 | | - temp_file = Tempfile.new(['docx_gem', '.docx']) |
332 | | - @new_doc_path = temp_file.path |
333 | | - @doc.save(@new_doc_path) |
334 | | - @new_doc = Docx::Document.open(@new_doc_path) |
335 | | - expect(@new_doc.paragraphs.size).to eq(@doc.paragraphs.size) |
| 357 | + context 'from a normal file' do |
| 358 | + before do |
| 359 | + @doc = Docx::Document.open(@fixtures_path + '/saving.docx') |
| 360 | + end |
336 | 361 |
|
337 | | - temp_file.close |
338 | | - temp_file.unlink |
339 | | - # ensure temp file has been removed |
340 | | - expect(File.exists?(@new_doc_path)).to eq(false) |
| 362 | + it_behaves_like 'saving to file' |
341 | 363 | end |
342 | 364 |
|
343 | | - after do |
344 | | - if File.exists?(@new_doc_path) |
345 | | - File.delete(@new_doc_path) |
| 365 | + context 'from a stream' do |
| 366 | + before do |
| 367 | + stream = File.binread(@fixtures_path + '/saving.docx') |
| 368 | + @doc = Docx::Document.open(stream) |
346 | 369 | end |
| 370 | + |
| 371 | + it_behaves_like 'saving to file' |
347 | 372 | end |
348 | 373 |
|
349 | 374 | context 'wps modified docx file' do |
|
357 | 382 | end |
358 | 383 | end |
359 | 384 |
|
| 385 | + describe 'streaming' do |
| 386 | + it 'should return a StringIO to send over HTTP' do |
| 387 | + doc = Docx::Document.open(@fixtures_path + '/basic.docx') |
| 388 | + expect(doc.stream).to be_a(StringIO) |
| 389 | + end |
| 390 | + |
| 391 | + context 'should return a valid docx stream' do |
| 392 | + before do |
| 393 | + doc = Docx::Document.open(@fixtures_path + '/basic.docx') |
| 394 | + result = doc.stream |
| 395 | + |
| 396 | + @doc = Docx::Document.open(result) |
| 397 | + end |
| 398 | + |
| 399 | + it_behaves_like 'reading' |
| 400 | + end |
| 401 | + end |
| 402 | + |
360 | 403 | describe 'outputting html' do |
361 | 404 | before do |
362 | 405 | @doc = Docx::Document.open(@fixtures_path + '/formatting.docx') |
|
400 | 443 | expect(@doc.paragraphs[8].to_html.scan(regex).flatten.first.split(';').include?('text-align:right')).to eq(true) |
401 | 444 | end |
402 | 445 |
|
403 | | - it "should set font size on styled paragraphs" do |
| 446 | + it 'should set font size on styled paragraphs' do |
404 | 447 | regex = /(\<p{1})[^\>]+style\=\"([^\"]+).+(<\/p>)/ |
405 | 448 | scan = @doc.paragraphs[9].to_html.scan(regex).flatten |
406 | 449 | expect(scan.first).to eq '<p' |
|
441 | 484 | it 'should output styled html' do |
442 | 485 | expect(@formatted_line.to_html.scan('<span style="text-decoration:underline;"><strong><em>all</em></strong></span>').size).to eq 1 |
443 | 486 | end |
444 | | - |
445 | 487 | end |
446 | 488 |
|
447 | 489 | describe 'replacing contents' do |
448 | 490 | let(:replacement_file_path) { @fixtures_path + '/replacement.png' } |
449 | | - let(:temp_file_path){ Tempfile.new(['docx_gem', '.docx']).path } |
450 | | - let(:entry_path){ 'word/media/image1.png' } |
451 | | - let(:doc){ Docx::Document.open(@fixtures_path + '/replacement.docx') } |
| 491 | + let(:temp_file_path) { Tempfile.new(['docx_gem', '.docx']).path } |
| 492 | + let(:entry_path) { 'word/media/image1.png' } |
| 493 | + let(:doc) { Docx::Document.open(@fixtures_path + '/replacement.docx') } |
452 | 494 |
|
453 | 495 | it 'should replace existing file within the document' do |
454 | | - File.open replacement_file_path, "rb" do |io| |
| 496 | + File.open replacement_file_path, 'rb' do |io| |
455 | 497 | doc.replace_entry entry_path, io.read |
456 | 498 | end |
457 | 499 |
|
458 | 500 | doc.save(temp_file_path) |
459 | 501 |
|
460 | | - File.open replacement_file_path, "rb" do |io| |
461 | | - expect(Zip::File.open(temp_file_path).read entry_path).to eq io.read |
| 502 | + File.open replacement_file_path, 'rb' do |io| |
| 503 | + expect(Zip::File.open(temp_file_path).read(entry_path)).to eq io.read |
462 | 504 | end |
463 | 505 | end |
464 | 506 |
|
465 | 507 | after do |
466 | | - if File.exists?(temp_file_path) |
467 | | - File.delete(temp_file_path) |
468 | | - end |
| 508 | + File.delete(temp_file_path) if File.exist?(temp_file_path) |
469 | 509 | end |
470 | 510 | end |
471 | 511 | end |
472 | | - |
|
0 commit comments