1#!/usr/bin/perl -w
2
3###############################################################################
4#
5# A test for Spreadsheet::WriteExcel.
6#
7# Tests for some of the internal method used to write the NOTE record that
8# is used in cell comments.
9#
10# reverse('�'), September 2005, John McNamara, jmcnamara@cpan.org
11#
12
13
14use strict;
15
16use Spreadsheet::WriteExcel;
17use Test::More tests => 5;
18
19
20###############################################################################
21#
22# Tests setup
23#
24my $test_file           = "temp_test_file.xls";
25my $workbook            = Spreadsheet::WriteExcel->new($test_file);
26my $worksheet           = $workbook->add_worksheet();
27my $target;
28my $result;
29my $caption;
30my $row;
31my $col;
32my $obj_id;
33my $visible;
34my $author;
35my $encoding;
36my @data;
37
38
39
40###############################################################################
41#
42# Test 1 NOTE. Blank author name.
43#
44
45@data = $worksheet->_comment_params(2, 0, 'Test');
46
47$row        = $data[0];
48$col        = $data[1];
49$author     = $data[4];
50$encoding   = $data[5];
51$visible    = $data[6];
52$obj_id     = 1;
53
54$caption    = " \t_store_note()";
55$target     = join " ",  qw(
56                            1C 00 0C 00 02 00 00 00 00 00 01 00 00 00 00 00
57                           );
58
59
60$result     = unpack_record($worksheet->_store_note($row,
61                                                    $col,
62                                                    $obj_id,
63                                                    $author,
64                                                    $encoding,
65                                                    $visible,
66                                                    ));
67is($result, $target, $caption);
68
69
70###############################################################################
71#
72# Test 2 NOTE. Defined author name
73#
74
75@data = $worksheet->_comment_params(2, 0, 'Test', author => 'Username');
76
77$row        = $data[0];
78$col        = $data[1];
79$author     = $data[4];
80$encoding   = $data[5];
81$visible    = $data[6];
82$obj_id     = 1;
83
84$caption    = " \t_store_note()";
85$target     = join " ",  qw(
86                            1C 00 14 00 02 00 00 00 00 00 01 00 08 00 00 55
87                            73 65 72 6E 61 6D 65 00
88                           );
89
90
91$result     = unpack_record($worksheet->_store_note($row,
92                                                    $col,
93                                                    $obj_id,
94                                                    $author,
95                                                    $encoding,
96                                                    $visible,
97                                                    ));
98is($result, $target, $caption);
99
100
101###############################################################################
102#
103# Test 3 NOTE. Visible note.
104#
105
106@data = $worksheet->_comment_params(4, 2, 'Test', author  => 'Username',
107                                                  visible => 1
108                                    );
109
110$row        = $data[0];
111$col        = $data[1];
112$author     = $data[4];
113$encoding   = $data[5];
114$visible    = $data[6];
115$obj_id     = 1;
116
117
118$caption    = " \t_store_note()";
119$target     = join " ",  qw(
120                            1C 00 14 00 04 00 02 00 02 00 01 00 08 00 00 55
121                            73 65 72 6E 61 6D 65 00
122                           );
123
124
125$result     = unpack_record($worksheet->_store_note($row,
126                                                    $col,
127                                                    $obj_id,
128                                                    $author,
129                                                    $encoding,
130                                                    $visible,
131                                                    ));
132is($result, $target, $caption);
133
134
135###############################################################################
136#
137# Test 3 NOTE. UTF16 author name.
138#
139
140$author     = pack "n", 0x20Ac; # Euro symbol
141
142@data = $worksheet->_comment_params(4, 2, 'Test', author          =>$author,
143                                                  author_encoding => 1
144                                    );
145
146$row        = $data[0];
147$col        = $data[1];
148$author     = $data[4];
149$encoding   = $data[5];
150$visible    = $data[6];
151$obj_id     = 1;
152
153$caption    = " \t_store_note()";
154$target     = join " ",  qw(
155                            1C 00 0E 00 04 00 02 00 00 00 01 00 01 00 01 AC
156                            20 00
157                           );
158
159
160$result     = unpack_record($worksheet->_store_note($row,
161                                                    $col,
162                                                    $obj_id,
163                                                    $author,
164                                                    $encoding,
165                                                    $visible,
166                                                    ));
167is($result, $target, $caption);
168
169
170###############################################################################
171#
172# Test 4 NOTE. UTF8 author name. Perl 5.8 only.
173#
174
175SKIP: {
176
177skip " \t_store_note() skipped test requires Perl 5.8 Unicode support", 1
178     if $] < 5.008;
179
180$author     = chr 0x20Ac; # Euro symbol
181
182@data       = $worksheet->_comment_params(4, 2, 'Test',  author =>$author);
183
184$row        = $data[0];
185$col        = $data[1];
186$author     = $data[4];
187$encoding   = $data[5];
188$visible    = $data[6];
189$obj_id     = 1;
190
191
192$caption    = " \t_store_note()";
193$target     = join " ",  qw(
194                            1C 00 0E 00 04 00 02 00 00 00 01 00 01 00 01 AC
195                            20 00
196                           );
197
198
199$result     = unpack_record($worksheet->_store_note($row,
200                                                    $col,
201                                                    $obj_id,
202                                                    $author,
203                                                    $encoding,
204                                                    $visible,
205                                                    ));
206is($result, $target, $caption);
207
208}
209
210
211###############################################################################
212#
213# Unpack the binary data into a format suitable for printing in tests.
214#
215sub unpack_record {
216    return join ' ', map {sprintf "%02X", $_} unpack "C*", $_[0];
217}
218
219
220# Cleanup
221$workbook->close();
222unlink $test_file;
223
224
225__END__
226
227
228
229