|
48 | 48 | end |
49 | 49 | end |
50 | 50 |
|
| 51 | + shared_examples_for 'saving' 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 | + |
51 | 77 | describe 'reading' do |
52 | 78 | context 'using normal file' do |
53 | 79 | before do |
|
328 | 354 | end |
329 | 355 |
|
330 | 356 | describe 'saving' do |
331 | | - before do |
332 | | - @doc = Docx::Document.open(@fixtures_path + '/saving.docx') |
333 | | - end |
| 357 | + context 'from a normal file' do |
| 358 | + before do |
| 359 | + @doc = Docx::Document.open(@fixtures_path + '/saving.docx') |
| 360 | + end |
334 | 361 |
|
335 | | - it 'should save to a normal file path' do |
336 | | - @new_doc_path = @fixtures_path + '/new_save.docx' |
337 | | - @doc.save(@new_doc_path) |
338 | | - @new_doc = Docx::Document.open(@new_doc_path) |
339 | | - expect(@new_doc.paragraphs.size).to eq(@doc.paragraphs.size) |
| 362 | + it_behaves_like 'saving' |
340 | 363 | end |
341 | 364 |
|
342 | | - it 'should save to a tempfile' do |
343 | | - temp_file = Tempfile.new(['docx_gem', '.docx']) |
344 | | - @new_doc_path = temp_file.path |
345 | | - @doc.save(@new_doc_path) |
346 | | - @new_doc = Docx::Document.open(@new_doc_path) |
347 | | - expect(@new_doc.paragraphs.size).to eq(@doc.paragraphs.size) |
348 | | - |
349 | | - temp_file.close |
350 | | - temp_file.unlink |
351 | | - # ensure temp file has been removed |
352 | | - expect(File.exist?(@new_doc_path)).to eq(false) |
353 | | - end |
| 365 | + context 'from a stream' do |
| 366 | + before do |
| 367 | + stream = File.binread(@fixtures_path + '/saving.docx') |
| 368 | + @doc = Docx::Document.open_buffer(stream) |
| 369 | + end |
354 | 370 |
|
355 | | - after do |
356 | | - File.delete(@new_doc_path) if File.exist?(@new_doc_path) |
| 371 | + it_behaves_like 'saving' |
357 | 372 | end |
358 | 373 |
|
359 | 374 | context 'wps modified docx file' do |
|
0 commit comments