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
|
#!/usr/bin/perl
use v5.14;
use warnings;
use Test2::V0;
use Syntax::Keyword::Try;
use Syntax::Keyword::Try::Deparse;
use B::Deparse;
my $deparser = B::Deparse->new();
sub is_deparsed
{
my ( $sub, $exp, $name ) = @_;
my $got = $deparser->coderef2text( $sub );
# Deparsed output is '{ ... }'-wrapped
$got = ( $got =~ m/^{\n(.*)\n}$/s )[0];
$got =~ s/^ //mg;
# Deparsed output will have a lot of pragmata and so on
1 while $got =~ s/^\s*(?:use|no) \w+.*\n//;
$got =~ s/^BEGIN \{\n.*?\n\}\n//s;
# Trim a trailing linefeed
chomp $got;
is( $got, $exp, $name );
}
is_deparsed
sub { try { ABC() } catch { DEF() } },
"try {\n ABC();\n}\ncatch {\n DEF();\n}",
'try/catch';
is_deparsed
sub { try { ABC() } catch($e) { DEF() } },
"try {\n ABC();\n}\ncatch {\n my \$e = \$@;\n DEF();\n}",
'try/catch(VAR)';
is_deparsed
sub { try { ABC() } finally { XYZ() } },
"try {\n ABC();\n}\nfinally {\n XYZ();\n}",
'try/finally';
is_deparsed
sub { try { ABC() } catch { DEF() } finally { XYZ() } },
"try {\n ABC();\n}\ncatch {\n DEF();\n}\nfinally {\n XYZ();\n}",
'try/catch/finally';
done_testing;
|