summaryrefslogtreecommitdiff
path: root/t/07-class-xpath.t
blob: 9aa8040166df5383c5c810b7abf990527d985bbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!perl 
package main; 
use Test::Most 'die';
BEGIN {
    eval 'use PPIx::XPath';
    plan skip_all => 'PPIx::XPath needed for this test' if $@;
}
 
package CXpEngine; {
use PPIx::XPath;
sub new bless {},shift }
sub findnodes {
    my ($self,$expr,$node)=@_;
    return PPIx::XPath->new($node)->match($expr);
}
 
sub PPI::Document::parent {
    return $_[0];
}
}
 
package PPITransform;{ 
 use Tree::Transform::XSLTish ':engine';
 use strict;
 use warnings;
 
 engine_class 'CXpEngine';
 
 #default_rules; 
 tree_rule match => '/'priority => 0, action => sub {
     print '# root';
     $_[0]->apply_rules
 };
 tree_rule match => '*'priority => 0, action => sub {
     print '',$_[0]->it->xpath;
     $_[0]->apply_rules
 };
 
 tree_rule match => 'Statement::Sub'action => sub {
     return $_[0]->it->name.'('.$_[0]->it->prototype.')';
 };
 
}
 
package main; 
use strict;
use warnings;
use PPI;
use Devel::TraceCalls;
 
plan tests=>1;
 
{
my $doc=PPI::Document->new(\<<'EOP');
sub gino($%) {}
sub pino {}
 
gino(\&pino,{1=>2});
EOP
 
my $x=PPIx::XPath->new($doc);
diag "$_\n" for $x->match('//Statement::Sub');
 
my $trans=PPITransform->new();
#trace_calls { 
# Package => 'PPIx::XPath', 
# Class => 'PPI::Node', 
# ShowStack => 1, 
# CaptureAll => 1, 
#}; 
 
my @results=$trans->transform($doc);
is_deeply \@results,['gino($%)','pino'],'PPI example';
}