[libcatmandu-perl] 22/101: specialized tabular exporter role; optionally collect field names

Jonas Smedegaard dr at jones.dk
Tue Feb 23 13:43:50 UTC 2016


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository libcatmandu-perl.

commit 8fad8bc6018a18b76fac1f2af899cd028b380e94
Author: Nicolas Steenlant <nicolas.steenlant at ugent.be>
Date:   Fri Dec 11 12:19:43 2015 +0100

    specialized tabular exporter role; optionally collect field names
---
 lib/Catmandu/TabularExporter.pm |  1 +
 t/Catmandu-Exporter-CSV.t       | 31 ++++++++++++++++++++++++++++---
 2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/lib/Catmandu/TabularExporter.pm b/lib/Catmandu/TabularExporter.pm
index e49bbc8..ff85952 100644
--- a/lib/Catmandu/TabularExporter.pm
+++ b/lib/Catmandu/TabularExporter.pm
@@ -4,6 +4,7 @@ use Catmandu::Sane;
 
 our $VERSION = '0.9505';
 
+use Catmandu::Util qw(:is);
 use Moo::Role;
 
 sub _coerce_array {
diff --git a/t/Catmandu-Exporter-CSV.t b/t/Catmandu-Exporter-CSV.t
index 3057cc6..a03f304 100644
--- a/t/Catmandu-Exporter-CSV.t
+++ b/t/Catmandu-Exporter-CSV.t
@@ -16,7 +16,7 @@ my $out = "";
 my $exporter = $pkg->new(file => \$out);
 isa_ok $exporter, $pkg;
 
-$exporter->add($_) for @$data;
+$exporter->add_many($data);
 $exporter->commit;
 
 my $csv = <<EOF;
@@ -29,11 +29,36 @@ EOF
 is $out, $csv, "CSV strings ok";
 is $exporter->count,3, "Count ok";
 
+$data = [{b => '1'}, {'a' => 'pony', b => '2'}, {'a' => 'shrimp', b => '3'}];
 $out = "";
-$exporter = $pkg->new( fields => { a => 'Longname', x => 'X' }, file => \$out );
+$exporter = $pkg->new(file => \$out);
+$exporter->add_many($data);
+$exporter->commit;
+$csv = <<EOF;
+b
+1
+2
+3
+EOF
+is $out, $csv, "first record determines fields without collect";
+
+$out = "";
+$exporter = $pkg->new(file => \$out, collect_fields => 1);
+$exporter->add_many($data);
+$exporter->commit;
+$csv = <<EOF;
+a,b
+,1
+pony,2
+shrimp,3
+EOF
+is $out, $csv, "collect field names";
+
+$out = "";
+$exporter = $pkg->new(fields => 'a,x', columns => 'Longname,X', file => \$out );
 $exporter->add( { a => 'Hello', b => 'World' } );
 $csv = "Longname,X\nHello,\n";
-is $out, $csv, "custom column names as HASH";
+is $out, $csv, "custom column names";
 
 $out="";
 my $fixer    = Catmandu->fixer('if exists(foo) reject() end');

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libcatmandu-perl.git



More information about the Pkg-perl-cvs-commits mailing list