From 0ab586e39537d889dca8fd4c2909e599993c977c Mon Sep 17 00:00:00 2001 From: sbertrand Date: Tue, 12 Jan 2016 09:09:42 -0700 Subject: [PATCH] Added tests for add_functionality() --- lib/Devel/Examine/Subs.pm | 7 ++++- t/02-file_extentions.t | 4 +-- t/50-add_func_engine.t | 54 +++++++++++++++++++++++++++++++++++++++ t/51-add_func_preproc.t | 54 +++++++++++++++++++++++++++++++++++++++ t/52-add_func_postproc.t | 54 +++++++++++++++++++++++++++++++++++++++ t/__clean.t | 5 +++- 6 files changed, 174 insertions(+), 4 deletions(-) create mode 100644 t/50-add_func_engine.t create mode 100644 t/51-add_func_preproc.t create mode 100644 t/52-add_func_postproc.t diff --git a/lib/Devel/Examine/Subs.pm b/lib/Devel/Examine/Subs.pm index d16be62..73e439e 100644 --- a/lib/Devel/Examine/Subs.pm +++ b/lib/Devel/Examine/Subs.pm @@ -240,7 +240,6 @@ sub add_functionality { $self->_config($p); my $to_add = $self->{params}{add_functionality}; - my $in_prod = $self->{params}{add_functionality_prod}; my @allowed = qw( @@ -304,6 +303,12 @@ sub add_functionality { } my $file = $dt{$to_add}->(); + my $copy = $self->{params}{copy}; + + if ($copy) { + copy $file, $copy or die $!; + $file = $copy; + } my $des = Devel::Examine::Subs->new( file => $file, diff --git a/t/02-file_extentions.t b/t/02-file_extentions.t index 496b435..31e243b 100644 --- a/t/02-file_extentions.t +++ b/t/02-file_extentions.t @@ -33,7 +33,7 @@ use_ok( 'File::Edit::Portable' ) || print "Bail out!\n"; types => [qw(*.t)], ); - is (@files, 54, "using *.t extension works properly"); + is (@files, 57, "using *.t extension works properly"); @files = $rw->dir( dir => $dir, @@ -49,7 +49,7 @@ use_ok( 'File::Edit::Portable' ) || print "Bail out!\n"; types => [qw(*.data *.t)], ); - is (@files, 61, "using *.data and *.t extensions works properly"); + is (@files, 64, "using *.data and *.t extensions works properly"); } diff --git a/t/50-add_func_engine.t b/t/50-add_func_engine.t new file mode 100644 index 0000000..75a1c5d --- /dev/null +++ b/t/50-add_func_engine.t @@ -0,0 +1,54 @@ +#!/usr/bin/perl +use warnings; +use strict; + +use Data::Dumper; +use Devel::Examine::Subs; +use File::Copy; +use Test::More tests => 4; + +my $file = 't/sample.data'; +my $copy = 't/add_func_engine.data'; + +my %params = ( + file => $file, + copy => $copy, + post_proc => [ 'file_lines_contain' ], + engine => testing(), +); + +# +sub testing { + + return sub { + + my $p = shift; + my $struct = shift; + + return $struct; + }; +} +# + +my $install = 1; # set this to true to install + +if ($install) { + my $des = Devel::Examine::Subs->new(copy => $copy); + my $ret = $des->add_functionality(add_functionality => 'engine'); + is ($ret, 1, "add_functionality engine succeeded"); +} +else { + my $des = Devel::Examine::Subs->new(%params); + my $struct = $des->run(\%params); + print Dumper $struct; +} + +open my $fh, '<', $copy or die $!; +my @file = <$fh>; +close $fh; + +is ((grep { $_ =~ /testing =>/ } @file), 1, "dt updated ok"); +is ((grep { $_ =~ /sub testing \{/ } @file), 1, "sub added ok"); + +eval { unlink $copy or die $!; }; +is ($@, '', "temp file removed ok"); diff --git a/t/51-add_func_preproc.t b/t/51-add_func_preproc.t new file mode 100644 index 0000000..2ad2294 --- /dev/null +++ b/t/51-add_func_preproc.t @@ -0,0 +1,54 @@ +#!/usr/bin/perl +use warnings; +use strict; + +use Data::Dumper; +use Devel::Examine::Subs; +use File::Copy; +use Test::More tests => 4; + +my $file = 't/sample.data'; +my $copy = 't/add_func_preproc.data'; + +my %params = ( + file => $file, + copy => $copy, + post_proc => [ 'file_lines_contain' ], + engine => testing(), +); + +# +sub testing { + + return sub { + + my $p = shift; + my $struct = shift; + + return $struct; + }; +} +# + +my $install = 1; # set this to true to install + +if ($install) { + my $des = Devel::Examine::Subs->new(copy => $copy); + my $ret = $des->add_functionality(add_functionality => 'pre_proc'); + is ($ret, 1, "add_functionality pre_proc succeeded"); +} +else { + my $des = Devel::Examine::Subs->new(%params); + my $struct = $des->run(\%params); + print Dumper $struct; +} + +open my $fh, '<', $copy or die $!; +my @file = <$fh>; +close $fh; + +is ((grep { $_ =~ /testing =>/ } @file), 1, "dt updated ok"); +is ((grep { $_ =~ /sub testing \{/ } @file), 1, "sub added correctly"); + +eval { unlink $copy or die $!; }; +is ($@, '', "temp file removed ok"); diff --git a/t/52-add_func_postproc.t b/t/52-add_func_postproc.t new file mode 100644 index 0000000..b71295e --- /dev/null +++ b/t/52-add_func_postproc.t @@ -0,0 +1,54 @@ +#!/usr/bin/perl +use warnings; +use strict; + +use Data::Dumper; +use Devel::Examine::Subs; +use File::Copy; +use Test::More tests => 4; + +my $file = 't/sample.data'; +my $copy = 't/add_func_postproc.data'; + +my %params = ( + file => $file, + copy => $copy, + post_proc => [ 'file_lines_contain' ], + engine => testing(), +); + +# +sub testing { + + return sub { + + my $p = shift; + my $struct = shift; + + return $struct; + }; +} +# + +my $install = 1; # set this to true to install + +if ($install) { + my $des = Devel::Examine::Subs->new(copy => $copy); + my $ret = $des->add_functionality(add_functionality => 'post_proc'); + is ($ret, 1, "add_functionality post_proc succeeded"); +} +else { + my $des = Devel::Examine::Subs->new(%params); + my $struct = $des->run(\%params); + print Dumper $struct; +} + +open my $fh, '<', $copy or die $!; +my @file = <$fh>; +close $fh; + +is ((grep { $_ =~ /testing =>/ } @file), 1, "dt updated ok"); +is ((grep { $_ =~ /sub testing \{/ } @file), 1, "sub added correctly"); + +eval { unlink $copy or die $!; }; +is ($@, '', "temp file removed ok"); diff --git a/t/__clean.t b/t/__clean.t index 6b12e2c..162fc67 100644 --- a/t/__clean.t +++ b/t/__clean.t @@ -2,7 +2,7 @@ use warnings; use strict; -use Test::More tests => 30; +use Test::More tests => 36; use File::Copy qw(copy); BEGIN {#1 @@ -36,6 +36,9 @@ my @files_to_delete = qw( t/post_proc_dump.debug t/pre_proc_dump.debug t/remove.data + t/add_func_engine.data + t/add_func_postproc.data + t/add_func_preproc.data ); my @bak_glob = <*.bak>;