|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +BEGIN { chdir ".." if -d "../t" and -d "../lib" } |
| 4 | +use lib 't/lib'; |
| 5 | +use PPI::Test::pragmas; |
| 6 | +use Test::More tests => 6 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); |
| 7 | + |
| 8 | +use B 'perlstring'; |
| 9 | + |
| 10 | +use PPI (); |
| 11 | +use PPI::Dumper; |
| 12 | + |
| 13 | +sub test_document; |
| 14 | + |
| 15 | +BASE_SPACE_SYMBOL: { |
| 16 | + local $TODO = "crashes"; |
| 17 | + test_document # |
| 18 | + '$ s', # |
| 19 | + [ |
| 20 | + 'PPI::Statement', '$ s', # |
| 21 | + 'PPI::Token::Symbol', '$ s', |
| 22 | + ], |
| 23 | + "base space symbol example"; |
| 24 | +} |
| 25 | + |
| 26 | +FOR_LOOP: { |
| 27 | + local $TODO = "crashes"; |
| 28 | + test_document |
| 29 | + 'for my $ s ( qw( a b ) ) { say $s }', |
| 30 | + [ |
| 31 | + 'PPI::Statement::Compound', 'for my $ s ( qw( a b ) ) { say $s }', |
| 32 | + 'PPI::Token::Word', 'for', |
| 33 | + 'PPI::Token::Word', 'my', |
| 34 | + 'PPI::Token::Symbol', '$ s', |
| 35 | + 'PPI::Structure::List', '( qw( a b ) )', |
| 36 | + 'PPI::Token::Structure', '(', |
| 37 | + 'PPI::Statement', 'qw( a b )', |
| 38 | + 'PPI::Token::QuoteLike::Words', 'qw( a b )', |
| 39 | + 'PPI::Token::Structure', ')', |
| 40 | + 'PPI::Structure::Block', '{ say $s }', |
| 41 | + 'PPI::Token::Structure', '{', |
| 42 | + 'PPI::Statement', 'say $s', |
| 43 | + 'PPI::Token::Word', 'say', |
| 44 | + 'PPI::Token::Symbol', '$s', |
| 45 | + 'PPI::Token::Structure', '}', |
| 46 | + ], |
| 47 | + "space symboln in for loop"; |
| 48 | +} |
| 49 | + |
| 50 | +SIGIL_WITH_TRASH: { |
| 51 | + local $TODO = "crashes"; |
| 52 | + test_document |
| 53 | + '$ \"8;b', |
| 54 | + [ |
| 55 | + 'PPI::Statement', '$ \\"8;b', |
| 56 | + 'PPI::Token::Cast', '$', |
| 57 | + 'PPI::Token::Cast', '\\', |
| 58 | + 'PPI::Token::Quote::Double', '"8;b', |
| 59 | + ], |
| 60 | + "sigil with a space and trash that is NOT a symbol"; |
| 61 | +} |
| 62 | + |
| 63 | +sub one_line_explain { |
| 64 | + my ($data) = @_; |
| 65 | + my @explain = explain $data; |
| 66 | + s/\n//g for @explain; |
| 67 | + return join "", @explain; |
| 68 | +} |
| 69 | + |
| 70 | +sub main_level_line { |
| 71 | + return "" if not $TODO; |
| 72 | + my @outer_final; |
| 73 | + my $level = 0; |
| 74 | + while ( my @outer = caller( $level++ ) ) { |
| 75 | + @outer_final = @outer; |
| 76 | + } |
| 77 | + return "l $outer_final[2] - "; |
| 78 | +} |
| 79 | + |
| 80 | +sub test_document { |
| 81 | + local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 82 | + my $args = ref $_[0] eq "ARRAY" ? shift : []; |
| 83 | + my ( $code, $expected, $msg ) = @_; |
| 84 | + $msg = perlstring $code if !defined $msg; |
| 85 | + |
| 86 | + my $d = PPI::Document->new( \$code, @{$args} ) or do { |
| 87 | + diag explain $@; |
| 88 | + fail "PPI::Document->new failed"; |
| 89 | + fail "code round trips"; |
| 90 | + return; |
| 91 | + }; |
| 92 | + my $tokens = $d->find( sub { $_[1]->significant } ); |
| 93 | + $tokens = [ map { ref($_), $_->content } @$tokens ]; |
| 94 | + |
| 95 | + is $d->serialize, $code, "code round trips"; |
| 96 | + |
| 97 | + return if # |
| 98 | + is_deeply( $tokens, $expected, main_level_line . $msg ); |
| 99 | + |
| 100 | + diag ">>> $code -- $msg\n"; |
| 101 | + diag( PPI::Dumper->new($d)->string ); |
| 102 | + diag one_line_explain $tokens; |
| 103 | + diag one_line_explain $expected; |
| 104 | + |
| 105 | + return; |
| 106 | +} |
0 commit comments