Skip to content

Commit 4edc599

Browse files
authored
Merge pull request #90 from ruby-docx/refactoring_spec_helper
Refactoring spec helper
2 parents 33f0d14 + ff89da6 commit 4edc599

3 files changed

Lines changed: 70 additions & 68 deletions

File tree

spec/docx/document_spec.rb

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -10,73 +10,6 @@
1010
@formatting_line_count = 12 # number of lines the formatting.docx file has
1111
end
1212

13-
shared_examples_for 'reading' do
14-
it 'should read the document' do
15-
expect(@doc.paragraphs.size).to eq(2)
16-
expect(@doc.paragraphs.first.text).to eq('hello')
17-
expect(@doc.paragraphs.last.text).to eq('world')
18-
expect(@doc.text).to eq("hello\nworld")
19-
end
20-
21-
it 'should read bookmarks' do
22-
expect(@doc.bookmarks.size).to eq(1)
23-
expect(@doc.bookmarks['test_bookmark']).to_not eq(nil)
24-
end
25-
26-
it 'should have paragraphs' do
27-
@doc.each_paragraph do |p|
28-
expect(p).to be_an_instance_of(Docx::Elements::Containers::Paragraph)
29-
end
30-
end
31-
32-
it 'should have properly formatted text runs' do
33-
@doc.each_paragraph do |p|
34-
p.each_text_run do |tr|
35-
expect(tr).to be_an_instance_of(Docx::Elements::Containers::TextRun)
36-
expect(tr.formatting).to eq(Docx::Elements::Containers::TextRun::DEFAULT_FORMATTING)
37-
end
38-
end
39-
end
40-
41-
describe '#font_size' do
42-
context 'When a docx files has no styles.xml' do
43-
before do
44-
@doc = Docx::Document.new(@fixtures_path + '/no_styles.docx')
45-
end
46-
47-
it 'should raise an error' do
48-
expect(@doc.font_size).to be_nil
49-
end
50-
end
51-
end
52-
end
53-
54-
shared_examples_for 'saving to file' do
55-
it 'should save to a normal file path' do
56-
@new_doc_path = @fixtures_path + '/new_save.docx'
57-
@doc.save(@new_doc_path)
58-
@new_doc = Docx::Document.open(@new_doc_path)
59-
expect(@new_doc.paragraphs.size).to eq(@doc.paragraphs.size)
60-
end
61-
62-
it 'should save to a tempfile' do
63-
temp_file = Tempfile.new(['docx_gem', '.docx'])
64-
@new_doc_path = temp_file.path
65-
@doc.save(@new_doc_path)
66-
@new_doc = Docx::Document.open(@new_doc_path)
67-
expect(@new_doc.paragraphs.size).to eq(@doc.paragraphs.size)
68-
69-
temp_file.close
70-
temp_file.unlink
71-
# ensure temp file has been removed
72-
expect(File.exist?(@new_doc_path)).to eq(false)
73-
end
74-
75-
after do
76-
File.delete(@new_doc_path) if File.exist?(@new_doc_path)
77-
end
78-
end
79-
8013
describe '#open' do
8114
context 'When reading a file made by Office365' do
8215
it 'supports it' do

spec/spec_helper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
Coveralls.wear!
77
end
88

9+
Dir['./spec/support/**/*.rb'].sort.each { |f| require f }
10+
911
# This file was generated by the `rspec --init` command. Conventionally, all
1012
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
1113
# Require this file using `require "spec_helper"` to ensure that it is only
1214
# loaded once.
1315
#
1416
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
1517
RSpec.configure do |config|
16-
config.treat_symbols_as_metadata_keys_with_true_values = true
1718
config.run_all_when_everything_filtered = true
1819
config.filter_run :focus
1920

spec/support/shared_examples.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.shared_examples_for 'reading' do
4+
it 'should read the document' do
5+
expect(@doc.paragraphs.size).to eq(2)
6+
expect(@doc.paragraphs.first.text).to eq('hello')
7+
expect(@doc.paragraphs.last.text).to eq('world')
8+
expect(@doc.text).to eq("hello\nworld")
9+
end
10+
11+
it 'should read bookmarks' do
12+
expect(@doc.bookmarks.size).to eq(1)
13+
expect(@doc.bookmarks['test_bookmark']).to_not eq(nil)
14+
end
15+
16+
it 'should have paragraphs' do
17+
@doc.each_paragraph do |p|
18+
expect(p).to be_an_instance_of(Docx::Elements::Containers::Paragraph)
19+
end
20+
end
21+
22+
it 'should have properly formatted text runs' do
23+
@doc.each_paragraph do |p|
24+
p.each_text_run do |tr|
25+
expect(tr).to be_an_instance_of(Docx::Elements::Containers::TextRun)
26+
expect(tr.formatting).to eq(Docx::Elements::Containers::TextRun::DEFAULT_FORMATTING)
27+
end
28+
end
29+
end
30+
31+
describe '#font_size' do
32+
context 'When a docx files has no styles.xml' do
33+
before do
34+
@doc = Docx::Document.new(@fixtures_path + '/no_styles.docx')
35+
end
36+
37+
it 'should raise an error' do
38+
expect(@doc.font_size).to be_nil
39+
end
40+
end
41+
end
42+
end
43+
44+
RSpec.shared_examples_for 'saving to file' do
45+
it 'should save to a normal file path' do
46+
@new_doc_path = @fixtures_path + '/new_save.docx'
47+
@doc.save(@new_doc_path)
48+
@new_doc = Docx::Document.open(@new_doc_path)
49+
expect(@new_doc.paragraphs.size).to eq(@doc.paragraphs.size)
50+
end
51+
52+
it 'should save to a tempfile' do
53+
temp_file = Tempfile.new(['docx_gem', '.docx'])
54+
@new_doc_path = temp_file.path
55+
@doc.save(@new_doc_path)
56+
@new_doc = Docx::Document.open(@new_doc_path)
57+
expect(@new_doc.paragraphs.size).to eq(@doc.paragraphs.size)
58+
59+
temp_file.close
60+
temp_file.unlink
61+
# ensure temp file has been removed
62+
expect(File.exist?(@new_doc_path)).to eq(false)
63+
end
64+
65+
after do
66+
File.delete(@new_doc_path) if File.exist?(@new_doc_path)
67+
end
68+
end

0 commit comments

Comments
 (0)