[libcatmandu-perl] 24/101: Adding Mock exporter and futher Catmandu tests

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 7dc7f1e0cf669efb143b08cdf19e9d47c2729309
Author: Patrick Hochstenbach <patrick.hochstenbach at ugent.be>
Date:   Fri Dec 11 14:19:32 2015 +0100

    Adding Mock exporter and futher Catmandu tests
---
 Build.PL                      |  2 ++
 lib/Catmandu.pm               |  5 ++--
 lib/Catmandu/Env.pm           |  3 ++
 lib/Catmandu/Exporter/Mock.pm | 68 +++++++++++++++++++++++++++++++++++++++++++
 lib/Catmandu/Exporter/Null.pm |  2 --
 lib/Catmandu/Exporter/YAML.pm |  5 ++++
 t/Catmandu-Exporter-Mock.t    | 27 +++++++++++++++++
 t/Catmandu.t                  | 16 +++++++++-
 8 files changed, 123 insertions(+), 5 deletions(-)

diff --git a/Build.PL b/Build.PL
index c96d651..e6255f0 100644
--- a/Build.PL
+++ b/Build.PL
@@ -67,6 +67,7 @@ my %module_build_args = (
     "Log::Any::Adapter::Log4perl" => 0,
     "Log::Any::Test" => "1.03",
     "Log::Log4perl" => 0,
+    "Perl::Tidy" => 0,
     "Test::Deep" => "0.112",
     "Test::Exception" => "0.32",
     "Test::LWP::UserAgent" => 0,
@@ -81,6 +82,7 @@ my %fallback_build_requires = (
   "Log::Any::Test" => "1.03",
   "Log::Log4perl" => 0,
   "Module::Build" => "0.28",
+  "Perl::Tidy" => 0,
   "Test::Deep" => "0.112",
   "Test::Exception" => "0.32",
   "Test::LWP::UserAgent" => 0,
diff --git a/lib/Catmandu.pm b/lib/Catmandu.pm
index ad2f009..35633a9 100644
--- a/lib/Catmandu.pm
+++ b/lib/Catmandu.pm
@@ -376,14 +376,15 @@ no name is given).  The NAME is set in the configuration file (see 'importer').
 
 =head2 export($data,[NAME])
 
-Export data using a default or named exporter.
+Export data using a default or named exporter or exporter instance.
 
     Catmandu->export({ foo=>'bar'});
 
     my $importer = Catmandu::Importer::Mock->new;
     Catmandu->export($importer, 'YAML', file => '/my/file');
     Catmandu->export($importer, 'my_exporter');
-    Catmandu->export($importer, 'my_exporter', foo => $bar);
+    Catmandu->export($importer, 'my_exporter', exporter_option => '...' , ...);
+    Catmantu->export($importer, Catmandu::Exporter::YAML->new);
 
 =head2 export_to_string
 
diff --git a/lib/Catmandu/Env.pm b/lib/Catmandu/Env.pm
index 35f5b75..4a63219 100644
--- a/lib/Catmandu/Env.pm
+++ b/lib/Catmandu/Env.pm
@@ -186,6 +186,9 @@ sub importer {
 sub exporter {
     my $self = shift;
     my $name = shift;
+
+    return $name if (is_invocant($name) && ref($name) =~ /^Catmandu::Exporter/);
+    
     my $ns = $self->exporter_namespace;
     if (exists $self->config->{exporter}) {
         if (my $c = $self->config->{exporter}{$name || $self->default_exporter}) {
diff --git a/lib/Catmandu/Exporter/Mock.pm b/lib/Catmandu/Exporter/Mock.pm
new file mode 100644
index 0000000..718e89c
--- /dev/null
+++ b/lib/Catmandu/Exporter/Mock.pm
@@ -0,0 +1,68 @@
+package Catmandu::Exporter::Mock;
+
+use Catmandu::Sane;
+
+our $VERSION = '0.9505';
+
+use Moo;
+use namespace::clean;
+
+with 'Catmandu::Exporter';
+
+has _data_ => (is => 'ro', default => sub { [] });
+
+sub add {
+	my ($self, $data) = @_;
+	push @{$self->_data_} , $data;
+	1;
+}
+
+sub as_arrayref {
+	my ($self) = @_;
+	return $self->_data_;
+}
+
+1;
+
+__END__
+
+=pod
+
+=head1 NAME
+
+Catmandu::Exporter::Mock - a expoter that doesn't export anything
+
+=head1 SYNOPSIS
+
+  	# From the commandline
+  	$ catmandu convert JSON --fix myfixes to Mock < /tmp/data.json
+
+    # From Perl
+
+    use Catmandu::Exporter::Mock;
+
+    # Print to STDOUT
+    my $exporter = Catmandu::Exporter::Mock->new(fix => 'myfix.txt');
+
+    $exporter->add_many($arrayref);
+    $exporter->add_many($iterator);
+    $exporter->add_many(sub { });
+
+    $exporter->add($hashref);
+
+    printf "exported %d objects\n" , $exporter->count;
+
+	# Get an array ref of all records exported
+    my $data = $exporter->as_arrayref;
+	
+=head1 DESCRIPTION
+
+This exporter exports nothing and can be used as in situations where you e.g. export
+data from a fix. Other the Null exporter, the Mock exporter will keep an internal
+array of all the records exported which can be retrieved with the 'as_arrayref' method.
+
+=head1 SEE ALSO
+
+L<Catmandu::Exporter::Null>
+
+=cut
\ No newline at end of file
diff --git a/lib/Catmandu/Exporter/Null.pm b/lib/Catmandu/Exporter/Null.pm
index 2c0f088..c44a665 100644
--- a/lib/Catmandu/Exporter/Null.pm
+++ b/lib/Catmandu/Exporter/Null.pm
@@ -9,8 +9,6 @@ use namespace::clean;
 
 with 'Catmandu::Exporter';
 
-has exporters => (is => 'ro', default => sub { [] });
-
 sub add {}
 
 1;
diff --git a/lib/Catmandu/Exporter/YAML.pm b/lib/Catmandu/Exporter/YAML.pm
index 5b932b2..37a11f1 100644
--- a/lib/Catmandu/Exporter/YAML.pm
+++ b/lib/Catmandu/Exporter/YAML.pm
@@ -30,6 +30,11 @@ Catmandu::Exporter::YAML - a YAML exporter
 
 =head1 SYNOPSIS
 
+    # From the commandline
+    $ catmandu convert JSON --fix myfixes to YAML < /tmp/data.json
+
+    # From Perl
+
     use Catmandu::Exporter::YAML;
 
     # Print to STDOUT
diff --git a/t/Catmandu-Exporter-Mock.t b/t/Catmandu-Exporter-Mock.t
new file mode 100644
index 0000000..5f8b5a6
--- /dev/null
+++ b/t/Catmandu-Exporter-Mock.t
@@ -0,0 +1,27 @@
+use strict;
+use warnings;
+use Test::More;
+use Test::Exception;
+
+my $pkg;
+BEGIN {
+    $pkg = 'Catmandu::Exporter::Mock';
+    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($_) for @$data;
+$exporter->commit;
+
+is $out,'', "Null is empty ok";
+is $exporter->count,3, "Count ok";
+ 
+is_deeply $exporter->as_arrayref , [{'a' => 'moose', b => '1'}, {'a' => 'pony', b => '2'}, {'a' => 'shrimp', b => '3'}] , 'as_arrayref';
+
+done_testing;
diff --git a/t/Catmandu.t b/t/Catmandu.t
index c6d2416..d6d4a62 100644
--- a/t/Catmandu.t
+++ b/t/Catmandu.t
@@ -6,6 +6,8 @@ use Test::More;
 use Test::Exception;
 use Log::Any::Adapter;
 use Data::Dumper;
+use Cwd;
+use File::Spec;
 
 my $pkg;
 BEGIN {
@@ -56,4 +58,16 @@ isa_ok(Catmandu->fixer,'Catmandu::Fix','fixer test');
 
 like(Catmandu->export_to_string({ foo => 'bar'}, 'JSON'),qr/{"foo":"bar"}/,'export_to_string');
 
-done_testing 23;
+my $root = File::Spec->catfile(getcwd(),'t');
+
+is(Catmandu->root, $root, 'root');
+is_deeply(Catmandu->roots,[$root],'roots');
+
+is(Catmandu->default_importer_package,'JSON','default_importer_package');
+is(Catmandu->default_exporter_package,'JSON','default_exporter_package');
+
+my $exporter = Catmandu->exporter('Mock');
+Catmandu->export({ n => 1} , $exporter);
+is_deeply($exporter->as_arrayref,[{ n => 1}]);
+
+done_testing 28;

-- 
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