Skip to content

Commit

Permalink
import Template::Patch 0.02 from CPAN
Browse files Browse the repository at this point in the history
git-cpan-module: Template::Patch
git-cpan-version: 0.02
  • Loading branch information
gaal authored and nothingmuch committed Jan 18, 2009
1 parent 9a0ad8e commit c78bb72
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 19 deletions.
6 changes: 6 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
Revision history for Template-Patch

0.02 2006-03-23
* If the IN template fails to match, don't clobber the output with
a measly version of the OUT template and nothing else. Instead,
passthrough the input cleanly.
* `metapatch --version`.

0.01 2006-03-20
First version, released on an unsuspecting world.

8 changes: 7 additions & 1 deletion bin/metapatch
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use Getopt::Long;

use Template::Patch;

GetOptions \our %Conf, qw(patch|p=s);
GetOptions \our %Conf, qw(patch|p=s version);

version(), exit 0 if $Conf{version};

my $p = Template::Patch->new_from_file($Conf{patch});

Expand All @@ -20,6 +22,10 @@ $p->print;
exit 0;

sub slurp_in { local $/; <> }
sub version {
(my $me = $0) =~ s,^.*/|\\,,;
print "$me ", Template::Patch->VERSION, "\n";
}

# vim: ts=4 et :

Expand Down
23 changes: 18 additions & 5 deletions lib/Template/Patch.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use Template;

use base 'Class::Accessor::Ref';

our $VERSION = '0.01';
our $VERSION = '0.02';

BEGIN {
my @accs = (qw/ inp outp vars output _ext _tt conf/);
my @accs = (qw/ inp outp vars routput rinput _ext _tt conf/);
__PACKAGE__->mk_accessors(@accs);
__PACKAGE__->mk_refaccessors(@accs);
}
Expand Down Expand Up @@ -50,7 +50,8 @@ sub new_from_file {

die "$0: must supply --patch arg" unless defined $pfile;

my $self = $class->new( { vars => {}, conf => {} } );
my $self = $class->new( { vars => {},
conf => {}, routput => do{\my $output_port} } );

open my $fh, "<", $pfile or die "$0: open: $pfile: $!";
while (<$fh>) {
Expand Down Expand Up @@ -95,16 +96,28 @@ sub extract {
$input, # actual data to parse
$self->vars, # dictionary for extracted data
);
# we need to keep a ref to input around for the case where no extraction
# was successful.
$self->rinput(\$input);
#::YY($self->vars);
}

sub patch {
my($self) = @_;

# if the dictionary is empty, extract didn't find anything.
# copy over the input, so we don't emit just a broken template.
# XXX: copy or ref?
if (0 == keys %{ $self->vars }) {
$self->routput( $self->rinput );
return;
}

$self->_tt( Template->new );
$self->_tt->process( \$self->outp, $self->vars, $self->_ref_output )
$self->_tt->process( \$self->outp, $self->vars, $self->routput )
}

sub print { print $_[0]->output }
sub print { print ${ $_[0]->routput } }

sub ::Y { require YAML::Syck; YAML::Syck::Dump(@_) }
sub ::YY { require Carp; Carp::confess(::Y(@_)) }
Expand Down
46 changes: 33 additions & 13 deletions t/01-basic.t
Original file line number Diff line number Diff line change
@@ -1,40 +1,60 @@
#!perl -T

use strict;
use warnings;

use File::Spec;
use Test::Exception;

use Test::More tests => 7;
use Test::More tests => 9;

our $FS = "File::Spec";

BEGIN {
use_ok( 'Template::Patch' );
}

diag( "Testing Template::Patch $Template::Patch::VERSION, Perl $], $^X" );

dies_ok { Template::Patch->new_from_file("no_such_file") }
"can't read from metapatch file that doesn't exist";

my $tp;
{
my $tp;

lives_ok { $tp = Template::Patch->new_from_file($FS->catfile(qw/t basic1.mp/)); }
"construct patch object with .mp file";
lives_ok { $tp = Template::Patch->new_from_file($FS->catfile(qw/t basic1.mp/)); }
"construct patch object with .mp file";

isa_ok $tp, "Template::Patch", "has correct type";
isa_ok $tp, "Template::Patch", "has correct type";

my $doc = <<'.';
my $doc = <<'.';
I went to the doctor and guess what he told me.
Say AAAHHH!
.

lives_ok { $tp->extract($doc) } "patch extraction lives";
lives_ok { $tp->extract($doc) } "patch extraction lives";
lives_ok { $tp->patch($doc) } "patch application lives";

(my $expected = $doc) =~ s/AAA/BBB/;

is ${$tp->routput}, $expected, "patch applied correctly";
}


lives_ok { $tp->patch($doc) } "patch application lives";
{
my $tp = Template::Patch->new_from_file($FS->catfile(qw/t basic1.mp/));
isa_ok $tp, "Template::Patch", "has correct type - 2";

(my $expected = $doc) =~ s/AAA/BBB/;
my $doc = my $expected = <<'.';
This document contains no triple As.
The patch application should not change anything in it.
.

$tp->extract($doc);
$tp->patch($doc);

is ${$tp->routput}, $expected, "patch applied correctly (did not ruin original)";
}

is $tp->output, $expected, "patch applied correctly";

# vim: ts=4 et :
# vim: ts=4 et ft=perl :

0 comments on commit c78bb72

Please sign in to comment.