forked from sandrods/odf-report
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimages_spec.rb
159 lines (102 loc) · 5.39 KB
/
images_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
RSpec.describe "Images" do
context('Adding Images') do
before(:context) do
@list = []
@list << OpenStruct.new({ name: "IMG - [1, 1]", path: 'spec/images/image_1.jpg', path2: 'spec/images/image_1.jpg' })
@list << OpenStruct.new({ name: "IMG - [2, 1]", path: 'spec/images/image_2.jpg', path2: 'spec/images/image_1.jpg' })
@list << OpenStruct.new({ name: "IMG - [3, 2]", path: 'spec/images/image_3.jpg', path2: 'spec/images/image_2.jpg' })
@list << OpenStruct.new({ name: "IMG - [1, 3]", path: 'spec/images/image_1.jpg', path2: 'spec/images/image_3.jpg' })
@list << OpenStruct.new({ name: "IMG - [2, 2]", path: 'spec/images/image_2.jpg', path2: 'spec/images/image_2.jpg' })
report = ODFReport::Report.new("spec/templates/images.odt") do |r|
r.add_image("IMAGE_01", 'spec/images/rails.png')
r.add_image("IMAGE_02", 'spec/images/piriapolis.jpg')
r.add_table('IMAGE_TABLE', @list) do |t|
t.add_column(:image_name, :name)
t.add_image('IMAGE_IN_TABLE_01', :path)
t.add_image('IMAGE_IN_TABLE_02', :path2)
end
r.add_section('SECTION', @list) do |t|
t.add_field(:image_name, :name)
t.add_image('IMAGE_IN_SECTION_01', :path2)
t.add_image('IMAGE_IN_SECTION_02', :path)
end
end
report.generate("spec/result/images.odt")
@data = Inspector.new("spec/result/images.odt")
end
it "simple image replacement" do
images = @data.xml.xpath("//draw:image")
expect(images[0].attribute('href').value).to eq "Pictures/rails.png"
expect(images[1].attribute('href').value).to eq "Pictures/piriapolis.jpg"
end
it "table columns replacement" do
table = @data.xml.at_xpath(".//table:table[@table:name='IMAGE_TABLE']")
@list.each_with_index do |item, idx|
row = table.xpath(".//table:table-row[#{idx+1}]")
images = row.xpath(".//draw:image")
expect(File.basename(images[0].attribute('href').value)).to eq File.basename(item.path)
expect(File.basename(images[1].attribute('href').value)).to eq File.basename(item.path2)
end
end
it "section fields replacement" do
@list.each_with_index do |item, idx|
section = @data.xml.at_xpath(".//text:section[#{idx+1}]")
images = section.xpath(".//draw:image")
expect(File.basename(images[0].attribute('href').value)).to eq File.basename(item.path)
expect(File.basename(images[1].attribute('href').value)).to eq File.basename(item.path2)
end
end
end
context "Removing Images" do
before(:context) do
@list = []
@list << OpenStruct.new({ name: "IMG - both ok", path: 'spec/images/image_1.jpg', path2: 'spec/images/image_1.jpg' })
@list << OpenStruct.new({ name: "IMG - 1 ok", path: 'spec/images/image_2.jpg', path2: nil })
@list << OpenStruct.new({ name: "IMG - 2 ok", path: nil, path2: 'spec/images/image_3.jpg' })
# @list << OpenStruct.new({ name: "IMG - 2 invalid", path: nil, path2: 'spec/images/invalid.jpg' })
report = ODFReport::Report.new("spec/templates/images.odt") do |r|
# r.add_image("IMAGE_01")
r.add_image("IMAGE_02", nil)
r.add_table('IMAGE_TABLE', @list) do |t|
t.add_column(:image_name, :name)
t.add_image('IMAGE_IN_TABLE_01', :path)
t.add_image('IMAGE_IN_TABLE_02', :path2)
end
r.add_section('SECTION', @list) do |t|
t.add_field(:image_name, :name)
t.add_image('IMAGE_IN_SECTION_01', :path2)
t.add_image('IMAGE_IN_SECTION_02', :path)
end
end
report.generate("spec/result/images.odt")
@data = Inspector.new("spec/result/images.odt")
end
it "removes nil images in report" do
expect(@data.xml.at_xpath(".//draw:frame[@draw:name='IMAGE_01']")).to be
expect(@data.xml.at_xpath(".//draw:frame[@draw:name='IMAGE_02']")).to be_nil
end
it "removes nil images in tables" do
table = @data.xml.at_xpath(".//table:table[@table:name='IMAGE_TABLE']")
images = table.xpath(".//table:table-row[1]//draw:image")
expect(File.basename(images[0].attribute('href').value)).to eq File.basename(@list[0].path)
expect(File.basename(images[1].attribute('href').value)).to eq File.basename(@list[0].path2)
images = table.xpath(".//table:table-row[2]//draw:image")
expect(File.basename(images[0].attribute('href').value)).to eq File.basename(@list[1].path)
expect(images[1]).to be_nil
images = table.xpath(".//table:table-row[3]//draw:image")
expect(File.basename(images[0].attribute('href').value)).to eq File.basename(@list[2].path2)
expect(images[1]).to be_nil
end
it "removes nil images in sections " do
images = @data.xml.xpath(".//text:section[1]//draw:image")
expect(File.basename(images[0].attribute('href').value)).to eq File.basename(@list[0].path)
expect(File.basename(images[1].attribute('href').value)).to eq File.basename(@list[0].path2)
images = @data.xml.xpath(".//text:section[2]//draw:image")
expect(File.basename(images[0].attribute('href').value)).to eq File.basename(@list[1].path)
expect(images[1]).to be_nil
images = @data.xml.xpath(".//text:section[3]//draw:image")
expect(File.basename(images[0].attribute('href').value)).to eq File.basename(@list[2].path2)
expect(images[1]).to be_nil
end
end
end