Skip to content

Commit 1707e83

Browse files
committed
Transition to rspec from TestUnit
1 parent a5db9a0 commit 1707e83

13 files changed

Lines changed: 235 additions & 252 deletions

.rspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--color
2+
--format progress

Rakefile

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
require 'rake'
2-
require 'rake/testtask'
3-
require "bundler/gem_tasks"
2+
require 'rspec/core/rake_task'
3+
require 'bundler/gem_tasks'
44

5-
Rake::TestTask.new(:test) do |t|
6-
t.libs << 'test'
7-
t.pattern = 'test/**/test_*.rb'
8-
t.verbose = true
9-
end
5+
RSpec::Core::RakeTask.new('spec')
106

117
desc 'Run tests.'
12-
task default: :test
8+
task default: :spec
139

1410
desc "Open an irb session preloaded with this library."
1511
task :console do

docx.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ Gem::Specification.new do |s|
1313

1414
s.add_dependency 'nokogiri', '~> 1.5'
1515
s.add_dependency 'rubyzip', '~> 0.9'
16+
17+
s.add_development_dependency 'rspec'
1618
end

spec/docx/document_spec.rb

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
require 'docx'
2+
require 'tempfile'
3+
4+
describe Docx::Document do
5+
before(:all) do
6+
@fixtures_path = "spec/fixtures"
7+
end
8+
9+
describe 'reading' do
10+
before do
11+
@doc = Docx::Document.open(@fixtures_path + '/basic.docx')
12+
end
13+
14+
it 'should read the document' do
15+
@doc.paragraphs.size.should eq 2
16+
@doc.paragraphs.first.text.should eq 'hello'
17+
@doc.paragraphs.last.text.should eq 'world'
18+
@doc.text.should eq "hello\nworld"
19+
end
20+
21+
it 'should read bookmarks' do
22+
@doc.bookmarks.size.should eq 1
23+
@doc.bookmarks['test_bookmark'].should_not be_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+
tr.formatting.should eq Docx::Elements::Containers::TextRun::DEFAULT_FORMATTING
37+
end
38+
end
39+
end
40+
end
41+
42+
describe 'editing' do
43+
before do
44+
@doc = Docx::Document.open(@fixtures_path + '/editing.docx')
45+
end
46+
47+
it 'should copy paragraphs' do
48+
old_p = @doc.paragraphs.first
49+
new_p = old_p.copy
50+
expect(new_p).to be_an_instance_of(Docx::Elements::Containers::Paragraph)
51+
new_p.should_not be_nil
52+
new_p.should_not eq(old_p)
53+
end
54+
55+
it 'allows insertion of text' do
56+
@doc.paragraphs.size.should eq 3
57+
first_p = @doc.paragraphs.first
58+
new_p = first_p.copy
59+
new_p.insert_after first_p
60+
@doc.paragraphs.size.should eq 4
61+
end
62+
63+
it 'should change text' do
64+
@doc.paragraphs.first.text.should eq 'test text'
65+
@doc.paragraphs.first.text = 'the real test'
66+
@doc.paragraphs.first.text.should eq 'the real test'
67+
end
68+
69+
it 'should allow insertion of text before a bookmark' do
70+
@doc.paragraphs.first.text.should eq 'test text'
71+
@doc.bookmarks['beginning_bookmark'].insert_text_before('foo')
72+
@doc.paragraphs.first.text.should eq 'footest text'
73+
end
74+
75+
it 'should allow insertion of text after a bookmark' do
76+
@doc.paragraphs.first.text.should eq 'test text'
77+
@doc.bookmarks['end_bookmark'].insert_text_after('bar')
78+
@doc.paragraphs.first.text.should eq 'test textbar'
79+
end
80+
81+
it 'should allow multiple lines of text to be inserted at a bookmark' do
82+
@doc.paragraphs.last.text.should eq ''
83+
new_lines = ['replacement test', 'second paragraph test', 'and a third paragraph test']
84+
@doc.bookmarks['isolated_bookmark'].insert_multiple_lines(new_lines)
85+
new_lines.each_index do |line|
86+
@doc.paragraphs[line + 2].text.should eq new_lines[line]
87+
end
88+
end
89+
90+
it 'should allow multi-line insertion with replacement' do
91+
@doc.paragraphs[1].text.should eq 'placeholder text'
92+
new_lines = ['replacement test', 'second paragraph test', 'and a third paragraph test']
93+
@doc.bookmarks['word_splitting_bookmark'].insert_multiple_lines(new_lines)
94+
new_lines.each_index do |line|
95+
@doc.paragraphs[line + 1].text.should eq new_lines[line]
96+
end
97+
end
98+
99+
it 'should allow content deletion' do
100+
@doc.paragraphs.first.text.should eq 'test text'
101+
@doc.paragraphs.first.blank!
102+
@doc.paragraphs.first.text.should eq ''
103+
end
104+
end
105+
106+
describe 'read formatting' do
107+
before do
108+
@doc = Docx::Document.open(@fixtures_path + '/formatting.docx')
109+
@formatting = @doc.paragraphs.map { |p| p.text_runs.map(&:formatting) }
110+
@default_formatting = Docx::Elements::Containers::TextRun::DEFAULT_FORMATTING
111+
@only_italic = @default_formatting.merge italic: true
112+
@only_bold = @default_formatting.merge bold: true
113+
@only_underline = @default_formatting.merge underline: true
114+
@all_formatted = @default_formatting.merge italic: true, bold: true, underline: true
115+
end
116+
117+
it 'should have the correct text' do
118+
@doc.paragraphs.size.should eq 6
119+
@doc.paragraphs[0].text.should eq 'Normal'
120+
@doc.paragraphs[1].text.should eq 'Italic'
121+
@doc.paragraphs[2].text.should eq 'Bold'
122+
@doc.paragraphs[3].text.should eq 'Underline'
123+
@doc.paragraphs[4].text.should eq 'Normal'
124+
@doc.paragraphs[5].text.should eq 'This is a sentence with all formatting options in the middle of the sentence.'
125+
end
126+
127+
it 'should contain a paragraph with multiple text runs' do
128+
129+
end
130+
131+
it 'should detect normal formatting' do
132+
[0, 4].each do |i|
133+
@formatting[i][0].should eq @default_formatting
134+
@doc.paragraphs[i].text_runs[0].italicized?.should be_false
135+
@doc.paragraphs[i].text_runs[0].bolded?.should be_false
136+
@doc.paragraphs[i].text_runs[0].underlined?.should be_false
137+
end
138+
end
139+
140+
it 'should detect italic formatting' do
141+
@formatting[1][0].should eq @only_italic
142+
@doc.paragraphs[1].text_runs[0].italicized?.should be_true
143+
@doc.paragraphs[1].text_runs[0].bolded?.should be_false
144+
@doc.paragraphs[1].text_runs[0].underlined?.should be_false
145+
end
146+
147+
it 'should detect bold formatting' do
148+
@formatting[2][0].should eq @only_bold
149+
@doc.paragraphs[2].text_runs[0].italicized?.should be_false
150+
@doc.paragraphs[2].text_runs[0].bolded?.should be_true
151+
@doc.paragraphs[2].text_runs[0].underlined?.should be_false
152+
end
153+
154+
it 'should detect underline formatting' do
155+
@formatting[3][0].should eq @only_underline
156+
@doc.paragraphs[3].text_runs[0].italicized?.should be_false
157+
@doc.paragraphs[3].text_runs[0].bolded?.should be_false
158+
@doc.paragraphs[3].text_runs[0].underlined?.should be_true
159+
end
160+
161+
it 'should detect mixed formatting' do
162+
@formatting[5][0].should eq @default_formatting
163+
@doc.paragraphs[5].text_runs[0].italicized?.should be_false
164+
@doc.paragraphs[5].text_runs[0].bolded?.should be_false
165+
@doc.paragraphs[5].text_runs[0].underlined?.should be_false
166+
167+
@formatting[5][1].should eq @all_formatted
168+
@doc.paragraphs[5].text_runs[1].italicized?.should be_true
169+
@doc.paragraphs[5].text_runs[1].bolded?.should be_true
170+
@doc.paragraphs[5].text_runs[1].underlined?.should be_true
171+
172+
@formatting[5][2].should eq @default_formatting
173+
@doc.paragraphs[5].text_runs[2].italicized?.should be_false
174+
@doc.paragraphs[5].text_runs[2].bolded?.should be_false
175+
@doc.paragraphs[5].text_runs[2].underlined?.should be_false
176+
end
177+
end
178+
179+
describe 'saving' do
180+
before do
181+
@doc = Docx::Document.open(@fixtures_path + '/saving.docx')
182+
end
183+
184+
it 'should save to a normal file path' do
185+
@new_doc_path = @fixtures_path + '/new_save.docx'
186+
@doc.save(@new_doc_path)
187+
@new_doc = Docx::Document.open(@new_doc_path)
188+
@new_doc.paragraphs.size.should eq @doc.paragraphs.size
189+
end
190+
191+
it 'should save to a tempfile' do
192+
temp_file = Tempfile.new(['docx_gem', '.docx'])
193+
@new_doc_path = temp_file.path
194+
@doc.save(@new_doc_path)
195+
@new_doc = Docx::Document.open(@new_doc_path)
196+
@new_doc.paragraphs.size.should eq @doc.paragraphs.size
197+
198+
temp_file.close
199+
temp_file.unlink
200+
# ensure temp file has been removed
201+
File.exists?(@new_doc_path).should be_false
202+
end
203+
204+
after do
205+
if File.exists?(@new_doc_path)
206+
File.delete(@new_doc_path)
207+
end
208+
end
209+
end
210+
end

spec/spec_helper.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# This file was generated by the `rspec --init` command. Conventionally, all
2+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3+
# Require this file using `require "spec_helper"` to ensure that it is only
4+
# loaded once.
5+
#
6+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7+
RSpec.configure do |config|
8+
config.treat_symbols_as_metadata_keys_with_true_values = true
9+
config.run_all_when_everything_filtered = true
10+
config.filter_run :focus
11+
12+
# Run specs in random order to surface order dependencies. If you find an
13+
# order dependency and want to debug it, you can fix the order by providing
14+
# the seed, which is printed after each run.
15+
# --seed 1234
16+
config.order = 'random'
17+
end

test/docx/test_document.rb

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)