docx_generator 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +18 -18
- data/.rspec +1 -0
- data/Gemfile +6 -4
- data/Guardfile +11 -11
- data/LICENSE.txt +22 -22
- data/README.md +41 -41
- data/Rakefile +7 -7
- data/docx_generator.gemspec +36 -36
- data/examples/basic_document_with_blocks.rb +40 -36
- data/examples/basic_document_without_blocks.rb +9 -9
- data/generators.thor +10 -0
- data/generators/word_base.rb +106 -0
- data/lib/docx_generator.rb +9 -9
- data/lib/docx_generator/dsl.rb +9 -9
- data/lib/docx_generator/dsl/document.rb +84 -75
- data/lib/docx_generator/dsl/paragraph.rb +125 -72
- data/lib/docx_generator/dsl/text.rb +121 -71
- data/lib/docx_generator/element.rb +52 -52
- data/lib/docx_generator/version.rb +4 -4
- data/lib/docx_generator/word.rb +8 -8
- data/lib/docx_generator/word/base.rb +83 -82
- data/lib/docx_generator/word/extensions.rb +30 -18
- data/lib/docx_generator/word/formatting.rb +162 -58
- data/spec/docx_generator/dsl/document_spec.rb +89 -89
- data/spec/docx_generator/dsl/paragraph_spec.rb +107 -71
- data/spec/docx_generator/dsl/text_spec.rb +91 -65
- data/spec/docx_generator/element_spec.rb +44 -44
- data/spec/docx_generator/word/base_spec.rb +57 -57
- data/spec/docx_generator/word/extensions_spec.rb +15 -15
- data/spec/docx_generator/word/formatting_spec.rb +89 -41
- data/spec/spec_helper.rb +7 -7
- metadata +29 -55
- data/.rvmrc +0 -60
@@ -1,89 +1,89 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe DocxGenerator::DSL::Document do
|
4
|
-
after do
|
5
|
-
File.delete("word.docx") if File.exists?("word.docx")
|
6
|
-
end
|
7
|
-
|
8
|
-
it "should create a new docx document with the filename specified" do
|
9
|
-
document = DocxGenerator::DSL::Document.new("word")
|
10
|
-
document.filename.should eq("word.docx")
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should pass itself to a block if a block is given" do
|
14
|
-
inner_doc = nil
|
15
|
-
document = DocxGenerator::DSL::Document.new("word") do |doc|
|
16
|
-
inner_doc = doc
|
17
|
-
end
|
18
|
-
inner_doc.should be(document)
|
19
|
-
end
|
20
|
-
|
21
|
-
describe "#save" do
|
22
|
-
let(:document) { DocxGenerator::DSL::Document.new("word") }
|
23
|
-
|
24
|
-
it "should save the document" do
|
25
|
-
document.save
|
26
|
-
File.exists?("word.docx").should be_true
|
27
|
-
end
|
28
|
-
|
29
|
-
describe "required documents" do
|
30
|
-
before { DocxGenerator::DSL::Document.new("word").save }
|
31
|
-
|
32
|
-
it "should generate a [Content_Types].xml file" do
|
33
|
-
Zip::
|
34
|
-
expect { docx.read("[Content_Types].xml") }.to_not raise_error
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
it "should generate a _rels/.rels file" do
|
39
|
-
Zip::
|
40
|
-
expect { docx.read("_rels/.rels") }.to_not raise_error
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
it "should generate a word/document.xml" do
|
45
|
-
Zip::
|
46
|
-
expect { docx.read("word/document.xml") }.to_not raise_error
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
describe "#paragraph" do
|
53
|
-
it "should pass a DocxGenerator::DSL::Paragraph object to a block" do
|
54
|
-
document = DocxGenerator::DSL::Document.new("word") do |doc|
|
55
|
-
par_class = nil
|
56
|
-
doc.paragraph { |par| par_class = par.class }
|
57
|
-
par_class.should be(DocxGenerator::DSL::Paragraph)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
it "should add a paragraph to the document" do
|
62
|
-
document = DocxGenerator::DSL::Document.new("word") do |doc|
|
63
|
-
doc.paragraph { |par| }
|
64
|
-
doc.save
|
65
|
-
end
|
66
|
-
open_file("word/document.xml").should include("<w:p")
|
67
|
-
end
|
68
|
-
|
69
|
-
it "should allow options as optional arguments" do
|
70
|
-
document = DocxGenerator::DSL::Document.new("word") do |doc|
|
71
|
-
doc.paragraph(alignment: :center) { |par| }
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
describe "#add" do
|
77
|
-
it "should add the object(s) to the document" do
|
78
|
-
doc = DocxGenerator::DSL::Document.new("word")
|
79
|
-
doc.add DocxGenerator::DSL::Paragraph.new
|
80
|
-
doc.save
|
81
|
-
open_file("word/document.xml").should include("<w:p />")
|
82
|
-
end
|
83
|
-
|
84
|
-
it "should return the document object" do
|
85
|
-
document = DocxGenerator::DSL::Document.new("word")
|
86
|
-
document.add(DocxGenerator::DSL::Paragraph.new, DocxGenerator::DSL::Paragraph.new).should be(document)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DocxGenerator::DSL::Document do
|
4
|
+
after do
|
5
|
+
File.delete("word.docx") if File.exists?("word.docx")
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should create a new docx document with the filename specified" do
|
9
|
+
document = DocxGenerator::DSL::Document.new("word")
|
10
|
+
document.filename.should eq("word.docx")
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should pass itself to a block if a block is given" do
|
14
|
+
inner_doc = nil
|
15
|
+
document = DocxGenerator::DSL::Document.new("word") do |doc|
|
16
|
+
inner_doc = doc
|
17
|
+
end
|
18
|
+
inner_doc.should be(document)
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#save" do
|
22
|
+
let(:document) { DocxGenerator::DSL::Document.new("word") }
|
23
|
+
|
24
|
+
it "should save the document" do
|
25
|
+
document.save
|
26
|
+
File.exists?("word.docx").should be_true
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "required documents" do
|
30
|
+
before { DocxGenerator::DSL::Document.new("word").save }
|
31
|
+
|
32
|
+
it "should generate a [Content_Types].xml file" do
|
33
|
+
Zip::File.open("word.docx") do |docx|
|
34
|
+
expect { docx.read("[Content_Types].xml") }.to_not raise_error
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should generate a _rels/.rels file" do
|
39
|
+
Zip::File.open("word.docx") do |docx|
|
40
|
+
expect { docx.read("_rels/.rels") }.to_not raise_error
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should generate a word/document.xml" do
|
45
|
+
Zip::File.open("word.docx") do |docx|
|
46
|
+
expect { docx.read("word/document.xml") }.to_not raise_error
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "#paragraph" do
|
53
|
+
it "should pass a DocxGenerator::DSL::Paragraph object to a block" do
|
54
|
+
document = DocxGenerator::DSL::Document.new("word") do |doc|
|
55
|
+
par_class = nil
|
56
|
+
doc.paragraph { |par| par_class = par.class }
|
57
|
+
par_class.should be(DocxGenerator::DSL::Paragraph)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should add a paragraph to the document" do
|
62
|
+
document = DocxGenerator::DSL::Document.new("word") do |doc|
|
63
|
+
doc.paragraph { |par| }
|
64
|
+
doc.save
|
65
|
+
end
|
66
|
+
open_file("word/document.xml").should include("<w:p")
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should allow options as optional arguments" do
|
70
|
+
document = DocxGenerator::DSL::Document.new("word") do |doc|
|
71
|
+
doc.paragraph(alignment: :center) { |par| }
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe "#add" do
|
77
|
+
it "should add the object(s) to the document" do
|
78
|
+
doc = DocxGenerator::DSL::Document.new("word")
|
79
|
+
doc.add DocxGenerator::DSL::Paragraph.new
|
80
|
+
doc.save
|
81
|
+
open_file("word/document.xml").should include("<w:p />")
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should return the document object" do
|
85
|
+
document = DocxGenerator::DSL::Document.new("word")
|
86
|
+
document.add(DocxGenerator::DSL::Paragraph.new, DocxGenerator::DSL::Paragraph.new).should be(document)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -1,71 +1,107 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe DocxGenerator::DSL::Paragraph do
|
4
|
-
describe "#new" do
|
5
|
-
context "with a block" do
|
6
|
-
it "should pass itself to a block" do
|
7
|
-
inner_par = nil
|
8
|
-
paragraph = DocxGenerator::DSL::Paragraph.new do |par|
|
9
|
-
inner_par = par
|
10
|
-
end
|
11
|
-
inner_par.should be(paragraph)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
describe "#text" do
|
17
|
-
context "with a block" do
|
18
|
-
it "should pass a DocxGenerator::DSL::Text object to a block" do
|
19
|
-
paragraph = DocxGenerator::DSL::Paragraph.new do |par|
|
20
|
-
text_class = nil
|
21
|
-
par.text("") { |t| text_class = t.class }
|
22
|
-
text_class.should be(DocxGenerator::DSL::Text)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
it "should allow options as optional arguments" do
|
28
|
-
paragraph = DocxGenerator::DSL::Paragraph.new do |par|
|
29
|
-
par.text "Title", underline: { style: "double" }, size: 20
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
describe "#generate" do
|
35
|
-
it "should return a new paragraph with rich text fragments in it separated by a space" do
|
36
|
-
paragraph = DocxGenerator::DSL::Paragraph.new.add(DocxGenerator::DSL::Text.new("The first characters"), DocxGenerator::DSL::Text.new("and the last ones."))
|
37
|
-
paragraph.generate.to_s.should eq("<w:p><w:r><w:t>The first characters</w:t></w:r><w:r><w:t xml:space=\"preserve\"> </w:t></w:r><w:r><w:t>and the last ones.</w:t></w:r></w:p>")
|
38
|
-
end
|
39
|
-
|
40
|
-
it "should not add a space when it encounters a NoSpace object" do
|
41
|
-
paragraph = DocxGenerator::DSL::Paragraph.new.add(DocxGenerator::DSL::Text.new("CO"), DocxGenerator::Word::Extensions::NoSpace.new, DocxGenerator::DSL::Text.new("2"))
|
42
|
-
paragraph.generate.to_s.should eq("<w:p><w:r><w:t>CO</w:t></w:r><w:r><w:t>2</w:t></w:r></w:p>")
|
43
|
-
|
44
|
-
paragraph = DocxGenerator::DSL::Paragraph.new do |p|
|
45
|
-
p.text "CO"
|
46
|
-
p.no_space
|
47
|
-
p.text "2"
|
48
|
-
end
|
49
|
-
paragraph.generate.to_s.should eq("<w:p><w:r><w:t>CO</w:t></w:r><w:r><w:t>2</w:t></w:r></w:p>")
|
50
|
-
end
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DocxGenerator::DSL::Paragraph do
|
4
|
+
describe "#new" do
|
5
|
+
context "with a block" do
|
6
|
+
it "should pass itself to a block" do
|
7
|
+
inner_par = nil
|
8
|
+
paragraph = DocxGenerator::DSL::Paragraph.new do |par|
|
9
|
+
inner_par = par
|
10
|
+
end
|
11
|
+
inner_par.should be(paragraph)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#text" do
|
17
|
+
context "with a block" do
|
18
|
+
it "should pass a DocxGenerator::DSL::Text object to a block" do
|
19
|
+
paragraph = DocxGenerator::DSL::Paragraph.new do |par|
|
20
|
+
text_class = nil
|
21
|
+
par.text("") { |t| text_class = t.class }
|
22
|
+
text_class.should be(DocxGenerator::DSL::Text)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should allow options as optional arguments" do
|
28
|
+
paragraph = DocxGenerator::DSL::Paragraph.new do |par|
|
29
|
+
par.text "Title", underline: { style: "double" }, size: 20
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#generate" do
|
35
|
+
it "should return a new paragraph with rich text fragments in it separated by a space" do
|
36
|
+
paragraph = DocxGenerator::DSL::Paragraph.new.add(DocxGenerator::DSL::Text.new("The first characters"), DocxGenerator::DSL::Text.new("and the last ones."))
|
37
|
+
paragraph.generate.to_s.should eq("<w:p><w:r><w:t>The first characters</w:t></w:r><w:r><w:t xml:space=\"preserve\"> </w:t></w:r><w:r><w:t>and the last ones.</w:t></w:r></w:p>")
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should not add a space when it encounters a NoSpace object" do
|
41
|
+
paragraph = DocxGenerator::DSL::Paragraph.new.add(DocxGenerator::DSL::Text.new("CO"), DocxGenerator::Word::Extensions::NoSpace.new, DocxGenerator::DSL::Text.new("2"))
|
42
|
+
paragraph.generate.to_s.should eq("<w:p><w:r><w:t>CO</w:t></w:r><w:r><w:t>2</w:t></w:r></w:p>")
|
43
|
+
|
44
|
+
paragraph = DocxGenerator::DSL::Paragraph.new do |p|
|
45
|
+
p.text "CO"
|
46
|
+
p.no_space
|
47
|
+
p.text "2"
|
48
|
+
end
|
49
|
+
paragraph.generate.to_s.should eq("<w:p><w:r><w:t>CO</w:t></w:r><w:r><w:t>2</w:t></w:r></w:p>")
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should add a newline when it encouters a Newline object" do
|
53
|
+
paragraph = DocxGenerator::DSL::Paragraph.new.add(DocxGenerator::DSL::Text.new("CO"), DocxGenerator::Word::Extensions::Newline.new, DocxGenerator::DSL::Text.new("2"))
|
54
|
+
paragraph.generate.to_s.should eq("<w:p><w:r><w:t>CO</w:t></w:r><w:r><w:br /></w:r><w:r><w:t>2</w:t></w:r></w:p>")
|
55
|
+
|
56
|
+
paragraph = DocxGenerator::DSL::Paragraph.new do |p|
|
57
|
+
p.text "CO"
|
58
|
+
p.newline
|
59
|
+
p.text "2"
|
60
|
+
end
|
61
|
+
paragraph.generate.to_s.should eq("<w:p><w:r><w:t>CO</w:t></w:r><w:r><w:br /></w:r><w:r><w:t>2</w:t></w:r></w:p>")
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should add a tab when it encouters a Tab object" do
|
65
|
+
raise
|
66
|
+
end
|
67
|
+
|
68
|
+
context "with styles" do
|
69
|
+
it "should align the paragraph" do
|
70
|
+
DocxGenerator::DSL::Paragraph.new(alignment: "center").add(DocxGenerator::DSL::Text.new("The first characters"), DocxGenerator::DSL::Text.new("and the last ones.")).generate.to_s.should eq("<w:p><w:pPr><w:jc w:val=\"center\" /></w:pPr><w:r><w:t>The first characters</w:t></w:r><w:r><w:t xml:space=\"preserve\"> </w:t></w:r><w:r><w:t>and the last ones.</w:t></w:r></w:p>")
|
71
|
+
(DocxGenerator::DSL::Paragraph.new { |p| p.alignment "center" }).add(DocxGenerator::DSL::Text.new("The first characters"), DocxGenerator::DSL::Text.new("and the last ones.")).generate.to_s.should eq("<w:p><w:pPr><w:jc w:val=\"center\" /></w:pPr><w:r><w:t>The first characters</w:t></w:r><w:r><w:t xml:space=\"preserve\"> </w:t></w:r><w:r><w:t>and the last ones.</w:t></w:r></w:p>")
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should adjust the spacing between lines in the paragraph and between paragraphs" do
|
75
|
+
DocxGenerator::DSL::Paragraph.new(spacing: { before: 12, after: 12, lines: 1.15 }).add(DocxGenerator::DSL::Text.new("The first characters"), DocxGenerator::DSL::Text.new("and the last ones.")).generate.to_s.should eq("<w:p><w:pPr><w:spacing w:before=\"240\" w:after=\"240\" w:lines=\"276\" w:lineRule=\"auto\" /></w:pPr><w:r><w:t>The first characters</w:t></w:r><w:r><w:t xml:space=\"preserve\"> </w:t></w:r><w:r><w:t>and the last ones.</w:t></w:r></w:p>")
|
76
|
+
(DocxGenerator::DSL::Paragraph.new { |p| p.spacing before: 12, after: 12, lines: 1.15 }).add(DocxGenerator::DSL::Text.new("The first characters"), DocxGenerator::DSL::Text.new("and the last ones.")).generate.to_s.should eq("<w:p><w:pPr><w:spacing w:before=\"240\" w:after=\"240\" w:lines=\"276\" w:lineRule=\"auto\" /></w:pPr><w:r><w:t>The first characters</w:t></w:r><w:r><w:t xml:space=\"preserve\"> </w:t></w:r><w:r><w:t>and the last ones.</w:t></w:r></w:p>")
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should indent the paragraph" do
|
80
|
+
DocxGenerator::DSL::Paragraph.new(indentation: { start: 20, end: 20, first_line: 20 }).add(DocxGenerator::DSL::Text.new("The first characters"), DocxGenerator::DSL::Text.new("and the last ones.")).generate.to_s.should eq("<w:p><w:pPr><w:ind w:start=\"400\" w:end=\"400\" w:firstLine=\"400\" /></w:pPr><w:r><w:t>The first characters</w:t></w:r><w:r><w:t xml:space=\"preserve\"> </w:t></w:r><w:r><w:t>and the last ones.</w:t></w:r></w:p>")
|
81
|
+
(DocxGenerator::DSL::Paragraph.new { |p| p.indentation start: 20, end: 20, first_line: 20 }).add(DocxGenerator::DSL::Text.new("The first characters"), DocxGenerator::DSL::Text.new("and the last ones.")).generate.to_s.should eq("<w:p><w:pPr><w:ind w:start=\"400\" w:end=\"400\" w:firstLine=\"400\" /></w:pPr><w:r><w:t>The first characters</w:t></w:r><w:r><w:t xml:space=\"preserve\"> </w:t></w:r><w:r><w:t>and the last ones.</w:t></w:r></w:p>")
|
82
|
+
|
83
|
+
DocxGenerator::DSL::Paragraph.new(indentation: { start: 20, end: 20, hanging: 20 }).add(DocxGenerator::DSL::Text.new("The first characters"), DocxGenerator::DSL::Text.new("and the last ones.")).generate.to_s.should eq("<w:p><w:pPr><w:ind w:start=\"400\" w:end=\"400\" w:hanging=\"400\" /></w:pPr><w:r><w:t>The first characters</w:t></w:r><w:r><w:t xml:space=\"preserve\"> </w:t></w:r><w:r><w:t>and the last ones.</w:t></w:r></w:p>")
|
84
|
+
(DocxGenerator::DSL::Paragraph.new { |p| p.indentation start: 20, end: 20, hanging: 20 }).add(DocxGenerator::DSL::Text.new("The first characters"), DocxGenerator::DSL::Text.new("and the last ones.")).generate.to_s.should eq("<w:p><w:pPr><w:ind w:start=\"400\" w:end=\"400\" w:hanging=\"400\" /></w:pPr><w:r><w:t>The first characters</w:t></w:r><w:r><w:t xml:space=\"preserve\"> </w:t></w:r><w:r><w:t>and the last ones.</w:t></w:r></w:p>")
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should add tabulations" do
|
88
|
+
result = "<w:p><w:pPr><w:tabs><w:tab w:leader=\"dot\" w:pos=\"1440\" w:val=\"end\" /><w:tab w:leader=\"underscore\" w:pos=\"2880\" w:val=\"start\" /></w:tabs></w:pPr><w:r><w:t>The first characters</w:t></w:r><w:r><w:t xml:space=\"preserve\"> </w:t></w:r><w:r><w:t>and the last ones.</w:t></w:r></w:p>"
|
89
|
+
text_fragments = [DocxGenerator::DSL::Text.new("The first characters"), DocxGenerator::DSL::Text.new("and the last ones.")]
|
90
|
+
DocxGenerator::DSL::Paragraph.new(tabs: [{ leader: "dot", pos: 72, val: "end" }, { leader: "underscore", pos: 144, val: "start" }]).add(*text_fragments).generate.to_s.should eq(result)
|
91
|
+
(DocxGenerator::DSL::Paragraph.new { |p| p.tabs [{ leader: "dot", pos: 72, val: "end" }, { leader: "underscore", pos: 144, val: "start" }] }).add(*text_fragments).generate.to_s.should eq(result)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "#add" do
|
97
|
+
it "should add the object(s) to the paragraph" do
|
98
|
+
paragraph = DocxGenerator::DSL::Paragraph.new.add(DocxGenerator::DSL::Text.new("The first characters"), DocxGenerator::DSL::Text.new("and the last ones."))
|
99
|
+
paragraph.generate.to_s.should eq("<w:p><w:r><w:t>The first characters</w:t></w:r><w:r><w:t xml:space=\"preserve\"> </w:t></w:r><w:r><w:t>and the last ones.</w:t></w:r></w:p>")
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should return the paragraph object" do
|
103
|
+
paragraph = DocxGenerator::DSL::Paragraph.new
|
104
|
+
paragraph.add(DocxGenerator::DSL::Text.new("The first characters"), DocxGenerator::DSL::Text.new("and the last ones.")).should be(paragraph)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -1,65 +1,91 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe DocxGenerator::DSL::Text do
|
4
|
-
describe "#new" do
|
5
|
-
it "should require some text" do
|
6
|
-
expect { DocxGenerator::DSL::Text.new }.to raise_error
|
7
|
-
end
|
8
|
-
|
9
|
-
context "with a block" do
|
10
|
-
it "should pass itself to a block" do
|
11
|
-
inner_text = nil
|
12
|
-
text = DocxGenerator::DSL::Text.new("") do |t|
|
13
|
-
inner_text = t
|
14
|
-
end
|
15
|
-
inner_text.should be(text)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
describe "#generate" do
|
21
|
-
it "should return a new Run with text in it" do
|
22
|
-
text_fragment = DocxGenerator::DSL::Text.new("Title")
|
23
|
-
text_fragment.generate.to_s.should eq("<w:r><w:t>Title</w:t></w:r>")
|
24
|
-
end
|
25
|
-
|
26
|
-
context "with styles" do
|
27
|
-
it "should return a text in bold" do
|
28
|
-
DocxGenerator::DSL::Text.new("Text", bold: true).generate.to_s.should eq("<w:r><w:rPr><w:b w:val=\"true\" /></w:rPr><w:t>Text</w:t></w:r>")
|
29
|
-
(DocxGenerator::DSL::Text.new("Text") { |t| t.bold true }).generate.to_s.should eq("<w:r><w:rPr><w:b w:val=\"true\" /></w:rPr><w:t>Text</w:t></w:r>")
|
30
|
-
end
|
31
|
-
|
32
|
-
it "should return a text in italics" do
|
33
|
-
DocxGenerator::DSL::Text.new("Text", italics: true).generate.to_s.should eq("<w:r><w:rPr><w:i w:val=\"true\" /></w:rPr><w:t>Text</w:t></w:r>")
|
34
|
-
(DocxGenerator::DSL::Text.new("Text") { |t| t.italics true }).generate.to_s.should eq("<w:r><w:rPr><w:i w:val=\"true\" /></w:rPr><w:t>Text</w:t></w:r>")
|
35
|
-
end
|
36
|
-
|
37
|
-
it "should return an underlined text" do
|
38
|
-
DocxGenerator::DSL::Text.new("Text", underline: { style: "single" }).generate.to_s.should eq("<w:r><w:rPr><w:u w:val=\"single\" /></w:rPr><w:t>Text</w:t></w:r>")
|
39
|
-
(DocxGenerator::DSL::Text.new("Text") { |t| t.underline style: "single" }).generate.to_s.should eq("<w:r><w:rPr><w:u w:val=\"single\" /></w:rPr><w:t>Text</w:t></w:r>")
|
40
|
-
end
|
41
|
-
|
42
|
-
it "should return a text with a font size" do
|
43
|
-
DocxGenerator::DSL::Text.new("Text", size: 20).generate.to_s.should eq("<w:r><w:rPr><w:sz w:val=\"40\" /></w:rPr><w:t>Text</w:t></w:r>")
|
44
|
-
(DocxGenerator::DSL::Text.new("Text") { |t| t.size 20 }).generate.to_s.should eq("<w:r><w:rPr><w:sz w:val=\"40\" /></w:rPr><w:t>Text</w:t></w:r>")
|
45
|
-
end
|
46
|
-
|
47
|
-
it "should render a text in superscript" do
|
48
|
-
DocxGenerator::DSL::Text.new("Text", superscript: true).generate.to_s.should eq("<w:r><w:rPr><w:vertAlign w:val=\"superscript\" /></w:rPr><w:t>Text</w:t></w:r>")
|
49
|
-
(DocxGenerator::DSL::Text.new("Text") { |t| t.superscript true }).generate.to_s.should eq("<w:r><w:rPr><w:vertAlign w:val=\"superscript\" /></w:rPr><w:t>Text</w:t></w:r>")
|
50
|
-
end
|
51
|
-
|
52
|
-
it "should render a text in subscript" do
|
53
|
-
DocxGenerator::DSL::Text.new("Text", subscript: true).generate.to_s.should eq("<w:r><w:rPr><w:vertAlign w:val=\"subscript\" /></w:rPr><w:t>Text</w:t></w:r>")
|
54
|
-
(DocxGenerator::DSL::Text.new("Text") { |t| t.subscript true }).generate.to_s.should eq("<w:r><w:rPr><w:vertAlign w:val=\"subscript\" /></w:rPr><w:t>Text</w:t></w:r>")
|
55
|
-
end
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
end
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DocxGenerator::DSL::Text do
|
4
|
+
describe "#new" do
|
5
|
+
it "should require some text" do
|
6
|
+
expect { DocxGenerator::DSL::Text.new }.to raise_error
|
7
|
+
end
|
8
|
+
|
9
|
+
context "with a block" do
|
10
|
+
it "should pass itself to a block" do
|
11
|
+
inner_text = nil
|
12
|
+
text = DocxGenerator::DSL::Text.new("") do |t|
|
13
|
+
inner_text = t
|
14
|
+
end
|
15
|
+
inner_text.should be(text)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#generate" do
|
21
|
+
it "should return a new Run with text in it" do
|
22
|
+
text_fragment = DocxGenerator::DSL::Text.new("Title")
|
23
|
+
text_fragment.generate.to_s.should eq("<w:r><w:t>Title</w:t></w:r>")
|
24
|
+
end
|
25
|
+
|
26
|
+
context "with styles" do
|
27
|
+
it "should return a text in bold" do
|
28
|
+
DocxGenerator::DSL::Text.new("Text", bold: true).generate.to_s.should eq("<w:r><w:rPr><w:b w:val=\"true\" /></w:rPr><w:t>Text</w:t></w:r>")
|
29
|
+
(DocxGenerator::DSL::Text.new("Text") { |t| t.bold true }).generate.to_s.should eq("<w:r><w:rPr><w:b w:val=\"true\" /></w:rPr><w:t>Text</w:t></w:r>")
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should return a text in italics" do
|
33
|
+
DocxGenerator::DSL::Text.new("Text", italics: true).generate.to_s.should eq("<w:r><w:rPr><w:i w:val=\"true\" /></w:rPr><w:t>Text</w:t></w:r>")
|
34
|
+
(DocxGenerator::DSL::Text.new("Text") { |t| t.italics true }).generate.to_s.should eq("<w:r><w:rPr><w:i w:val=\"true\" /></w:rPr><w:t>Text</w:t></w:r>")
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should return an underlined text" do
|
38
|
+
DocxGenerator::DSL::Text.new("Text", underline: { style: "single" }).generate.to_s.should eq("<w:r><w:rPr><w:u w:val=\"single\" /></w:rPr><w:t>Text</w:t></w:r>")
|
39
|
+
(DocxGenerator::DSL::Text.new("Text") { |t| t.underline style: "single" }).generate.to_s.should eq("<w:r><w:rPr><w:u w:val=\"single\" /></w:rPr><w:t>Text</w:t></w:r>")
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should return a text with a font size" do
|
43
|
+
DocxGenerator::DSL::Text.new("Text", size: 20).generate.to_s.should eq("<w:r><w:rPr><w:sz w:val=\"40\" /></w:rPr><w:t>Text</w:t></w:r>")
|
44
|
+
(DocxGenerator::DSL::Text.new("Text") { |t| t.size 20 }).generate.to_s.should eq("<w:r><w:rPr><w:sz w:val=\"40\" /></w:rPr><w:t>Text</w:t></w:r>")
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should render a text in superscript" do
|
48
|
+
DocxGenerator::DSL::Text.new("Text", superscript: true).generate.to_s.should eq("<w:r><w:rPr><w:vertAlign w:val=\"superscript\" /></w:rPr><w:t>Text</w:t></w:r>")
|
49
|
+
(DocxGenerator::DSL::Text.new("Text") { |t| t.superscript true }).generate.to_s.should eq("<w:r><w:rPr><w:vertAlign w:val=\"superscript\" /></w:rPr><w:t>Text</w:t></w:r>")
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should render a text in subscript" do
|
53
|
+
DocxGenerator::DSL::Text.new("Text", subscript: true).generate.to_s.should eq("<w:r><w:rPr><w:vertAlign w:val=\"subscript\" /></w:rPr><w:t>Text</w:t></w:r>")
|
54
|
+
(DocxGenerator::DSL::Text.new("Text") { |t| t.subscript true }).generate.to_s.should eq("<w:r><w:rPr><w:vertAlign w:val=\"subscript\" /></w:rPr><w:t>Text</w:t></w:r>")
|
55
|
+
end
|
56
|
+
|
57
|
+
it "shoud render a text as capital letters" do
|
58
|
+
DocxGenerator::DSL::Text.new("Text", caps: true).generate.to_s.should eq("<w:r><w:rPr><w:caps w:val=\"true\" /></w:rPr><w:t>Text</w:t></w:r>")
|
59
|
+
(DocxGenerator::DSL::Text.new("Text") { |t| t.caps true }).generate.to_s.should eq("<w:r><w:rPr><w:caps w:val=\"true\" /></w:rPr><w:t>Text</w:t></w:r>")
|
60
|
+
end
|
61
|
+
|
62
|
+
it "shoud render a text as small capital letters" do
|
63
|
+
DocxGenerator::DSL::Text.new("Text", small_caps: true).generate.to_s.should eq("<w:r><w:rPr><w:smallCaps w:val=\"true\" /></w:rPr><w:t>Text</w:t></w:r>")
|
64
|
+
(DocxGenerator::DSL::Text.new("Text") { |t| t.small_caps true }).generate.to_s.should eq("<w:r><w:rPr><w:smallCaps w:val=\"true\" /></w:rPr><w:t>Text</w:t></w:r>")
|
65
|
+
end
|
66
|
+
|
67
|
+
xit "shoud render a text with a single horizontal line through the center of the line" do
|
68
|
+
DocxGenerator::DSL::Text.new("Text", strike: true).generate.to_s.should eq("<w:r><w:rPr><w:strike w:val=\"true\" /></w:rPr><w:t>Text</w:t></w:r>")
|
69
|
+
(DocxGenerator::DSL::Text.new("Text") { |t| t.strike true }).generate.to_s.should eq("<w:r><w:rPr><w:strike w:val=\"true\" /></w:rPr><w:t>Text</w:t></w:r>")
|
70
|
+
end
|
71
|
+
|
72
|
+
xit "shoud render a text with two horizontal lines through the center of the line" do
|
73
|
+
DocxGenerator::DSL::Text.new("Text", dstrike: true).generate.to_s.should eq("<w:r><w:rPr><w:dstrike w:val=\"true\" /></w:rPr><w:t>Text</w:t></w:r>")
|
74
|
+
(DocxGenerator::DSL::Text.new("Text") { |t| t.dstrike true }).generate.to_s.should eq("<w:r><w:rPr><w:dstrike w:val=\"true\" /></w:rPr><w:t>Text</w:t></w:r>")
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should render the name of the font" do
|
78
|
+
result = "<w:r><w:rPr><w:rFonts w:ascii=\"Arial\" /></w:rPr><w:t>Text</w:t></w:r>"
|
79
|
+
DocxGenerator::DSL::Text.new("Text", font: "Arial").generate.to_s.should eq(result)
|
80
|
+
(DocxGenerator::DSL::Text.new("Text") { |t| t.font "Arial" }).generate.to_s.should eq(result)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "#to_s" do
|
86
|
+
it "should render the XML representation" do
|
87
|
+
text_fragment = DocxGenerator::DSL::Text.new("Title")
|
88
|
+
text_fragment.to_s.should eq("<w:r><w:t>Title</w:t></w:r>")
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|