WriteExcel 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +47 -0
- data/VERSION +1 -0
- data/examples/a_simple.rb +42 -0
- data/examples/autofilters.rb +266 -0
- data/examples/bigfile.rb +30 -0
- data/examples/copyformat.rb +51 -0
- data/examples/data_validate.rb +278 -0
- data/examples/date_time.rb +86 -0
- data/examples/demo.rb +118 -0
- data/examples/diag_border.rb +35 -0
- data/examples/formats.rb +489 -0
- data/examples/header.rb +136 -0
- data/examples/hidden.rb +28 -0
- data/examples/hyperlink.rb +42 -0
- data/examples/images.rb +52 -0
- data/examples/merge1.rb +39 -0
- data/examples/merge2.rb +44 -0
- data/examples/merge3.rb +65 -0
- data/examples/merge4.rb +82 -0
- data/examples/merge5.rb +79 -0
- data/examples/protection.rb +46 -0
- data/examples/regions.rb +52 -0
- data/examples/repeat.rb +42 -0
- data/examples/stats.rb +75 -0
- data/examples/stocks.rb +80 -0
- data/examples/tab_colors.rb +30 -0
- data/lib/WriteExcel.rb +30 -0
- data/lib/WriteExcel/biffwriter.rb +259 -0
- data/lib/WriteExcel/chart.rb +217 -0
- data/lib/WriteExcel/excelformula.y +138 -0
- data/lib/WriteExcel/excelformulaparser.rb +573 -0
- data/lib/WriteExcel/format.rb +1108 -0
- data/lib/WriteExcel/formula.rb +986 -0
- data/lib/WriteExcel/olewriter.rb +322 -0
- data/lib/WriteExcel/properties.rb +250 -0
- data/lib/WriteExcel/storage_lite.rb +590 -0
- data/lib/WriteExcel/workbook.rb +2602 -0
- data/lib/WriteExcel/worksheet.rb +6378 -0
- data/spec/WriteExcel_spec.rb +7 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- data/test/tc_all.rb +31 -0
- data/test/tc_biff.rb +104 -0
- data/test/tc_chart.rb +22 -0
- data/test/tc_example_match.rb +1280 -0
- data/test/tc_format.rb +1264 -0
- data/test/tc_formula.rb +63 -0
- data/test/tc_ole.rb +110 -0
- data/test/tc_storage_lite.rb +102 -0
- data/test/tc_workbook.rb +115 -0
- data/test/tc_worksheet.rb +115 -0
- data/test/test_00_IEEE_double.rb +14 -0
- data/test/test_01_add_worksheet.rb +12 -0
- data/test/test_02_merge_formats.rb +58 -0
- data/test/test_04_dimensions.rb +397 -0
- data/test/test_05_rows.rb +182 -0
- data/test/test_06_extsst.rb +80 -0
- data/test/test_11_date_time.rb +484 -0
- data/test/test_12_date_only.rb +506 -0
- data/test/test_13_date_seconds.rb +486 -0
- data/test/test_21_escher.rb +629 -0
- data/test/test_22_mso_drawing_group.rb +739 -0
- data/test/test_23_note.rb +78 -0
- data/test/test_24_txo.rb +80 -0
- data/test/test_26_autofilter.rb +327 -0
- data/test/test_27_autofilter.rb +144 -0
- data/test/test_28_autofilter.rb +174 -0
- data/test/test_29_process_jpg.rb +131 -0
- data/test/test_30_validation_dval.rb +82 -0
- data/test/test_31_validation_dv_strings.rb +131 -0
- data/test/test_32_validation_dv_formula.rb +211 -0
- data/test/test_40_property_types.rb +191 -0
- data/test/test_41_properties.rb +238 -0
- data/test/test_42_set_properties.rb +430 -0
- data/test/ts_all.rb +34 -0
- metadata +154 -0
@@ -0,0 +1,82 @@
|
|
1
|
+
##########################################################################
|
2
|
+
# test_30_validation_dval.rb
|
3
|
+
#
|
4
|
+
# Tests for the Excel DVAL structure used in data validation.
|
5
|
+
#
|
6
|
+
# reverse('©'), September 2005, John McNamara, jmcnamara@cpan.org
|
7
|
+
#
|
8
|
+
# original written in Perl by John McNamara
|
9
|
+
# converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
|
10
|
+
#
|
11
|
+
#########################################################################
|
12
|
+
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
|
13
|
+
|
14
|
+
require "test/unit"
|
15
|
+
require 'WriteExcel'
|
16
|
+
|
17
|
+
class TC_validation_dval < Test::Unit::TestCase
|
18
|
+
|
19
|
+
def setup
|
20
|
+
t = Time.now.strftime("%Y%m%d")
|
21
|
+
path = "temp#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
|
22
|
+
@test_file = File.join(Dir.tmpdir, path)
|
23
|
+
@workbook = Spreadsheet::WriteExcel.new(@test_file)
|
24
|
+
@worksheet = @workbook.add_worksheet
|
25
|
+
end
|
26
|
+
|
27
|
+
def teardown
|
28
|
+
@workbook.close
|
29
|
+
File.unlink(@test_file) if FileTest.exist?(@test_file)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_1
|
33
|
+
obj_id = 1
|
34
|
+
dv_count = 1
|
35
|
+
|
36
|
+
caption = " \tData validation: _store_dval(#{obj_id}, #{dv_count})"
|
37
|
+
target = %w(
|
38
|
+
B2 01 12 00 04 00 00 00 00 00 00 00 00 00 01 00
|
39
|
+
00 00 01 00 00 00
|
40
|
+
).join(' ')
|
41
|
+
|
42
|
+
result = unpack_record(@worksheet.store_dval(obj_id, dv_count))
|
43
|
+
assert_equal(target, result, caption)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_2
|
47
|
+
obj_id = -1
|
48
|
+
dv_count = 1
|
49
|
+
|
50
|
+
caption = " \tData validation: _store_dval(#{obj_id}, #{dv_count})"
|
51
|
+
target = %w(
|
52
|
+
B2 01 12 00 04 00 00 00 00 00 00 00 00 00 FF FF
|
53
|
+
FF FF 01 00 00 00
|
54
|
+
).join(' ')
|
55
|
+
|
56
|
+
result = unpack_record(@worksheet.store_dval(obj_id, dv_count))
|
57
|
+
assert_equal(target, result, caption)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_3
|
61
|
+
obj_id = 1
|
62
|
+
dv_count = 2
|
63
|
+
|
64
|
+
caption = " \tData validation: _store_dval(#{obj_id}, #{dv_count})"
|
65
|
+
target = %w(
|
66
|
+
B2 01 12 00 04 00 00 00 00 00 00 00 00 00 01 00
|
67
|
+
00 00 02 00 00 00
|
68
|
+
).join(' ')
|
69
|
+
|
70
|
+
result = unpack_record(@worksheet.store_dval(obj_id, dv_count))
|
71
|
+
assert_equal(target, result, caption)
|
72
|
+
end
|
73
|
+
|
74
|
+
###############################################################################
|
75
|
+
#
|
76
|
+
# Unpack the binary data into a format suitable for printing in tests.
|
77
|
+
#
|
78
|
+
def unpack_record(data)
|
79
|
+
data.unpack('C*').map! {|c| sprintf("%02X", c) }.join(' ')
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
##########################################################################
|
2
|
+
# test_31_validation_dv_strings.rb
|
3
|
+
#
|
4
|
+
# Tests for the packed caption/message strings used in the Excel DV structure
|
5
|
+
# as part of data validation.
|
6
|
+
#
|
7
|
+
# reverse('©'), September 2005, John McNamara, jmcnamara@cpan.org
|
8
|
+
#
|
9
|
+
# original written in Perl by John McNamara
|
10
|
+
# converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
|
11
|
+
#
|
12
|
+
#########################################################################
|
13
|
+
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
|
14
|
+
|
15
|
+
require "test/unit"
|
16
|
+
require 'WriteExcel'
|
17
|
+
|
18
|
+
class TC_validation_dv_strings < Test::Unit::TestCase
|
19
|
+
|
20
|
+
def setup
|
21
|
+
t = Time.now.strftime("%Y%m%d")
|
22
|
+
path = "temp#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
|
23
|
+
@test_file = File.join(Dir.tmpdir, path)
|
24
|
+
@workbook = Spreadsheet::WriteExcel.new(@test_file)
|
25
|
+
@worksheet = @workbook.add_worksheet
|
26
|
+
end
|
27
|
+
|
28
|
+
def teardown
|
29
|
+
@workbook.close
|
30
|
+
File.unlink(@test_file) if FileTest.exist?(@test_file)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_empty_string
|
34
|
+
string = ''
|
35
|
+
max_length = 32
|
36
|
+
|
37
|
+
caption = " \tData validation: _pack_dv_string('', #{max_length})"
|
38
|
+
target = %w(
|
39
|
+
01 00 00 00
|
40
|
+
).join(' ')
|
41
|
+
result = unpack_record(@worksheet.pack_dv_string(string, max_length))
|
42
|
+
assert_equal(target, result, caption)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_nil
|
46
|
+
string = nil
|
47
|
+
max_length = 32
|
48
|
+
|
49
|
+
caption = " \tData validation: _pack_dv_string('', #{max_length})"
|
50
|
+
target = %w(
|
51
|
+
01 00 00 00
|
52
|
+
).join(' ')
|
53
|
+
result = unpack_record(@worksheet.pack_dv_string(string, max_length))
|
54
|
+
assert_equal(target, result, caption)
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_single_space
|
58
|
+
string = ' '
|
59
|
+
max_length = 32
|
60
|
+
|
61
|
+
caption = " \tData validation: _pack_dv_string('', #{max_length})"
|
62
|
+
target = %w(
|
63
|
+
01 00 00 20
|
64
|
+
).join(' ')
|
65
|
+
result = unpack_record(@worksheet.pack_dv_string(string, max_length))
|
66
|
+
assert_equal(target, result, caption)
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_single_character
|
70
|
+
string = 'A'
|
71
|
+
max_length = 32
|
72
|
+
|
73
|
+
caption = " \tData validation: _pack_dv_string('', #{max_length})"
|
74
|
+
target = %w(
|
75
|
+
01 00 00 41
|
76
|
+
).join(' ')
|
77
|
+
result = unpack_record(@worksheet.pack_dv_string(string, max_length))
|
78
|
+
assert_equal(target, result, caption)
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_string_longer_than_32_characters_for_dialog_captions
|
82
|
+
string = 'This string is longer than 32 characters'
|
83
|
+
max_length = 32
|
84
|
+
|
85
|
+
caption = " \tData validation: _pack_dv_string('', #{max_length})"
|
86
|
+
target = %w(
|
87
|
+
20 00 00 54 68 69 73 20
|
88
|
+
73 74 72 69 6E 67 20 69 73 20 6C 6F 6E 67 65 72
|
89
|
+
20 74 68 61 6E 20 33 32 20 63 68
|
90
|
+
).join(' ')
|
91
|
+
result = unpack_record(@worksheet.pack_dv_string(string, max_length))
|
92
|
+
assert_equal(target, result, caption)
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_string_longer_than_32_characters_for_dialog_messages
|
96
|
+
string = 'ABCD' * 64
|
97
|
+
max_length = 255
|
98
|
+
|
99
|
+
caption = " \tData validation: _pack_dv_string('', #{max_length})"
|
100
|
+
target = %w(
|
101
|
+
FF 00 00 41 42 43 44 41 42 43 44 41 42 43 44 41
|
102
|
+
42 43 44 41 42 43 44 41 42 43 44 41 42 43 44 41
|
103
|
+
42 43 44 41 42 43 44 41 42 43 44 41 42 43 44 41
|
104
|
+
42 43 44 41 42 43 44 41 42 43 44 41 42 43 44 41
|
105
|
+
42 43 44 41 42 43 44 41 42 43 44 41 42 43 44 41
|
106
|
+
42 43 44 41 42 43 44 41 42 43 44 41 42 43 44 41
|
107
|
+
42 43 44 41 42 43 44 41 42 43 44 41 42 43 44 41
|
108
|
+
42 43 44 41 42 43 44 41 42 43 44 41 42 43 44 41
|
109
|
+
42 43 44 41 42 43 44 41 42 43 44 41 42 43 44 41
|
110
|
+
42 43 44 41 42 43 44 41 42 43 44 41 42 43 44 41
|
111
|
+
42 43 44 41 42 43 44 41 42 43 44 41 42 43 44 41
|
112
|
+
42 43 44 41 42 43 44 41 42 43 44 41 42 43 44 41
|
113
|
+
42 43 44 41 42 43 44 41 42 43 44 41 42 43 44 41
|
114
|
+
42 43 44 41 42 43 44 41 42 43 44 41 42 43 44 41
|
115
|
+
42 43 44 41 42 43 44 41 42 43 44 41 42 43 44 41
|
116
|
+
42 43 44 41 42 43 44 41 42 43 44 41 42 43 44 41
|
117
|
+
42 43
|
118
|
+
).join(' ')
|
119
|
+
result = unpack_record(@worksheet.pack_dv_string(string, max_length))
|
120
|
+
assert_equal(target, result, caption)
|
121
|
+
end
|
122
|
+
|
123
|
+
###############################################################################
|
124
|
+
#
|
125
|
+
# Unpack the binary data into a format suitable for printing in tests.
|
126
|
+
#
|
127
|
+
def unpack_record(data)
|
128
|
+
data.unpack('C*').map! {|c| sprintf("%02X", c) }.join(' ')
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
@@ -0,0 +1,211 @@
|
|
1
|
+
##########################################################################
|
2
|
+
# test_32_validation_dv_formula.rb
|
3
|
+
#
|
4
|
+
# Tests for the Excel DVAL structure used in data validation.
|
5
|
+
#
|
6
|
+
# reverse('©'), September 2005, John McNamara, jmcnamara@cpan.org
|
7
|
+
#
|
8
|
+
# original written in Perl by John McNamara
|
9
|
+
# converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
|
10
|
+
#
|
11
|
+
#########################################################################
|
12
|
+
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
|
13
|
+
|
14
|
+
require "test/unit"
|
15
|
+
require 'WriteExcel'
|
16
|
+
|
17
|
+
class TC_validation_dv_formula < Test::Unit::TestCase
|
18
|
+
|
19
|
+
def setup
|
20
|
+
t = Time.now.strftime("%Y%m%d")
|
21
|
+
path = "temp#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
|
22
|
+
@test_file = File.join(Dir.tmpdir, path)
|
23
|
+
@workbook = Spreadsheet::WriteExcel.new(@test_file)
|
24
|
+
@worksheet = @workbook.add_worksheet
|
25
|
+
@worksheet2 = @workbook.add_worksheet
|
26
|
+
end
|
27
|
+
|
28
|
+
def teardown
|
29
|
+
@workbook.close
|
30
|
+
File.unlink(@test_file) if FileTest.exist?(@test_file)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_integer_values
|
34
|
+
formula = '10'
|
35
|
+
|
36
|
+
caption = " \tData validation: _pack_dv_formula('#{formula}')"
|
37
|
+
bytes = %w(
|
38
|
+
03 00 00 E0 1E 0A 00
|
39
|
+
)
|
40
|
+
|
41
|
+
# Zero out Excel's random unused word to allow comparison.
|
42
|
+
bytes[2] = '00'
|
43
|
+
bytes[3] = '00'
|
44
|
+
target = bytes.join(" ")
|
45
|
+
|
46
|
+
result = unpack_record(@worksheet.pack_dv_formula(formula))
|
47
|
+
assert_equal(target, result, caption)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_decimal_values
|
51
|
+
formula = '1.2345'
|
52
|
+
|
53
|
+
caption = " \tData validation: _pack_dv_formula('#{formula}')"
|
54
|
+
bytes = %w(
|
55
|
+
09 00 E0 3F 1F 8D 97 6E 12 83 C0 F3 3F
|
56
|
+
)
|
57
|
+
|
58
|
+
# Zero out Excel's random unused word to allow comparison.
|
59
|
+
bytes[2] = '00'
|
60
|
+
bytes[3] = '00'
|
61
|
+
target = bytes.join(" ")
|
62
|
+
|
63
|
+
result = unpack_record(@worksheet.pack_dv_formula(formula))
|
64
|
+
assert_equal(target, result, caption)
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_date_values
|
68
|
+
formula = @worksheet.convert_date_time('2008-07-24T')
|
69
|
+
|
70
|
+
caption = " \tData validation: _pack_dv_formula('2008-07-24T')"
|
71
|
+
bytes = %w(
|
72
|
+
03 00 E0 3F 1E E5 9A
|
73
|
+
)
|
74
|
+
|
75
|
+
# Zero out Excel's random unused word to allow comparison.
|
76
|
+
bytes[2] = '00'
|
77
|
+
bytes[3] = '00'
|
78
|
+
target = bytes.join(" ")
|
79
|
+
|
80
|
+
result = unpack_record(@worksheet.pack_dv_formula(formula))
|
81
|
+
assert_equal(target, result, caption)
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_time_values
|
85
|
+
formula = @worksheet.convert_date_time('T12:00')
|
86
|
+
|
87
|
+
caption = " \tData validation: _pack_dv_formula('T12:00')"
|
88
|
+
bytes = %w(
|
89
|
+
09 00 E0 3F 1F 00 00 00 00 00 00 E0 3F
|
90
|
+
)
|
91
|
+
|
92
|
+
# Zero out Excel's random unused word to allow comparison.
|
93
|
+
bytes[2] = '00'
|
94
|
+
bytes[3] = '00'
|
95
|
+
target = bytes.join(" ")
|
96
|
+
|
97
|
+
result = unpack_record(@worksheet.pack_dv_formula(formula))
|
98
|
+
assert_equal(target, result, caption)
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_cell_reference_value_C9
|
102
|
+
formula = '=C9'
|
103
|
+
|
104
|
+
caption = " \tData validation: _pack_dv_formula('#{formula}')"
|
105
|
+
bytes = %w(
|
106
|
+
05 00 E0 3F 44 08 00 02 C0
|
107
|
+
)
|
108
|
+
|
109
|
+
# Zero out Excel's random unused word to allow comparison.
|
110
|
+
bytes[2] = '00'
|
111
|
+
bytes[3] = '00'
|
112
|
+
target = bytes.join(" ")
|
113
|
+
|
114
|
+
result = unpack_record(@worksheet.pack_dv_formula(formula))
|
115
|
+
assert_equal(target, result, caption)
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_cell_reference_value_E3_E6
|
119
|
+
formula = '=E3:E6'
|
120
|
+
|
121
|
+
caption = " \tData validation: _pack_dv_formula('#{formula}')"
|
122
|
+
bytes = %w(
|
123
|
+
09 00 0C 00 25 02 00 05 00 04 C0 04 C0
|
124
|
+
)
|
125
|
+
|
126
|
+
# Zero out Excel's random unused word to allow comparison.
|
127
|
+
bytes[2] = '00'
|
128
|
+
bytes[3] = '00'
|
129
|
+
target = bytes.join(" ")
|
130
|
+
|
131
|
+
result = unpack_record(@worksheet.pack_dv_formula(formula))
|
132
|
+
assert_equal(target, result, caption)
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_cell_reference_value_E3_E6_absolute
|
136
|
+
formula = '=$E$3:$E$6'
|
137
|
+
|
138
|
+
caption = " \tData validation: _pack_dv_formula('#{formula}')"
|
139
|
+
bytes = %w(
|
140
|
+
09 00 0C 00 25 02 00 05 00 04 00 04 00
|
141
|
+
)
|
142
|
+
|
143
|
+
# Zero out Excel's random unused word to allow comparison.
|
144
|
+
bytes[2] = '00'
|
145
|
+
bytes[3] = '00'
|
146
|
+
target = bytes.join(" ")
|
147
|
+
|
148
|
+
result = unpack_record(@worksheet.pack_dv_formula(formula))
|
149
|
+
assert_equal(target, result, caption)
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_list_values
|
153
|
+
formula = ['a', 'bb', 'ccc']
|
154
|
+
|
155
|
+
caption = " \tData validation: _pack_dv_formula(['a', 'bb', 'ccc'])"
|
156
|
+
bytes = %w(
|
157
|
+
0B 00 0C 00 17 08 00 61 00 62 62 00 63 63 63
|
158
|
+
)
|
159
|
+
|
160
|
+
# Zero out Excel's random unused word to allow comparison.
|
161
|
+
bytes[2] = '00'
|
162
|
+
bytes[3] = '00'
|
163
|
+
target = bytes.join(" ")
|
164
|
+
|
165
|
+
result = unpack_record(@worksheet.pack_dv_formula(formula))
|
166
|
+
assert_equal(target, result, caption)
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_empty_string
|
170
|
+
formula = ''
|
171
|
+
|
172
|
+
caption = " \tData validation: _pack_dv_formula('')"
|
173
|
+
bytes = %w(
|
174
|
+
00 00 00
|
175
|
+
)
|
176
|
+
|
177
|
+
# Zero out Excel's random unused word to allow comparison.
|
178
|
+
bytes[2] = '00'
|
179
|
+
bytes[3] = '00'
|
180
|
+
target = bytes.join(" ")
|
181
|
+
|
182
|
+
result = unpack_record(@worksheet.pack_dv_formula(formula))
|
183
|
+
assert_equal(target, result, caption)
|
184
|
+
end
|
185
|
+
|
186
|
+
def test_undefined_value
|
187
|
+
formula = nil
|
188
|
+
|
189
|
+
caption = " \tData validation: _pack_dv_formula(nil)"
|
190
|
+
bytes = %w(
|
191
|
+
00 00 00
|
192
|
+
)
|
193
|
+
|
194
|
+
# Zero out Excel's random unused word to allow comparison.
|
195
|
+
bytes[2] = '00'
|
196
|
+
bytes[3] = '00'
|
197
|
+
target = bytes.join(" ")
|
198
|
+
|
199
|
+
result = unpack_record(@worksheet.pack_dv_formula(formula))
|
200
|
+
assert_equal(target, result, caption)
|
201
|
+
end
|
202
|
+
|
203
|
+
###############################################################################
|
204
|
+
#
|
205
|
+
# Unpack the binary data into a format suitable for printing in tests.
|
206
|
+
#
|
207
|
+
def unpack_record(data)
|
208
|
+
data.unpack('C*').map! {|c| sprintf("%02X", c) }.join(' ')
|
209
|
+
end
|
210
|
+
|
211
|
+
end
|
@@ -0,0 +1,191 @@
|
|
1
|
+
##########################################################################
|
2
|
+
# test_40_property_types.rb
|
3
|
+
#
|
4
|
+
# Tests for the basic property types used in OLE property sets.
|
5
|
+
#
|
6
|
+
# reverse('©'), September 2005, John McNamara, jmcnamara@cpan.org
|
7
|
+
#
|
8
|
+
# original written in Perl by John McNamara
|
9
|
+
# converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
|
10
|
+
#
|
11
|
+
#########################################################################
|
12
|
+
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
|
13
|
+
|
14
|
+
require 'test/unit'
|
15
|
+
require 'rubygems'
|
16
|
+
require 'WriteExcel'
|
17
|
+
|
18
|
+
class TC_property_types < Test::Unit::TestCase
|
19
|
+
|
20
|
+
def setup
|
21
|
+
@smiley = '☺' # chr 0x263A; in perl
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_pack_a_VT_I2
|
25
|
+
caption = " \tDoc properties: pack_VT_I2(1252)"
|
26
|
+
target = %w(
|
27
|
+
02 00 00 00 E4 04 00 00
|
28
|
+
).join(' ')
|
29
|
+
|
30
|
+
result = unpack_record( pack_VT_I2(1252) )
|
31
|
+
assert_equal(target, result, caption)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_pack_a_VT_LPSTR_string_and_check_for_padding
|
35
|
+
string = ''
|
36
|
+
codepage = 0x04E4
|
37
|
+
caption = " \tDoc properties: _pack_VT_LPSTR('#{string}',\t#{codepage}')"
|
38
|
+
target = %w(
|
39
|
+
1E 00 00 00 01 00 00 00 00 00 00 00
|
40
|
+
).join(' ')
|
41
|
+
|
42
|
+
result = unpack_record( pack_VT_LPSTR(string, codepage) )
|
43
|
+
assert_equal(target, result, caption)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_pack_a_VT_LPSTR_string_and_check_for_padding_2
|
47
|
+
string = 'a'
|
48
|
+
codepage = 0x04E4
|
49
|
+
caption = " \tDoc properties: _pack_VT_LPSTR('#{string}',\t#{codepage}')"
|
50
|
+
target = %w(
|
51
|
+
1E 00 00 00 02 00 00 00 61 00 00 00
|
52
|
+
).join(' ')
|
53
|
+
|
54
|
+
result = unpack_record( pack_VT_LPSTR(string, codepage) )
|
55
|
+
assert_equal(target, result, caption)
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_pack_a_VT_LPSTR_string_and_check_for_padding_3
|
59
|
+
string = 'bb'
|
60
|
+
codepage = 0x04E4
|
61
|
+
caption = " \tDoc properties: _pack_VT_LPSTR('#{string}',\t#{codepage}')"
|
62
|
+
target = %w(
|
63
|
+
1E 00 00 00 03 00 00 00 62 62 00 00
|
64
|
+
).join(' ')
|
65
|
+
|
66
|
+
result = unpack_record( pack_VT_LPSTR(string, codepage) )
|
67
|
+
assert_equal(target, result, caption)
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_pack_a_VT_LPSTR_string_and_check_for_padding_4
|
71
|
+
string = 'ccc'
|
72
|
+
codepage = 0x04E4
|
73
|
+
caption = " \tDoc properties: _pack_VT_LPSTR('#{string}',\t#{codepage}')"
|
74
|
+
target = %w(
|
75
|
+
1E 00 00 00 04 00 00 00 63 63 63 00
|
76
|
+
).join(' ')
|
77
|
+
|
78
|
+
result = unpack_record( pack_VT_LPSTR(string, codepage) )
|
79
|
+
assert_equal(target, result, caption)
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_pack_a_VT_LPSTR_string_and_check_for_padding_5
|
83
|
+
string = 'dddd'
|
84
|
+
codepage = 0x04E4
|
85
|
+
caption = " \tDoc properties: _pack_VT_LPSTR('#{string}',\t#{codepage}')"
|
86
|
+
target = %w(
|
87
|
+
1E 00 00 00 05 00 00 00 64 64 64 64 00 00 00 00
|
88
|
+
).join(' ')
|
89
|
+
|
90
|
+
result = unpack_record( pack_VT_LPSTR(string, codepage) )
|
91
|
+
assert_equal(target, result, caption)
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_pack_a_VT_LPSTR_string_and_check_for_padding_6
|
95
|
+
string = 'Username'
|
96
|
+
codepage = 0x04E4
|
97
|
+
caption = " \tDoc properties: _pack_VT_LPSTR('#{string}',\t#{codepage}')"
|
98
|
+
target = %w(
|
99
|
+
1E 00 00 00 09 00 00 00 55 73 65 72 6E 61 6D 65
|
100
|
+
00 00 00 00
|
101
|
+
).join(' ')
|
102
|
+
|
103
|
+
result = unpack_record( pack_VT_LPSTR(string, codepage) )
|
104
|
+
assert_equal(target, result, caption)
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_pack_a_VT_LPSTR_UTF8_string
|
108
|
+
string = @smiley
|
109
|
+
codepage = 0xFDE9
|
110
|
+
caption = " \tDoc properties: _pack_VT_LPSTR('#{@smiley}',\t#{codepage}')"
|
111
|
+
target = %w(
|
112
|
+
1E 00 00 00 04 00 00 00 E2 98 BA 00
|
113
|
+
).join(' ')
|
114
|
+
|
115
|
+
result = unpack_record( pack_VT_LPSTR(string, codepage) )
|
116
|
+
assert_equal(target, result, caption)
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_pack_a_VT_LPSTR_UTF8_string_2
|
120
|
+
string = "a" + @smiley
|
121
|
+
codepage = 0xFDE9
|
122
|
+
caption = " \tDoc properties: _pack_VT_LPSTR('a#{@smiley}',\t#{codepage}')"
|
123
|
+
target = %w(
|
124
|
+
1E 00 00 00 05 00 00 00 61 E2 98 BA 00 00 00 00
|
125
|
+
).join(' ')
|
126
|
+
|
127
|
+
result = unpack_record( pack_VT_LPSTR(string, codepage) )
|
128
|
+
assert_equal(target, result, caption)
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_pack_a_VT_LPSTR_UTF8_string_3
|
132
|
+
string = "aa" + @smiley
|
133
|
+
codepage = 0xFDE9
|
134
|
+
caption = " \tDoc properties: _pack_VT_LPSTR('#{string}',\t#{codepage}')"
|
135
|
+
target = %w(
|
136
|
+
1E 00 00 00 06 00 00 00 61 61 E2 98 BA 00 00 00
|
137
|
+
).join(' ')
|
138
|
+
|
139
|
+
result = unpack_record( pack_VT_LPSTR(string, codepage) )
|
140
|
+
assert_equal(target, result, caption)
|
141
|
+
end
|
142
|
+
|
143
|
+
def test_pack_a_VT_LPSTR_UTF8_string_4
|
144
|
+
string = "aaa" + @smiley
|
145
|
+
codepage = 0xFDE9
|
146
|
+
caption = " \tDoc properties: _pack_VT_LPSTR('#{string}',\t#{codepage}')"
|
147
|
+
target = %w(
|
148
|
+
1E 00 00 00 07 00 00 00 61 61 61 E2 98 BA 00 00
|
149
|
+
).join(' ')
|
150
|
+
|
151
|
+
result = unpack_record( pack_VT_LPSTR(string, codepage) )
|
152
|
+
assert_equal(target, result, caption)
|
153
|
+
end
|
154
|
+
|
155
|
+
def test_pack_a_VT_LPSTR_UTF8_string_5
|
156
|
+
string = "aaaa" + @smiley
|
157
|
+
codepage = 0xFDE9
|
158
|
+
caption = " \tDoc properties: _pack_VT_LPSTR('#{string}',\t#{codepage}')"
|
159
|
+
target = %w(
|
160
|
+
1E 00 00 00 08 00 00 00 61 61 61 61 E2 98 BA 00
|
161
|
+
).join(' ')
|
162
|
+
|
163
|
+
result = unpack_record( pack_VT_LPSTR(string, codepage) )
|
164
|
+
assert_equal(target, result, caption)
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_pack_a_VT_FILETIME
|
168
|
+
# Wed Aug 13 00:40:00 2008
|
169
|
+
# $sec,$min,$hour,$mday,$mon,$year
|
170
|
+
# We normalise the time using timegm() so that the tests don't fail due to
|
171
|
+
# different timezones.
|
172
|
+
filetime = Time.gm(2008,8,13,0,40,0)
|
173
|
+
|
174
|
+
caption = " \tDoc properties: _pack_VT_FILETIME()"
|
175
|
+
target = %w(
|
176
|
+
40 00 00 00 00 70 EB 1D DD FC C8 01
|
177
|
+
).join(' ')
|
178
|
+
|
179
|
+
result = unpack_record( pack_VT_FILETIME(filetime) )
|
180
|
+
assert_equal(target, result, caption)
|
181
|
+
end
|
182
|
+
|
183
|
+
###############################################################################
|
184
|
+
#
|
185
|
+
# Unpack the binary data into a format suitable for printing in tests.
|
186
|
+
#
|
187
|
+
def unpack_record(data)
|
188
|
+
data.unpack('C*').map! {|c| sprintf("%02X", c) }.join(' ')
|
189
|
+
end
|
190
|
+
|
191
|
+
end
|