6 use File::Slurp qw( write_file );
9 rewrite_doc($_) for grep { -w } @ARGV;
14 my $doc = PPI::Document->new($file);
16 return unless $doc =~ /Test::Exception/;
24 if $elt->isa('PPI::Statement')
26 =~ /^\s*(?:::)?(?:lives_|throws_|dies_)(?:ok|and)/;
31 for my $elt ( @{ $doc->find($pattern) || [] } ) {
32 transform_statement($elt);
35 my $content = $doc->content();
36 $content =~ s/Test::Exception/Test::Fatal/g;
38 write_file( $file, $content );
41 sub transform_statement {
44 my @children = $stmt->schildren;
46 my $func = shift @children;
48 my $colons = $func =~ /^::/ ? '::' : q{};
51 if ( $func =~ /lives_/ ) {
59 elsif ( $func =~ /dies_/ ) {
67 elsif ( $func =~ /throws_/ ) {
69 # $children[2] is always a comma if it exists
70 if ( $children[1]->isa('PPI::Token::QuoteLike::Regexp') ) {
88 $stmt->insert_before($code);
94 my $exception = shift;
98 my $exc_func = $func =~ /^::/ ? '::exception' : 'exception';
103 PPI::Token::Word->new($func),
104 PPI::Token::Structure->new('('),
105 PPI::Token::Whitespace->new(q{ }),
106 PPI::Token::Word->new($exc_func),
107 PPI::Token::Whitespace->new(q{ }),
109 PPI::Token::Operator->new(','),
110 PPI::Token::Whitespace->new(q{ }),
111 ( ref $expect ? $expect->clone : PPI::Token::Word->new($expect) );
113 if ( $desc && $desc->isa('PPI::Token::Quote') ) {
114 push @code, PPI::Token::Operator->new(','),
115 PPI::Token::Whitespace->new(q{ }),
120 PPI::Token::Whitespace->new(q{ }),
121 PPI::Token::Structure->new(')'),
122 PPI::Token::Structure->new(';');
124 my $stmt = PPI::Statement->new;
125 $stmt->add_element($_) for @code;