File: Catmandu-Exporter-Text.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 (54 lines) | stat: -rw-r--r-- 1,201 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
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use Test::Exception;
use YAML::XS ();

BEGIN {use_ok 'Catmandu::Exporter::Text'}
require_ok 'Catmandu::Exporter::Text';

{
    my $data
        = [{'a' => 'moose'}, {'a' => 'pony'}, {'a' => ['shrimp', 'lobster']}];
    my $file = "";

    my $exporter
        = Catmandu::Exporter::Text->new(file => \$file, field_sep => ',');
    isa_ok $exporter, 'Catmandu::Exporter::Text';

    $exporter->add($_) for @$data;
    $exporter->commit;

    is $exporter->count, 3, 'Count ok';

    my $text = <<EOF;
moose
pony
shrimp,lobster
EOF

    is $file, $text, 'Text doc hash, default line_sep';
}

{
    my $data
        = [{'a' => 'moose'}, {'a' => 'pony'}, {'a' => ['shrimp', 'lobster']}];
    my $file = "";

    my $exporter = Catmandu::Exporter::Text->new(
        file      => \$file,
        line_sep  => '\t',
        field_sep => ','
    );
    isa_ok $exporter, 'Catmandu::Exporter::Text';

    $exporter->add_many($data);

    # don't call commit to test streaming output

    is $exporter->count, 3,                               'Count ok';
    is $file,            "moose\tpony\tshrimp,lobster\t", 'Text doc array';
}

done_testing;