writeexcel 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,1192 +1,1192 @@
1
- # -*- coding: utf-8 -*-
2
- require 'helper'
3
- require 'stringio'
4
-
5
- class TC_Format < Test::Unit::TestCase
6
-
7
- TEST_DIR = File.expand_path(File.dirname(__FILE__))
8
- PERL_OUTDIR = File.join(TEST_DIR, 'perl_output')
9
-
10
- def setup
11
- @ruby_file = StringIO.new
12
- @format = Writeexcel::Format.new
13
- end
14
-
15
- def teardown
16
- end
17
-
18
- def test_set_format_properties
19
- end
20
-
21
- def test_format_properties_with_valid_value
22
- # set_format_properties( propty => val )
23
- valid_properties = get_valid_format_properties
24
- valid_properties.each do |k,v|
25
- fmt = Writeexcel::Format.new
26
- before = get_format_property(fmt)
27
- fmt.set_format_properties(k => v)
28
- after = get_format_property(fmt)
29
- after.delete_if {|key, val| before[key] == val }
30
- assert_equal(1, after.size, "change 1 property[:#{k}] but #{after.size} was changed.#{after.inspect}")
31
- assert_equal(v, after[k], "[:#{k}] doesn't match.")
32
- end
33
-
34
- # set_format_properties( propty_1 => val1, propty_2 => val2)
35
- valid_properties.each do |k,v|
36
- fmt = Writeexcel::Format.new
37
- before = get_format_property(fmt)
38
- fmt.set_format_properties(k => v, :bold => 1)
39
- after = get_format_property(fmt)
40
- after.delete_if {|key, val| before[key] == val }
41
- assert_equal(2, after.size, "change 1 property[:#{k}] but #{after.size} was changed.#{after.inspect}")
42
- assert_equal(v, after[k], "[:#{k}] doesn't match.")
43
- assert_equal(700, after[:bold])
44
- end
45
-
46
- # set_format_properties( hash_variable )
47
- valid_properties = get_valid_format_properties
48
- valid_properties.each do |k,v|
49
- arg = {k => v}
50
- fmt = Writeexcel::Format.new
51
- before = get_format_property(fmt)
52
- fmt.set_format_properties(arg)
53
- after = get_format_property(fmt)
54
- after.delete_if {|key, val| before[key] == val }
55
- assert_equal(1, after.size, "change 1 property[:#{k}] but #{after.size} was changed.#{after.inspect}")
56
- assert_equal(v, after[k], "[:#{k}] doesn't match.")
57
- end
58
-
59
- # set_format_properties( hash_variable, hash_variable... )
60
- valid_properties = get_valid_format_properties
61
- valid_properties.each do |k,v|
62
- arg = {k => v}
63
- arg2 = {:bold => 1}
64
- fmt = Writeexcel::Format.new
65
- before = get_format_property(fmt)
66
- fmt.set_format_properties(arg, arg2)
67
- after = get_format_property(fmt)
68
- after.delete_if {|key, val| before[key] == val }
69
- assert_equal(2, after.size, "change 1 property[:#{k}] but #{after.size} was changed.#{after.inspect}")
70
- assert_equal(v, after[k], "[:#{k}] doesn't match.")
71
- assert_equal(700, after[:bold])
72
- end
73
-
74
- # set_color by string
75
- valid_color_string_number = get_valid_color_string_number
76
- [:color , :bg_color, :fg_color].each do |coltype|
77
- valid_color_string_number.each do |str, num|
78
- fmt = Writeexcel::Format.new
79
- before = get_format_property(fmt)
80
- fmt.set_format_properties(coltype => str)
81
- after = get_format_property(fmt)
82
- after.delete_if {|key, val| before[key] == val }
83
- assert_equal(1, after.size, "change 1 property[:#{coltype}:#{str}] but #{after.size} was changed.#{after.inspect}")
84
- assert_equal(num, after[:"#{coltype}"], "[:#{coltype}:#{str}] doesn't match.")
85
- end
86
- end
87
-
88
-
89
- end
90
-
91
- def test_format_properties_with_invalid_value
92
- end
93
-
94
- def test_set_font
95
- end
96
-
97
- =begin
98
- set_size()
99
- Default state: Font size is 10
100
- Default action: Set font size to 1
101
- Valid args: Integer values from 1 to as big as your screen.
102
- Set the font size. Excel adjusts the height of a row to accommodate the largest font size in the row. You can also explicitly specify the height of a row using the set_row() worksheet method.
103
- =end
104
- def test_set_size
105
- # default state
106
- assert_equal(10, @format.size)
107
-
108
- # valid size from low to high
109
- [1, 100, 100**10].each do |size|
110
- fmt = Writeexcel::Format.new
111
- fmt.set_size(size)
112
- assert_equal(size, fmt.size, "valid size:#{size} - doesn't match.")
113
- end
114
-
115
- # invalid size -- size doesn't change
116
- [-1, 0, 1/2.0, 'hello', true, false, nil, [0,0], {:invalid => "val"}].each do |size|
117
- fmt = Writeexcel::Format.new
118
- default = fmt.size
119
- fmt.set_size(size)
120
- assert_equal(default, fmt.size, "size:#{size.inspect} doesn't match.")
121
- end
122
- end
123
-
124
- =begin
125
- set_color()
126
-
127
- Default state: Excels default color, usually black
128
- Default action: Set the default color
129
- Valid args: Integers from 8..63 or the following strings:
130
- 'black'
131
- 'blue'
132
- 'brown'
133
- 'cyan'
134
- 'gray'
135
- 'green'
136
- 'lime'
137
- 'magenta'
138
- 'navy'
139
- 'orange'
140
- 'pink'
141
- 'purple'
142
- 'red'
143
- 'silver'
144
- 'white'
145
- 'yellow'
146
-
147
- Set the font colour. The set_color() method is used as follows:
148
-
149
- format = workbook.add_format()
150
- format.set_color('red')
151
- worksheet.write(0, 0, 'wheelbarrow', format)
152
-
153
- Note: The set_color() method is used to set the colour of the font in a cell.
154
- To set the colour of a cell use the set_bg_color() and set_pattern() methods.
155
- =end
156
- def test_set_color
157
- # default state
158
- default_col = 0x7FFF
159
- assert_equal(default_col, @format.color)
160
-
161
- # valid color
162
- # set by string
163
- str_num = get_valid_color_string_number
164
- str_num.each do |str,num|
165
- fmt = Writeexcel::Format.new
166
- fmt.set_color(str)
167
- assert_equal(num, fmt.color)
168
- end
169
-
170
- # valid color
171
- # set by number
172
- [8, 36, 63].each do |color|
173
- fmt = Writeexcel::Format.new
174
- fmt.set_color(color)
175
- assert_equal(color, fmt.color)
176
- end
177
-
178
- # invalid color
179
- ['color', :col, -1, 63.5, 10*10].each do |color|
180
- fmt = Writeexcel::Format.new
181
- fmt.set_color(color)
182
- assert_equal(default_col, fmt.color, "color : #{color}")
183
- end
184
-
185
- # invalid color ...but...
186
- # 0 <= color < 8 then color += 8 in order to valid value
187
- [0, 7.5].each do |color|
188
- fmt = Writeexcel::Format.new
189
- fmt.set_color(color)
190
- assert_equal((color + 8).to_i, fmt.color, "color : #{color}")
191
- end
192
-
193
-
194
- end
195
-
196
- =begin
197
- set_bold()
198
-
199
- Default state: bold is off (internal value = 400)
200
- Default action: Turn bold on
201
- Valid args: 0, 1 [1]
202
-
203
- Set the bold property of the font:
204
-
205
- $format->set_bold(); # Turn bold on
206
-
207
- [1] Actually, values in the range 100..1000 are also valid.
208
- 400 is normal, 700 is bold and 1000 is very bold indeed.
209
- It is probably best to set the value to 1 and use normal bold.
210
- =end
211
-
212
- def test_set_bold
213
- # default state
214
- assert_equal(400, @format.bold)
215
-
216
- # valid weight
217
- fmt = Writeexcel::Format.new
218
- fmt.set_bold
219
- assert_equal(700, fmt.bold)
220
- {0 => 400, 1 => 700, 100 => 100, 1000 => 1000}.each do |weight, value|
221
- fmt = Writeexcel::Format.new
222
- fmt.set_bold(weight)
223
- assert_equal(value, fmt.bold)
224
- end
225
-
226
- # invalid weight
227
- [-1, 99, 1001, 'bold'].each do |weight|
228
- fmt = Writeexcel::Format.new
229
- fmt.set_bold(weight)
230
- assert_equal(400, fmt.bold, "weight : #{weight}")
231
- end
232
- end
233
-
234
- =begin
235
- set_italic()
236
-
237
- Default state: Italic is off
238
- Default action: Turn italic on
239
- Valid args: 0, 1
240
-
241
- Set the italic property of the font:
242
-
243
- format.set_italic() # Turn italic on
244
- =end
245
- def test_set_italic
246
- # default state
247
- assert_equal(0, @format.italic)
248
-
249
- # valid arg
250
- fmt = Writeexcel::Format.new
251
- fmt.set_italic
252
- assert_equal(1, fmt.italic)
253
- {0=>0, 1=>1}.each do |arg,value|
254
- fmt = Writeexcel::Format.new
255
- fmt.set_italic(arg)
256
- assert_equal(value, fmt.italic, "arg : #{arg}")
257
- end
258
-
259
- # invalid arg
260
- [-1, 0.2, 100, 'italic', true, false, nil].each do |arg|
261
- assert_raise(ArgumentError,
262
- "set_italic(#{arg}) : invalid arg. arg must be 0, 1 or none."){
263
- fmt = Writeexcel::Format.new
264
- fmt.set_italic(arg)
265
- }
266
- end
267
- end
268
-
269
- =begin
270
- set_underline()
271
-
272
- Default state: Underline is off
273
- Default action: Turn on single underline
274
- Valid args: 0 = No underline
275
- 1 = Single underline
276
- 2 = Double underline
277
- 33 = Single accounting underline
278
- 34 = Double accounting underline
279
-
280
- Set the underline property of the font.
281
-
282
- format.set_underline() # Single underline
283
- =end
284
- def test_set_underline
285
- # default state
286
- assert_equal(0, @format.underline, "default state")
287
-
288
- # valid args
289
- fmt = Writeexcel::Format.new
290
- fmt.set_underline
291
- assert_equal(1, fmt.underline, "No arg")
292
-
293
- [0, 1, 2, 33, 34].each do |arg|
294
- fmt = Writeexcel::Format.new
295
- fmt.set_underline(arg)
296
- assert_equal(arg, fmt.underline, "arg : #{arg}")
297
- end
298
-
299
- # invalid args
300
- [-1, 0.2, 100, 'under', true, false, nil].each do |arg|
301
- assert_raise(ArgumentError,
302
- "set_underline(#{arg}) : arg must be 0, 1 or none, 2, 33, 34."){
303
- fmt = Writeexcel::Format.new
304
- fmt.set_underline(arg)
305
- }
306
- end
307
- end
308
-
309
- =begin
310
- set_font_strikeout()
311
-
312
- Default state: Strikeout is off
313
- Default action: Turn strikeout on
314
- Valid args: 0, 1
315
-
316
- Set the strikeout property of the font.
317
- =end
318
- def test_set_font_strikeout
319
- # default state
320
- assert_equal(0, @format.font_strikeout, "default state")
321
-
322
- # valid args
323
- fmt = Writeexcel::Format.new
324
- fmt.set_font_strikeout
325
- assert_equal(1, fmt.font_strikeout, "No arg")
326
-
327
- [0, 1].each do |arg|
328
- fmt = Writeexcel::Format.new
329
- fmt.set_font_strikeout(arg)
330
- assert_equal(arg, fmt.font_strikeout, "arg : #{arg}")
331
- end
332
-
333
- # invalid args
334
- [-1, 0.2, 100, 'strikeout', true, false, nil].each do |arg|
335
- assert_raise(ArgumentError,
336
- "set_font_strikeout(#{arg}) : arg must be 0, 1 or none."){
337
- fmt = Writeexcel::Format.new
338
- fmt.set_font_strikeout(arg)
339
- }
340
- end
341
- end
342
-
343
- =begin
344
- set_font_script()
345
-
346
- Default state: Super/Subscript is off
347
- Default action: Turn Superscript on
348
- Valid args: 0 = Normal
349
- 1 = Superscript
350
- 2 = Subscript
351
-
352
- Set the superscript/subscript property of the font. This format is currently not very useful.
353
- =end
354
- def test_set_font_script
355
- # default state
356
- assert_equal(0, @format.font_script, "default state")
357
-
358
- # valid args
359
- fmt = Writeexcel::Format.new
360
- fmt.set_font_script
361
- assert_equal(1, fmt.font_script, "No arg")
362
-
363
- [0, 1, 2].each do |arg|
364
- fmt = Writeexcel::Format.new
365
- fmt.set_font_script(arg)
366
- assert_equal(arg, fmt.font_script, "arg : #{arg}")
367
- end
368
-
369
- # invalid args
370
- [-1, 0.2, 100, 'script', true, false, nil].each do |arg|
371
- assert_raise(ArgumentError,
372
- "set_font_script(#{arg}) : arg must be 0, 1 or none, or 2."){
373
- fmt = Writeexcel::Format.new
374
- fmt.set_font_script(arg)
375
- }
376
- end
377
-
378
- end
379
-
380
- =begin
381
- set_font_outline()
382
-
383
- Default state: Outline is off
384
- Default action: Turn outline on
385
- Valid args: 0, 1
386
-
387
- Macintosh only.
388
- =end
389
- def test_set_font_outline
390
- # default state
391
- assert_equal(0, @format.font_outline, "default state")
392
-
393
- # valid args
394
- fmt = Writeexcel::Format.new
395
- fmt.set_font_outline
396
- assert_equal(1, fmt.font_outline, "No arg")
397
-
398
- [0, 1].each do |arg|
399
- fmt = Writeexcel::Format.new
400
- fmt.set_font_outline(arg)
401
- assert_equal(arg, fmt.font_outline, "arg : #{arg}")
402
- end
403
-
404
- # invalid args
405
- [-1, 0.2, 100, 'outline', true, false, nil].each do |arg|
406
- assert_raise(ArgumentError,
407
- "set_font_outline(#{arg}) : arg must be 0, 1 or none."){
408
- fmt = Writeexcel::Format.new
409
- fmt.set_font_outline(arg)
410
- }
411
- end
412
- end
413
-
414
- =begin
415
- set_font_shadow()
416
-
417
- Default state: Shadow is off
418
- Default action: Turn shadow on
419
- Valid args: 0, 1
420
-
421
- Macintosh only.
422
- =end
423
- def test_set_font_shadow
424
- # default state
425
- assert_equal(0, @format.font_shadow, "default state")
426
-
427
- # valid args
428
- fmt = Writeexcel::Format.new
429
- fmt.set_font_shadow
430
- assert_equal(1, fmt.font_shadow, "No arg")
431
-
432
- [0, 1].each do |arg|
433
- fmt = Writeexcel::Format.new
434
- fmt.set_font_shadow(arg)
435
- assert_equal(arg, fmt.font_shadow, "arg : #{arg}")
436
- end
437
-
438
- # invalid args
439
- [-1, 0.2, 100, 'shadow', true, false, nil].each do |arg|
440
- assert_raise(ArgumentError,
441
- "set_font_shadow(#{arg}) : arg must be 0, 1 or none."){
442
- fmt = Writeexcel::Format.new
443
- fmt.set_font_shadow(arg)
444
- }
445
- end
446
- end
447
-
448
- =begin
449
- set_num_format()
450
-
451
- Default state: General format
452
- Default action: Format index 1
453
- Valid args: See the following table
454
-
455
- This method is used to define the numerical format of a number in Excel. It controls whether a number is displayed as an integer, a floating point number, a date, a currency value or some other user defined format.
456
- =end
457
- def test_set_num_format
458
- # default state
459
- assert_equal(0, @format.num_format)
460
-
461
- # Excel built in Format Index (0 .. 49)
462
- [0, 49].each do |n|
463
- fmt = Writeexcel::Format.new
464
- fmt.set_num_format(n)
465
- assert_equal(n, fmt.num_format, "n: #{n}")
466
- end
467
-
468
- # Format string
469
- ["#,##0", "m/d/yy", "hh:mm:ss"].each do |string|
470
- fmt = Writeexcel::Format.new
471
- fmt.set_num_format(string)
472
- assert_equal(string, fmt.num_format, "string: #{string}")
473
- end
474
- end
475
-
476
- =begin
477
- set_locked()
478
-
479
- Default state: Cell locking is on
480
- Default action: Turn locking on
481
- Valid args: 0, 1
482
-
483
- This property can be used to prevent modification of a cells
484
- contents. Following Excel's convention, cell locking is
485
- turned on by default. However, it only has an effect if
486
- the worksheet has been protected, see the worksheet protect()
487
- method.
488
-
489
- locked = workbook.add_format()
490
- locked.set_locked(1) # A non-op
491
-
492
- unlocked = workbook.add_format()
493
- locked.set_locked(0)
494
-
495
- # Enable worksheet protection
496
- worksheet.protect()
497
-
498
- # This cell cannot be edited.
499
- worksheet.write('A1', '=1+2', locked)
500
-
501
- # This cell can be edited.
502
- worksheet->write('A2', '=1+2', unlocked)
503
-
504
- Note: This offers weak protection even with a password,
505
- see the note in relation to the protect() method.
506
- =end
507
- def test_set_locked
508
- # default state
509
- assert_equal(1, @format.locked, "default state")
510
-
511
- # valid args
512
- fmt = Writeexcel::Format.new
513
- fmt.set_locked
514
- assert_equal(1, fmt.locked, "No arg")
515
-
516
- [0, 1].each do |arg|
517
- fmt = Writeexcel::Format.new
518
- fmt.set_locked(arg)
519
- assert_equal(arg, fmt.locked, "arg : #{arg}")
520
- end
521
-
522
- # invalid args
523
- [-1, 0.2, 100, 'locked', true, false, nil].each do |arg|
524
- assert_raise(ArgumentError,
525
- "set_font_shadow(#{arg}) : arg must be 0, 1 or none."){
526
- fmt = Writeexcel::Format.new
527
- fmt.set_locked(arg)
528
- }
529
- end
530
- end
531
- =begin
532
- set_hidden()
533
-
534
- Default state: Formula hiding is off
535
- Default action: Turn hiding on
536
- Valid args: 0, 1
537
-
538
- This property is used to hide a formula while still displaying
539
- its result. This is generally used to hide complex calculations
540
- from end users who are only interested in the result.
541
- It only has an effect if the worksheet has been protected,
542
- see the worksheet protect() method.
543
-
544
- my hidden = workbook.add_format()
545
- hidden.set_hidden()
546
-
547
- # Enable worksheet protection
548
- worksheet.protect()
549
-
550
- # The formula in this cell isn't visible
551
- worksheet.write('A1', '=1+2', hidden)
552
-
553
- Note: This offers weak protection even with a password,
554
- see the note in relation to the protect() method.
555
- =end
556
- def test_set_hidden
557
- # default state
558
- assert_equal(0, @format.hidden, "default state")
559
-
560
- # valid args
561
- fmt = Writeexcel::Format.new
562
- fmt.set_hidden
563
- assert_equal(1, fmt.hidden, "No arg")
564
-
565
- [0, 1].each do |arg|
566
- fmt = Writeexcel::Format.new
567
- fmt.set_hidden(arg)
568
- assert_equal(arg, fmt.hidden, "arg : #{arg}")
569
- end
570
-
571
- # invalid args
572
- [-1, 0.2, 100, 'hidden', true, false, nil].each do |arg|
573
- assert_raise(ArgumentError,
574
- "set_font_shadow(#{arg}) : arg must be 0, 1 or none."){
575
- fmt = Writeexcel::Format.new
576
- fmt.set_hidden(arg)
577
- }
578
- end
579
- end
580
-
581
- =begin
582
- set_align()
583
-
584
- Default state: Alignment is off
585
- Default action: Left alignment
586
- Valid args: 'left' Horizontal
587
- 'center'
588
- 'right'
589
- 'fill'
590
- 'justify'
591
- 'center_across'
592
-
593
- 'top' Vertical
594
- 'vcenter'
595
- 'bottom'
596
- 'vjustify'
597
-
598
- This method is used to set the horizontal and vertical text alignment
599
- within a cell. Vertical and horizontal alignments can be combined.
600
- The method is used as follows:
601
-
602
- my $format = $workbook->add_format();
603
- $format->set_align('center');
604
- $format->set_align('vcenter');
605
- $worksheet->set_row(0, 30);
606
- $worksheet->write(0, 0, 'X', $format);
607
-
608
- Text can be aligned across two or more adjacent cells using
609
- the center_across property. However, for genuine merged cells
610
- it is better to use the merge_range() worksheet method.
611
-
612
- The vjustify (vertical justify) option can be used to provide
613
- automatic text wrapping in a cell. The height of the cell will be
614
- adjusted to accommodate the wrapped text. To specify where the text
615
- wraps use the set_text_wrap() method.
616
- =end
617
- def test_set_align
618
- # default state
619
- assert_equal(0, @format.text_h_align)
620
- assert_equal(2, @format.text_v_align)
621
-
622
- # valid arg
623
- valid_args = {'left'=>1, 'center'=>2, 'centre'=>2, 'right'=>3,
624
- 'fill'=>4, 'justify'=>5, 'center_across'=>6,
625
- 'centre_across'=>6, 'merge'=>6,
626
- 'top'=>0, 'vcenter'=>1, 'vcentre'=>1, 'bottom'=>2,
627
- 'vjustify'=>3 }
628
- valid_args.each do |arg, value|
629
- fmt = Writeexcel::Format.new
630
- fmt.set_align(arg)
631
- case arg
632
- when 'left', 'center', 'centre', 'right', 'fill', 'justify',
633
- 'center_across', 'centre_across', 'merge'
634
- assert_equal(value, fmt.text_h_align, "arg: #{arg}")
635
- when 'top', 'vcenter', 'vcentre', 'bottom', 'vjustify'
636
- assert_equal(value, fmt.text_v_align, "arg: #{arg}")
637
- end
638
- end
639
-
640
- # invalid arg
641
- [-1, 0, 1.5, nil, true, false, ['left','top'], {'top'=>0}].each do |arg|
642
- fmt = Writeexcel::Format.new
643
- val = get_format_property(fmt)
644
- #print val.inspect
645
- #exit
646
- fmt.set_align(arg)
647
- assert_equal(val[:align], fmt.text_h_align, "arg: #{arg} - text_h_align changed.")
648
- assert_equal(val[:valign], fmt.text_v_align, "arg: #{arg} - text_v_align changed.")
649
- end
650
- end
651
-
652
- =begin
653
- set_center_across()
654
-
655
- Default state: Center across selection is off
656
- Default action: Turn center across on
657
- Valid args: 1
658
-
659
- Text can be aligned across two or more adjacent cells
660
- using the set_center_across() method. This is an alias
661
- for the set_align('center_across') method call.
662
-
663
- Only one cell should contain the text,
664
- the other cells should be blank:
665
-
666
- format = workbook.add_format()
667
- format.set_center_across()
668
-
669
- worksheet.write(1, 1, 'Center across selection', format)
670
- worksheet.write_blank(1, 2, format)
671
- =end
672
- def test_set_center_across
673
- # default state
674
- assert_equal(0, @format.text_h_align)
675
-
676
- # method call then center_across is set. if arg is none, numeric, string, whatever.
677
- @format.set_center_across
678
- assert_equal(6, @format.text_h_align)
679
- end
680
-
681
- =begin
682
- set_text_wrap()
683
-
684
- Default state: Text wrap is off
685
- Default action: Turn text wrap on
686
- Valid args: 0, 1
687
-
688
- Here is an example using the text wrap property, the escape
689
- character \n is used to indicate the end of line:
690
-
691
- format = workbook.add_format()
692
- format.set_text_wrap()
693
- worksheet.write(0, 0, "It's\na bum\nwrap", format)
694
- =end
695
- def test_set_text_wrap
696
- # default state
697
- assert_equal(0, @format.text_wrap, "default state")
698
-
699
- # valid args
700
- fmt = Writeexcel::Format.new
701
- fmt.set_text_wrap
702
- assert_equal(1, fmt.text_wrap, "No arg")
703
-
704
- [0, 1].each do |arg|
705
- fmt = Writeexcel::Format.new
706
- fmt.set_text_wrap(arg)
707
- assert_equal(arg, fmt.text_wrap, "arg : #{arg}")
708
- end
709
-
710
- # invalid args
711
- [-1, 0.2, 100, 'text_wrap', true, false, nil].each do |arg|
712
- assert_raise(ArgumentError,
713
- "set_text_wrap(#{arg}) : arg must be 0, 1 or none."){
714
- fmt = Writeexcel::Format.new
715
- fmt.set_text_wrap(arg)
716
- }
717
- end
718
- end
719
-
720
- =begin
721
- set_rotation()
722
-
723
- Default state: Text rotation is off
724
- Default action: None
725
- Valid args: Integers in the range -90 to 90 and 270
726
-
727
- Set the rotation of the text in a cell. The rotation can be
728
- any angle in the range -90 to 90 degrees.
729
-
730
- format = workbook.add_format()
731
- format.set_rotation(30)
732
- worksheet.write(0, 0, 'This text is rotated', format)
733
-
734
- The angle 270 is also supported. This indicates text where
735
- the letters run from top to bottom.
736
- =end
737
- def test_set_rotation
738
- # default state
739
- assert(0, @format.rotation)
740
-
741
- # # valid args -90 <= angle <= 90, 270 angle can be float or double
742
- # [-90.0, 89, 0, 89, 90, 270].each do |angle|
743
- # fmt = Writeexcel::Format.new
744
- # fmt.set_rotation(angle)
745
- # assert_equal(angle, fmt.rotation, "angle: #{angle}")
746
- # end
747
- end
748
-
749
- =begin
750
- set_indent()
751
-
752
- Default state: Text indentation is off
753
- Default action: Indent text 1 level
754
- Valid args: Positive integers
755
-
756
- This method can be used to indent text. The argument, which should
757
- be an integer, is taken as the level of indentation:
758
-
759
- format = workbook.add_format()
760
- format.set_indent(2)
761
- worksheet.write(0, 0, 'This text is indented', format)
762
-
763
- Indentation is a horizontal alignment property. It will override
764
- any other horizontal properties but it can be used in conjunction
765
- with vertical properties.
766
- =end
767
- def test_set_indent
768
- # default state
769
- assert_equal(0, @format.indent)
770
-
771
- # valid arg -- Positive integers
772
- [1, 10000000].each do |indent|
773
- fmt = Writeexcel::Format.new
774
- fmt.set_indent(indent)
775
- assert_equal(indent, fmt.indent, "indent: #{indent}")
776
- end
777
-
778
- # invalid arg
779
- [].each do |indent|
780
-
781
- end
782
- end
783
-
784
- =begin
785
- set_shrink()
786
-
787
- Default state: Text shrinking is off
788
- Default action: Turn "shrink to fit" on
789
- Valid args: 1
790
-
791
- This method can be used to shrink text so that it fits in a cell.
792
-
793
- format = workbook.add_format()
794
- format.set_shrink()
795
- worksheet.write(0, 0, 'Honey, I shrunk the text!', format)
796
- =end
797
- def test_set_shrink
798
- # default state
799
- assert_equal(0, @format.shrink)
800
- end
801
-
802
- =begin
803
- set_text_justlast()
804
-
805
- Default state: Justify last is off
806
- Default action: Turn justify last on
807
- Valid args: 0, 1
808
-
809
- Only applies to Far Eastern versions of Excel.
810
- =end
811
- def test_set_text_justlast
812
- # default state
813
- assert_equal(0, @format.text_justlast)
814
- end
815
-
816
- =begin
817
- set_pattern()
818
-
819
- Default state: Pattern is off
820
- Default action: Solid fill is on
821
- Valid args: 0 .. 18
822
-
823
- Set the background pattern of a cell.
824
- =end
825
- def test_set_pattern
826
- # default state
827
- assert_equal(0, @format.pattern)
828
- end
829
-
830
- =begin
831
- set_bg_color()
832
-
833
- Default state: Color is off
834
- Default action: Solid fill.
835
- Valid args: See set_color()
836
-
837
- The set_bg_color() method can be used to set the background
838
- colour of a pattern. Patterns are defined via the set_pattern()
839
- method. If a pattern hasn't been defined then a solid fill
840
- pattern is used as the default.
841
-
842
- Here is an example of how to set up a solid fill in a cell:
843
-
844
- format = workbook.add_format()
845
- format.set_pattern() # This is optional when using a solid fill
846
- format.set_bg_color('green')
847
- worksheet.write('A1', 'Ray', format)
848
- =end
849
- def test_set_bg_color
850
- end
851
-
852
- =begin
853
- set_fg_color()
854
-
855
- Default state: Color is off
856
- Default action: Solid fill.
857
- Valid args: See set_color()
858
-
859
- The set_fg_color() method can be used to set
860
- the foreground colour of a pattern.
861
- =end
862
- def test_set_fg_color
863
- end
864
-
865
- =begin
866
- set_border()
867
-
868
- Also applies to: set_bottom()
869
- set_top()
870
- set_left()
871
- set_right()
872
-
873
- Default state: Border is off
874
- Default action: Set border type 1
875
- Valid args: 0-13, See below.
876
-
877
- A cell border is comprised of a border on the bottom, top,
878
- left and right. These can be set to the same value using
879
- set_border() or individually using the relevant method
880
- calls shown above.
881
-
882
- The following shows the border styles sorted
883
- by WriteExcel index number:
884
-
885
- Index Name Weight Style
886
- ===== ============= ====== ===========
887
- 0 None 0
888
- 1 Continuous 1 -----------
889
- 2 Continuous 2 -----------
890
- 3 Dash 1 - - - - - -
891
- 4 Dot 1 . . . . . .
892
- 5 Continuous 3 -----------
893
- 6 Double 3 ===========
894
- 7 Continuous 0 -----------
895
- 8 Dash 2 - - - - - -
896
- 9 Dash Dot 1 - . - . - .
897
- 10 Dash Dot 2 - . - . - .
898
- 11 Dash Dot Dot 1 - . . - . .
899
- 12 Dash Dot Dot 2 - . . - . .
900
- 13 SlantDash Dot 2 / - . / - .
901
-
902
- The following shows the borders sorted by style:
903
-
904
- Name Weight Style Index
905
- ============= ====== =========== =====
906
- Continuous 0 ----------- 7
907
- Continuous 1 ----------- 1
908
- Continuous 2 ----------- 2
909
- Continuous 3 ----------- 5
910
- Dash 1 - - - - - - 3
911
- Dash 2 - - - - - - 8
912
- Dash Dot 1 - . - . - . 9
913
- Dash Dot 2 - . - . - . 10
914
- Dash Dot Dot 1 - . . - . . 11
915
- Dash Dot Dot 2 - . . - . . 12
916
- Dot 1 . . . . . . 4
917
- Double 3 =========== 6
918
- None 0 0
919
- SlantDash Dot 2 / - . / - . 13
920
-
921
- The following shows the borders in the order shown in the Excel Dialog.
922
-
923
- Index Style Index Style
924
- ===== ===== ===== =====
925
- 0 None 12 - . . - . .
926
- 7 ----------- 13 / - . / - .
927
- 4 . . . . . . 10 - . - . - .
928
- 11 - . . - . . 8 - - - - - -
929
- 9 - . - . - . 2 -----------
930
- 3 - - - - - - 5 -----------
931
- 1 ----------- 6 ===========
932
- =end
933
- def test_set_border
934
- end
935
-
936
- =begin
937
- set_border_color()
938
-
939
- Also applies to: set_bottom_color()
940
- set_top_color()
941
- set_left_color()
942
- set_right_color()
943
-
944
- Default state: Color is off
945
- Default action: Undefined
946
- Valid args: See set_color()
947
-
948
- Set the colour of the cell borders. A cell border is comprised of a border
949
- on the bottom, top, left and right. These can be set to the same colour
950
- using set_border_color() or individually using the relevant method
951
- calls shown above.
952
- =end
953
- def test_set_border_color
954
- end
955
-
956
- =begin
957
- copy($format)
958
-
959
- This method is used to copy all of the properties
960
- from one Format object to another:
961
-
962
- lorry1 = workbook.add_format()
963
- lorry1.set_bold()
964
- lorry1.set_italic()
965
- lorry1.set_color('red') # lorry1 is bold, italic and red
966
-
967
- my lorry2 = workbook.add_format()
968
- lorry2.copy(lorry1)
969
- lorry2.set_color('yellow') # lorry2 is bold, italic and yellow
970
-
971
- The copy() method is only useful if you are using the method interface
972
- to Format properties. It generally isn't required if you are setting
973
- Format properties directly using hashes.
974
-
975
- Note: this is not a copy constructor, both objects must exist prior to copying.
976
- =end
977
-
978
- def test_xf_biff_size
979
- perl_file = "#{PERL_OUTDIR}/file_xf_biff"
980
- size = File.size(perl_file)
981
- @ruby_file.print(@format.get_xf)
982
- rsize = @ruby_file.size
983
- assert_equal(size, rsize, "File sizes not the same")
984
- end
985
-
986
- # Because of the modifications to bg_color and fg_color, I know this
987
- # test will fail. This is ok.
988
- #def test_xf_biff_contents
989
- # perl_file = "perl_output/f_xf_biff"
990
- # @fh = File.new(@ruby_file,"w+")
991
- # @fh.print(@format.xf_biff)
992
- # @fh.close
993
- # contents = IO.readlines(perl_file)
994
- # rcontents = IO.readlines(@ruby_file)
995
- # assert_equal(contents,rcontents,"Contents not the same")
996
- #end
997
-
998
- def test_font_biff_size
999
- perl_file = "#{PERL_OUTDIR}/file_font_biff"
1000
- @ruby_file.print(@format.get_font)
1001
- contents = IO.readlines(perl_file)
1002
- @ruby_file.rewind
1003
- rcontents = @ruby_file.readlines
1004
- assert_equal(contents, rcontents, "Contents not the same")
1005
- end
1006
-
1007
- def test_font_biff_contents
1008
- perl_file = "#{PERL_OUTDIR}/file_font_biff"
1009
- @ruby_file.print(@format.get_font)
1010
- contents = IO.readlines(perl_file)
1011
- @ruby_file.rewind
1012
- rcontents = @ruby_file.readlines
1013
- assert_equal(contents, rcontents, "Contents not the same")
1014
- end
1015
-
1016
- def test_get_font_key_size
1017
- perl_file = "#{PERL_OUTDIR}/file_font_key"
1018
- @ruby_file.print(@format.get_font_key)
1019
- assert_equal(File.size(perl_file), @ruby_file.size, "Bad file size")
1020
- end
1021
-
1022
- def test_get_font_key_contents
1023
- perl_file = "#{PERL_OUTDIR}/file_font_key"
1024
- @ruby_file.print(@format.get_font_key)
1025
- contents = IO.readlines(perl_file)
1026
- @ruby_file.rewind
1027
- rcontents = @ruby_file.readlines
1028
- assert_equal(contents, rcontents, "Contents not the same")
1029
- end
1030
-
1031
- def test_initialize
1032
- assert_nothing_raised {
1033
- Writeexcel::Format.new(
1034
- :bold => true,
1035
- :size => 10,
1036
- :color => 'black',
1037
- :fg_color => 43,
1038
- :align => 'top',
1039
- :text_wrap => true,
1040
- :border => 1
1041
- )
1042
- }
1043
- end
1044
-
1045
- # added by Nakamura
1046
-
1047
- def test_get_xf
1048
- perl_file = "#{PERL_OUTDIR}/file_xf_biff"
1049
- size = File.size(perl_file)
1050
- @ruby_file.print(@format.get_xf)
1051
- rsize = @ruby_file.size
1052
- assert_equal(size, rsize, "File sizes not the same")
1053
-
1054
- compare_file(perl_file, @ruby_file)
1055
- end
1056
-
1057
- def test_get_font
1058
- perl_file = "#{PERL_OUTDIR}/file_font_biff"
1059
- size = File.size(perl_file)
1060
- @ruby_file.print(@format.get_font)
1061
- rsize = @ruby_file.size
1062
- assert_equal(size, rsize, "File sizes not the same")
1063
-
1064
- compare_file(perl_file, @ruby_file)
1065
- end
1066
-
1067
- def test_get_font_key
1068
- perl_file = "#{PERL_OUTDIR}/file_font_key"
1069
- size = File.size(perl_file)
1070
- @ruby_file.print(@format.get_font_key)
1071
- rsize = @ruby_file.size
1072
- assert_equal(size, rsize, "File sizes not the same")
1073
-
1074
- compare_file(perl_file, @ruby_file)
1075
- end
1076
-
1077
- def test_copy
1078
- format1 = Writeexcel::Format.new
1079
- format2 = Writeexcel::Format.new
1080
-
1081
- format1.set_size(12)
1082
-
1083
- format2.copy(format1)
1084
-
1085
- assert_equal(format1.size, format2.size)
1086
- end
1087
-
1088
-
1089
- # -----------------------------------------------------------------------------
1090
-
1091
- def get_valid_format_properties
1092
- {
1093
- :font => 'Times New Roman',
1094
- :size => 30,
1095
- :color => 8,
1096
- :italic => 1,
1097
- :underline => 1,
1098
- :font_strikeout => 1,
1099
- :font_script => 1,
1100
- :font_outline => 1,
1101
- :font_shadow => 1,
1102
- :locked => 0,
1103
- :hidden => 1,
1104
- :text_wrap => 1,
1105
- :text_justlast => 1,
1106
- :indent => 2,
1107
- :shrink => 1,
1108
- :pattern => 18,
1109
- :bg_color => 30,
1110
- :fg_color => 63
1111
- }
1112
- end
1113
-
1114
- def get_valid_color_string_number
1115
- {
1116
- :black => 8,
1117
- :blue => 12,
1118
- :brown => 16,
1119
- :cyan => 15,
1120
- :gray => 23,
1121
- :green => 17,
1122
- :lime => 11,
1123
- :magenta => 14,
1124
- :navy => 18,
1125
- :orange => 53,
1126
- :pink => 33,
1127
- :purple => 20,
1128
- :red => 10,
1129
- :silver => 22,
1130
- :white => 9,
1131
- :yellow => 13
1132
- }
1133
- end
1134
- # :rotation => -90,
1135
- # :center_across => 1,
1136
- # :align => 'left',
1137
-
1138
- def get_format_property(format)
1139
- text_h_align = {
1140
- 1 => 'left',
1141
- 2 => 'center/centre',
1142
- 3 => 'right',
1143
- 4 => 'fill',
1144
- 5 => 'justiry',
1145
- 6 => 'center_across/centre_across/merge',
1146
- 7 => 'distributed/equal_space'
1147
- }
1148
-
1149
- text_v_align = {
1150
- 0 => 'top',
1151
- 1 => 'vcenter/vcentre',
1152
- 2 => 'bottom',
1153
- 3 => 'vjustify',
1154
- 4 => 'vdistributed/vequal_space'
1155
- }
1156
-
1157
- return {
1158
- :font => format.font,
1159
- :size => format.size,
1160
- :color => format.color,
1161
- :bold => format.bold,
1162
- :italic => format.italic,
1163
- :underline => format.underline,
1164
- :font_strikeout => format.font_strikeout,
1165
- :font_script => format.font_script,
1166
- :font_outline => format.font_outline,
1167
- :font_shadow => format.font_shadow,
1168
- :num_format => format.num_format,
1169
- :locked => format.locked,
1170
- :hidden => format.hidden,
1171
- :align => format.text_h_align,
1172
- :valign => format.text_v_align,
1173
- :rotation => format.rotation,
1174
- :text_wrap => format.text_wrap,
1175
- :text_justlast => format.text_justlast,
1176
- :center_across => format.text_h_align,
1177
- :indent => format.indent,
1178
- :shrink => format.shrink,
1179
- :pattern => format.pattern,
1180
- :bg_color => format.bg_color,
1181
- :fg_color => format.fg_color,
1182
- :bottom => format.bottom,
1183
- :top => format.top,
1184
- :left => format.left,
1185
- :right => format.right,
1186
- :bottom_color => format.bottom_color,
1187
- :top_color => format.top_color,
1188
- :left_color => format.left_color,
1189
- :right_color => format.right_color
1190
- }
1191
- end
1192
- end
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+ require 'stringio'
4
+
5
+ class TC_Format < Test::Unit::TestCase
6
+
7
+ TEST_DIR = File.expand_path(File.dirname(__FILE__))
8
+ PERL_OUTDIR = File.join(TEST_DIR, 'perl_output')
9
+
10
+ def setup
11
+ @ruby_file = StringIO.new
12
+ @format = Writeexcel::Format.new
13
+ end
14
+
15
+ def teardown
16
+ end
17
+
18
+ def test_set_format_properties
19
+ end
20
+
21
+ def test_format_properties_with_valid_value
22
+ # set_format_properties( propty => val )
23
+ valid_properties = get_valid_format_properties
24
+ valid_properties.each do |k,v|
25
+ fmt = Writeexcel::Format.new
26
+ before = get_format_property(fmt)
27
+ fmt.set_format_properties(k => v)
28
+ after = get_format_property(fmt)
29
+ after.delete_if {|key, val| before[key] == val }
30
+ assert_equal(1, after.size, "change 1 property[:#{k}] but #{after.size} was changed.#{after.inspect}")
31
+ assert_equal(v, after[k], "[:#{k}] doesn't match.")
32
+ end
33
+
34
+ # set_format_properties( propty_1 => val1, propty_2 => val2)
35
+ valid_properties.each do |k,v|
36
+ fmt = Writeexcel::Format.new
37
+ before = get_format_property(fmt)
38
+ fmt.set_format_properties(k => v, :bold => 1)
39
+ after = get_format_property(fmt)
40
+ after.delete_if {|key, val| before[key] == val }
41
+ assert_equal(2, after.size, "change 1 property[:#{k}] but #{after.size} was changed.#{after.inspect}")
42
+ assert_equal(v, after[k], "[:#{k}] doesn't match.")
43
+ assert_equal(700, after[:bold])
44
+ end
45
+
46
+ # set_format_properties( hash_variable )
47
+ valid_properties = get_valid_format_properties
48
+ valid_properties.each do |k,v|
49
+ arg = {k => v}
50
+ fmt = Writeexcel::Format.new
51
+ before = get_format_property(fmt)
52
+ fmt.set_format_properties(arg)
53
+ after = get_format_property(fmt)
54
+ after.delete_if {|key, val| before[key] == val }
55
+ assert_equal(1, after.size, "change 1 property[:#{k}] but #{after.size} was changed.#{after.inspect}")
56
+ assert_equal(v, after[k], "[:#{k}] doesn't match.")
57
+ end
58
+
59
+ # set_format_properties( hash_variable, hash_variable... )
60
+ valid_properties = get_valid_format_properties
61
+ valid_properties.each do |k,v|
62
+ arg = {k => v}
63
+ arg2 = {:bold => 1}
64
+ fmt = Writeexcel::Format.new
65
+ before = get_format_property(fmt)
66
+ fmt.set_format_properties(arg, arg2)
67
+ after = get_format_property(fmt)
68
+ after.delete_if {|key, val| before[key] == val }
69
+ assert_equal(2, after.size, "change 1 property[:#{k}] but #{after.size} was changed.#{after.inspect}")
70
+ assert_equal(v, after[k], "[:#{k}] doesn't match.")
71
+ assert_equal(700, after[:bold])
72
+ end
73
+
74
+ # set_color by string
75
+ valid_color_string_number = get_valid_color_string_number
76
+ [:color , :bg_color, :fg_color].each do |coltype|
77
+ valid_color_string_number.each do |str, num|
78
+ fmt = Writeexcel::Format.new
79
+ before = get_format_property(fmt)
80
+ fmt.set_format_properties(coltype => str)
81
+ after = get_format_property(fmt)
82
+ after.delete_if {|key, val| before[key] == val }
83
+ assert_equal(1, after.size, "change 1 property[:#{coltype}:#{str}] but #{after.size} was changed.#{after.inspect}")
84
+ assert_equal(num, after[:"#{coltype}"], "[:#{coltype}:#{str}] doesn't match.")
85
+ end
86
+ end
87
+
88
+
89
+ end
90
+
91
+ def test_format_properties_with_invalid_value
92
+ end
93
+
94
+ def test_set_font
95
+ end
96
+
97
+ =begin
98
+ set_size()
99
+ Default state: Font size is 10
100
+ Default action: Set font size to 1
101
+ Valid args: Integer values from 1 to as big as your screen.
102
+ Set the font size. Excel adjusts the height of a row to accommodate the largest font size in the row. You can also explicitly specify the height of a row using the set_row() worksheet method.
103
+ =end
104
+ def test_set_size
105
+ # default state
106
+ assert_equal(10, @format.size)
107
+
108
+ # valid size from low to high
109
+ [1, 100, 100**10].each do |size|
110
+ fmt = Writeexcel::Format.new
111
+ fmt.set_size(size)
112
+ assert_equal(size, fmt.size, "valid size:#{size} - doesn't match.")
113
+ end
114
+
115
+ # invalid size -- size doesn't change
116
+ [-1, 0, 1/2.0, 'hello', true, false, nil, [0,0], {:invalid => "val"}].each do |size|
117
+ fmt = Writeexcel::Format.new
118
+ default = fmt.size
119
+ fmt.set_size(size)
120
+ assert_equal(default, fmt.size, "size:#{size.inspect} doesn't match.")
121
+ end
122
+ end
123
+
124
+ =begin
125
+ set_color()
126
+
127
+ Default state: Excels default color, usually black
128
+ Default action: Set the default color
129
+ Valid args: Integers from 8..63 or the following strings:
130
+ 'black'
131
+ 'blue'
132
+ 'brown'
133
+ 'cyan'
134
+ 'gray'
135
+ 'green'
136
+ 'lime'
137
+ 'magenta'
138
+ 'navy'
139
+ 'orange'
140
+ 'pink'
141
+ 'purple'
142
+ 'red'
143
+ 'silver'
144
+ 'white'
145
+ 'yellow'
146
+
147
+ Set the font colour. The set_color() method is used as follows:
148
+
149
+ format = workbook.add_format()
150
+ format.set_color('red')
151
+ worksheet.write(0, 0, 'wheelbarrow', format)
152
+
153
+ Note: The set_color() method is used to set the colour of the font in a cell.
154
+ To set the colour of a cell use the set_bg_color() and set_pattern() methods.
155
+ =end
156
+ def test_set_color
157
+ # default state
158
+ default_col = 0x7FFF
159
+ assert_equal(default_col, @format.color)
160
+
161
+ # valid color
162
+ # set by string
163
+ str_num = get_valid_color_string_number
164
+ str_num.each do |str,num|
165
+ fmt = Writeexcel::Format.new
166
+ fmt.set_color(str)
167
+ assert_equal(num, fmt.color)
168
+ end
169
+
170
+ # valid color
171
+ # set by number
172
+ [8, 36, 63].each do |color|
173
+ fmt = Writeexcel::Format.new
174
+ fmt.set_color(color)
175
+ assert_equal(color, fmt.color)
176
+ end
177
+
178
+ # invalid color
179
+ ['color', :col, -1, 63.5, 10*10].each do |color|
180
+ fmt = Writeexcel::Format.new
181
+ fmt.set_color(color)
182
+ assert_equal(default_col, fmt.color, "color : #{color}")
183
+ end
184
+
185
+ # invalid color ...but...
186
+ # 0 <= color < 8 then color += 8 in order to valid value
187
+ [0, 7.5].each do |color|
188
+ fmt = Writeexcel::Format.new
189
+ fmt.set_color(color)
190
+ assert_equal((color + 8).to_i, fmt.color, "color : #{color}")
191
+ end
192
+
193
+
194
+ end
195
+
196
+ =begin
197
+ set_bold()
198
+
199
+ Default state: bold is off (internal value = 400)
200
+ Default action: Turn bold on
201
+ Valid args: 0, 1 [1]
202
+
203
+ Set the bold property of the font:
204
+
205
+ $format->set_bold(); # Turn bold on
206
+
207
+ [1] Actually, values in the range 100..1000 are also valid.
208
+ 400 is normal, 700 is bold and 1000 is very bold indeed.
209
+ It is probably best to set the value to 1 and use normal bold.
210
+ =end
211
+
212
+ def test_set_bold
213
+ # default state
214
+ assert_equal(400, @format.bold)
215
+
216
+ # valid weight
217
+ fmt = Writeexcel::Format.new
218
+ fmt.set_bold
219
+ assert_equal(700, fmt.bold)
220
+ {0 => 400, 1 => 700, 100 => 100, 1000 => 1000}.each do |weight, value|
221
+ fmt = Writeexcel::Format.new
222
+ fmt.set_bold(weight)
223
+ assert_equal(value, fmt.bold)
224
+ end
225
+
226
+ # invalid weight
227
+ [-1, 99, 1001, 'bold'].each do |weight|
228
+ fmt = Writeexcel::Format.new
229
+ fmt.set_bold(weight)
230
+ assert_equal(400, fmt.bold, "weight : #{weight}")
231
+ end
232
+ end
233
+
234
+ =begin
235
+ set_italic()
236
+
237
+ Default state: Italic is off
238
+ Default action: Turn italic on
239
+ Valid args: 0, 1
240
+
241
+ Set the italic property of the font:
242
+
243
+ format.set_italic() # Turn italic on
244
+ =end
245
+ def test_set_italic
246
+ # default state
247
+ assert_equal(0, @format.italic)
248
+
249
+ # valid arg
250
+ fmt = Writeexcel::Format.new
251
+ fmt.set_italic
252
+ assert_equal(1, fmt.italic)
253
+ {0=>0, 1=>1}.each do |arg,value|
254
+ fmt = Writeexcel::Format.new
255
+ fmt.set_italic(arg)
256
+ assert_equal(value, fmt.italic, "arg : #{arg}")
257
+ end
258
+
259
+ # invalid arg
260
+ [-1, 0.2, 100, 'italic', true, false, nil].each do |arg|
261
+ assert_raise(ArgumentError,
262
+ "set_italic(#{arg}) : invalid arg. arg must be 0, 1 or none."){
263
+ fmt = Writeexcel::Format.new
264
+ fmt.set_italic(arg)
265
+ }
266
+ end
267
+ end
268
+
269
+ =begin
270
+ set_underline()
271
+
272
+ Default state: Underline is off
273
+ Default action: Turn on single underline
274
+ Valid args: 0 = No underline
275
+ 1 = Single underline
276
+ 2 = Double underline
277
+ 33 = Single accounting underline
278
+ 34 = Double accounting underline
279
+
280
+ Set the underline property of the font.
281
+
282
+ format.set_underline() # Single underline
283
+ =end
284
+ def test_set_underline
285
+ # default state
286
+ assert_equal(0, @format.underline, "default state")
287
+
288
+ # valid args
289
+ fmt = Writeexcel::Format.new
290
+ fmt.set_underline
291
+ assert_equal(1, fmt.underline, "No arg")
292
+
293
+ [0, 1, 2, 33, 34].each do |arg|
294
+ fmt = Writeexcel::Format.new
295
+ fmt.set_underline(arg)
296
+ assert_equal(arg, fmt.underline, "arg : #{arg}")
297
+ end
298
+
299
+ # invalid args
300
+ [-1, 0.2, 100, 'under', true, false, nil].each do |arg|
301
+ assert_raise(ArgumentError,
302
+ "set_underline(#{arg}) : arg must be 0, 1 or none, 2, 33, 34."){
303
+ fmt = Writeexcel::Format.new
304
+ fmt.set_underline(arg)
305
+ }
306
+ end
307
+ end
308
+
309
+ =begin
310
+ set_font_strikeout()
311
+
312
+ Default state: Strikeout is off
313
+ Default action: Turn strikeout on
314
+ Valid args: 0, 1
315
+
316
+ Set the strikeout property of the font.
317
+ =end
318
+ def test_set_font_strikeout
319
+ # default state
320
+ assert_equal(0, @format.font_strikeout, "default state")
321
+
322
+ # valid args
323
+ fmt = Writeexcel::Format.new
324
+ fmt.set_font_strikeout
325
+ assert_equal(1, fmt.font_strikeout, "No arg")
326
+
327
+ [0, 1].each do |arg|
328
+ fmt = Writeexcel::Format.new
329
+ fmt.set_font_strikeout(arg)
330
+ assert_equal(arg, fmt.font_strikeout, "arg : #{arg}")
331
+ end
332
+
333
+ # invalid args
334
+ [-1, 0.2, 100, 'strikeout', true, false, nil].each do |arg|
335
+ assert_raise(ArgumentError,
336
+ "set_font_strikeout(#{arg}) : arg must be 0, 1 or none."){
337
+ fmt = Writeexcel::Format.new
338
+ fmt.set_font_strikeout(arg)
339
+ }
340
+ end
341
+ end
342
+
343
+ =begin
344
+ set_font_script()
345
+
346
+ Default state: Super/Subscript is off
347
+ Default action: Turn Superscript on
348
+ Valid args: 0 = Normal
349
+ 1 = Superscript
350
+ 2 = Subscript
351
+
352
+ Set the superscript/subscript property of the font. This format is currently not very useful.
353
+ =end
354
+ def test_set_font_script
355
+ # default state
356
+ assert_equal(0, @format.font_script, "default state")
357
+
358
+ # valid args
359
+ fmt = Writeexcel::Format.new
360
+ fmt.set_font_script
361
+ assert_equal(1, fmt.font_script, "No arg")
362
+
363
+ [0, 1, 2].each do |arg|
364
+ fmt = Writeexcel::Format.new
365
+ fmt.set_font_script(arg)
366
+ assert_equal(arg, fmt.font_script, "arg : #{arg}")
367
+ end
368
+
369
+ # invalid args
370
+ [-1, 0.2, 100, 'script', true, false, nil].each do |arg|
371
+ assert_raise(ArgumentError,
372
+ "set_font_script(#{arg}) : arg must be 0, 1 or none, or 2."){
373
+ fmt = Writeexcel::Format.new
374
+ fmt.set_font_script(arg)
375
+ }
376
+ end
377
+
378
+ end
379
+
380
+ =begin
381
+ set_font_outline()
382
+
383
+ Default state: Outline is off
384
+ Default action: Turn outline on
385
+ Valid args: 0, 1
386
+
387
+ Macintosh only.
388
+ =end
389
+ def test_set_font_outline
390
+ # default state
391
+ assert_equal(0, @format.font_outline, "default state")
392
+
393
+ # valid args
394
+ fmt = Writeexcel::Format.new
395
+ fmt.set_font_outline
396
+ assert_equal(1, fmt.font_outline, "No arg")
397
+
398
+ [0, 1].each do |arg|
399
+ fmt = Writeexcel::Format.new
400
+ fmt.set_font_outline(arg)
401
+ assert_equal(arg, fmt.font_outline, "arg : #{arg}")
402
+ end
403
+
404
+ # invalid args
405
+ [-1, 0.2, 100, 'outline', true, false, nil].each do |arg|
406
+ assert_raise(ArgumentError,
407
+ "set_font_outline(#{arg}) : arg must be 0, 1 or none."){
408
+ fmt = Writeexcel::Format.new
409
+ fmt.set_font_outline(arg)
410
+ }
411
+ end
412
+ end
413
+
414
+ =begin
415
+ set_font_shadow()
416
+
417
+ Default state: Shadow is off
418
+ Default action: Turn shadow on
419
+ Valid args: 0, 1
420
+
421
+ Macintosh only.
422
+ =end
423
+ def test_set_font_shadow
424
+ # default state
425
+ assert_equal(0, @format.font_shadow, "default state")
426
+
427
+ # valid args
428
+ fmt = Writeexcel::Format.new
429
+ fmt.set_font_shadow
430
+ assert_equal(1, fmt.font_shadow, "No arg")
431
+
432
+ [0, 1].each do |arg|
433
+ fmt = Writeexcel::Format.new
434
+ fmt.set_font_shadow(arg)
435
+ assert_equal(arg, fmt.font_shadow, "arg : #{arg}")
436
+ end
437
+
438
+ # invalid args
439
+ [-1, 0.2, 100, 'shadow', true, false, nil].each do |arg|
440
+ assert_raise(ArgumentError,
441
+ "set_font_shadow(#{arg}) : arg must be 0, 1 or none."){
442
+ fmt = Writeexcel::Format.new
443
+ fmt.set_font_shadow(arg)
444
+ }
445
+ end
446
+ end
447
+
448
+ =begin
449
+ set_num_format()
450
+
451
+ Default state: General format
452
+ Default action: Format index 1
453
+ Valid args: See the following table
454
+
455
+ This method is used to define the numerical format of a number in Excel. It controls whether a number is displayed as an integer, a floating point number, a date, a currency value or some other user defined format.
456
+ =end
457
+ def test_set_num_format
458
+ # default state
459
+ assert_equal(0, @format.num_format)
460
+
461
+ # Excel built in Format Index (0 .. 49)
462
+ [0, 49].each do |n|
463
+ fmt = Writeexcel::Format.new
464
+ fmt.set_num_format(n)
465
+ assert_equal(n, fmt.num_format, "n: #{n}")
466
+ end
467
+
468
+ # Format string
469
+ ["#,##0", "m/d/yy", "hh:mm:ss"].each do |string|
470
+ fmt = Writeexcel::Format.new
471
+ fmt.set_num_format(string)
472
+ assert_equal(string, fmt.num_format, "string: #{string}")
473
+ end
474
+ end
475
+
476
+ =begin
477
+ set_locked()
478
+
479
+ Default state: Cell locking is on
480
+ Default action: Turn locking on
481
+ Valid args: 0, 1
482
+
483
+ This property can be used to prevent modification of a cells
484
+ contents. Following Excel's convention, cell locking is
485
+ turned on by default. However, it only has an effect if
486
+ the worksheet has been protected, see the worksheet protect()
487
+ method.
488
+
489
+ locked = workbook.add_format()
490
+ locked.set_locked(1) # A non-op
491
+
492
+ unlocked = workbook.add_format()
493
+ locked.set_locked(0)
494
+
495
+ # Enable worksheet protection
496
+ worksheet.protect()
497
+
498
+ # This cell cannot be edited.
499
+ worksheet.write('A1', '=1+2', locked)
500
+
501
+ # This cell can be edited.
502
+ worksheet->write('A2', '=1+2', unlocked)
503
+
504
+ Note: This offers weak protection even with a password,
505
+ see the note in relation to the protect() method.
506
+ =end
507
+ def test_set_locked
508
+ # default state
509
+ assert_equal(1, @format.locked, "default state")
510
+
511
+ # valid args
512
+ fmt = Writeexcel::Format.new
513
+ fmt.set_locked
514
+ assert_equal(1, fmt.locked, "No arg")
515
+
516
+ [0, 1].each do |arg|
517
+ fmt = Writeexcel::Format.new
518
+ fmt.set_locked(arg)
519
+ assert_equal(arg, fmt.locked, "arg : #{arg}")
520
+ end
521
+
522
+ # invalid args
523
+ [-1, 0.2, 100, 'locked', true, false, nil].each do |arg|
524
+ assert_raise(ArgumentError,
525
+ "set_font_shadow(#{arg}) : arg must be 0, 1 or none."){
526
+ fmt = Writeexcel::Format.new
527
+ fmt.set_locked(arg)
528
+ }
529
+ end
530
+ end
531
+ =begin
532
+ set_hidden()
533
+
534
+ Default state: Formula hiding is off
535
+ Default action: Turn hiding on
536
+ Valid args: 0, 1
537
+
538
+ This property is used to hide a formula while still displaying
539
+ its result. This is generally used to hide complex calculations
540
+ from end users who are only interested in the result.
541
+ It only has an effect if the worksheet has been protected,
542
+ see the worksheet protect() method.
543
+
544
+ my hidden = workbook.add_format()
545
+ hidden.set_hidden()
546
+
547
+ # Enable worksheet protection
548
+ worksheet.protect()
549
+
550
+ # The formula in this cell isn't visible
551
+ worksheet.write('A1', '=1+2', hidden)
552
+
553
+ Note: This offers weak protection even with a password,
554
+ see the note in relation to the protect() method.
555
+ =end
556
+ def test_set_hidden
557
+ # default state
558
+ assert_equal(0, @format.hidden, "default state")
559
+
560
+ # valid args
561
+ fmt = Writeexcel::Format.new
562
+ fmt.set_hidden
563
+ assert_equal(1, fmt.hidden, "No arg")
564
+
565
+ [0, 1].each do |arg|
566
+ fmt = Writeexcel::Format.new
567
+ fmt.set_hidden(arg)
568
+ assert_equal(arg, fmt.hidden, "arg : #{arg}")
569
+ end
570
+
571
+ # invalid args
572
+ [-1, 0.2, 100, 'hidden', true, false, nil].each do |arg|
573
+ assert_raise(ArgumentError,
574
+ "set_font_shadow(#{arg}) : arg must be 0, 1 or none."){
575
+ fmt = Writeexcel::Format.new
576
+ fmt.set_hidden(arg)
577
+ }
578
+ end
579
+ end
580
+
581
+ =begin
582
+ set_align()
583
+
584
+ Default state: Alignment is off
585
+ Default action: Left alignment
586
+ Valid args: 'left' Horizontal
587
+ 'center'
588
+ 'right'
589
+ 'fill'
590
+ 'justify'
591
+ 'center_across'
592
+
593
+ 'top' Vertical
594
+ 'vcenter'
595
+ 'bottom'
596
+ 'vjustify'
597
+
598
+ This method is used to set the horizontal and vertical text alignment
599
+ within a cell. Vertical and horizontal alignments can be combined.
600
+ The method is used as follows:
601
+
602
+ my $format = $workbook->add_format();
603
+ $format->set_align('center');
604
+ $format->set_align('vcenter');
605
+ $worksheet->set_row(0, 30);
606
+ $worksheet->write(0, 0, 'X', $format);
607
+
608
+ Text can be aligned across two or more adjacent cells using
609
+ the center_across property. However, for genuine merged cells
610
+ it is better to use the merge_range() worksheet method.
611
+
612
+ The vjustify (vertical justify) option can be used to provide
613
+ automatic text wrapping in a cell. The height of the cell will be
614
+ adjusted to accommodate the wrapped text. To specify where the text
615
+ wraps use the set_text_wrap() method.
616
+ =end
617
+ def test_set_align
618
+ # default state
619
+ assert_equal(0, @format.text_h_align)
620
+ assert_equal(2, @format.text_v_align)
621
+
622
+ # valid arg
623
+ valid_args = {'left'=>1, 'center'=>2, 'centre'=>2, 'right'=>3,
624
+ 'fill'=>4, 'justify'=>5, 'center_across'=>6,
625
+ 'centre_across'=>6, 'merge'=>6,
626
+ 'top'=>0, 'vcenter'=>1, 'vcentre'=>1, 'bottom'=>2,
627
+ 'vjustify'=>3 }
628
+ valid_args.each do |arg, value|
629
+ fmt = Writeexcel::Format.new
630
+ fmt.set_align(arg)
631
+ case arg
632
+ when 'left', 'center', 'centre', 'right', 'fill', 'justify',
633
+ 'center_across', 'centre_across', 'merge'
634
+ assert_equal(value, fmt.text_h_align, "arg: #{arg}")
635
+ when 'top', 'vcenter', 'vcentre', 'bottom', 'vjustify'
636
+ assert_equal(value, fmt.text_v_align, "arg: #{arg}")
637
+ end
638
+ end
639
+
640
+ # invalid arg
641
+ [-1, 0, 1.5, nil, true, false, ['left','top'], {'top'=>0}].each do |arg|
642
+ fmt = Writeexcel::Format.new
643
+ val = get_format_property(fmt)
644
+ #print val.inspect
645
+ #exit
646
+ fmt.set_align(arg)
647
+ assert_equal(val[:align], fmt.text_h_align, "arg: #{arg} - text_h_align changed.")
648
+ assert_equal(val[:valign], fmt.text_v_align, "arg: #{arg} - text_v_align changed.")
649
+ end
650
+ end
651
+
652
+ =begin
653
+ set_center_across()
654
+
655
+ Default state: Center across selection is off
656
+ Default action: Turn center across on
657
+ Valid args: 1
658
+
659
+ Text can be aligned across two or more adjacent cells
660
+ using the set_center_across() method. This is an alias
661
+ for the set_align('center_across') method call.
662
+
663
+ Only one cell should contain the text,
664
+ the other cells should be blank:
665
+
666
+ format = workbook.add_format()
667
+ format.set_center_across()
668
+
669
+ worksheet.write(1, 1, 'Center across selection', format)
670
+ worksheet.write_blank(1, 2, format)
671
+ =end
672
+ def test_set_center_across
673
+ # default state
674
+ assert_equal(0, @format.text_h_align)
675
+
676
+ # method call then center_across is set. if arg is none, numeric, string, whatever.
677
+ @format.set_center_across
678
+ assert_equal(6, @format.text_h_align)
679
+ end
680
+
681
+ =begin
682
+ set_text_wrap()
683
+
684
+ Default state: Text wrap is off
685
+ Default action: Turn text wrap on
686
+ Valid args: 0, 1
687
+
688
+ Here is an example using the text wrap property, the escape
689
+ character \n is used to indicate the end of line:
690
+
691
+ format = workbook.add_format()
692
+ format.set_text_wrap()
693
+ worksheet.write(0, 0, "It's\na bum\nwrap", format)
694
+ =end
695
+ def test_set_text_wrap
696
+ # default state
697
+ assert_equal(0, @format.text_wrap, "default state")
698
+
699
+ # valid args
700
+ fmt = Writeexcel::Format.new
701
+ fmt.set_text_wrap
702
+ assert_equal(1, fmt.text_wrap, "No arg")
703
+
704
+ [0, 1].each do |arg|
705
+ fmt = Writeexcel::Format.new
706
+ fmt.set_text_wrap(arg)
707
+ assert_equal(arg, fmt.text_wrap, "arg : #{arg}")
708
+ end
709
+
710
+ # invalid args
711
+ [-1, 0.2, 100, 'text_wrap', true, false, nil].each do |arg|
712
+ assert_raise(ArgumentError,
713
+ "set_text_wrap(#{arg}) : arg must be 0, 1 or none."){
714
+ fmt = Writeexcel::Format.new
715
+ fmt.set_text_wrap(arg)
716
+ }
717
+ end
718
+ end
719
+
720
+ =begin
721
+ set_rotation()
722
+
723
+ Default state: Text rotation is off
724
+ Default action: None
725
+ Valid args: Integers in the range -90 to 90 and 270
726
+
727
+ Set the rotation of the text in a cell. The rotation can be
728
+ any angle in the range -90 to 90 degrees.
729
+
730
+ format = workbook.add_format()
731
+ format.set_rotation(30)
732
+ worksheet.write(0, 0, 'This text is rotated', format)
733
+
734
+ The angle 270 is also supported. This indicates text where
735
+ the letters run from top to bottom.
736
+ =end
737
+ def test_set_rotation
738
+ # default state
739
+ assert_equal(0, @format.rotation)
740
+
741
+ # # valid args -90 <= angle <= 90, 270 angle can be float or double
742
+ # [-90.0, 89, 0, 89, 90, 270].each do |angle|
743
+ # fmt = Writeexcel::Format.new
744
+ # fmt.set_rotation(angle)
745
+ # assert_equal(angle, fmt.rotation, "angle: #{angle}")
746
+ # end
747
+ end
748
+
749
+ =begin
750
+ set_indent()
751
+
752
+ Default state: Text indentation is off
753
+ Default action: Indent text 1 level
754
+ Valid args: Positive integers
755
+
756
+ This method can be used to indent text. The argument, which should
757
+ be an integer, is taken as the level of indentation:
758
+
759
+ format = workbook.add_format()
760
+ format.set_indent(2)
761
+ worksheet.write(0, 0, 'This text is indented', format)
762
+
763
+ Indentation is a horizontal alignment property. It will override
764
+ any other horizontal properties but it can be used in conjunction
765
+ with vertical properties.
766
+ =end
767
+ def test_set_indent
768
+ # default state
769
+ assert_equal(0, @format.indent)
770
+
771
+ # valid arg -- Positive integers
772
+ [1, 10000000].each do |indent|
773
+ fmt = Writeexcel::Format.new
774
+ fmt.set_indent(indent)
775
+ assert_equal(indent, fmt.indent, "indent: #{indent}")
776
+ end
777
+
778
+ # invalid arg
779
+ [].each do |indent|
780
+
781
+ end
782
+ end
783
+
784
+ =begin
785
+ set_shrink()
786
+
787
+ Default state: Text shrinking is off
788
+ Default action: Turn "shrink to fit" on
789
+ Valid args: 1
790
+
791
+ This method can be used to shrink text so that it fits in a cell.
792
+
793
+ format = workbook.add_format()
794
+ format.set_shrink()
795
+ worksheet.write(0, 0, 'Honey, I shrunk the text!', format)
796
+ =end
797
+ def test_set_shrink
798
+ # default state
799
+ assert_equal(0, @format.shrink)
800
+ end
801
+
802
+ =begin
803
+ set_text_justlast()
804
+
805
+ Default state: Justify last is off
806
+ Default action: Turn justify last on
807
+ Valid args: 0, 1
808
+
809
+ Only applies to Far Eastern versions of Excel.
810
+ =end
811
+ def test_set_text_justlast
812
+ # default state
813
+ assert_equal(0, @format.text_justlast)
814
+ end
815
+
816
+ =begin
817
+ set_pattern()
818
+
819
+ Default state: Pattern is off
820
+ Default action: Solid fill is on
821
+ Valid args: 0 .. 18
822
+
823
+ Set the background pattern of a cell.
824
+ =end
825
+ def test_set_pattern
826
+ # default state
827
+ assert_equal(0, @format.pattern)
828
+ end
829
+
830
+ =begin
831
+ set_bg_color()
832
+
833
+ Default state: Color is off
834
+ Default action: Solid fill.
835
+ Valid args: See set_color()
836
+
837
+ The set_bg_color() method can be used to set the background
838
+ colour of a pattern. Patterns are defined via the set_pattern()
839
+ method. If a pattern hasn't been defined then a solid fill
840
+ pattern is used as the default.
841
+
842
+ Here is an example of how to set up a solid fill in a cell:
843
+
844
+ format = workbook.add_format()
845
+ format.set_pattern() # This is optional when using a solid fill
846
+ format.set_bg_color('green')
847
+ worksheet.write('A1', 'Ray', format)
848
+ =end
849
+ def test_set_bg_color
850
+ end
851
+
852
+ =begin
853
+ set_fg_color()
854
+
855
+ Default state: Color is off
856
+ Default action: Solid fill.
857
+ Valid args: See set_color()
858
+
859
+ The set_fg_color() method can be used to set
860
+ the foreground colour of a pattern.
861
+ =end
862
+ def test_set_fg_color
863
+ end
864
+
865
+ =begin
866
+ set_border()
867
+
868
+ Also applies to: set_bottom()
869
+ set_top()
870
+ set_left()
871
+ set_right()
872
+
873
+ Default state: Border is off
874
+ Default action: Set border type 1
875
+ Valid args: 0-13, See below.
876
+
877
+ A cell border is comprised of a border on the bottom, top,
878
+ left and right. These can be set to the same value using
879
+ set_border() or individually using the relevant method
880
+ calls shown above.
881
+
882
+ The following shows the border styles sorted
883
+ by WriteExcel index number:
884
+
885
+ Index Name Weight Style
886
+ ===== ============= ====== ===========
887
+ 0 None 0
888
+ 1 Continuous 1 -----------
889
+ 2 Continuous 2 -----------
890
+ 3 Dash 1 - - - - - -
891
+ 4 Dot 1 . . . . . .
892
+ 5 Continuous 3 -----------
893
+ 6 Double 3 ===========
894
+ 7 Continuous 0 -----------
895
+ 8 Dash 2 - - - - - -
896
+ 9 Dash Dot 1 - . - . - .
897
+ 10 Dash Dot 2 - . - . - .
898
+ 11 Dash Dot Dot 1 - . . - . .
899
+ 12 Dash Dot Dot 2 - . . - . .
900
+ 13 SlantDash Dot 2 / - . / - .
901
+
902
+ The following shows the borders sorted by style:
903
+
904
+ Name Weight Style Index
905
+ ============= ====== =========== =====
906
+ Continuous 0 ----------- 7
907
+ Continuous 1 ----------- 1
908
+ Continuous 2 ----------- 2
909
+ Continuous 3 ----------- 5
910
+ Dash 1 - - - - - - 3
911
+ Dash 2 - - - - - - 8
912
+ Dash Dot 1 - . - . - . 9
913
+ Dash Dot 2 - . - . - . 10
914
+ Dash Dot Dot 1 - . . - . . 11
915
+ Dash Dot Dot 2 - . . - . . 12
916
+ Dot 1 . . . . . . 4
917
+ Double 3 =========== 6
918
+ None 0 0
919
+ SlantDash Dot 2 / - . / - . 13
920
+
921
+ The following shows the borders in the order shown in the Excel Dialog.
922
+
923
+ Index Style Index Style
924
+ ===== ===== ===== =====
925
+ 0 None 12 - . . - . .
926
+ 7 ----------- 13 / - . / - .
927
+ 4 . . . . . . 10 - . - . - .
928
+ 11 - . . - . . 8 - - - - - -
929
+ 9 - . - . - . 2 -----------
930
+ 3 - - - - - - 5 -----------
931
+ 1 ----------- 6 ===========
932
+ =end
933
+ def test_set_border
934
+ end
935
+
936
+ =begin
937
+ set_border_color()
938
+
939
+ Also applies to: set_bottom_color()
940
+ set_top_color()
941
+ set_left_color()
942
+ set_right_color()
943
+
944
+ Default state: Color is off
945
+ Default action: Undefined
946
+ Valid args: See set_color()
947
+
948
+ Set the colour of the cell borders. A cell border is comprised of a border
949
+ on the bottom, top, left and right. These can be set to the same colour
950
+ using set_border_color() or individually using the relevant method
951
+ calls shown above.
952
+ =end
953
+ def test_set_border_color
954
+ end
955
+
956
+ =begin
957
+ copy($format)
958
+
959
+ This method is used to copy all of the properties
960
+ from one Format object to another:
961
+
962
+ lorry1 = workbook.add_format()
963
+ lorry1.set_bold()
964
+ lorry1.set_italic()
965
+ lorry1.set_color('red') # lorry1 is bold, italic and red
966
+
967
+ my lorry2 = workbook.add_format()
968
+ lorry2.copy(lorry1)
969
+ lorry2.set_color('yellow') # lorry2 is bold, italic and yellow
970
+
971
+ The copy() method is only useful if you are using the method interface
972
+ to Format properties. It generally isn't required if you are setting
973
+ Format properties directly using hashes.
974
+
975
+ Note: this is not a copy constructor, both objects must exist prior to copying.
976
+ =end
977
+
978
+ def test_xf_biff_size
979
+ perl_file = "#{PERL_OUTDIR}/file_xf_biff"
980
+ size = File.size(perl_file)
981
+ @ruby_file.print(@format.get_xf)
982
+ rsize = @ruby_file.size
983
+ assert_equal(size, rsize, "File sizes not the same")
984
+ end
985
+
986
+ # Because of the modifications to bg_color and fg_color, I know this
987
+ # test will fail. This is ok.
988
+ #def test_xf_biff_contents
989
+ # perl_file = "perl_output/f_xf_biff"
990
+ # @fh = File.new(@ruby_file,"w+")
991
+ # @fh.print(@format.xf_biff)
992
+ # @fh.close
993
+ # contents = IO.readlines(perl_file)
994
+ # rcontents = IO.readlines(@ruby_file)
995
+ # assert_equal(contents,rcontents,"Contents not the same")
996
+ #end
997
+
998
+ def test_font_biff_size
999
+ perl_file = "#{PERL_OUTDIR}/file_font_biff"
1000
+ @ruby_file.print(@format.get_font)
1001
+ contents = IO.readlines(perl_file)
1002
+ @ruby_file.rewind
1003
+ rcontents = @ruby_file.readlines
1004
+ assert_equal(contents, rcontents, "Contents not the same")
1005
+ end
1006
+
1007
+ def test_font_biff_contents
1008
+ perl_file = "#{PERL_OUTDIR}/file_font_biff"
1009
+ @ruby_file.print(@format.get_font)
1010
+ contents = IO.readlines(perl_file)
1011
+ @ruby_file.rewind
1012
+ rcontents = @ruby_file.readlines
1013
+ assert_equal(contents, rcontents, "Contents not the same")
1014
+ end
1015
+
1016
+ def test_get_font_key_size
1017
+ perl_file = "#{PERL_OUTDIR}/file_font_key"
1018
+ @ruby_file.print(@format.get_font_key)
1019
+ assert_equal(File.size(perl_file), @ruby_file.size, "Bad file size")
1020
+ end
1021
+
1022
+ def test_get_font_key_contents
1023
+ perl_file = "#{PERL_OUTDIR}/file_font_key"
1024
+ @ruby_file.print(@format.get_font_key)
1025
+ contents = IO.readlines(perl_file)
1026
+ @ruby_file.rewind
1027
+ rcontents = @ruby_file.readlines
1028
+ assert_equal(contents, rcontents, "Contents not the same")
1029
+ end
1030
+
1031
+ def test_initialize
1032
+ assert_nothing_raised {
1033
+ Writeexcel::Format.new(
1034
+ :bold => true,
1035
+ :size => 10,
1036
+ :color => 'black',
1037
+ :fg_color => 43,
1038
+ :align => 'top',
1039
+ :text_wrap => true,
1040
+ :border => 1
1041
+ )
1042
+ }
1043
+ end
1044
+
1045
+ # added by Nakamura
1046
+
1047
+ def test_get_xf
1048
+ perl_file = "#{PERL_OUTDIR}/file_xf_biff"
1049
+ size = File.size(perl_file)
1050
+ @ruby_file.print(@format.get_xf)
1051
+ rsize = @ruby_file.size
1052
+ assert_equal(size, rsize, "File sizes not the same")
1053
+
1054
+ compare_file(perl_file, @ruby_file)
1055
+ end
1056
+
1057
+ def test_get_font
1058
+ perl_file = "#{PERL_OUTDIR}/file_font_biff"
1059
+ size = File.size(perl_file)
1060
+ @ruby_file.print(@format.get_font)
1061
+ rsize = @ruby_file.size
1062
+ assert_equal(size, rsize, "File sizes not the same")
1063
+
1064
+ compare_file(perl_file, @ruby_file)
1065
+ end
1066
+
1067
+ def test_get_font_key
1068
+ perl_file = "#{PERL_OUTDIR}/file_font_key"
1069
+ size = File.size(perl_file)
1070
+ @ruby_file.print(@format.get_font_key)
1071
+ rsize = @ruby_file.size
1072
+ assert_equal(size, rsize, "File sizes not the same")
1073
+
1074
+ compare_file(perl_file, @ruby_file)
1075
+ end
1076
+
1077
+ def test_copy
1078
+ format1 = Writeexcel::Format.new
1079
+ format2 = Writeexcel::Format.new
1080
+
1081
+ format1.set_size(12)
1082
+
1083
+ format2.copy(format1)
1084
+
1085
+ assert_equal(format1.size, format2.size)
1086
+ end
1087
+
1088
+
1089
+ # -----------------------------------------------------------------------------
1090
+
1091
+ def get_valid_format_properties
1092
+ {
1093
+ :font => 'Times New Roman',
1094
+ :size => 30,
1095
+ :color => 8,
1096
+ :italic => 1,
1097
+ :underline => 1,
1098
+ :font_strikeout => 1,
1099
+ :font_script => 1,
1100
+ :font_outline => 1,
1101
+ :font_shadow => 1,
1102
+ :locked => 0,
1103
+ :hidden => 1,
1104
+ :text_wrap => 1,
1105
+ :text_justlast => 1,
1106
+ :indent => 2,
1107
+ :shrink => 1,
1108
+ :pattern => 18,
1109
+ :bg_color => 30,
1110
+ :fg_color => 63
1111
+ }
1112
+ end
1113
+
1114
+ def get_valid_color_string_number
1115
+ {
1116
+ :black => 8,
1117
+ :blue => 12,
1118
+ :brown => 16,
1119
+ :cyan => 15,
1120
+ :gray => 23,
1121
+ :green => 17,
1122
+ :lime => 11,
1123
+ :magenta => 14,
1124
+ :navy => 18,
1125
+ :orange => 53,
1126
+ :pink => 33,
1127
+ :purple => 20,
1128
+ :red => 10,
1129
+ :silver => 22,
1130
+ :white => 9,
1131
+ :yellow => 13
1132
+ }
1133
+ end
1134
+ # :rotation => -90,
1135
+ # :center_across => 1,
1136
+ # :align => 'left',
1137
+
1138
+ def get_format_property(format)
1139
+ text_h_align = {
1140
+ 1 => 'left',
1141
+ 2 => 'center/centre',
1142
+ 3 => 'right',
1143
+ 4 => 'fill',
1144
+ 5 => 'justiry',
1145
+ 6 => 'center_across/centre_across/merge',
1146
+ 7 => 'distributed/equal_space'
1147
+ }
1148
+
1149
+ text_v_align = {
1150
+ 0 => 'top',
1151
+ 1 => 'vcenter/vcentre',
1152
+ 2 => 'bottom',
1153
+ 3 => 'vjustify',
1154
+ 4 => 'vdistributed/vequal_space'
1155
+ }
1156
+
1157
+ return {
1158
+ :font => format.font,
1159
+ :size => format.size,
1160
+ :color => format.color,
1161
+ :bold => format.bold,
1162
+ :italic => format.italic,
1163
+ :underline => format.underline,
1164
+ :font_strikeout => format.font_strikeout,
1165
+ :font_script => format.font_script,
1166
+ :font_outline => format.font_outline,
1167
+ :font_shadow => format.font_shadow,
1168
+ :num_format => format.num_format,
1169
+ :locked => format.locked,
1170
+ :hidden => format.hidden,
1171
+ :align => format.text_h_align,
1172
+ :valign => format.text_v_align,
1173
+ :rotation => format.rotation,
1174
+ :text_wrap => format.text_wrap,
1175
+ :text_justlast => format.text_justlast,
1176
+ :center_across => format.text_h_align,
1177
+ :indent => format.indent,
1178
+ :shrink => format.shrink,
1179
+ :pattern => format.pattern,
1180
+ :bg_color => format.bg_color,
1181
+ :fg_color => format.fg_color,
1182
+ :bottom => format.bottom,
1183
+ :top => format.top,
1184
+ :left => format.left,
1185
+ :right => format.right,
1186
+ :bottom_color => format.bottom_color,
1187
+ :top_color => format.top_color,
1188
+ :left_color => format.left_color,
1189
+ :right_color => format.right_color
1190
+ }
1191
+ end
1192
+ end