-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathsheet_spec.rb
More file actions
210 lines (169 loc) · 7 KB
/
sheet_spec.rb
File metadata and controls
210 lines (169 loc) · 7 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# frozen_string_literal: true
require './spec/spec_helper'
describe 'sheet' do
let(:book_with_images) { Creek::Book.new('spec/fixtures/sample-with-images.xlsx') }
let(:sheetfile) { 'worksheets/sheet1.xml' }
let(:sheet_with_images) { Creek::Sheet.new(book_with_images, 'Sheet 1', 1, '', '', '1', sheetfile) }
def load_cell(rows, cell_name)
cell = rows.find { |row| row[cell_name] }
cell[cell_name] if cell
end
context 'escaped ampersand' do
let(:book_escaped) { Creek::Book.new('spec/fixtures/escaped.xlsx') }
it 'does NOT escape ampersand' do
expect(book_escaped.sheets[0].rows.to_enum.map(&:values)).to eq([%w[abc def], %w[ghi j&k]])
end
let(:book_escaped2) { Creek::Book.new('spec/fixtures/escaped2.xlsx') }
it 'does escape ampersand' do
expect(book_escaped2.sheets[0].rows.to_enum.map(&:values)).to eq([%w[abc def], %w[ghi j&k]])
end
end
describe '#rows' do
context 'with excel with images' do
context 'with images preloading' do
let(:rows) { sheet_with_images.with_images.rows.map { |r| r } }
it 'parses single image in a cell' do
expect(load_cell(rows, 'A2').size).to eq(1)
end
it 'returns nil for cells without images' do
expect(load_cell(rows, 'A3')).to eq(nil)
expect(load_cell(rows, 'A7')).to eq(nil)
expect(load_cell(rows, 'A9')).to eq(nil)
end
it 'returns nil for merged cell within empty row' do
expect(load_cell(rows, 'A5')).to eq(nil)
end
it 'returns nil for image in a cell with empty row' do
expect(load_cell(rows, 'A8')).to eq(nil)
end
it 'returns images for merged cells' do
expect(load_cell(rows, 'A4').size).to eq(1)
expect(load_cell(rows, 'A6').size).to eq(1)
end
it 'returns multiple images' do
expect(load_cell(rows, 'A10').size).to eq(2)
end
end
it 'ignores images' do
rows = sheet_with_images.rows.map { |r| r }
expect(load_cell(rows, 'A2')).to eq(nil)
expect(load_cell(rows, 'A3')).to eq(nil)
expect(load_cell(rows, 'A4')).to eq(nil)
end
end
context 'when one cell anchored images in cell' do
let(:book_with_one_cell_anchored_images) do
Creek::Book.new('spec/fixtures/sample-with-one-cell-anchored-images.xlsx')
end
let(:sheet_with_one_cell_anchored_images) do
Creek::Sheet.new(book_with_one_cell_anchored_images, 'Sheet 1', 1, '', '', '1', sheetfile)
end
let(:rows) { sheet_with_one_cell_anchored_images.with_images.rows.map { |r| r } }
it 'returns image for anchored cell' do
expect(load_cell(rows, 'A2').size).to eq(1)
end
it 'returns nil for non-anchored cell' do
expect(load_cell(rows, 'A4')).to eq(nil)
end
end
context 'with excel without images' do
let(:book_no_images) { Creek::Book.new('spec/fixtures/sample.xlsx') }
let(:sheet_no_images) { Creek::Sheet.new(book_no_images, 'Sheet 1', 1, '', '', '1', sheetfile) }
it 'does not break on with_images' do
rows = sheet_no_images.with_images.rows.map { |r| r }
expect(load_cell(rows, 'A10')).to eq(0.15)
end
end
context 'when nodes are namespaced' do
let(:namespaced_book) { Creek::Book.new('spec/fixtures/sample_namespaced.xlsx') }
let(:namespaced_sheet) { Creek::Sheet.new(namespaced_book, 'Sheet 1', 1, '', '', '1', sheetfile) }
it 'parses rows correctly' do
rows = namespaced_sheet.rows.map { |r| r }
expect(load_cell(rows, 'A10')).to eq(0.15)
end
end
context 'when cell references are missing' do
let(:book_missing_refs) { Creek::Book.new('spec/fixtures/sample-missing-cell-ref.xlsx') }
let(:missing_refs_sheet) { book_missing_refs.sheets[0] }
after { book_missing_refs.close }
it 'infers cell references from column order' do
rows = missing_refs_sheet.rows.to_a
expect(rows[0]['A1']).to eq('Content 1')
expect(rows[0]['C1']).to eq('Content 2')
expect(rows[5]['B6']).to eq('2')
expect(rows[5]['C6']).to eq('3')
end
end
end
describe '#images_at' do
it 'returns images for merged cell' do
image = sheet_with_images.with_images.images_at('A5')[0]
expect(image.class).to eq(Pathname)
end
it 'returns images for empty row' do
image = sheet_with_images.with_images.images_at('A8')[0]
expect(image.class).to eq(Pathname)
end
it 'returns nil for empty cell' do
image = sheet_with_images.with_images.images_at('B3')
expect(image).to eq(nil)
end
it 'returns nil for empty cell without preloading images' do
image = sheet_with_images.images_at('B3')
expect(image).to eq(nil)
end
end
describe '#simple_rows' do
let(:book_with_headers) { Creek::Book.new('spec/fixtures/sample-with-headers.xlsx') }
let(:sheet) { Creek::Sheet.new(book_with_headers, 'Sheet 1', 1, '', '', '1', sheetfile) }
subject { sheet.simple_rows.to_a[1] }
it 'returns values by letters' do
expect(subject['A']).to eq 'value1'
expect(subject['B']).to eq 'value2'
end
context 'when enable with_headers property' do
before { sheet.with_headers = true }
it 'returns values by headers name' do
expect(subject['HeaderA']).to eq 'value1'
expect(subject['HeaderB']).to eq 'value2'
expect(subject['HeaderC']).to eq 'value3'
end
it 'returns headers correctly when called multiple times' do
row = sheet.simple_rows.to_a[1]
expect(row['HeaderA']).to eq 'value1'
expect(row['HeaderB']).to eq 'value2'
expect(row['HeaderC']).to eq 'value3'
row = sheet.simple_rows.to_a[1]
expect(row['HeaderA']).to eq 'value1'
expect(row['HeaderB']).to eq 'value2'
expect(row['HeaderC']).to eq 'value3'
end
end
context 'when nodes are namespaced' do
let(:namespaced_book) { Creek::Book.new('spec/fixtures/sample-with-headers_namespaced.xlsx') }
let(:sheet) { Creek::Sheet.new(namespaced_book, 'Sheet 1', 1, '', '', '1', sheetfile) }
it 'returns values by letters' do
expect(subject['A']).to eq 'value1'
expect(subject['B']).to eq 'value2'
end
context 'when enable with_headers property' do
before { sheet.with_headers = true }
it 'returns values by headers name' do
expect(subject['HeaderA']).to eq 'value1'
expect(subject['HeaderB']).to eq 'value2'
expect(subject['HeaderC']).to eq 'value3'
end
it 'returns headers correctly when called multiple times' do
row = sheet.simple_rows.to_a[1]
expect(row['HeaderA']).to eq 'value1'
expect(row['HeaderB']).to eq 'value2'
expect(row['HeaderC']).to eq 'value3'
row = sheet.simple_rows.to_a[1]
expect(row['HeaderA']).to eq 'value1'
expect(row['HeaderB']).to eq 'value2'
expect(row['HeaderC']).to eq 'value3'
end
end
end
end
end