parseexcel 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,733 @@
1
+ #!/usr/bin/env ruby
2
+ # TestParser -- Spreadsheet -- 02.06.2003 -- hwyss@ywesee.com
3
+
4
+ $: << File.expand_path("../lib", File.dirname(__FILE__))
5
+
6
+ require 'test/unit'
7
+ require 'parseexcel/parser'
8
+
9
+ module Spreadsheet
10
+ module ParseExcel
11
+ class Parser
12
+ attr_reader :proc_table
13
+ attr_accessor :workbook, :current_sheet, :buff
14
+ attr_writer :bigendian, :prev_pos, :prev_info, :prev_cond
15
+ end
16
+ class Workbook
17
+ attr_reader :pkg_strs
18
+ end
19
+ end
20
+ end
21
+ class StubParserWorksheet
22
+ attr_accessor :header, :footer, :row_height, :dimensions, :default_row_height
23
+ attr_accessor :paper, :scale, :page_start, :fit_width, :fit_height
24
+ attr_accessor :resolution, :v_resolution
25
+ attr_accessor :header_margin, :footer_margin, :copies, :left_to_right
26
+ attr_accessor :no_pls, :no_color, :draft, :notes, :no_orient, :use_page
27
+ attr_accessor :landscape
28
+ def initialize
29
+ @cells = []
30
+ end
31
+ def add_cell(x, y, cell)
32
+ (@cells[x] ||= [])[y] = cell
33
+ end
34
+ def cell(x, y)
35
+ (@cells[x] ||= [])[y]
36
+ end
37
+ def set_row_height(row, hght)
38
+ @row_height = hght
39
+ end
40
+ def set_dimensions(row, scol, ecol)
41
+ @dimensions = [row, scol, ecol]
42
+ end
43
+ end
44
+ class StubParserWorkbook
45
+ attr_accessor :header, :footer, :worksheets, :text_format
46
+ attr_accessor :biffversion, :version, :flg_1904
47
+ attr_writer :format, :pkg_strs
48
+ def add_cell_format(format)
49
+ @format = format
50
+ end
51
+ def add_text_format(idx, fmt)
52
+ @text_format = fmt
53
+ end
54
+ def worksheet(index)
55
+ @worksheets[index] ||= StubParserWorksheet.new
56
+ end
57
+ def format(index=nil)
58
+ @format
59
+ end
60
+ def sheet_count
61
+ (@worksheets.respond_to? :size) ? @worksheets.size : 0
62
+ end
63
+ def pkg_str(idx)
64
+ @pkg_strs[idx]
65
+ end
66
+ end
67
+ class StubParserFormat
68
+ attr_writer :cell_type
69
+ def text_format(txt, code=nil)
70
+ txt
71
+ end
72
+ def cell_type(foo)
73
+ @cell_type
74
+ end
75
+ end
76
+
77
+ class TestParser < Test::Unit::TestCase
78
+ def setup
79
+ @source = File.expand_path('data/foo.xls', File.dirname(__FILE__))
80
+ @book = StubParserWorkbook.new
81
+ @sheet = StubParserWorksheet.new
82
+ @format = StubParserFormat.new
83
+ @book.worksheets = [@sheet]
84
+ @book.format = @format
85
+ @parser = Spreadsheet::ParseExcel::Parser.new
86
+ @parser.workbook = @book
87
+ @parser.current_sheet = @sheet
88
+ @parser.private_methods.each { |m|
89
+ @parser.class.instance_eval("public :#{m}")
90
+ }
91
+ @bin_header = "\x03\x00\x02\x00\x01\x00"
92
+ end
93
+ def test_initialize
94
+ @parser.proc_table.each { |key, val|
95
+ assert_instance_of(Method, val)
96
+ assert_equal(3, val.arity)
97
+ }
98
+ bigendian = ([2].pack('L').unpack('H8').first != '02000000')
99
+ assert_equal(bigendian, @parser.bigendian)
100
+ parser = Spreadsheet::ParseExcel::Parser.new(:bigendian => false)
101
+ assert_equal(false, parser.bigendian)
102
+ parser = Spreadsheet::ParseExcel::Parser.new(:bigendian => true)
103
+ assert_equal(true, parser.bigendian)
104
+ end
105
+ def test_blank
106
+ @parser.blank(nil, nil, @bin_header)
107
+ cell = @sheet.cell(3,2)
108
+ assert_instance_of(Spreadsheet::ParseExcel::Worksheet::Cell, cell)
109
+ assert_equal(:blank, cell.kind)
110
+ end
111
+ def test_bof
112
+ bof = [0x500, 0x5].pack("v2")
113
+ @parser.bof(nil, nil, bof)
114
+ assert_nil(@parser.current_sheet)
115
+ assert_equal(0x08, @book.biffversion)
116
+ bof = [0x000, 0x3].pack("v2")
117
+ @parser.bof(0x02*0x100, nil, bof)
118
+ assert_instance_of(StubParserWorksheet, @parser.current_sheet)
119
+ end
120
+ def test_bool_err1
121
+ @parser.bool_err(nil, nil, @bin_header + "\x01\x00")
122
+ cell = @sheet.cell(3,2)
123
+ assert_equal(:bool_error, cell.kind)
124
+ assert_equal("TRUE", cell.value)
125
+ end
126
+ def test_bool_err2
127
+ @parser.bool_err(nil, nil, @bin_header + "\x00\x00")
128
+ cell = @sheet.cell(3,2)
129
+ assert_equal("FALSE", cell.value)
130
+ assert_equal(:bool_error, cell.kind)
131
+ end
132
+ def test_bool_err3
133
+ @parser.bool_err(nil, nil, @bin_header + "\x01\x01")
134
+ cell = @sheet.cell(3,2)
135
+ assert_equal(:bool_error, cell.kind)
136
+ assert_equal("#ERR", cell.value)
137
+ end
138
+ def test_bool_err4
139
+ @parser.bool_err(nil, nil, @bin_header + "\x00\x01")
140
+ cell = @sheet.cell(3,2)
141
+ assert_equal(:bool_error, cell.kind)
142
+ assert_equal("#NULL!", cell.value)
143
+ end
144
+ def test_cell_factory
145
+ @format.cell_type = :text
146
+ params = {
147
+ :kind => :blank,
148
+ :value => '',
149
+ :format_no => 1,
150
+ :numeric => false,
151
+ }
152
+ cell = @parser.cell_factory(1,1,params)
153
+ assert_equal('', cell.value)
154
+ assert_equal(false, cell.numeric)
155
+ assert_nil(cell.code)
156
+ assert_equal(@book, cell.book)
157
+ assert_equal(@format, cell.format)
158
+ assert_equal(1, cell.format_no)
159
+ assert_equal(:text, cell.type)
160
+ end
161
+ def test_conv_biff8_data1 # compressed
162
+ biff8_data = [
163
+ "\3\0\0foo"
164
+ ].join
165
+ expected = [
166
+ [ "f\0o\0o\0", false, "", "" ],
167
+ 6, 3, 3,
168
+ ]
169
+ assert_equal(expected, @parser.conv_biff8_data(biff8_data))
170
+ end
171
+ def test_conv_biff8_data2 # short string
172
+ biff8_data = [
173
+ "\x04\x00\x00foo"
174
+ ].join
175
+ expected = [
176
+ [ nil, false, nil, nil ],
177
+ 7, 3, 4,
178
+ ]
179
+ assert_equal(expected, @parser.conv_biff8_data(biff8_data))
180
+ end
181
+ def test_conv_biff8_data3 # uncompressed
182
+ biff8_data = [
183
+ "\3\0\1\0f\0o\0o"
184
+ ].join
185
+ expected = [
186
+ [ "\0f\0o\0o" , true, "", "" ],
187
+ 9, 3, 6,
188
+ ]
189
+ assert_equal(expected, @parser.conv_biff8_data(biff8_data))
190
+ end
191
+ def test_conv_biff8_data4 # ext
192
+ biff8_data = [
193
+ "\x03\x00\x04\x01\x00\x00\x00foobar"
194
+ ].join
195
+ expected = [
196
+ [ "f\0o\0o\0" , false, "bar", "b" ],
197
+ 11, 7, 3,
198
+ ]
199
+ assert_equal(expected, @parser.conv_biff8_data(biff8_data))
200
+ end
201
+ def test_conv_biff8_data5 # rich
202
+ biff8_data = [
203
+ "\x03\x00\x08\x01\x00foobaar"
204
+ ].join
205
+ expected = [
206
+ [ "f\0o\0o\0" , false, "", "" ],
207
+ 12, 5, 3,
208
+ ]
209
+ assert_equal(expected, @parser.conv_biff8_data(biff8_data))
210
+ end
211
+ def test_conv_biff8_data6 # ext && rich
212
+ biff8_data = [
213
+ "\x03\x00\x0C\x01\x00\x01\x00\x00\x00foobaarbaz"
214
+ ].join
215
+ expected = [
216
+ [ "f\0o\0o\0" , false, "baz", "b" ],
217
+ 17, 9, 3,
218
+ ]
219
+ assert_equal(expected, @parser.conv_biff8_data(biff8_data))
220
+ end
221
+ def test_conv_biff8_string1
222
+ str = "\010\000\000DD/MM/YY"
223
+ assert_equal("D\0D\0/\0M\0M\0/\0Y\0Y\0", @parser.conv_biff8_string(str))
224
+ end
225
+ def test_conv_dval
226
+ @parser.bigendian = true
227
+ assert_equal(0.0, @parser.conv_dval("\x00"))
228
+ last_bit = "\x00\x00\x00\x00\x00\x00\x00\x01"
229
+ frst_bit = "\x01\x00\x00\x00\x00\x00\x00\x00"
230
+ assert_in_delta(4.940656458412465e-324, @parser.conv_dval(last_bit), 1e-300)
231
+ assert_in_delta(7.291122019556397e-304, @parser.conv_dval(frst_bit), 1e-300)
232
+ @parser.bigendian = false
233
+ assert_in_delta(7.291122019556397e-304, @parser.conv_dval(last_bit), 1e-300)
234
+ assert_in_delta(4.940656458412465e-324, @parser.conv_dval(frst_bit), 1e-300)
235
+ end
236
+ def test_decode_bool_err
237
+ assert_equal("FALSE", @parser.decode_bool_err(0x00))
238
+ assert_equal("TRUE", @parser.decode_bool_err(0x01))
239
+ assert_equal("#NULL!", @parser.decode_bool_err(0x00, true))
240
+ assert_equal("#DIV/0!", @parser.decode_bool_err(0x07, true))
241
+ assert_equal("#VALUE!", @parser.decode_bool_err(0x0F, true))
242
+ assert_equal("#REF!", @parser.decode_bool_err(0x17, true))
243
+ assert_equal("#NAME?", @parser.decode_bool_err(0x1D, true))
244
+ assert_equal("#NUM!", @parser.decode_bool_err(0x24, true))
245
+ assert_equal("#N/A!", @parser.decode_bool_err(0x2A, true))
246
+ assert_equal("#ERR", @parser.decode_bool_err(0x02, true))
247
+ end
248
+ def test_default_row_height
249
+ @parser.default_row_height(nil,nil,"\x00\x00\x28\x00")
250
+ assert_equal(2.0, @sheet.default_row_height)
251
+ end
252
+ def test_flg_1904
253
+ @parser.flg_1904(nil, nil, "\x00\x00")
254
+ assert_nil(@book.flg_1904)
255
+ @parser.flg_1904(nil, nil, "\x01\x00")
256
+ assert(@book.flg_1904)
257
+ end
258
+ def test_footer1
259
+ @book.biffversion = 0x08
260
+ @parser.footer(nil, nil, "\x03\x00\x00\x00")
261
+ assert_equal(nil, @sheet.footer)
262
+ @parser.footer(nil, nil, "\x03foo")
263
+ assert_equal('foo', @sheet.footer)
264
+ @parser.footer(nil, nil, "\x03foobarbaz")
265
+ assert_equal('foo', @sheet.footer)
266
+ end
267
+ def test_footer2
268
+ @book.biffversion = 0x18
269
+ @parser.footer(nil, nil, "\x01\x00\x00\x00")
270
+ assert_equal(nil, @sheet.footer)
271
+ @parser.footer(nil, nil, "\x03\x00\x00foo")
272
+ assert_equal("f\0o\0o\0", @sheet.footer)
273
+ @parser.footer(nil, nil, "\x03\x00\x00foobarbaz")
274
+ assert_equal("f\0o\0o\0", @sheet.footer)
275
+ end
276
+ def test_format
277
+ @book.biffversion = 0x02
278
+ fmt = "\x00\x00\x03foo"
279
+ @parser.format(nil, nil, fmt)
280
+ assert_equal("foo", @book.text_format)
281
+ @book.biffversion = 0x18
282
+ fmt = "\x00\x00\x03\000\000foo"
283
+ @parser.format(nil, nil, fmt)
284
+ assert_equal("f\0o\0o\0", @book.text_format)
285
+ fmt = "\0\0\6\0\1f\0o\0o\0"
286
+ @parser.format(nil, nil, fmt)
287
+ assert_equal("f\0o\0o\0", @book.text_format)
288
+ end
289
+ def test_header1
290
+ @book.biffversion = 0x08
291
+ @parser.header(nil, nil, "\x03\x00\x00\x00")
292
+ assert_equal(nil, @sheet.header)
293
+ @parser.header(nil, nil, "\x03foo")
294
+ assert_equal('foo', @sheet.header)
295
+ @parser.header(nil, nil, "\x03foobarbaz")
296
+ assert_equal('foo', @sheet.header)
297
+ end
298
+ def test_header2
299
+ @book.biffversion = 0x18
300
+ @parser.header(nil, nil, "\x01\x00\x00\x00")
301
+ assert_equal(nil, @sheet.header)
302
+ @parser.header(nil, nil, "\x03\x00\x00foo")
303
+ assert_equal("f\0o\0o\0", @sheet.header)
304
+ @parser.header(nil, nil, "\x03\x00\x00foobarbaz")
305
+ assert_equal("f\0o\0o\0", @sheet.header)
306
+ end
307
+ def test_integer
308
+ @parser.integer(nil, nil, @bin_header + "x\x1A\x00")
309
+ cell = @sheet.cell(3,2)
310
+ assert_equal(:integer, cell.kind)
311
+ assert_equal(26, cell.value)
312
+ end
313
+ def test_label1
314
+ @parser.label(nil, nil, @bin_header+'abfoo')
315
+ cell = @sheet.cell(3,2)
316
+ assert_equal(:label, cell.kind)
317
+ assert_equal('foo', cell.value)
318
+ assert_equal(:_native_, cell.code)
319
+ end
320
+ def test_label2
321
+ @book.biffversion = 0x18
322
+ @parser.label(nil, nil, @bin_header+"\x03\x003\x00f\x00o\x00o")
323
+ cell = @sheet.cell(3,2)
324
+ assert_equal(:label, cell.kind)
325
+ assert_equal("\0f\0o\0o", cell.value)
326
+ assert_equal(:ucs2, cell.code)
327
+ end
328
+ def test_label_sst
329
+ foo = Spreadsheet::ParseExcel::Worksheet::PkgString.new("foo", nil, 0, 0)
330
+ @book.pkg_strs = [nil, nil, nil, nil, foo]
331
+ label = [
332
+ "\x01\x00",
333
+ "\x02\x00",
334
+ "\x03\x00",
335
+ "\x04\x00\x00\x00",
336
+ ].join
337
+ @parser.label_sst(nil, nil, label)
338
+ cell = @sheet.cell(1,2)
339
+ assert_equal("foo", cell.value)
340
+ end
341
+ def test_mul_rk
342
+ mul_rk = [
343
+ "\x01\x00",
344
+ "\x02\x00",
345
+ "\x01\x00\x00\x00\xF0\x3F",
346
+ "\x01\x00\x01\x00\xF0\x3F",
347
+ "\x01\x00\x46\x56\x4B\x00",
348
+ "\x04\x00",
349
+ ].join
350
+ @parser.mul_rk(nil, nil, mul_rk)
351
+ cell1 = @sheet.cell(1,2)
352
+ assert_equal(:mul_rk, cell1.kind)
353
+ assert_equal(1.0, cell1.value)
354
+ cell2 = @sheet.cell(1,3)
355
+ assert_equal(:mul_rk, cell2.kind)
356
+ assert_equal(0.01, cell2.value)
357
+ cell3 = @sheet.cell(1,4)
358
+ assert_equal(:mul_rk, cell3.kind)
359
+ assert_equal(1234321, cell3.value)
360
+ end
361
+ def test_mul_blank
362
+ blanks = [
363
+ "\x02\x00",
364
+ "\x01\x00",
365
+ "\x01\x00",
366
+ "\x02\x00",
367
+ "\x03\x00",
368
+ "\x04\x00",
369
+ "\x04\x00",
370
+ ].join
371
+ @parser.mul_blank(nil, nil, blanks)
372
+ cell1 = @sheet.cell(2,1)
373
+ assert_equal(:mul_blank, cell1.kind)
374
+ cell2 = @sheet.cell(2,2)
375
+ assert_equal(:mul_blank, cell2.kind)
376
+ cell3 = @sheet.cell(2,3)
377
+ assert_equal(:mul_blank, cell3.kind)
378
+ cell4 = @sheet.cell(2,4)
379
+ assert_equal(:mul_blank, cell4.kind)
380
+ end
381
+ def test_number
382
+ @parser.number(nil, nil, @bin_header + "\x00\x00\x00\x00\x00\x00\x00\x00")
383
+ cell = @sheet.cell(3,2)
384
+ assert_equal(:number, cell.kind)
385
+ assert_equal(0.0, cell.value)
386
+ end
387
+ def test_rk
388
+ rk = "\x01\x00\x02\x00\x04\x00\x46\x56\x4B\x00"
389
+ @parser.rk(nil, nil, rk)
390
+ cell = @sheet.cell(1,2)
391
+ assert_equal(:rk, cell.kind)
392
+ assert_equal(1234321, cell.value)
393
+ end
394
+ def test_row
395
+ row = [
396
+ "\x01\x00", #row
397
+ "\x01\x00", #scol
398
+ "\x04\x00", #ecol
399
+ "\x14\x00", #hght
400
+ "\x00\x00", #nil
401
+ "\x00\x00", #nil
402
+ "\x00\x20", #gr
403
+ "\x00\x00", #xf
404
+ ]
405
+ @parser.row(nil, nil, row.join)
406
+ assert_equal([1,1,3], @sheet.dimensions)
407
+ assert_equal(1.0, @sheet.row_height)
408
+ end
409
+ def test_rstring
410
+ rstr = "\x01\x00\x01\x00\x04\x00\x03\x00foo"
411
+ @parser.rstring(nil, nil, rstr)
412
+ cell = @sheet.cell(1,1)
413
+ assert_equal("foo", cell.value)
414
+ assert_nil(cell.rich)
415
+ rstr = "\x01\x00\x02\x00\x04\x00\x03\x00foorich"
416
+ @parser.rstring(nil, nil, rstr)
417
+ cell = @sheet.cell(1,2)
418
+ assert_equal("foo", cell.value)
419
+ assert_equal("rich", cell.rich)
420
+ end
421
+ def test_setup
422
+ setup = [
423
+ "\x01\x00",
424
+ "\x02\x00",
425
+ "\x03\x00",
426
+ "\x04\x00",
427
+ "\x05\x00",
428
+ "\x06\x00",
429
+ "\x07\x00",
430
+ "\x08\x00",
431
+ "\x00\x00\x00\x00\x00\x00\xF0\x3F",
432
+ "\x00\x00\x00\x00\x00\x00\xF1\x3F",
433
+ "\x0B\x00",
434
+ ].join
435
+ @parser.setup(nil, nil, setup)
436
+ assert_equal(1, @sheet.paper)
437
+ assert_equal(2, @sheet.scale)
438
+ assert_equal(3, @sheet.page_start)
439
+ assert_equal(4, @sheet.fit_width)
440
+ assert_equal(5, @sheet.fit_height)
441
+ assert_equal(7, @sheet.resolution)
442
+ assert_equal(8, @sheet.v_resolution)
443
+ assert_equal(2.54, @sheet.header_margin)
444
+ assert_equal(2.69875, @sheet.footer_margin)
445
+ assert(!@sheet.left_to_right, "left_to_right")
446
+ assert(@sheet.landscape, "landscape")
447
+ assert(@sheet.no_pls, "no_pls")
448
+ assert(!@sheet.no_color, "no_color")
449
+ assert(!@sheet.draft, "draft")
450
+ assert(!@sheet.notes, "notes")
451
+ assert(!@sheet.no_orient, "no_orient")
452
+ assert(!@sheet.use_page, "use_page")
453
+ end
454
+ def test_sst
455
+ @parser.sst(nil, nil, '12345678foo')
456
+ assert_equal('foo', @parser.buff)
457
+ end
458
+ def test_string1
459
+ @parser.prev_pos = [1,2,3]
460
+ @parser.string(nil, nil, "\x06foobar")
461
+ cell = @sheet.cell(1,2)
462
+ assert_equal(:string, cell.kind)
463
+ assert_equal("foobar", cell.value)
464
+ end
465
+ def test_string2 # BIFF5
466
+ @parser.prev_pos = [1,2,3]
467
+ @book.biffversion = 0x08
468
+ @parser.string(nil, nil, "\x00\x06foobar")
469
+ cell = @sheet.cell(1,2)
470
+ assert_equal(:string, cell.kind)
471
+ assert_equal("foobar", cell.value)
472
+ end
473
+ def test_string3 # BIFF5
474
+ @parser.prev_pos = [1,2,3]
475
+ @book.biffversion = 0x18
476
+ @parser.string(nil, nil, "\x06\x00\x00foobar")
477
+ cell = @sheet.cell(1,2)
478
+ assert_equal(:string, cell.kind)
479
+ assert_equal("f\0o\0o\0b\0a\0r\0", cell.value)
480
+ assert_equal("foobar", cell.to_s('latin1'))
481
+ end
482
+ def test_str_wk1
483
+ @parser.str_wk('foo')
484
+ assert_equal('foo', @parser.buff)
485
+ end
486
+ def test_str_wk2
487
+ @parser.str_wk('foo', true)
488
+ assert_equal('foo', @parser.buff)
489
+ end
490
+ def test_str_wk3
491
+ @parser.buff = 'f'
492
+ @parser.str_wk('foo', true)
493
+ assert_equal('foo', @parser.buff)
494
+ end
495
+ def test_str_wk4
496
+ @parser.buff = 'f'
497
+ @parser.prev_cond = true
498
+ @parser.prev_info = [0,1]
499
+ @parser.str_wk("oo", true)
500
+ assert_equal('foo', @parser.buff)
501
+ end
502
+ def test_str_wk5
503
+ @parser.buff = 'f'
504
+ @parser.prev_cond = true
505
+ @parser.prev_info = [1,2]
506
+ @parser.str_wk("\x01oo", true)
507
+ assert_equal('foo', @parser.buff)
508
+ end
509
+ def test_str_wk6
510
+ @parser.buff = 'f'
511
+ @parser.prev_cond = true
512
+ @parser.prev_info = [1,2]
513
+ @parser.str_wk("\x01oo", true)
514
+ assert_equal('foo', @parser.buff)
515
+ end
516
+ def test_str_wk7
517
+ @parser.buff = "fo"
518
+ @parser.prev_cond = true
519
+ @parser.prev_info = [1,2]
520
+ @parser.str_wk("\x00o", true)
521
+ assert_equal("foo", @parser.buff)
522
+ end
523
+ def test_str_wk8
524
+ @parser.buff = "\x00fo"
525
+ @parser.prev_cond = false
526
+ @parser.prev_info = [1,3]
527
+ @parser.str_wk("\x01o\x00", true)
528
+ assert_equal("\x00f\x00o\x00o\x00", @parser.buff)
529
+ end
530
+ def test_unpack_rk_rec
531
+ rk_rec = "\030\000\001\330\200@"
532
+ result = @parser.unpack_rk_rec(rk_rec)
533
+ expected = [25, 5.39]
534
+ rk_rec = "\017\000o\010\000\000"
535
+ rk_rec = "\031\000o\010\000\000"
536
+ result = @parser.unpack_rk_rec(rk_rec)
537
+ expected = [25, 5.39]
538
+ #assert_equal(expected, result)
539
+ rk_rec = "\x01\x00\x00\x00\xF0\x3F"
540
+ result = @parser.unpack_rk_rec(rk_rec)
541
+ expected = [1, 1.0]
542
+ assert_equal(expected, result)
543
+ rk_rec = "\x01\x00\x01\x00\xF0\x3F"
544
+ result = @parser.unpack_rk_rec(rk_rec)
545
+ expected = [1, 0.01]
546
+ assert_equal(expected, result)
547
+ rk_rec = "\x01\x00\x46\x56\x4B\x00"
548
+ result = @parser.unpack_rk_rec(rk_rec)
549
+ expected = [1, 1234321]
550
+ assert_equal(expected, result)
551
+ rk_rec = "\x01\x00\x47\x56\x4B\x00"
552
+ result = @parser.unpack_rk_rec(rk_rec)
553
+ expected = [1, 12343.21]
554
+ assert_equal(expected, result)
555
+ end
556
+ def test_xf
557
+ @book.biffversion = 0x18
558
+ fmt = "\006\000\244\000\001\000 \000\000\000\000\000\010\004\010\004\002\000\t\004"
559
+ format = @parser.xf(nil, nil, fmt)
560
+ assert_equal(6, format.font_no)
561
+ assert_equal(164, format.fmt_idx)
562
+ assert_not_nil(format.lock)
563
+ assert_equal(false, format.hidden)
564
+ assert_equal(false, format.style)
565
+ assert_equal(false, format.key_123)
566
+ assert_equal(0, format.align_h)
567
+ assert_equal(false, format.wrap)
568
+ assert_equal(2, format.align_v)
569
+ assert_equal(false, format.just_last)
570
+ assert_equal(0, format.rotate)
571
+ assert_equal(0, format.indent)
572
+ assert_equal(false, format.shrink)
573
+ assert_equal(false, format.merge)
574
+ assert_equal(0, format.read_dir)
575
+ assert_equal([0,0,0,0], format.border_style)
576
+ assert_equal([8,8,8,8], format.border_color)
577
+ assert_equal([0,0,0], format.border_diag)
578
+ assert_equal([0,9,8], format.fill)
579
+ end
580
+ end
581
+ class TestParser2 < Test::Unit::TestCase
582
+ def setup
583
+ @parser = Spreadsheet::ParseExcel::Parser.new
584
+ end
585
+ def test_file_bar # Simple text-values
586
+ source = File.expand_path('data/bar.xls', File.dirname(__FILE__))
587
+ book = @parser.parse(source)
588
+ assert_equal(1, book.sheet_count)
589
+ sheet = book.worksheet(0)
590
+ assert_equal('A1',sheet.cell(0,0).value)
591
+ assert_equal('A2',sheet.cell(1,0).value)
592
+ assert_equal('A3',sheet.cell(2,0).value)
593
+ assert_equal('B1',sheet.cell(0,1).value)
594
+ assert_equal('B2',sheet.cell(1,1).value)
595
+ assert_equal('B3',sheet.cell(2,1).value)
596
+ assert_equal('C1',sheet.cell(0,2).value)
597
+ assert_equal('C2',sheet.cell(1,2).value)
598
+ assert_equal('C3',sheet.cell(2,2).value)
599
+ end
600
+ def test_file_foo
601
+ source = File.expand_path('data/foo.xls', File.dirname(__FILE__))
602
+ book = @parser.parse(source)
603
+ assert_equal(3, book.sheet_count)
604
+ sheet = book.worksheet(0)
605
+ cell0 = sheet.cell(0,0)
606
+ assert_equal("F\0o\0o\0",cell0.value)
607
+ assert_equal(:text, cell0.type)
608
+ assert_equal('UTF-16LE', cell0.encoding)
609
+ assert_equal('Foo', cell0.to_s('latin1'))
610
+ cell1 = sheet.cell(0,1)
611
+ assert_equal(12,cell1.value)
612
+ assert_equal(:numeric, cell1.type)
613
+ cell2 = sheet.cell(1,0)
614
+ assert_equal(27627,cell2.value)
615
+ assert_equal(:date, cell2.type)
616
+ # once formulas are implemented:
617
+ # cell3 = sheet.cell(1,1)
618
+ # assert_equal(27627,cell3.value)
619
+ # assert_equal(:numeric, cell3.type)
620
+ end
621
+ def test_file_float
622
+ source = File.expand_path('data/float.xls', File.dirname(__FILE__))
623
+ book = @parser.parse(source)
624
+ sheet = book.worksheet(0)
625
+ cell0 = sheet.cell(0,0)
626
+ assert_equal(5.39,cell0.value)
627
+ end
628
+ def test_file_image
629
+ source = File.expand_path('data/image.xls', File.dirname(__FILE__))
630
+ book = nil
631
+ assert_nothing_raised {
632
+ book = @parser.parse(source)
633
+ }
634
+ sheet = book.worksheet(0)
635
+ cell0 = sheet.cell(48,0)
636
+ assert_equal('TEST', cell0.to_s('latin1'))
637
+ end
638
+ def test_file_umlaut__5_0
639
+ source = File.expand_path('data/umlaut.5.0.xls', File.dirname(__FILE__))
640
+ book = nil
641
+ assert_nothing_raised {
642
+ book = @parser.parse(source)
643
+ }
644
+ sheet = book.worksheet(0)
645
+ cell0 = sheet.cell(0,0)
646
+ assert_equal('WINDOWS-1252', cell0.encoding)
647
+ assert_equal('�', cell0.to_s('latin1'))
648
+ end
649
+ def test_file_umlaut__biff8
650
+ source = File.expand_path('data/umlaut.biff8.xls', File.dirname(__FILE__))
651
+ book = nil
652
+ assert_nothing_raised {
653
+ book = @parser.parse(source)
654
+ }
655
+ sheet = book.worksheet(0)
656
+ cell0 = sheet.cell(0,0)
657
+ assert_equal('UTF-16LE', cell0.encoding)
658
+ assert_equal('�', cell0.to_s('latin1'))
659
+ end
660
+ def test_file_uncompressed_str
661
+ source = File.expand_path('data/uncompressed.str.xls', File.dirname(__FILE__))
662
+ book = nil
663
+ assert_nothing_raised {
664
+ book = @parser.parse(source)
665
+ }
666
+ sheet = book.worksheet(0)
667
+ cell0 = sheet.cell(0,0)
668
+ assert_equal('UTF-16LE', cell0.encoding)
669
+ assert_equal('Aaaaa Aaaaaaaaa Aaaaaa', cell0.to_s('latin1'))
670
+ end
671
+ def test_file_comment
672
+ source = File.expand_path('data/comment.xls', File.dirname(__FILE__))
673
+ book = nil
674
+ assert_nothing_raised {
675
+ book = @parser.parse(source)
676
+ }
677
+ sheet = book.worksheet(0)
678
+ cell0 = sheet.cell(0,0)
679
+ assert_equal('cellcontent', cell0.to_s('latin1'))
680
+ assert_equal('cellcomment', cell0.annotation)
681
+ cell1 = sheet.cell(1,1)
682
+ assert_equal('cellcontent', cell1.to_s('latin1'))
683
+ assert_equal('annotation', cell1.annotation)
684
+ end
685
+ def test_file_comment__5_0
686
+ source = File.expand_path('data/comment.5.0.xls', File.dirname(__FILE__))
687
+ book = nil
688
+ assert_nothing_raised {
689
+ book = @parser.parse(source)
690
+ }
691
+ sheet = book.worksheet(0)
692
+ cell0 = sheet.cell(0,0)
693
+ assert_equal('cellcontent', cell0.to_s('latin1'))
694
+ assert_equal('cellcomment', cell0.annotation)
695
+ cell1 = sheet.cell(1,1)
696
+ assert_equal('cellcontent', cell1.to_s('latin1'))
697
+ assert_equal('annotation', cell1.annotation)
698
+ end
699
+ def test_file_comment__ds
700
+ source = File.expand_path('data/annotation.xls', File.dirname(__FILE__))
701
+ book = nil
702
+ assert_nothing_raised {
703
+ book = @parser.parse(source)
704
+ }
705
+ sheet = book.worksheet(0)
706
+ cell0 = sheet.cell(0,0)
707
+ assert_equal('hello', cell0.to_s('latin1'))
708
+ ann = cell0.annotation
709
+ assert_equal("david surmon:\nnow is the time for all good men to come to the aid of their country!", ann)
710
+ assert_equal('david surmon', ann.author)
711
+ cell1 = sheet.cell(0,1)
712
+ assert_equal('there', cell1.to_s('latin1'))
713
+ ann = cell1.annotation
714
+ assert_equal("david surmon:\nwhat should this comment be? Now what?", ann)
715
+ assert_equal('david surmon', ann.author)
716
+ cell2 = sheet.cell(0,2)
717
+ assert_equal('whos', cell2.to_s('latin1'))
718
+ cell3 = sheet.cell(1,0)
719
+ assert_equal('I', cell3.to_s('latin1'))
720
+ end
721
+ def test_file_several_sheets
722
+ source = File.expand_path('data/annotation.xls', File.dirname(__FILE__))
723
+ book = nil
724
+ assert_nothing_raised {
725
+ book = @parser.parse(source)
726
+ }
727
+ sheet = book.worksheet(1)
728
+ cell0 = sheet.cell(0,0)
729
+ assert_equal('version', cell0.to_s('latin1'))
730
+ cell1 = sheet.cell(1,0)
731
+ assert_equal(1, cell1.to_i)
732
+ end
733
+ end