use utf-8 encoding
[webpac2] / t / 5-output-excel.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use lib 'lib';
5
6 use Test::More tests => 22;
7
8 BEGIN {
9 use_ok( 'WebPAC::Test' );
10 use_ok( 'WebPAC::Output::Excel' );
11 use_ok( 'WebPAC::Input' );
12 }
13
14 my $path = "$abs_path/out/test.xls";
15
16 ok(my $out = new WebPAC::Output::Excel({ path => $path, %LOG }), "new");
17
18 ok( $out->init, 'init' );
19
20 throws_ok { $out->add( ) } qr/need id/, 'add without params';
21 throws_ok { $out->add( 42 ) } qr/need ds/, 'add without ds';
22
23 my @funny_chars = ( qw/è æ ¾ ¹ ð È Æ ® © Ð/ );
24
25 my @expected;
26
27 foreach my $line ( 1 .. 5 ) {
28         my $ds;
29         my $tmp;
30         foreach my $col ( 'A' .. 'Z' ) {
31                 my $text = $line . $col;
32                 $text .= shift @funny_chars if @funny_chars;
33                 $ds->{ $col } = { csv => $text };
34                 $tmp->{$col} = $text;
35         }
36         ok( $out->add( $line, $ds ), "add $line" );
37         push @expected, $tmp;
38 }
39
40 ok( $out->finish );
41
42 ok( -e $out->path, "created $path" );
43
44 cmp_ok( $out->path, 'eq', $path, 'path' );
45
46 diag dump( @expected ) if $debug;
47
48 ok(my $input = WebPAC::Input->new( module => 'WebPAC::Input::Excel', no_progress_bar => 1, encoding => 'utf-16', %LOG ), 'new input' );
49 ok(my $db = $input->open( path => $path ), "input->open $path");
50
51 cmp_ok( $input->size, '==', $#expected, "same size" );
52
53 foreach my $mfn ( 1 ... $input->size ) {
54         my $ds = shift @expected;
55         my $rec = $input->fetch;
56         diag dump( $ds, $rec ) if $debug;
57         is_deeply( $rec, $ds, "$mfn same?" );
58 }