From 4b6abab465d1c759b6470a48d5cd5f302153e120 Mon Sep 17 00:00:00 2001 From: Gianni Ceccarelli Date: Thu, 19 Mar 2009 12:56:38 +0100 Subject: test coverage! --- lib/Tree/Transform/Transformer.pm | 23 +++++++++++++++++---- t/04-errors.t | 43 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 t/04-errors.t diff --git a/lib/Tree/Transform/Transformer.pm b/lib/Tree/Transform/Transformer.pm index a773f29..95448af 100644 --- a/lib/Tree/Transform/Transformer.pm +++ b/lib/Tree/Transform/Transformer.pm @@ -47,7 +47,10 @@ sub apply_rules { @nodes=$self->it->xpath_get_child_nodes(); }; - $self->enter(Tree::Transform::Context->new(node_list=>\@nodes)); + my $guard=Tree::Transform::ContextGuard->new + ($self, + Tree::Transform::Context->new(node_list=>\@nodes) + ); my @ret; for my $node (@nodes) { @@ -59,8 +62,6 @@ sub apply_rules { push @ret,$rule->{action}->($self); } - $self->leave; - return @ret; } @@ -167,5 +168,19 @@ sub rule_matches { return; } -__PACKAGE__->meta->make_immutable;no Moose;1; +__PACKAGE__->meta->make_immutable;no Moose; + +package Tree::Transform::ContextGuard; + +sub new { + my ($class,$trans,$context)=@_; + $trans->enter($context); + return bless {trans=>$trans},$class; +} + +sub DESTROY { + $_[0]->{trans}->leave(); +} + +1; __END__ diff --git a/t/04-errors.t b/t/04-errors.t new file mode 100644 index 0000000..5333303 --- /dev/null +++ b/t/04-errors.t @@ -0,0 +1,43 @@ +#!perl +package main; +use Test::Most qw(no_plan); +use strict; +use warnings; +use Tree::DAG_Node::XPath; + +sub Tree::DAG_Node::XPath::Root::xpath_get_root_node { return $_[0] } + +my $tree=Tree::DAG_Node::XPath->new(); +$tree->name('base'); +$tree->new_daughter->name("coso$_") for 1..5; + +warning_like { eval <<'PACK' } [qr/duplicate rule name/i],'Name collision'; +package BadTransform;{ + use Tree::Transform; + use strict; + use warnings; + + tree_rule name => 'one', action => sub { }; + + tree_rule name => 'one', action => sub { }; + + tree_rule match => '*', action => sub { }; + tree_rule match => '*', action => sub { }; + + tree_rule match => 'coso1', priority=> 5, action => sub { $_[0]->call_rule('not-there') }; +} +PACK + +my $trans=BadTransform->new(); + +throws_ok { $trans->transform($tree) } qr/no valid rule/i,'No rule found'; + +throws_ok { $trans->apply_rules($tree) } qr/ambiguous rule/i,'Priority collision'; + +warning_like { $trans->apply_rules() } qr/without nodes nor context/i,'Apply without nodes'; + +warning_like { $trans->call_rule() } qr/without a rule name/i,'Call without name'; + +warning_like { $trans->call_rule('one') } qr/without context/i,'Call without context'; + +throws_ok { $trans->apply_rules($tree->findnodes('coso1')) } qr/no rule named not-there/i, 'Call with bad name'; -- cgit v1.2.3