|
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 | + describe 'reading' do |
| 52 | + context 'using normal file' do |
| 53 | + before do |
| 54 | + @doc = Docx::Document.open(@fixtures_path + '/basic.docx') |
| 55 | + end |
| 56 | + |
| 57 | + it_behaves_like 'reading' |
| 58 | + end |
| 59 | + |
| 60 | + context 'using stream' do |
| 61 | + before do |
| 62 | + stream = File.binread(@fixtures_path + '/basic.docx') |
| 63 | + @doc = Docx::Document.open_buffer(stream) |
| 64 | + end |
| 65 | + |
| 66 | + it_behaves_like 'reading' |
| 67 | + end |
| 68 | + end |
| 69 | + |
56 | 70 | describe 'read tables' do |
57 | 71 | before do |
58 | 72 | @doc = Docx::Document.open(@fixtures_path + '/tables.docx') |
59 | 73 | end |
60 | 74 |
|
61 | | - it "should have tables with rows and cells" do |
| 75 | + it 'should have tables with rows and cells' do |
62 | 76 | expect(@doc.tables.count).to eq 2 |
63 | 77 | @doc.tables.each do |table| |
64 | 78 | expect(table).to be_an_instance_of(Docx::Elements::Containers::Table) |
|
71 | 85 | end |
72 | 86 | end |
73 | 87 |
|
74 | | - it "should have tables with columns and cells" do |
| 88 | + it 'should have tables with columns and cells' do |
75 | 89 | @doc.tables.each do |table| |
76 | 90 | table.columns.each do |column| |
77 | 91 | expect(column).to be_an_instance_of(Docx::Elements::Containers::TableColumn) |
|
82 | 96 | end |
83 | 97 | end |
84 | 98 |
|
85 | | - it "should have proper count" do |
| 99 | + it 'should have proper count' do |
86 | 100 | expect(@doc.tables[0].row_count).to eq 171 |
87 | 101 | expect(@doc.tables[1].row_count).to eq 2 |
88 | 102 | expect(@doc.tables[0].column_count).to eq 2 |
89 | 103 | expect(@doc.tables[1].column_count).to eq 2 |
90 | 104 | end |
91 | 105 |
|
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" |
| 106 | + it 'should have tables with proper text' do |
| 107 | + expect(@doc.tables[0].rows[0].cells[0].text).to eq 'ENGLISH' |
| 108 | + expect(@doc.tables[0].rows[0].cells[1].text).to eq 'FRANÇAIS' |
| 109 | + expect(@doc.tables[1].rows[0].cells[0].text).to eq 'Second table' |
| 110 | + expect(@doc.tables[1].rows[0].cells[1].text).to eq 'Second tableau' |
| 111 | + expect(@doc.tables[0].columns[0].cells[5].text).to eq 'aphids' |
| 112 | + expect(@doc.tables[0].columns[1].cells[5].text).to eq 'puceron' |
99 | 113 | end |
100 | 114 |
|
101 | | - it "should read embedded links" do |
| 115 | + it 'should read embedded links' do |
102 | 116 | expect(@doc.tables[0].columns[1].cells[1].text).to match(/^Directive/) |
103 | 117 | end |
104 | 118 |
|
|
109 | 123 | end |
110 | 124 | end |
111 | 125 |
|
112 | | - describe 'editing' do |
| 126 | + describe 'editing' do |
113 | 127 | before do |
114 | 128 | @doc = Docx::Document.open(@fixtures_path + '/editing.docx') |
115 | 129 | end |
|
173 | 187 | end |
174 | 188 |
|
175 | 189 | it 'should allow content deletion' do |
176 | | - expect{@doc.paragraphs.first.remove!}.to change{@doc.paragraphs.size}.by(-1) |
177 | | - |
| 190 | + expect { @doc.paragraphs.first.remove! }.to change { @doc.paragraphs.size }.by(-1) |
178 | 191 | end |
179 | 192 | end |
180 | 193 |
|
|
223 | 236 | end |
224 | 237 |
|
225 | 238 | it 'should contain a paragraph with multiple text runs' do |
226 | | - |
227 | 239 | end |
228 | 240 |
|
229 | 241 | it 'should detect normal formatting' do |
|
337 | 349 | temp_file.close |
338 | 350 | temp_file.unlink |
339 | 351 | # ensure temp file has been removed |
340 | | - expect(File.exists?(@new_doc_path)).to eq(false) |
| 352 | + expect(File.exist?(@new_doc_path)).to eq(false) |
341 | 353 | end |
342 | 354 |
|
343 | 355 | after do |
344 | | - if File.exists?(@new_doc_path) |
345 | | - File.delete(@new_doc_path) |
346 | | - end |
| 356 | + File.delete(@new_doc_path) if File.exist?(@new_doc_path) |
347 | 357 | end |
348 | 358 |
|
349 | 359 | context 'wps modified docx file' do |
|
400 | 410 | expect(@doc.paragraphs[8].to_html.scan(regex).flatten.first.split(';').include?('text-align:right')).to eq(true) |
401 | 411 | end |
402 | 412 |
|
403 | | - it "should set font size on styled paragraphs" do |
| 413 | + it 'should set font size on styled paragraphs' do |
404 | 414 | regex = /(\<p{1})[^\>]+style\=\"([^\"]+).+(<\/p>)/ |
405 | 415 | scan = @doc.paragraphs[9].to_html.scan(regex).flatten |
406 | 416 | expect(scan.first).to eq '<p' |
|
441 | 451 | it 'should output styled html' do |
442 | 452 | expect(@formatted_line.to_html.scan('<span style="text-decoration:underline;"><strong><em>all</em></strong></span>').size).to eq 1 |
443 | 453 | end |
444 | | - |
445 | 454 | end |
446 | 455 |
|
447 | 456 | describe 'replacing contents' do |
448 | 457 | 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') } |
| 458 | + let(:temp_file_path) { Tempfile.new(['docx_gem', '.docx']).path } |
| 459 | + let(:entry_path) { 'word/media/image1.png' } |
| 460 | + let(:doc) { Docx::Document.open(@fixtures_path + '/replacement.docx') } |
452 | 461 |
|
453 | 462 | it 'should replace existing file within the document' do |
454 | | - File.open replacement_file_path, "rb" do |io| |
| 463 | + File.open replacement_file_path, 'rb' do |io| |
455 | 464 | doc.replace_entry entry_path, io.read |
456 | 465 | end |
457 | 466 |
|
458 | 467 | doc.save(temp_file_path) |
459 | 468 |
|
460 | | - File.open replacement_file_path, "rb" do |io| |
461 | | - expect(Zip::File.open(temp_file_path).read entry_path).to eq io.read |
| 469 | + File.open replacement_file_path, 'rb' do |io| |
| 470 | + expect(Zip::File.open(temp_file_path).read(entry_path)).to eq io.read |
462 | 471 | end |
463 | 472 | end |
464 | 473 |
|
465 | 474 | after do |
466 | | - if File.exists?(temp_file_path) |
467 | | - File.delete(temp_file_path) |
468 | | - end |
| 475 | + File.delete(temp_file_path) if File.exist?(temp_file_path) |
469 | 476 | end |
470 | 477 | end |
471 | 478 | end |
472 | | - |
|
0 commit comments