File: Catmandu-Exporter-TSV.t

package info (click to toggle)
libcatmandu-perl 1.2024-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,552 kB
  • sloc: perl: 17,037; makefile: 24; sh: 1
file content (78 lines) | stat: -rw-r--r-- 1,552 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use strict;
use warnings;
use Test::More;
use Test::Exception;

my $pkg;

BEGIN {
    $pkg = 'Catmandu::Exporter::TSV';
    use_ok $pkg;
}
require_ok $pkg;

my $data = [
    {'a' => 'moose',  b => '1'},
    {'a' => 'pony',   b => '2'},
    {'a' => 'shrimp', b => '3'}
];
my $out = "";

my $exporter = $pkg->new(file => \$out);
isa_ok $exporter, $pkg;

$exporter->add_many($data);
$exporter->commit;

my $tsv = <<EOF;
a\tb
moose\t1
pony\t2
shrimp\t3
EOF

is $out,             $tsv, "TSV strings ok";
is $exporter->count, 3,    "Count ok";

$data = [{b => '1'}, {'a' => 'pony', b => '2'}, {'a' => 'shrimp', b => '3'}];
$out  = "";
$exporter = $pkg->new(file => \$out);
$exporter->add_many($data);
$exporter->commit;
$tsv = <<EOF;
b
1
2
3
EOF
is $out, $tsv, "first record determines fields without collect";

$out      = "";
$exporter = $pkg->new(file => \$out, collect_fields => 1);
$exporter->add_many($data);
$exporter->commit;
$tsv = <<EOF;
a\tb
\t1
pony\t2
shrimp\t3
EOF
is $out, $tsv, "collect field names";

$out = "";
$exporter
    = $pkg->new(fields => 'a,x', columns => 'Longname,X', file => \$out);
$exporter->add({a => 'Hello', b => 'World'});
$tsv = "Longname\tX\nHello\t\n";
is $out, $tsv, "custom column names";

$out = "";
my $fixer    = Catmandu->fixer('if exists(foo) reject() end');
my $importer = Catmandu->importer('JSON', file => 't/csv_test.json');

$exporter = $pkg->new(file => \$out);
$exporter->add_many($fixer->fix($importer));
$tsv = "fob\ntest\n";
is $out, $tsv, "custom column names as HASH with reject fix";

done_testing;