Skip to content

Commit bef239d

Browse files
committed
Added specs for saving from a file opened via stream
1 parent b16fbbd commit bef239d

1 file changed

Lines changed: 37 additions & 22 deletions

File tree

spec/docx/document_spec.rb

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,32 @@
4848
end
4949
end
5050

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+
5177
describe 'reading' do
5278
context 'using normal file' do
5379
before do
@@ -328,32 +354,21 @@
328354
end
329355

330356
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
334361

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'
340363
end
341364

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
354370

355-
after do
356-
File.delete(@new_doc_path) if File.exist?(@new_doc_path)
371+
it_behaves_like 'saving'
357372
end
358373

359374
context 'wps modified docx file' do

0 commit comments

Comments
 (0)