diff --git a/.perltidyrc b/.perltidyrc index 8eeb2196..c6bd61ac 100644 --- a/.perltidyrc +++ b/.perltidyrc @@ -1,3 +1,5 @@ --indent-columns=4 --entab-leading-whitespace=4 --tabs +--noblanks-before-comments +--converge diff --git a/README.pod b/README.pod index a8d79a71..5daa7dd0 100644 --- a/README.pod +++ b/README.pod @@ -1,3 +1,4 @@ + =pod =head1 NAME diff --git a/lib/PPI.pm b/lib/PPI.pm index 12f852bf..20d80a40 100644 --- a/lib/PPI.pm +++ b/lib/PPI.pm @@ -8,7 +8,7 @@ use strict; # Set the version for CPAN our $VERSION = '1.282'; -our ( $XS_COMPATIBLE, @XS_EXCLUDE ) = ( '0.845' ); +our ( $XS_COMPATIBLE, @XS_EXCLUDE ) = ('0.845'); # Load everything use PPI::Util (); @@ -29,7 +29,7 @@ use PPI::Lexer (); die if !$PPI::XS_DISABLE and !eval { require PPI::XS; 1 } - and $@ !~ /^Can't locate .*? at /; # ignore failure to load if not installed + and $@ !~ /^Can't locate .*? at /; # ignore failure to load if not installed 1; diff --git a/lib/PPI/Cache.pm b/lib/PPI/Cache.pm index 7a0a9f07..4c975f93 100644 --- a/lib/PPI/Cache.pm +++ b/lib/PPI/Cache.pm @@ -56,7 +56,7 @@ use PPI::Document (); our $VERSION = '1.282'; -use constant VMS => !! ( $^O eq 'VMS' ); +use constant VMS => !!( $^O eq 'VMS' ); sub import { my $class = ref $_[0] ? ref shift : shift; @@ -66,17 +66,13 @@ sub import { my $cache = $class->new(@_); # Make PPI::Document use it - unless ( PPI::Document->set_cache( $cache ) ) { + unless ( PPI::Document->set_cache($cache) ) { Carp::croak("Failed to set cache in PPI::Document"); } 1; } - - - - ##################################################################### # Constructor and Accessors @@ -115,21 +111,21 @@ sub new { # Path should exist and be usable my $path = $params{path} - or Carp::croak("Cannot create PPI::Cache, no path provided"); + or Carp::croak("Cannot create PPI::Cache, no path provided"); unless ( -d $path ) { Carp::croak("Cannot create PPI::Cache, path does not exist"); } unless ( -r $path and -x $path ) { Carp::croak("Cannot create PPI::Cache, no read permissions for path"); } - if ( ! $params{readonly} and ! -w $path ) { + if ( !$params{readonly} and !-w $path ) { Carp::croak("Cannot create PPI::Cache, no write permissions for path"); } # Create the basic object my $self = bless { path => $path, - readonly => !! $params{readonly}, + readonly => !!$params{readonly}, }, $class; $self; @@ -157,10 +153,6 @@ to the cache. sub readonly { $_[0]->{readonly} } - - - - ##################################################################### # PPI::Cache Methods @@ -174,9 +166,10 @@ cache and retrieves it if so. =cut sub get_document { - my $self = ref $_[0] - ? shift - : Carp::croak('PPI::Cache::get_document called as static method'); + my $self = + ref $_[0] + ? shift + : Carp::croak('PPI::Cache::get_document called as static method'); my $md5hex = $self->_md5hex(shift) or return undef; $self->_load($md5hex); } @@ -196,7 +189,7 @@ FIXME (make this return either one or the other, not both) sub store_document { my $self = shift; - my $Document = _INSTANCE(shift, 'PPI::Document') or return undef; + my $Document = _INSTANCE( shift, 'PPI::Document' ) or return undef; # Shortcut if we are readonly return 1 if $self->readonly; @@ -208,24 +201,21 @@ sub store_document { $self->_store( $md5hex, $Document ); } - - - - ##################################################################### # Support Methods # Store an arbitrary PPI::Document object (using Storable) to a particular # path within the cache filesystem. sub _store { - my ($self, $md5hex, $object) = @_; - my ($dir, $file) = $self->_paths($md5hex); + my ( $self, $md5hex, $object ) = @_; + my ( $dir, $file ) = $self->_paths($md5hex); # Save the file File::Path::mkpath( $dir, 0, 0755 ) unless -d $dir; - if ( VMS ) { + if (VMS) { Storable::lock_nstore( $object, $file ); - } else { + } + else { Storable::nstore( $object, $file ); } } @@ -233,18 +223,20 @@ sub _store { # Load an arbitrary object (using Storable) from a particular # path within the cache filesystem. sub _load { - my ($self, $md5hex) = @_; - my (undef, $file) = $self->_paths($md5hex); + my ( $self, $md5hex ) = @_; + my ( undef, $file ) = $self->_paths($md5hex); # Load the file return '' unless -f $file; - my $object = VMS - ? Storable::retrieve( $file ) - : Storable::lock_retrieve( $file ); + my $object = + VMS + ? Storable::retrieve($file) + : Storable::lock_retrieve($file); # Security check - unless ( _INSTANCE($object, 'PPI::Document') ) { - Carp::croak("Security Violation: Object in '$file' is not a PPI::Document"); + unless ( _INSTANCE( $object, 'PPI::Document' ) ) { + Carp::croak( + "Security Violation: Object in '$file' is not a PPI::Document"); } $object; @@ -254,20 +246,25 @@ sub _load { sub _paths { my $self = shift; my $md5hex = lc shift; - my $dir = File::Spec->catdir( $self->path, substr($md5hex, 0, 1), substr($md5hex, 0, 2) ); - my $file = File::Spec->catfile( $dir, $md5hex . '.ppi' ); - return ($dir, $file); + my $dir = File::Spec->catdir( + $self->path, + substr( $md5hex, 0, 1 ), + substr( $md5hex, 0, 2 ) + ); + my $file = File::Spec->catfile( $dir, $md5hex . '.ppi' ); + return ( $dir, $file ); } # Check a md5hex param sub _md5hex { my $either = shift; - my $it = _SCALAR($_[0]) - ? PPI::Util::md5hex(${$_[0]}) - : $_[0]; - return (defined $it and ! ref $it and $it =~ /^[[:xdigit:]]{32}\z/s) - ? lc $it - : undef; + my $it = + _SCALAR( $_[0] ) + ? PPI::Util::md5hex( ${ $_[0] } ) + : $_[0]; + return ( defined $it and !ref $it and $it =~ /^[[:xdigit:]]{32}\z/s ) + ? lc $it + : undef; } 1; diff --git a/lib/PPI/Document.pm b/lib/PPI/Document.pm index 169748c2..ffbe299d 100644 --- a/lib/PPI/Document.pm +++ b/lib/PPI/Document.pm @@ -64,14 +64,14 @@ Document-specific. =cut use strict; -use Carp (); -use List::Util 1.33 (); -use Params::Util 1.00 qw{_SCALAR0 _ARRAY0 _INSTANCE}; -use Digest::MD5 (); -use PPI::Util (); -use PPI (); -use PPI::Node (); -use YAML::PP (); +use Carp (); +use List::Util 1.33 (); +use Params::Util 1.00 qw{_SCALAR0 _ARRAY0 _INSTANCE}; +use Digest::MD5 (); +use PPI::Util (); +use PPI (); +use PPI::Node (); +use YAML::PP (); use overload 'bool' => \&PPI::Util::TRUE; use overload '""' => 'content'; @@ -92,10 +92,6 @@ use constant LOCATION_COLUMN => 2; use constant LOCATION_LOGICAL_LINE => 3; use constant LOCATION_LOGICAL_FILE => 4; - - - - ##################################################################### # Constructor and Static Methods @@ -184,36 +180,39 @@ This can be useful when your work project has a complex boilerplate module. =cut sub new { - local $_; # An extra one, just in case + local $_; # An extra one, just in case my $class = ref $_[0] ? ref shift : shift; - unless ( @_ ) { + unless (@_) { my $self = $class->SUPER::new; - $self->{readonly} = ! 1; + $self->{readonly} = !1; $self->{tab_width} = 1; return $self; } # Check constructor attributes - my $source = shift; - my %attr = @_; + my $source = shift; + my %attr = @_; # Check the data source - if ( ! defined $source ) { + if ( !defined $source ) { $class->_error("An undefined value was passed to PPI::Document::new"); - } elsif ( ! ref $source ) { + } + elsif ( !ref $source ) { # Catch people using the old API if ( $source =~ /(?:\012|\015)/ ) { - Carp::croak("API CHANGE: Source code should only be passed to PPI::Document->new as a SCALAR reference"); + Carp::croak( +"API CHANGE: Source code should only be passed to PPI::Document->new as a SCALAR reference" + ); } # Save the filename $attr{filename} ||= $source; # When loading from a filename, use the caching layer if it exists. - if ( $CACHE ) { - my $file_contents = PPI::Util::_slurp( $source ); + if ($CACHE) { + my $file_contents = PPI::Util::_slurp($source); # Errors returned as plain string return $class->_error($file_contents) if !ref $file_contents; @@ -223,47 +222,57 @@ sub new { return $class->_setattr( $document, %attr ) if $document; $document = PPI::Lexer->lex_source( $$file_contents, %attr ); - if ( $document ) { + if ($document) { # Save in the cache - $CACHE->store_document( $document ); + $CACHE->store_document($document); return $document; } - } else { + } + else { my $document = PPI::Lexer->lex_file( $source, %attr ); return $document if $document; } - } elsif ( _SCALAR0($source) ) { + } + elsif ( _SCALAR0($source) ) { my $document = PPI::Lexer->lex_source( $$source, %attr ); return $document if $document; - } elsif ( _ARRAY0($source) ) { + } + elsif ( _ARRAY0($source) ) { $source = join '', map { "$_\n" } @$source; my $document = PPI::Lexer->lex_source( $source, %attr ); return $document if $document; - } else { - $class->_error("Unknown object or reference was passed to PPI::Document::new"); + } + else { + $class->_error( + "Unknown object or reference was passed to PPI::Document::new"); } # Pull and store the error from the lexer my $errstr; - if ( _INSTANCE($@, 'PPI::Exception') ) { + if ( _INSTANCE( $@, 'PPI::Exception' ) ) { $errstr = $@->message; - } elsif ( $@ ) { + } + elsif ($@) { $errstr = $@; $errstr =~ s/\sat line\s.+$//; - } elsif ( PPI::Lexer->errstr ) { + } + elsif ( PPI::Lexer->errstr ) { $errstr = PPI::Lexer->errstr; - } else { + } + else { $errstr = "Unknown error parsing Perl document"; } PPI::Lexer->_clear; - $class->_error( $errstr ); + $class->_error($errstr); } sub load { - Carp::croak("API CHANGE: File names should now be passed to PPI::Document->new to load a file"); + Carp::croak( +"API CHANGE: File names should now be passed to PPI::Document->new to load a file" + ); } sub _setattr { @@ -308,13 +317,14 @@ Returns true on success, or C if not passed a valid param. =cut sub set_cache { - my $class = ref $_[0] ? ref shift : shift; + my $class = ref $_[0] ? ref shift : shift; if ( defined $_[0] ) { # Enable the cache - my $object = _INSTANCE(shift, 'PPI::Cache') or return undef; + my $object = _INSTANCE( shift, 'PPI::Cache' ) or return undef; $CACHE = $object; - } else { + } + else { # Disable the cache $CACHE = undef; } @@ -335,13 +345,9 @@ currently set for C. =cut sub get_cache { - $CACHE; + $CACHE; } - - - - ##################################################################### # PPI::Document Instance Methods @@ -442,7 +448,7 @@ or write to the file. sub save { my $self = shift; local *FILE; - open( FILE, '>', $_[0] ) or return undef; + open( FILE, '>', $_[0] ) or return undef; binmode FILE; print FILE $self->serialize or return undef; close FILE or return undef; @@ -496,7 +502,8 @@ sub serialize { if ( $content eq "\n" ) { # Shortcut the most common case for speed $output .= $content . $heredoc; - } else { + } + else { # Slower and more general version $content =~ s/\n/\n$heredoc/; $output .= $content; @@ -541,8 +548,9 @@ sub serialize { # content part of the file my $last_line = List::Util::none { $tokens[$_] and $tokens[$_]->{content} =~ /\n/ - } (($i + 1) .. $last_index); - if ( ! defined $last_line ) { + } + ( ( $i + 1 ) .. $last_index ); + if ( !defined $last_line ) { # Handles the null list case $last_line = 1; } @@ -551,20 +559,18 @@ sub serialize { # (with content or a terminator) my $any_after = List::Util::any { $tokens[$_]->isa('PPI::Token::HereDoc') - and ( - scalar(@{$tokens[$_]->{_heredoc}}) - or - defined $tokens[$_]->{_terminator_line} - ) - } (($i + 1) .. $#tokens); - if ( ! defined $any_after ) { + and ( scalar( @{ $tokens[$_]->{_heredoc} } ) + or defined $tokens[$_]->{_terminator_line} ) + } + ( ( $i + 1 ) .. $#tokens ); + if ( !defined $any_after ) { # Handles the null list case $any_after = ''; } # We don't need to repair the last here-doc on the # last line. But we do need to repair anything else. - unless ( $last_line and ! $any_after ) { + unless ( $last_line and !$any_after ) { # Add a terminating string if it didn't have one unless ( defined $Token->{_terminator_line} ) { $Token->{_terminator_line} = $Token->{_terminator}; @@ -619,7 +625,7 @@ Returns a 32 character hexadecimal string. =cut sub hex_id { - PPI::Util::md5hex($_[0]->serialize); + PPI::Util::md5hex( $_[0]->serialize ); } =pod @@ -651,7 +657,7 @@ sub index_locations { my $heredoc = 0; # Find the first Token without a location - my ($first, $location) = (); + my ( $first, $location ) = (); foreach ( 0 .. $#tokens ) { my $Token = $tokens[$_]; next if $Token->{_location}; @@ -660,10 +666,10 @@ sub index_locations { # Calculate the new location if needed. if ($_) { $location = - $self->_add_location( $location, $tokens[$_ - 1], \$heredoc ); - } else { - my $logical_file = - $self->can('filename') ? $self->filename : undef; + $self->_add_location( $location, $tokens[ $_ - 1 ], \$heredoc ); + } + else { + my $logical_file = $self->can('filename') ? $self->filename : undef; $location = [ 1, 1, 1, 1, $logical_file ]; } $first = $_; @@ -688,24 +694,21 @@ sub index_locations { } sub _add_location { - my ($self, $start, $Token, $heredoc) = @_; + my ( $self, $start, $Token, $heredoc ) = @_; my $content = $Token->{content}; # Does the content contain any newlines - my $newlines =()= $content =~ /\n/g; - my ($logical_line, $logical_file) = - $self->_logical_line_and_file($start, $Token, $newlines); + my $newlines = () = $content =~ /\n/g; + my ( $logical_line, $logical_file ) = + $self->_logical_line_and_file( $start, $Token, $newlines ); - unless ( $newlines ) { + unless ($newlines) { # Handle the simple case return [ $start->[LOCATION_LINE], $start->[LOCATION_CHARACTER] + length($content), - $start->[LOCATION_COLUMN] - + $self->_visual_length( - $content, - $start->[LOCATION_COLUMN] - ), + $start->[LOCATION_COLUMN] + + $self->_visual_length( $content, $start->[LOCATION_COLUMN] ), $logical_line, $logical_file, ]; @@ -714,7 +717,7 @@ sub _add_location { # This is the more complex case where we hit or # span a newline boundary. my $physical_line = $start->[LOCATION_LINE] + $newlines; - my $location = [ $physical_line, 1, 1, $logical_line, $logical_file ]; + my $location = [ $physical_line, 1, 1, $logical_line, $logical_file ]; if ( $heredoc and $$heredoc ) { $location->[LOCATION_LINE] += $$heredoc; $location->[LOCATION_LOGICAL_LINE] += $$heredoc; @@ -726,20 +729,18 @@ sub _add_location { if ( $content =~ /\n([^\n]+?)\z/ ) { $location->[LOCATION_CHARACTER] += length($1); $location->[LOCATION_COLUMN] += - $self->_visual_length( - $1, $location->[LOCATION_COLUMN], - ); + $self->_visual_length( $1, $location->[LOCATION_COLUMN], ); } $location; } sub _logical_line_and_file { - my ($self, $start, $Token, $newlines) = @_; + my ( $self, $start, $Token, $newlines ) = @_; # Regex taken from perlsyn, with the correction that there's no space # required between the line number and the file name. - if ($start->[LOCATION_CHARACTER] == 1) { + if ( $start->[LOCATION_CHARACTER] == 1 ) { if ( $Token->isa('PPI::Token::Comment') ) { if ( $Token->content =~ m< @@ -751,8 +752,9 @@ sub _logical_line_and_file { \s* \z >xms - ) { - return $1, ($3 || $start->[LOCATION_LOGICAL_FILE]); + ) + { + return $1, ( $3 || $start->[LOCATION_LOGICAL_FILE] ); } } elsif ( $Token->isa('PPI::Token::Pod') ) { @@ -770,37 +772,39 @@ sub _logical_line_and_file { \s*? $ >xmsg - ) { - ($line, $file) = ($1, ( $3 || $file ) ); + ) + { + ( $line, $file ) = ( $1, ( $3 || $file ) ); $end_of_directive = pos $content; } - if (defined $line) { + if ( defined $line ) { pos $content = $end_of_directive; - my $post_directive_newlines =()= $content =~ m< \G [^\n]* \n >xmsg; + my $post_directive_newlines = () = + $content =~ m< \G [^\n]* \n >xmsg; return $line + $post_directive_newlines - 1, $file; } } } return - $start->[LOCATION_LOGICAL_LINE] + $newlines, - $start->[LOCATION_LOGICAL_FILE]; + $start->[LOCATION_LOGICAL_LINE] + $newlines, + $start->[LOCATION_LOGICAL_FILE]; } sub _visual_length { - my ($self, $content, $pos) = @_; + my ( $self, $content, $pos ) = @_; my $tab_width = $self->tab_width; - my ($length, $vis_inc); + my ( $length, $vis_inc ); return length $content if $content !~ /\t/; # Split the content in tab and non-tab parts and calculate the # "visual increase" of each part. - for my $part ( split(/(\t)/, $content) ) { - if ($part eq "\t") { - $vis_inc = $tab_width - ($pos-1) % $tab_width; + for my $part ( split( /(\t)/, $content ) ) { + if ( $part eq "\t" ) { + $vis_inc = $tab_width - ( $pos - 1 ) % $tab_width; } else { $vis_inc = length $part; @@ -867,12 +871,12 @@ sub complete { my $self = shift; # Every structure has to be complete - $self->find_any( sub { - $_[1]->isa('PPI::Structure') - and - ! $_[1]->complete - } ) - and return ''; + $self->find_any( + sub { + $_[1]->isa('PPI::Structure') + and !$_[1]->complete; + } + ) and return ''; # Strip anything that isn't a statement off the end my @child = $self->children; @@ -887,10 +891,6 @@ sub complete { return $child[-1]->_complete; } - - - - ##################################################################### # PPI::Node Methods @@ -898,10 +898,6 @@ sub complete { ### XS -> PPI/XS.xs:_PPI_Document__scope 0.903+ sub scope() { 1 } - - - - ##################################################################### # PPI::Element Methods @@ -920,10 +916,6 @@ sub replace { # die "Cannot replace a PPI::Document"; } - - - - ##################################################################### # Error Handling @@ -955,10 +947,6 @@ sub errstr { $errstr; } - - - - ##################################################################### # Native Storable Support @@ -966,11 +954,11 @@ sub STORABLE_freeze { my $self = shift; my $class = ref $self; my %hash = %$self; - return ($class, \%hash); + return ( $class, \%hash ); } sub STORABLE_thaw { - my ($self, undef, $class, $hash) = @_; + my ( $self, undef, $class, $hash ) = @_; bless $self, $class; foreach ( keys %$hash ) { $self->{$_} = delete $hash->{$_}; diff --git a/lib/PPI/Document/File.pm b/lib/PPI/Document/File.pm index c957f7f3..bb0d2aaa 100755 --- a/lib/PPI/Document/File.pm +++ b/lib/PPI/Document/File.pm @@ -26,10 +26,6 @@ our $VERSION = '1.282'; our @ISA = 'PPI::Document'; - - - - ##################################################################### # Constructor and Accessors @@ -62,10 +58,12 @@ sub new { # Unlike a normal inheritance situation, due to our need to stay # compatible with caching magic, this actually returns a regular # anonymous document. We need to rebless if - if ( _INSTANCE($self, 'PPI::Document') ) { + if ( _INSTANCE( $self, 'PPI::Document' ) ) { bless $self, 'PPI::Document::File'; - } else { - die "PPI::Document::File SUPER call returned an object of the wrong type"; + } + else { + die + "PPI::Document::File SUPER call returned an object of the wrong type"; } $self; diff --git a/lib/PPI/Document/Fragment.pm b/lib/PPI/Document/Fragment.pm index 3f870a7b..36814b47 100644 --- a/lib/PPI/Document/Fragment.pm +++ b/lib/PPI/Document/Fragment.pm @@ -25,10 +25,6 @@ our $VERSION = '1.282'; our @ISA = 'PPI::Document'; - - - - ##################################################################### # PPI::Document Methods @@ -49,10 +45,6 @@ sub index_locations { undef; } - - - - ##################################################################### # PPI::Element Methods diff --git a/lib/PPI/Document/Normalized.pm b/lib/PPI/Document/Normalized.pm index d7fdc79e..60f40312 100644 --- a/lib/PPI/Document/Normalized.pm +++ b/lib/PPI/Document/Normalized.pm @@ -50,11 +50,6 @@ our $VERSION = '1.282'; use overload 'bool' => \&PPI::Util::TRUE; use overload '==' => 'equal'; - - - - - ##################################################################### # Constructor and Accessors @@ -74,21 +69,22 @@ sub new { my %args = @_; # Check the required params - my $Document = _INSTANCE($args{Document}, 'PPI::Document') or return undef; + my $Document = _INSTANCE( $args{Document}, 'PPI::Document' ) + or return undef; my $version = $args{version}; - my $functions = _ARRAY($args{functions}) or return undef; + my $functions = _ARRAY( $args{functions} ) or return undef; # Create the object my $self = bless { Document => $Document, version => $version, functions => $functions, - }, $class; + }, $class; $self; } -sub _Document { $_[0]->{Document} } +sub _Document { $_[0]->{Document} } =pod @@ -99,7 +95,7 @@ the object. =cut -sub version { $_[0]->{version} } +sub version { $_[0]->{version} } =pod @@ -113,10 +109,6 @@ the object. sub functions { $_[0]->{functions} } - - - - ##################################################################### # Comparison Methods @@ -144,13 +136,13 @@ or C if there is an error. sub equal { my $self = shift; - my $other = _INSTANCE(shift, 'PPI::Document::Normalized') or return undef; + my $other = _INSTANCE( shift, 'PPI::Document::Normalized' ) or return undef; # Prevent multiple concurrent runs return undef if $self->{processing}; # Check the version and function list first - my $v1 = $self->version || "undef"; + my $v1 = $self->version || "undef"; my $v2 = $other->version || "undef"; return '' if $v1 ne $v2; $self->_equal_ARRAY( $self->functions, $other->functions ) or return ''; @@ -165,8 +157,8 @@ sub equal { # Check that two objects are matched sub _equal_blessed { - my ($self, $this, $that) = @_; - my ($bthis, $bthat) = (blessed $this, blessed $that); + my ( $self, $this, $that ) = @_; + my ( $bthis, $bthat ) = ( blessed $this, blessed $that ); $bthis and $bthat and $bthis eq $bthat or return ''; # Check the object as a reference @@ -175,8 +167,8 @@ sub _equal_blessed { # Check that two references match their types sub _equal_reference { - my ($self, $this, $that) = @_; - my ($rthis, $rthat) = (refaddr $this, refaddr $that); + my ( $self, $this, $that ) = @_; + my ( $rthis, $rthat ) = ( refaddr $this, refaddr $that ); $rthis and $rthat or return undef; # If we have seen this before, are the pointing @@ -187,25 +179,25 @@ sub _equal_reference { } # Check the reference types - my ($tthis, $tthat) = (reftype $this, reftype $that); + my ( $tthis, $tthat ) = ( reftype $this, reftype $that ); $tthis and $tthat and $tthis eq $tthat or return undef; # Check the children of the reference type $self->{seen}->{$rthis} = $rthat; my $method = "_equal_$tthat"; - my $rv = $self->$method( $this, $that ); + my $rv = $self->$method( $this, $that ); delete $self->{seen}->{$rthis}; $rv; } # Compare the children of two SCALAR references sub _equal_SCALAR { - my ($self, $this, $that) = @_; - my ($cthis, $cthat) = ($$this, $$that); + my ( $self, $this, $that ) = @_; + my ( $cthis, $cthat ) = ( $$this, $$that ); return $self->_equal_blessed( $cthis, $cthat ) if blessed $cthis; return $self->_equal_reference( $cthis, $cthat ) if ref $cthis; - return (defined $cthat and $cthis eq $cthat) if defined $cthis; - ! defined $cthat; + return ( defined $cthat and $cthis eq $cthat ) if defined $cthis; + !defined $cthat; } # For completeness sake, lets just treat REF as a specialist SCALAR case @@ -213,7 +205,7 @@ sub _equal_REF { shift->_equal_SCALAR(@_) } # Compare the children of two ARRAY references sub _equal_ARRAY { - my ($self, $this, $that) = @_; + my ( $self, $this, $that ) = @_; # Compare the number of elements scalar(@$this) == scalar(@$that) or return ''; @@ -221,14 +213,17 @@ sub _equal_ARRAY { # Check each element in the array. # Descend depth-first. foreach my $i ( 0 .. scalar(@$this) ) { - my ($cthis, $cthat) = ($this->[$i], $that->[$i]); + my ( $cthis, $cthat ) = ( $this->[$i], $that->[$i] ); if ( blessed $cthis ) { return '' unless $self->_equal_blessed( $cthis, $cthat ); - } elsif ( ref $cthis ) { + } + elsif ( ref $cthis ) { return '' unless $self->_equal_reference( $cthis, $cthat ); - } elsif ( defined $cthis ) { - return '' unless (defined $cthat and $cthis eq $cthat); - } else { + } + elsif ( defined $cthis ) { + return '' unless ( defined $cthat and $cthis eq $cthat ); + } + else { return '' if defined $cthat; } } @@ -238,45 +233,48 @@ sub _equal_ARRAY { # Compare the children of a HASH reference sub _equal_HASH { - my ($self, $this, $that) = @_; + my ( $self, $this, $that ) = @_; # Compare the number of keys - return '' unless scalar(keys %$this) == scalar(keys %$that); + return '' unless scalar( keys %$this ) == scalar( keys %$that ); # Compare each key, descending depth-first. foreach my $k ( keys %$this ) { return '' unless exists $that->{$k}; - my ($cthis, $cthat) = ($this->{$k}, $that->{$k}); + my ( $cthis, $cthat ) = ( $this->{$k}, $that->{$k} ); if ( blessed $cthis ) { return '' unless $self->_equal_blessed( $cthis, $cthat ); - } elsif ( ref $cthis ) { + } + elsif ( ref $cthis ) { return '' unless $self->_equal_reference( $cthis, $cthat ); - } elsif ( defined $cthis ) { - return '' unless (defined $cthat and $cthis eq $cthat); - } else { + } + elsif ( defined $cthis ) { + return '' unless ( defined $cthat and $cthis eq $cthat ); + } + else { return '' if defined $cthat; } } 1; -} +} # We do not support GLOB comparisons sub _equal_GLOB { - my ($self, $this, $that) = @_; + my ( $self, $this, $that ) = @_; warn('GLOB comparisons are not supported'); ''; } # We do not support CODE comparisons sub _equal_CODE { - my ($self, $this, $that) = @_; + my ( $self, $this, $that ) = @_; refaddr $this == refaddr $that; } # We don't support IO comparisons sub _equal_IO { - my ($self, $this, $that) = @_; + my ( $self, $this, $that ) = @_; warn('IO comparisons are not supported'); ''; } @@ -312,4 +310,4 @@ The full text of the license can be found in the LICENSE file included with this module. =cut - + diff --git a/lib/PPI/Dumper.pm b/lib/PPI/Dumper.pm index b0894d46..ed29db7c 100644 --- a/lib/PPI/Dumper.pm +++ b/lib/PPI/Dumper.pm @@ -37,10 +37,6 @@ use Params::Util qw{_INSTANCE}; our $VERSION = '1.282'; - - - - ##################################################################### # Constructor @@ -103,44 +99,42 @@ what these values really are. True/false value, off by default. sub new { my $class = shift; - my $Element = _INSTANCE(shift, 'PPI::Element') or return undef; + my $Element = _INSTANCE( shift, 'PPI::Element' ) or return undef; # Create the object my $self = bless { root => $Element, display => { - memaddr => '', # Show the refaddr of the item - indent => 2, # Indent the structures - class => 1, # Show the object class - content => 1, # Show the object contents - whitespace => 1, # Show whitespace tokens - comments => 1, # Show comment tokens - locations => 0, # Show token locations - }, - }, $class; + memaddr => '', # Show the refaddr of the item + indent => 2, # Indent the structures + class => 1, # Show the object class + content => 1, # Show the object contents + whitespace => 1, # Show whitespace tokens + comments => 1, # Show comment tokens + locations => 0, # Show token locations + }, + }, $class; # Handle the options - my @options = map { lc $_ } @_; # strict hashpairs # https://github.com/Perl-Critic/PPI/issues/201 + my @options = map { lc $_ } + @_; # strict hashpairs # https://github.com/Perl-Critic/PPI/issues/201 my %options = @options; - foreach ( keys %{$self->{display}} ) { + foreach ( keys %{ $self->{display} } ) { if ( exists $options{$_} ) { if ( $_ eq 'indent' ) { $self->{display}->{indent} = $options{$_}; - } else { - $self->{display}->{$_} = !! $options{$_}; + } + else { + $self->{display}->{$_} = !!$options{$_}; } } } - $self->{indent_string} = join '', (' ' x $self->{display}->{indent}); + $self->{indent_string} = join '', ( ' ' x $self->{display}->{indent} ); $self; } - - - - ##################################################################### # Main Interface Methods @@ -155,7 +149,7 @@ Returns as for the internal print function. =cut sub print { - CORE::print(shift->string); + CORE::print( shift->string ); } =pod @@ -191,16 +185,12 @@ sub list { @$array_ref; } - - - - ##################################################################### # Generation Support Methods sub _dump { my $self = ref $_[0] ? shift : shift->new(shift); - my $Element = _INSTANCE($_[0], 'PPI::Element') ? shift : $self->{root}; + my $Element = _INSTANCE( $_[0], 'PPI::Element' ) ? shift : $self->{root}; my $indent = shift || ''; my $output = shift || []; @@ -208,7 +198,8 @@ sub _dump { my $show = 1; if ( $Element->isa('PPI::Token::Whitespace') ) { $show = 0 unless $self->{display}->{whitespace}; - } elsif ( $Element->isa('PPI::Token::Comment') ) { + } + elsif ( $Element->isa('PPI::Token::Comment') ) { $show = 0 unless $self->{display}->{comments}; } push @$output, $self->_element_string( $Element, $indent ) if $show; @@ -216,7 +207,7 @@ sub _dump { # Recurse into our children if ( $Element->isa('PPI::Node') ) { my $child_indent = $indent . $self->{indent_string}; - foreach my $child ( @{$Element->{children}} ) { + foreach my $child ( @{ $Element->{children} } ) { $self->_dump( $child, $child_indent, $output ); } } @@ -226,7 +217,7 @@ sub _dump { sub _element_string { my $self = ref $_[0] ? shift : shift->new(shift); - my $Element = _INSTANCE($_[0], 'PPI::Element') ? shift : $self->{root}; + my $Element = _INSTANCE( $_[0], 'PPI::Element' ) ? shift : $self->{root}; my $indent = shift || ''; my $string = ''; @@ -234,20 +225,20 @@ sub _element_string { if ( $self->{display}->{memaddr} ) { $string .= $Element->refaddr . ' '; } - - # Add the location if such exists + + # Add the location if such exists if ( $self->{display}->{locations} ) { my $loc_string; if ( $Element->isa('PPI::Token') ) { my $location = $Element->location; if ($location) { - $loc_string = sprintf("[ % 4d, % 3d, % 3d ] ", @$location); + $loc_string = sprintf( "[ % 4d, % 3d, % 3d ] ", @$location ); } } # Output location or pad with 20 spaces $string .= $loc_string || " " x 20; } - + # Add the indent if ( $self->{display}->{indent} ) { $string .= $indent; @@ -268,19 +259,22 @@ sub _element_string { $string .= " \t'$content'"; } - } elsif ( $Element->isa('PPI::Structure') ) { + } + elsif ( $Element->isa('PPI::Structure') ) { # Add the content if ( $self->{display}->{content} ) { - my $start = $Element->start - ? $Element->start->content - : '???'; - my $finish = $Element->finish - ? $Element->finish->content - : '???'; + my $start = + $Element->start + ? $Element->start->content + : '???'; + my $finish = + $Element->finish + ? $Element->finish->content + : '???'; $string .= " \t$start ... $finish"; } } - + $string; } diff --git a/lib/PPI/Element.pm b/lib/PPI/Element.pm index 9b3e15de..be39d4c5 100644 --- a/lib/PPI/Element.pm +++ b/lib/PPI/Element.pm @@ -22,11 +22,11 @@ implementations. =cut use strict; -use Clone 0.30 (); -use Scalar::Util qw{refaddr}; -use Params::Util qw{_INSTANCE _ARRAY}; -use PPI::Util (); -use PPI::Node (); +use Clone 0.30 (); +use Scalar::Util qw{refaddr}; +use Params::Util qw{_INSTANCE _ARRAY}; +use PPI::Util (); +use PPI::Node (); use PPI::Singletons '%_PARENT', '%_POSITION_CACHE'; our $VERSION = '1.282'; @@ -40,10 +40,6 @@ use overload '!=' => '__nequals'; use overload 'eq' => '__eq'; use overload 'ne' => '__ne'; - - - - ##################################################################### # General Properties @@ -77,7 +73,7 @@ Returns the class of the Element as a string =cut -sub class { ref($_[0]) } +sub class { ref( $_[0] ) } =pod @@ -119,10 +115,6 @@ Returns the basic code as a string (excluding here-doc content). ### XS -> PPI/XS.xs:_PPI_Element__content 0.900+ sub content() { '' } - - - - ##################################################################### # Navigation Methods @@ -139,7 +131,7 @@ Node. =cut -sub parent { $_PARENT{refaddr $_[0]} } +sub parent { $_PARENT{ refaddr $_[0] } } =pod @@ -155,7 +147,7 @@ sub descendant_of { my $cursor = shift; my $parent = shift or return undef; while ( refaddr $cursor != refaddr $parent ) { - $cursor = $_PARENT{refaddr $cursor} or return ''; + $cursor = $_PARENT{ refaddr $cursor } or return ''; } return 1; } @@ -174,7 +166,7 @@ sub ancestor_of { my $self = shift; my $cursor = shift or return undef; while ( refaddr $cursor != refaddr $self ) { - $cursor = $_PARENT{refaddr $cursor} or return ''; + $cursor = $_PARENT{ refaddr $cursor } or return ''; } return 1; } @@ -197,8 +189,8 @@ a Statement. sub statement { my $cursor = shift; - while ( ! _INSTANCE($cursor, 'PPI::Statement') ) { - $cursor = $_PARENT{refaddr $cursor} or return ''; + while ( !_INSTANCE( $cursor, 'PPI::Statement' ) ) { + $cursor = $_PARENT{ refaddr $cursor } or return ''; } $cursor; } @@ -220,7 +212,7 @@ not within any parent PDOM object. sub top { my $cursor = shift; - while ( my $parent = $_PARENT{refaddr $cursor} ) { + while ( my $parent = $_PARENT{ refaddr $cursor } ) { $cursor = $parent; } $cursor; @@ -240,7 +232,7 @@ contained within a Document. sub document { my $top = shift->top; - _INSTANCE($top, 'PPI::Document') and $top; + _INSTANCE( $top, 'PPI::Document' ) and $top; } =pod @@ -259,7 +251,7 @@ sub next_sibling { my $parent = $_PARENT{$key} or return ''; my $elements = $parent->{children}; my $position = $parent->__position($self); - $elements->[$position + 1] || ''; + $elements->[ $position + 1 ] || ''; } =pod @@ -280,7 +272,7 @@ sub snext_sibling { my $parent = $_PARENT{$key} or return ''; my $elements = $parent->{children}; my $position = $parent->__position($self); - while ( defined(my $it = $elements->[++$position]) ) { + while ( defined( my $it = $elements->[ ++$position ] ) ) { return $it if $it->significant; } ''; @@ -303,7 +295,7 @@ sub previous_sibling { my $parent = $_PARENT{$key} or return ''; my $elements = $parent->{children}; my $position = $parent->__position($self); - $position and $elements->[$position - 1] or ''; + $position and $elements->[ $position - 1 ] or ''; } =pod @@ -324,7 +316,7 @@ sub sprevious_sibling { my $parent = $_PARENT{$key} or return ''; my $elements = $parent->{children}; my $position = $parent->__position($self); - while ( $position-- and defined(my $it = $elements->[$position]) ) { + while ( $position-- and defined( my $it = $elements->[$position] ) ) { return $it if $it->significant; } ''; @@ -352,12 +344,11 @@ sub first_token { my $cursor = shift; while ( $cursor->isa('PPI::Node') ) { $cursor = $cursor->first_element - or die "Found empty PPI::Node while getting first token"; + or die "Found empty PPI::Node while getting first token"; } $cursor; } - =pod =head2 last_token @@ -380,7 +371,7 @@ sub last_token { my $cursor = shift; while ( $cursor->isa('PPI::Node') ) { $cursor = $cursor->last_element - or die "Found empty PPI::Node while getting first token"; + or die "Found empty PPI::Node while getting first token"; } $cursor; } @@ -409,9 +400,9 @@ sub next_token { my $cursor = shift; # Find the next element, going upwards as needed - while ( 1 ) { + while (1) { my $element = $cursor->next_sibling; - if ( $element ) { + if ($element) { return $element if $element->isa('PPI::Token'); return $element->first_token; } @@ -445,9 +436,9 @@ sub previous_token { my $cursor = shift; # Find the previous element, going upwards as needed - while ( 1 ) { + while (1) { my $element = $cursor->previous_sibling; - if ( $element ) { + if ($element) { return $element if $element->isa('PPI::Token'); return $element->last_token; } @@ -569,7 +560,7 @@ occurs while trying to remove the C. sub remove { my $self = shift; my $parent = $self->parent or return $self; - $parent->remove_child( $self ); + $parent->remove_child($self); } =pod @@ -609,7 +600,7 @@ If successful, returns the replace element. Otherwise, returns C. sub replace { my $self = ref $_[0] ? shift : return undef; - my $replace = _INSTANCE(shift, ref $self) or return undef; + my $replace = _INSTANCE( shift, ref $self ) or return undef; return $self->parent->replace_child( $self, $replace ); } @@ -646,7 +637,7 @@ sub location { $self->_ensure_location_present or return undef; # Return a copy, not the original - return [ @{$self->{_location}} ]; + return [ @{ $self->{_location} } ]; } =pod @@ -784,9 +775,9 @@ sub _ensure_location_present { # selectively flush only the part of the document that occurs after the # element for which the flush is called. sub _flush_locations { - my $self = shift; + my $self = shift; unless ( $self == $self->top ) { - return $self->top->_flush_locations( $self ); + return $self->top->_flush_locations($self); } # Get the full list of all Tokens @@ -794,11 +785,11 @@ sub _flush_locations { # Optionally allow starting from an arbitrary element (or rather, # the first Token equal-to-or-within an arbitrary element) - if ( _INSTANCE($_[0], 'PPI::Element') ) { + if ( _INSTANCE( $_[0], 'PPI::Element' ) ) { my $start = shift->first_token; while ( my $Token = shift @Tokens ) { return 1 unless $Token->{_location}; - next unless refaddr($Token) == refaddr($start); + next unless refaddr($Token) == refaddr($start); # Found the start. Flush its location delete $$Token->{_location}; @@ -807,24 +798,20 @@ sub _flush_locations { } # Iterate over any remaining Tokens and flush their location - foreach my $Token ( @Tokens ) { + foreach my $Token (@Tokens) { delete $Token->{_location}; } 1; } - - - - ##################################################################### # XML Compatibility Methods sub _xml_name { my $class = ref $_[0] || $_[0]; my $name = lc join( '_', split /::/, $class ); - substr($name, 4); + substr( $name, 4 ); } sub _xml_attr { @@ -835,10 +822,6 @@ sub _xml_content { defined $_[0]->{content} ? $_[0]->{content} : ''; } - - - - ##################################################################### # Internals @@ -859,16 +842,17 @@ sub _clear { # Therefore we don't need to remove ourselves from our parent, # just the index ( just in case ). sub DESTROY { - delete $_PARENT{refaddr $_[0]}; - delete $_POSITION_CACHE{refaddr $_[0]}; + delete $_PARENT{ refaddr $_[0] }; + delete $_POSITION_CACHE{ refaddr $_[0] }; } # Operator overloads -sub __equals { ref $_[1] and refaddr($_[0]) == refaddr($_[1]) } +sub __equals { ref $_[1] and refaddr( $_[0] ) == refaddr( $_[1] ) } sub __nequals { !__equals(@_) } + sub __eq { - my $self = _INSTANCE($_[0], 'PPI::Element') ? $_[0]->content : $_[0]; - my $other = _INSTANCE($_[1], 'PPI::Element') ? $_[1]->content : $_[1]; + my $self = _INSTANCE( $_[0], 'PPI::Element' ) ? $_[0]->content : $_[0]; + my $other = _INSTANCE( $_[1], 'PPI::Element' ) ? $_[1]->content : $_[1]; $self eq $other; } sub __ne { !__eq(@_) } diff --git a/lib/PPI/Exception.pm b/lib/PPI/Exception.pm index 781cd9a2..c73b139d 100755 --- a/lib/PPI/Exception.pm +++ b/lib/PPI/Exception.pm @@ -26,7 +26,6 @@ use Params::Util qw{_INSTANCE}; our $VERSION = '1.282'; - =head1 METHODS =head2 new $message | message => $message, ... @@ -41,12 +40,11 @@ C method. sub new { my $class = shift; - return bless { @_ }, $class if @_ > 1; + return bless {@_}, $class if @_ > 1; return bless { message => $_[0] }, $class if @_; return bless { message => 'Unknown Exception' }, $class; } - =head2 throw If called on a C object, throws the object. @@ -62,25 +60,24 @@ This method never returns. sub throw { my $it = shift; - if ( _INSTANCE($it, 'PPI::Exception') ) { + if ( _INSTANCE( $it, 'PPI::Exception' ) ) { if ( $it->{callers} ) { push @{ $it->{callers} }, [ caller(0) ]; - } else { + } + else { $it->{callers} ||= []; } - } else { + } + else { my $message = $_[0] || 'Unknown Exception'; $it = $it->new( message => $message, - callers => [ - [ caller(0) ], - ], + callers => [ [ caller(0) ], ], ); } die $it; } - =head2 message Returns the exception message passed to the object's constructor, @@ -92,7 +89,6 @@ sub message { $_[0]->{message}; } - =head2 callers Returns a listref, each element of which is a listref of C @@ -104,5 +100,4 @@ sub callers { @{ $_[0]->{callers} || [] }; } - 1; diff --git a/lib/PPI/Find.pm b/lib/PPI/Find.pm index 6c1c1d9b..d3b8d584 100644 --- a/lib/PPI/Find.pm +++ b/lib/PPI/Find.pm @@ -76,10 +76,6 @@ use Params::Util qw{_INSTANCE}; our $VERSION = '1.282'; - - - - ##################################################################### # Constructor @@ -95,13 +91,11 @@ Returns a new PPI::Find object, or C if not passed a CODE reference. =cut sub new { - my $class = ref $_[0] ? ref shift : shift; - my $wanted = ref $_[0] eq 'CODE' ? shift : return undef; + my $class = ref $_[0] ? ref shift : shift; + my $wanted = ref $_[0] eq 'CODE' ? shift : return undef; # Create the object - my $self = bless { - wanted => $wanted, - }, $class; + my $self = bless { wanted => $wanted, }, $class; $self; } @@ -121,22 +115,18 @@ Returns a duplicate PPI::Find object. =cut sub clone { - my $self = ref $_[0] ? shift - : die "->clone can only be called as an object method"; + my $self = + ref $_[0] + ? shift + : die "->clone can only be called as an object method"; my $class = ref $self; # Create the object - my $clone = bless { - wanted => $self->{wanted}, - }, $class; + my $clone = bless { wanted => $self->{wanted}, }, $class; $clone; } - - - - #################################################################### # Search Execution Methods @@ -171,15 +161,17 @@ sub in { my $Element = shift; my %params = @_; delete $self->{errstr}; - + # Are we already acting as an iterator if ( $self->{in} ) { - return $self->_error('->in called while another search is in progress', %params); + return $self->_error( '->in called while another search is in progress', + %params ); } # Get the root element for the search - unless ( _INSTANCE($Element, 'PPI::Element') ) { - return $self->_error('->in was not passed a PPI::Element object', %params); + unless ( _INSTANCE( $Element, 'PPI::Element' ) ) { + return $self->_error( '->in was not passed a PPI::Element object', + %params ); } # Prepare the search @@ -190,13 +182,13 @@ sub in { if ( !eval { $self->_execute; 1 } ) { my $errstr = $@; $errstr =~ s/\s+at\s+line\s+.+$//; - return $self->_error("Error while searching: $errstr", %params); + return $self->_error( "Error while searching: $errstr", %params ); } # Clean up and return delete $self->{in}; if ( $params{array_ref} ) { - if ( @{$self->{matches}} ) { + if ( @{ $self->{matches} } ) { return delete $self->{matches}; } delete $self->{matches}; @@ -234,7 +226,7 @@ sub start { } # Get the root element for the search - unless ( _INSTANCE($Element, 'PPI::Element') ) { + unless ( _INSTANCE( $Element, 'PPI::Element' ) ) { return $self->_error('->in was not passed a PPI::Element object'); } @@ -269,7 +261,7 @@ sub match { return undef unless $self->{matches}; # Fetch and return the next match - my $match = shift @{$self->{matches}}; + my $match = shift @{ $self->{matches} }; return $match if $match; $self->finish; @@ -303,10 +295,6 @@ sub finish { 1; } - - - - ##################################################################### # Support Methods and Error Handling @@ -320,7 +308,7 @@ sub _execute { my $rv = &$wanted( $Element, $self->{in} ); # Add to the matches if returns true - push @{$self->{matches}}, $Element if $rv; + push @{ $self->{matches} }, $Element if $rv; # Continue and don't descend if it returned undef # or if it doesn't have children @@ -332,7 +320,8 @@ sub _execute { unshift @queue, $Element->finish if $Element->finish; unshift @queue, $Element->children; unshift @queue, $Element->start if $Element->start; - } else { + } + else { unshift @queue, $Element->children; } } diff --git a/lib/PPI/Lexer.pm b/lib/PPI/Lexer.pm index 469823cf..8ef6bad7 100644 --- a/lib/PPI/Lexer.pm +++ b/lib/PPI/Lexer.pm @@ -54,10 +54,10 @@ For more unusual tasks, by all means forge onwards. =cut use strict; -use Scalar::Util (); -use Params::Util qw{_STRING _INSTANCE}; -use PPI (); -use PPI::Exception (); +use Scalar::Util (); +use Params::Util qw{_STRING _INSTANCE}; +use PPI (); +use PPI::Exception (); use PPI::Singletons '%_PARENT'; our $VERSION = '1.282'; @@ -89,10 +89,6 @@ my %RESOLVE = ( our $X_TOKENIZER = "PPI::Tokenizer"; sub X_TOKENIZER { $X_TOKENIZER } - - - - ##################################################################### # Constructor @@ -111,16 +107,12 @@ Returns a new C object sub new { my $class = shift->_clear; bless { - Tokenizer => undef, # Where we store the tokenizer for a run - buffer => [], # The input token buffer - delayed => [], # The "delayed insignificant tokens" buffer + Tokenizer => undef, # Where we store the tokenizer for a run + buffer => [], # The input token buffer + delayed => [], # The "delayed insignificant tokens" buffer }, $class; } - - - - ##################################################################### # Main Lexing Methods @@ -148,13 +140,12 @@ sub lex_file { my %args = @_; # Create the Tokenizer - my $Tokenizer = eval { - X_TOKENIZER->new($file); - }; - if ( _INSTANCE($@, 'PPI::Exception') ) { + my $Tokenizer = eval { X_TOKENIZER->new($file); }; + if ( _INSTANCE( $@, 'PPI::Exception' ) ) { return $self->_error( $@->message ); - } elsif ( $@ ) { - return $self->_error( $errstr ); + } + elsif ($@) { + return $self->_error($errstr); } $self->lex_tokenizer( $Tokenizer, %args ); @@ -183,13 +174,12 @@ sub lex_source { my %args = @_; # Create the Tokenizer and hand off to the next method - my $Tokenizer = eval { - X_TOKENIZER->new(\$source); - }; - if ( _INSTANCE($@, 'PPI::Exception') ) { + my $Tokenizer = eval { X_TOKENIZER->new( \$source ); }; + if ( _INSTANCE( $@, 'PPI::Exception' ) ) { return $self->_error( $@->message ); - } elsif ( $@ ) { - return $self->_error( $errstr ); + } + elsif ($@) { + return $self->_error($errstr); } $self->lex_tokenizer( $Tokenizer, %args ); @@ -210,10 +200,10 @@ Returns a L object, or C on error. sub lex_tokenizer { my $self = ref $_[0] ? shift : shift->new; - my $Tokenizer = _INSTANCE(shift, 'PPI::Tokenizer'); + my $Tokenizer = _INSTANCE( shift, 'PPI::Tokenizer' ); return $self->_error( - "Did not pass a PPI::Tokenizer object to PPI::Lexer::lex_tokenizer" - ) unless $Tokenizer; + "Did not pass a PPI::Tokenizer object to PPI::Lexer::lex_tokenizer") + unless $Tokenizer; my %args = @_; # Create the empty document @@ -227,31 +217,28 @@ sub lex_tokenizer { # If an error occurs DESTROY the partially built document. $Tokenizer->_document(undef); undef $Document; - if ( _INSTANCE($@, 'PPI::Exception') ) { + if ( _INSTANCE( $@, 'PPI::Exception' ) ) { return $self->_error( $@->message ); - } else { - return $self->_error( $errstr ); + } + else { + return $self->_error($errstr); } } return $Document; } - - - - ##################################################################### # Lex Methods - Document Object sub _lex_document { - my ($self, $Document) = @_; + my ( $self, $Document ) = @_; # my $self = shift; # my $Document = _INSTANCE(shift, 'PPI::Document') or return undef; # Start the processing loop my $Token; - while ( ref($Token = $self->_get_token) ) { + while ( ref( $Token = $self->_get_token ) ) { # Add insignificant tokens directly beneath us unless ( $Token->significant ) { $self->_add_element( $Document, $Token ); @@ -261,22 +248,20 @@ sub _lex_document { if ( $Token->content eq ';' ) { # It's a semi-colon on its own. # We call this a null statement. - $self->_add_element( - $Document, - PPI::Statement::Null->new($Token), - ); + $self->_add_element( $Document, + PPI::Statement::Null->new($Token), ); next; } # Handle anything other than a structural element unless ( ref $Token eq 'PPI::Token::Structure' ) { # Determine the class for the Statement, and create it - my $Statement = $self->_statement($Document, $Token)->new($Token); + my $Statement = $self->_statement( $Document, $Token )->new($Token); # Move the lexing down into the statement - $self->_add_delayed( $Document ); + $self->_add_delayed($Document); $self->_add_element( $Document, $Statement ); - $self->_lex_statement( $Statement ); + $self->_lex_statement($Statement); next; } @@ -284,10 +269,10 @@ sub _lex_document { # Is this the opening of a structure? if ( $Token->__LEXER__opens ) { # This should actually have a Statement instead - $self->_rollback( $Token ); + $self->_rollback($Token); my $Statement = PPI::Statement->new; $self->_add_element( $Document, $Statement ); - $self->_lex_statement( $Statement ); + $self->_lex_statement($Statement); next; } @@ -297,8 +282,7 @@ sub _lex_document { # This means either a mis-parsing, or a mistake in the code. # To handle this, we create a "Naked Close" statement $self->_add_element( $Document, - PPI::Statement::UnmatchedBrace->new($Token) - ); + PPI::Statement::UnmatchedBrace->new($Token) ); next; } @@ -315,16 +299,16 @@ sub _lex_document { # No error, it's just the end of file. # Add any insignificant trailing tokens. - $self->_add_delayed( $Document ); + $self->_add_delayed($Document); # If the Tokenizer has any v6 blocks to attach, do so now. # Checking once at the end is faster than adding a special # case check for every statement parsed. my $perl6 = $self->{Tokenizer}->{'perl6'}; - if ( @$perl6 ) { - my $includes = $Document->find( 'PPI::Statement::Include::Perl6' ); - foreach my $include ( @$includes ) { - unless ( @$perl6 ) { + if (@$perl6) { + my $includes = $Document->find('PPI::Statement::Include::Perl6'); + foreach my $include (@$includes) { + unless (@$perl6) { PPI::Exception->throw('Failed to find a perl6 section'); } $include->{perl6} = shift @$perl6; @@ -334,10 +318,6 @@ sub _lex_document { return 1; } - - - - ##################################################################### # Lex Methods - Statement Object @@ -351,89 +331,86 @@ my %STATEMENT_CLASSES = ( 'END' => 'PPI::Statement::Scheduled', # Special subroutines for which 'sub' is optional - 'AUTOLOAD' => 'PPI::Statement::Sub', - 'DESTROY' => 'PPI::Statement::Sub', + 'AUTOLOAD' => 'PPI::Statement::Sub', + 'DESTROY' => 'PPI::Statement::Sub', # Loading and context statement - 'package' => 'PPI::Statement::Package', + 'package' => 'PPI::Statement::Package', # 'use' => 'PPI::Statement::Include', - 'no' => 'PPI::Statement::Include', - 'require' => 'PPI::Statement::Include', + 'no' => 'PPI::Statement::Include', + 'require' => 'PPI::Statement::Include', # Various declarations - 'my' => 'PPI::Statement::Variable', - 'local' => 'PPI::Statement::Variable', - 'our' => 'PPI::Statement::Variable', - 'state' => 'PPI::Statement::Variable', + 'my' => 'PPI::Statement::Variable', + 'local' => 'PPI::Statement::Variable', + 'our' => 'PPI::Statement::Variable', + 'state' => 'PPI::Statement::Variable', # Statements starting with 'sub' could be any one of... # 'sub' => 'PPI::Statement::Sub', # 'sub' => 'PPI::Statement::Scheduled', # 'sub' => 'PPI::Statement', # Compound statement - 'if' => 'PPI::Statement::Compound', - 'unless' => 'PPI::Statement::Compound', - 'for' => 'PPI::Statement::Compound', - 'foreach' => 'PPI::Statement::Compound', - 'while' => 'PPI::Statement::Compound', - 'until' => 'PPI::Statement::Compound', + 'if' => 'PPI::Statement::Compound', + 'unless' => 'PPI::Statement::Compound', + 'for' => 'PPI::Statement::Compound', + 'foreach' => 'PPI::Statement::Compound', + 'while' => 'PPI::Statement::Compound', + 'until' => 'PPI::Statement::Compound', # Switch statement - 'given' => 'PPI::Statement::Given', - 'when' => 'PPI::Statement::When', - 'default' => 'PPI::Statement::When', + 'given' => 'PPI::Statement::Given', + 'when' => 'PPI::Statement::When', + 'default' => 'PPI::Statement::When', # Various ways of breaking out of scope - 'redo' => 'PPI::Statement::Break', - 'next' => 'PPI::Statement::Break', - 'last' => 'PPI::Statement::Break', - 'return' => 'PPI::Statement::Break', - 'goto' => 'PPI::Statement::Break', + 'redo' => 'PPI::Statement::Break', + 'next' => 'PPI::Statement::Break', + 'last' => 'PPI::Statement::Break', + 'return' => 'PPI::Statement::Break', + 'goto' => 'PPI::Statement::Break', # Special sections of the file - '__DATA__' => 'PPI::Statement::Data', - '__END__' => 'PPI::Statement::End', + '__DATA__' => 'PPI::Statement::Data', + '__END__' => 'PPI::Statement::End', ); sub _statement { - my ($self, $Parent, $Token) = @_; + my ( $self, $Parent, $Token ) = @_; # my $self = shift; # my $Parent = _INSTANCE(shift, 'PPI::Node') or die "Bad param 1"; # my $Token = _INSTANCE(shift, 'PPI::Token') or die "Bad param 2"; # Check for things like ( parent => ... ) - if ( - $Parent->isa('PPI::Structure::List') - or - $Parent->isa('PPI::Structure::Constructor') - ) { + if ( $Parent->isa('PPI::Structure::List') + or $Parent->isa('PPI::Structure::Constructor') ) + { if ( $Token->isa('PPI::Token::Word') ) { # Is the next significant token a => # Read ahead to the next significant token my $Next; while ( $Next = $self->_get_token ) { unless ( $Next->significant ) { - push @{$self->{delayed}}, $Next; + push @{ $self->{delayed} }, $Next; # $self->_delay_element( $Next ); next; } # Got the next token - if ( - $Next->isa('PPI::Token::Operator') - and - $Next->content eq '=>' - ) { + if ( $Next->isa('PPI::Token::Operator') + and $Next->content eq '=>' ) + { # Is an ordinary expression - $self->_rollback( $Next ); + $self->_rollback($Next); return 'PPI::Statement::Expression'; - } else { + } + else { last; } } # Rollback and continue - $self->_rollback( $Next ); + $self->_rollback($Next); } } @@ -446,13 +423,13 @@ sub _statement { !!( $Parent->schild(-1) || $Parent )->presumed_features->{try}, }->{ $Token->content }; - if ( $class ) { + if ($class) { # Is the next significant token a => # Read ahead to the next significant token my $Next; while ( $Next = $self->_get_token ) { if ( !$Next->significant ) { - push @{$self->{delayed}}, $Next; + push @{ $self->{delayed} }, $Next; next; } @@ -461,35 +438,37 @@ sub _statement { # open( CHECK, ... ); if ( 'PPI::Statement::Scheduled' eq $class - and not ( $Next->isa( 'PPI::Token::Structure' ) - and $Next->content =~ m/\A[{;]\z/ ) # } - ) { + and not($Next->isa('PPI::Token::Structure') + and $Next->content =~ m/\A[{;]\z/ ) # } + ) + { $class = undef; last; } # Lexical subroutine - if ( - $Token->content =~ /^(?:my|our|state)$/ - and $Next->isa( 'PPI::Token::Word' ) and $Next->content eq 'sub' - ) { - # This should be PPI::Statement::Sub rather than PPI::Statement::Variable - $class = undef; + if ( $Token->content =~ /^(?:my|our|state)$/ + and $Next->isa('PPI::Token::Word') + and $Next->content eq 'sub' ) + { + # This should be PPI::Statement::Sub rather than PPI::Statement::Variable + $class = undef; $is_lexsub = 1; last; } - last if - !$Next->isa( 'PPI::Token::Operator' ) or $Next->content ne '=>'; + last + if !$Next->isa('PPI::Token::Operator') + or $Next->content ne '=>'; # Got the next token # Is an ordinary expression - $self->_rollback( $Next ); + $self->_rollback($Next); return 'PPI::Statement'; } # Rollback and continue - $self->_rollback( $Next ); + $self->_rollback($Next); } # Handle potential barewords for subscripts @@ -505,7 +484,7 @@ sub _statement { my $Next; while ( $Next = $self->_get_token ) { unless ( $Next->significant ) { - push @{$self->{delayed}}, $Next; + push @{ $self->{delayed} }, $Next; # $self->_delay_element( $Next ); next; } @@ -513,17 +492,18 @@ sub _statement { # Found the next significant token. # Is it a closing curly brace? if ( $Next->content eq '}' ) { - $self->_rollback( $Next ); + $self->_rollback($Next); return 'PPI::Statement::Expression'; - } else { - $self->_rollback( $Next ); + } + else { + $self->_rollback($Next); return $class; } } # End of file... this means it is something like $h{our # which is probably going to be $h{our} ... I think - $self->_rollback( $Next ); + $self->_rollback($Next); return 'PPI::Statement::Expression'; } @@ -536,19 +516,19 @@ sub _statement { my $Next; while ( $Next = $self->_get_token ) { unless ( $Next->significant ) { - push @{$self->{delayed}}, $Next; + push @{ $self->{delayed} }, $Next; # $self->_delay_element( $Next ); next; } # Got the next significant token - my $sclass = $STATEMENT_CLASSES{$Next->content}; + my $sclass = $STATEMENT_CLASSES{ $Next->content }; if ( $sclass and $sclass eq 'PPI::Statement::Scheduled' ) { - $self->_rollback( $Next ); + $self->_rollback($Next); return 'PPI::Statement::Scheduled'; } if ( $Next->isa('PPI::Token::Word') ) { - $self->_rollback( $Next ); + $self->_rollback($Next); return 'PPI::Statement::Sub'; } @@ -564,12 +544,12 @@ sub _statement { # } # PPI::Statement is the safest fall-through - $self->_rollback( $Next ); + $self->_rollback($Next); return 'PPI::Statement'; } # End of file... PPI::Statement::Sub is the most likely - $self->_rollback( $Next ); + $self->_rollback($Next); return 'PPI::Statement::Sub'; } @@ -578,33 +558,33 @@ sub _statement { my $Next; while ( $Next = $self->_get_token ) { unless ( $Next->significant ) { - push @{$self->{delayed}}, $Next; + push @{ $self->{delayed} }, $Next; # $self->_delay_element( $Next ); next; } # Found the next significant token. - if ( - $Next->isa('PPI::Token::Operator') - and - $Next->content eq '=>' - ) { + if ( $Next->isa('PPI::Token::Operator') + and $Next->content eq '=>' ) + { # Is an ordinary expression - $self->_rollback( $Next ); + $self->_rollback($Next); return 'PPI::Statement'; - # Is it a v6 use? - } elsif ( $Next->content eq 'v6' ) { - $self->_rollback( $Next ); + # Is it a v6 use? + } + elsif ( $Next->content eq 'v6' ) { + $self->_rollback($Next); return 'PPI::Statement::Include::Perl6'; - } else { - $self->_rollback( $Next ); + } + else { + $self->_rollback($Next); return 'PPI::Statement::Include'; } } # End of file... this means it is an incomplete use # line, just treat it as a normal include. - $self->_rollback( $Next ); + $self->_rollback($Next); return 'PPI::Statement::Include'; } @@ -619,15 +599,13 @@ sub _statement { } # Switch statements use expressions, as well. - if ( - $Parent->isa('PPI::Structure::Given') - or - $Parent->isa('PPI::Structure::When') - ) { + if ( $Parent->isa('PPI::Structure::Given') + or $Parent->isa('PPI::Structure::When') ) + { return 'PPI::Statement::Expression'; } - if ( _INSTANCE($Token, 'PPI::Token::Label') ) { + if ( _INSTANCE( $Token, 'PPI::Token::Label' ) ) { return 'PPI::Statement::Compound'; } @@ -637,13 +615,13 @@ sub _statement { } sub _lex_statement { - my ($self, $Statement) = @_; + my ( $self, $Statement ) = @_; # my $self = shift; # my $Statement = _INSTANCE(shift, 'PPI::Statement') or die "Bad param 1"; # Handle some special statements if ( $Statement->isa('PPI::Statement::End') ) { - return $self->_lex_end( $Statement ); + return $self->_lex_end($Statement); } # Begin processing tokens @@ -651,20 +629,18 @@ sub _lex_statement { while ( ref( $Token = $self->_get_token ) ) { # Delay whitespace and comment tokens unless ( $Token->significant ) { - push @{$self->{delayed}}, $Token; + push @{ $self->{delayed} }, $Token; # $self->_delay_element( $Token ); next; } # Structual closes, and __DATA__ and __END__ tags implicitly # end every type of statement - if ( - $Token->__LEXER__closes - or - $Token->isa('PPI::Token::Separator') - ) { + if ( $Token->__LEXER__closes + or $Token->isa('PPI::Token::Separator') ) + { # Rollback and end the statement - return $self->_rollback( $Token ); + return $self->_rollback($Token); } # Normal statements never implicitly end @@ -672,7 +648,7 @@ sub _lex_statement { # Have we hit an implicit end to the statement unless ( $self->_continues( $Statement, $Token ) ) { # Rollback and finish the statement - return $self->_rollback( $Token ); + return $self->_rollback($Token); } } @@ -691,13 +667,13 @@ sub _lex_statement { # Which leaves us with a new structure # Determine the class for the structure and create it - my $method = $RESOLVE{$Token->content}; + my $method = $RESOLVE{ $Token->content }; my $Structure = $self->$method($Statement)->new($Token); # Move the lexing down into the Structure - $self->_add_delayed( $Statement ); + $self->_add_delayed($Statement); $self->_add_element( $Statement, $Structure ); - $self->_lex_structure( $Structure ); + $self->_lex_structure($Structure); } # Was it an error in the tokenizer? @@ -705,24 +681,23 @@ sub _lex_statement { PPI::Exception->throw; } - # No, it's just the end of the file... - # Roll back any insignificant tokens, they'll get added at the Document level + # No, it's just the end of the file... + # Roll back any insignificant tokens, they'll get added at the Document level $self->_rollback; } sub _lex_end { - my ($self, $Statement) = @_; - # my $self = shift; - # my $Statement = _INSTANCE(shift, 'PPI::Statement::End') or die "Bad param 1"; + my ( $self, $Statement ) = @_; + # my $self = shift; + # my $Statement = _INSTANCE(shift, 'PPI::Statement::End') or die "Bad param 1"; # End of the file, EVERYTHING is ours my $Token; while ( $Token = $self->_get_token ) { # Inlined $Statement->__add_element($Token); - Scalar::Util::weaken( - $_PARENT{Scalar::Util::refaddr $Token} = $Statement - ); - push @{$Statement->{children}}, $Token; + Scalar::Util::weaken( $_PARENT{ Scalar::Util::refaddr $Token } = + $Statement ); + push @{ $Statement->{children} }, $Token; } # Was it an error in the tokenizer? @@ -740,18 +715,16 @@ sub _lex_end { # to determine if the there is a statement boundary between the two, or if # the statement can continue with the token. sub _continues { - my ($self, $Statement, $Token) = @_; + my ( $self, $Statement, $Token ) = @_; # my $self = shift; # my $Statement = _INSTANCE(shift, 'PPI::Statement') or die "Bad param 1"; # my $Token = _INSTANCE(shift, 'PPI::Token') or die "Bad param 2"; # Handle the simple block case # { print 1; } - if ( - $Statement->schildren == 1 - and - $Statement->schild(0)->isa('PPI::Structure::Block') - ) { + if ( $Statement->schildren == 1 + and $Statement->schild(0)->isa('PPI::Structure::Block') ) + { return ''; } @@ -759,7 +732,7 @@ sub _continues { # ::Scheduled blocks, ::Sub declarations, ::Compound, ::Given, ::When, # and ::Package statements. return 1 - if ref $Statement !~ /\b(?:Scheduled|Sub|Compound|Given|When|Package)$/; + if ref $Statement !~ /\b(?:Scheduled|Sub|Compound|Given|When|Package)$/; # Of these six, ::Scheduled, ::Sub, ::Given, and ::When follow the same # simple rule and can be handled first. The block form of ::Package @@ -771,7 +744,7 @@ sub _continues { # If the last significant element of the statement is a block, # then an implied-end statement is done, no questions asked. return !$LastChild->isa('PPI::Structure::Block') - if !$Statement->isa('PPI::Statement::Compound'); + if !$Statement->isa('PPI::Statement::Compound'); # Now we get to compound statements, which kind of suck (to lex). # However, of them all, the 'if' type, which includes unless, are @@ -794,27 +767,21 @@ sub _continues { # If the token before the block is an 'else', # it's over, no matter what. my $NextLast = $Statement->schild(-2); - if ( - $NextLast - and - $NextLast->isa('PPI::Token') - and - $NextLast->isa('PPI::Token::Word') - and - $NextLast->content eq 'else' - ) { + if ( $NextLast + and $NextLast->isa('PPI::Token') + and $NextLast->isa('PPI::Token::Word') + and $NextLast->content eq 'else' ) + { return ''; } # Otherwise, we continue for 'elsif' or 'else' only. if ( $Token->isa('PPI::Token::Word') - and ( - $Token->content eq 'else' - or - $Token->content eq 'elsif' - ) - ) { + and ( $Token->content eq 'else' + or $Token->content eq 'elsif' ) + ) + { return 1; } @@ -831,11 +798,9 @@ sub _continues { # LABEL BLOCK continue BLOCK # Handle cases with a word after the label - if ( - $Token->isa('PPI::Token::Word') - and - $Token->content =~ /^(?:while|until|for|foreach)$/ - ) { + if ( $Token->isa('PPI::Token::Word') + and $Token->content =~ /^(?:while|until|for|foreach)$/ ) + { return 1; } @@ -860,21 +825,18 @@ sub _continues { if ( $type eq 'for' ) { # LABEL for (EXPR; EXPR; EXPR) BLOCK - if ( - $LastChild->isa('PPI::Token::Word') - and - $LastChild->content =~ /^for(?:each)?\z/ - ) { + if ( $LastChild->isa('PPI::Token::Word') + and $LastChild->content =~ /^for(?:each)?\z/ ) + { # LABEL for ... if ( ( - $Token->isa('PPI::Token::Structure') - and - $Token->content eq '(' + $Token->isa('PPI::Token::Structure') + and $Token->content eq '(' ) - or - $Token->isa('PPI::Token::QuoteLike::Words') - ) { + or $Token->isa('PPI::Token::QuoteLike::Words') + ) + { return 1; } @@ -882,27 +844,33 @@ sub _continues { # LABEL for VAR QW{} ... # LABEL foreach VAR QW{} ... # Only a block will do - return $Token->isa('PPI::Token::Structure') && $Token->content eq '{'; + return $Token->isa('PPI::Token::Structure') + && $Token->content eq '{'; } # In this case, we can also behave like a foreach $type = 'foreach'; - } elsif ( $LastChild->isa('PPI::Structure::Block') ) { + } + elsif ( $LastChild->isa('PPI::Structure::Block') ) { # LABEL for (EXPR; EXPR; EXPR) BLOCK # That's it, nothing can continue return ''; - } elsif ( $LastChild->isa('PPI::Token::QuoteLike::Words') ) { + } + elsif ( $LastChild->isa('PPI::Token::QuoteLike::Words') ) { # LABEL for VAR QW{} ... # LABEL foreach VAR QW{} ... # Only a block will do - return $Token->isa('PPI::Token::Structure') && $Token->content eq '{'; + return $Token->isa('PPI::Token::Structure') + && $Token->content eq '{'; } } # Handle the common continue case - if ( $LastChild->isa('PPI::Token::Word') and $LastChild->content eq 'continue' ) { + if ( $LastChild->isa('PPI::Token::Word') + and $LastChild->content eq 'continue' ) + { # LABEL while (EXPR) BLOCK continue ... # LABEL foreach VAR (LIST) BLOCK continue ... # LABEL BLOCK continue ... @@ -935,7 +903,9 @@ sub _continues { # LABEL foreach VAR (LIST) BLOCK ... # LABEL BLOCK ... # Is this the block for a continue? - if ( _INSTANCE($part[-2], 'PPI::Token::Word') and $part[-2]->content eq 'continue' ) { + if ( _INSTANCE( $part[-2], 'PPI::Token::Word' ) + and $part[-2]->content eq 'continue' ) + { # LABEL while (EXPR) BLOCK continue BLOCK # LABEL foreach VAR (LIST) BLOCK continue BLOCK # LABEL BLOCK continue BLOCK @@ -960,16 +930,15 @@ sub _continues { # The only case not covered is the while ... if ( $LastChild->isa('PPI::Token::Word') - and ( - $LastChild->content eq 'while' - or - $LastChild->content eq 'until' - ) - ) { + and ( $LastChild->content eq 'while' + or $LastChild->content eq 'until' ) + ) + { # LABEL while ... # LABEL until ... # Only a condition structure will do - return $Token->isa('PPI::Token::Structure') && $Token->content eq '('; + return $Token->isa('PPI::Token::Structure') + && $Token->content eq '('; } } @@ -982,40 +951,44 @@ sub _continues { if ( $LastChild->isa('PPI::Token::Symbol') ) { # LABEL foreach my $scalar ... # Open round brace, or a quotewords - return 1 if $Token->isa('PPI::Token::Structure') && $Token->content eq '('; + return 1 + if $Token->isa('PPI::Token::Structure') && $Token->content eq '('; return 1 if $Token->isa('PPI::Token::QuoteLike::Words'); return ''; } - if ( $LastChild->content eq 'foreach' or $LastChild->content eq 'for' ) { + if ( $LastChild->content eq 'foreach' or $LastChild->content eq 'for' ) + { # There are three possibilities here if ( $Token->isa('PPI::Token::Word') - and ( - ($STATEMENT_CLASSES{ $Token->content } || '') - eq - 'PPI::Statement::Variable' - ) - ) { + and ( ( $STATEMENT_CLASSES{ $Token->content } || '' ) eq + 'PPI::Statement::Variable' ) + ) + { # VAR == 'my ...' return 1; - } elsif ( $Token->content =~ /^\$/ ) { + } + elsif ( $Token->content =~ /^\$/ ) { # VAR == '$scalar' return 1; - } elsif ( $Token->isa('PPI::Token::Structure') and $Token->content eq '(' ) { + } + elsif ( $Token->isa('PPI::Token::Structure') + and $Token->content eq '(' ) + { return 1; - } elsif ( $Token->isa('PPI::Token::QuoteLike::Words') ) { + } + elsif ( $Token->isa('PPI::Token::QuoteLike::Words') ) { return 1; - } else { + } + else { return ''; } } - if ( - ($STATEMENT_CLASSES{ $LastChild->content } || '') - eq - 'PPI::Statement::Variable' - ) { + if ( ( $STATEMENT_CLASSES{ $LastChild->content } || '' ) eq + 'PPI::Statement::Variable' ) + { # LABEL foreach my ... # Only a scalar will do return $Token->content =~ /^\$/; @@ -1026,7 +999,8 @@ sub _continues { # LABEL for VAR QW ... # LABEL foreach VAR QW ... # Only a block will do - return $Token->isa('PPI::Token::Structure') && $Token->content eq '{'; + return $Token->isa('PPI::Token::Structure') + && $Token->content eq '{'; } } @@ -1034,25 +1008,21 @@ sub _continues { PPI::Exception->throw("Illegal state in '$type' compound statement"); } - - - - ##################################################################### # Lex Methods - Structure Object # Given a parent element, and a ( token to open a structure, determine # the class that the structure should be. sub _round { - my ($self, $Parent) = @_; + my ( $self, $Parent ) = @_; # my $self = shift; # my $Parent = _INSTANCE(shift, 'PPI::Node') or die "Bad param 1"; # Get the last significant element in the parent my $Element = $Parent->schild(-1); - if ( _INSTANCE($Element, 'PPI::Token::Word') ) { + if ( _INSTANCE( $Element, 'PPI::Token::Word' ) ) { # Can it be determined because it is a keyword? - my $rclass = $ROUND{$Element->content}; + my $rclass = $ROUND{ $Element->content }; return $rclass if $rclass; } @@ -1061,28 +1031,33 @@ sub _round { if ( $Parent->type =~ /^for(?:each)?$/ ) { return 'PPI::Structure::For'; } - } elsif ( $Parent->isa('PPI::Statement::Given') ) { + } + elsif ( $Parent->isa('PPI::Statement::Given') ) { return 'PPI::Structure::Given'; - } elsif ( $Parent->isa('PPI::Statement::When') ) { + } + elsif ( $Parent->isa('PPI::Statement::When') ) { return 'PPI::Structure::When'; - } elsif ( $Parent->isa('PPI::Statement::Sub') ) { + } + elsif ( $Parent->isa('PPI::Statement::Sub') ) { return 'PPI::Structure::Signature'; } # Otherwise, it must be a list # If the previous element is -> then we mark it as a dereference - if ( _INSTANCE($Element, 'PPI::Token::Operator') and $Element->content eq '->' ) { + if ( _INSTANCE( $Element, 'PPI::Token::Operator' ) + and $Element->content eq '->' ) + { $Element->{_dereference} = 1; } - 'PPI::Structure::List' + 'PPI::Structure::List'; } # Given a parent element, and a [ token to open a structure, determine # the class that the structure should be. sub _square { - my ($self, $Parent) = @_; + my ( $self, $Parent ) = @_; # my $self = shift; # my $Parent = _INSTANCE(shift, 'PPI::Node') or die "Bad param 1"; @@ -1090,9 +1065,11 @@ sub _square { my $Element = $Parent->schild(-1); # Is this a subscript, like $foo[1] or $foo{expr} - - if ( $Element ) { - if ( $Element->isa('PPI::Token::Operator') and $Element->content eq '->' ) { + + if ($Element) { + if ( $Element->isa('PPI::Token::Operator') + and $Element->content eq '->' ) + { # $foo->[] $Element->{_dereference} = 1; return 'PPI::Structure::Subscript'; @@ -1101,13 +1078,20 @@ sub _square { # $foo{}[] return 'PPI::Structure::Subscript'; } - if ( $Element->isa('PPI::Token::Symbol') and $Element->content =~ /^(?:\$|\@)/ ) { + if ( $Element->isa('PPI::Token::Symbol') + and $Element->content =~ /^(?:\$|\@)/ ) + { # $foo[], @foo[] return 'PPI::Structure::Subscript'; } - if ( $Element->isa('PPI::Token::Cast') and $Element->content =~ /^(?:\@|\%)/ ) { + if ( $Element->isa('PPI::Token::Cast') + and $Element->content =~ /^(?:\@|\%)/ ) + { my $prior = $Parent->schild(-2); - if ( $prior and $prior->isa('PPI::Token::Operator') and $prior->content eq '->' ) { + if ( $prior + and $prior->isa('PPI::Token::Operator') + and $prior->content eq '->' ) + { # Postfix dereference: ->@[...] ->%[...] return 'PPI::Structure::Subscript'; } @@ -1143,29 +1127,28 @@ my %CURLY_CLASSES = ( ':' => 'PPI::Structure::Constructor', ',' => 'PPI::Structure::Constructor', '=>' => 'PPI::Structure::Constructor', - '+' => 'PPI::Structure::Constructor', # per perlref - 'return' => 'PPI::Structure::Constructor', # per perlref - 'bless' => 'PPI::Structure::Constructor', # pragmatic -- - # perlfunc says first arg is a reference, and - # bless {; ... } fails to compile. + '+' => 'PPI::Structure::Constructor', # per perlref + 'return' => 'PPI::Structure::Constructor', # per perlref + 'bless' => 'PPI::Structure::Constructor', # pragmatic -- + # perlfunc says first arg is a reference, and + # bless {; ... } fails to compile. ); my @CURLY_LOOKAHEAD_CLASSES = ( - {}, # not used + {}, # not used { - ';' => 'PPI::Structure::Block', # per perlref - '}' => 'PPI::Structure::Constructor', + ';' => 'PPI::Structure::Block', # per perlref + '}' => 'PPI::Structure::Constructor', }, { - '=>' => 'PPI::Structure::Constructor', + '=>' => 'PPI::Structure::Constructor', }, ); - # Given a parent element, and a { token to open a structure, determine # the class that the structure should be. sub _curly { - my ($self, $Parent) = @_; + my ( $self, $Parent ) = @_; # my $self = shift; # my $Parent = _INSTANCE(shift, 'PPI::Node') or die "Bad param 1"; @@ -1174,7 +1157,7 @@ sub _curly { my $content = $Element ? $Element->content : ''; # Is this a subscript, like $foo[1] or $foo{expr} - if ( $Element ) { + if ($Element) { if ( $content eq '->' and $Element->isa('PPI::Token::Operator') ) { # $foo->{} $Element->{_dereference} = 1; @@ -1184,13 +1167,19 @@ sub _curly { # $foo[]{} return 'PPI::Structure::Subscript'; } - if ( $content =~ /^(?:\$|\@)/ and $Element->isa('PPI::Token::Symbol') ) { + if ( $content =~ /^(?:\$|\@)/ and $Element->isa('PPI::Token::Symbol') ) + { # $foo{}, @foo{} return 'PPI::Structure::Subscript'; } - if ( $Element->isa('PPI::Token::Cast') and $Element->content =~ /^(?:\@|\%|\*)/ ) { + if ( $Element->isa('PPI::Token::Cast') + and $Element->content =~ /^(?:\@|\%|\*)/ ) + { my $prior = $Parent->schild(-2); - if ( $prior and $prior->isa('PPI::Token::Operator') and $prior->content eq '->' ) { + if ( $prior + and $prior->isa('PPI::Token::Operator') + and $prior->content eq '->' ) + { # Postfix dereference: ->@{...} ->%{...} ->*{...} return 'PPI::Structure::Subscript'; } @@ -1201,10 +1190,10 @@ sub _curly { # hash slice - @{$hash_ref}{'foo', 'bar'} if ( my $prior = $Parent->schild(-2) ) { my $prior_content = $prior->content(); - $prior->isa( 'PPI::Token::Cast' ) - and ( $prior_content eq '@' || - $prior_content eq '$' ) - and return 'PPI::Structure::Subscript'; + $prior->isa('PPI::Token::Cast') + and ($prior_content eq '@' + || $prior_content eq '$' ) + and return 'PPI::Structure::Subscript'; } } @@ -1215,7 +1204,7 @@ sub _curly { # Are we the second or third argument of package? # E.g.: 'package Foo {}' or 'package Foo v1.2.3 {}' return 'PPI::Structure::Block' - if $Parent->isa('PPI::Statement::Package'); + if $Parent->isa('PPI::Statement::Package'); if ( $CURLY_CLASSES{$content} ) { # Known type @@ -1231,34 +1220,33 @@ sub _curly { # Are we the second or third argument of use if ( $Parent->isa('PPI::Statement::Include') ) { - if ( $Parent->schildren == 2 || - $Parent->schildren == 3 && - $Parent->schild(2)->isa('PPI::Token::Number') - ) { + if ( $Parent->schildren == 2 + || $Parent->schildren == 3 + && $Parent->schild(2)->isa('PPI::Token::Number') ) + { # This is something like use constant { ... }; return 'PPI::Structure::Constructor'; } } - # Unless we are at the start of the statement, everything else should be a block +# Unless we are at the start of the statement, everything else should be a block ### FIXME This is possibly a bad choice, but will have to do for now. return 'PPI::Structure::Block' if $Element; - if ( - $Parent->isa('PPI::Statement') - and - _INSTANCE($Parent->parent, 'PPI::Structure::List') - ) { + if ( $Parent->isa('PPI::Statement') + and _INSTANCE( $Parent->parent, 'PPI::Structure::List' ) ) + { my $function = $Parent->parent->parent->schild(-2); # Special case: Are we the param of a core function # i.e. map({ $_ => 1 } @foo) return 'PPI::Structure::Block' - if $function and $function->content =~ /^(?:map|grep|sort|eval|do)$/; + if $function and $function->content =~ /^(?:map|grep|sort|eval|do)$/; - # If not part of a block print, list-embedded curlies are most likely constructors +# If not part of a block print, list-embedded curlies are most likely constructors return 'PPI::Structure::Constructor' - if not $function or $function->content !~ /^(?:print|say)$/; + if not $function + or $function->content !~ /^(?:print|say)$/; } # We need to scan ahead. @@ -1276,9 +1264,11 @@ sub _curly { # default to block. $self->_buffer( splice(@delayed), $Next ); last; - # If the content at this position is known - } elsif ( my $class = $CURLY_LOOKAHEAD_CLASSES[$position] - {$Next->content} ) { + # If the content at this position is known + } + elsif ( my $class = + $CURLY_LOOKAHEAD_CLASSES[$position]{ $Next->content } ) + { # return the associated class. $self->_buffer( splice(@delayed), $Next ); return $class; @@ -1296,18 +1286,17 @@ sub _curly { return 'PPI::Structure::Block'; } - sub _lex_structure { - my ($self, $Structure) = @_; + my ( $self, $Structure ) = @_; # my $self = shift; # my $Structure = _INSTANCE(shift, 'PPI::Structure') or die "Bad param 1"; # Start the processing loop my $Token; - while ( ref($Token = $self->_get_token) ) { + while ( ref( $Token = $self->_get_token ) ) { # Is this a direct type token unless ( $Token->significant ) { - push @{$self->{delayed}}, $Token; + push @{ $self->{delayed} }, $Token; # $self->_delay_element( $Token ); next; } @@ -1316,14 +1305,15 @@ sub _lex_structure { unless ( $Token->isa('PPI::Token::Structure') ) { # Because _statement may well delay and rollback itself, # we need to add the delayed tokens early - $self->_add_delayed( $Structure ); + $self->_add_delayed($Structure); # Determine the class for the Statement and create it - my $Statement = $self->_statement($Structure, $Token)->new($Token); + my $Statement = + $self->_statement( $Structure, $Token )->new($Token); # Move the lexing down into the Statement $self->_add_element( $Structure, $Statement ); - $self->_lex_statement( $Statement ); + $self->_lex_statement($Statement); next; } @@ -1331,10 +1321,10 @@ sub _lex_structure { # Is this the opening of another structure directly inside us? if ( $Token->__LEXER__opens ) { # Rollback the Token, and recurse into the statement - $self->_rollback( $Token ); + $self->_rollback($Token); my $Statement = PPI::Statement->new; $self->_add_element( $Structure, $Statement ); - $self->_lex_statement( $Statement ); + $self->_lex_statement($Statement); next; } @@ -1343,19 +1333,18 @@ sub _lex_structure { # Is this OUR closing structure if ( $Token->content eq $Structure->start->__LEXER__opposite ) { # Add any delayed tokens, and the finishing token (the ugly way) - $self->_add_delayed( $Structure ); + $self->_add_delayed($Structure); $Structure->{finish} = $Token; Scalar::Util::weaken( - $_PARENT{Scalar::Util::refaddr $Token} = $Structure - ); + $_PARENT{ Scalar::Util::refaddr $Token } = $Structure ); # Confirm that ForLoop structures are actually so, and # aren't really a list. if ( $Structure->isa('PPI::Structure::For') ) { - if ( 2 > scalar grep { - $_->isa('PPI::Statement') - } $Structure->children ) { - bless($Structure, 'PPI::Structure::List'); + if ( 2 > scalar grep { $_->isa('PPI::Statement') } + $Structure->children ) + { + bless( $Structure, 'PPI::Structure::List' ); } } return 1; @@ -1368,15 +1357,12 @@ sub _lex_structure { # as implicitly ending the structure. This causes the # least damage across the various reasons why this # might have happened. - return $self->_rollback( $Token ); + return $self->_rollback($Token); } # It's a semi-colon on its own, just inside the block. # This is a null statement. - $self->_add_element( - $Structure, - PPI::Statement::Null->new($Token), - ); + $self->_add_element( $Structure, PPI::Statement::Null->new($Token), ); } # Is this an error @@ -1386,19 +1372,15 @@ sub _lex_structure { # No, it's just the end of file. # Add any insignificant trailing tokens. - $self->_add_delayed( $Structure ); + $self->_add_delayed($Structure); } - - - - ##################################################################### # Support Methods # Get the next token for processing, handling buffering sub _get_token { - shift(@{$_[0]->{buffer}}) or $_[0]->{Tokenizer}->get_token; + shift( @{ $_[0]->{buffer} } ) or $_[0]->{Tokenizer}->get_token; } # Old long version of the above @@ -1422,33 +1404,32 @@ sub _get_token { # Add an Element to a Node, including any delayed Elements sub _add_element { - my ($self, $Parent, $Element) = @_; + my ( $self, $Parent, $Element ) = @_; # my $self = shift; # my $Parent = _INSTANCE(shift, 'PPI::Node') or die "Bad param 1"; # my $Element = _INSTANCE(shift, 'PPI::Element') or die "Bad param 2"; # Handle a special case, where a statement is not fully resolved if ( ref $Parent eq 'PPI::Statement' - and my $first = $Parent->schild(0) ) { + and my $first = $Parent->schild(0) ) + { if ( $first->isa('PPI::Token::Label') - and !(my $second = $Parent->schild(1)) ) { - my $new_class = $STATEMENT_CLASSES{$second->content}; + and !( my $second = $Parent->schild(1) ) ) + { + my $new_class = $STATEMENT_CLASSES{ $second->content }; # It's a labelled statement bless $Parent, $new_class if $new_class; } } # Add first the delayed, from the front, then the passed element - foreach my $el ( @{$self->{delayed}} ) { - Scalar::Util::weaken( - $_PARENT{Scalar::Util::refaddr $el} = $Parent - ); + foreach my $el ( @{ $self->{delayed} } ) { + Scalar::Util::weaken( $_PARENT{ Scalar::Util::refaddr $el } = $Parent ); # Inlined $Parent->__add_element($el); } - Scalar::Util::weaken( - $_PARENT{Scalar::Util::refaddr $Element} = $Parent - ); - push @{$Parent->{children}}, @{$self->{delayed}}, $Element; + Scalar::Util::weaken( $_PARENT{ Scalar::Util::refaddr $Element } = + $Parent ); + push @{ $Parent->{children} }, @{ $self->{delayed} }, $Element; # Clear the delayed elements $self->{delayed} = []; @@ -1456,18 +1437,16 @@ sub _add_element { # Specifically just add any delayed tokens, if any. sub _add_delayed { - my ($self, $Parent) = @_; + my ( $self, $Parent ) = @_; # my $self = shift; # my $Parent = _INSTANCE(shift, 'PPI::Node') or die "Bad param 1"; # Add any delayed - foreach my $el ( @{$self->{delayed}} ) { - Scalar::Util::weaken( - $_PARENT{Scalar::Util::refaddr $el} = $Parent - ); + foreach my $el ( @{ $self->{delayed} } ) { + Scalar::Util::weaken( $_PARENT{ Scalar::Util::refaddr $el } = $Parent ); # Inlined $Parent->__add_element($el); } - push @{$Parent->{children}}, @{$self->{delayed}}; + push @{ $Parent->{children} }, @{ $self->{delayed} }; # Clear the delayed elements $self->{delayed} = []; @@ -1480,13 +1459,13 @@ sub _rollback { my $self = shift; # First, put any passed objects back - if ( @_ ) { - unshift @{$self->{buffer}}, splice @_; + if (@_) { + unshift @{ $self->{buffer} }, splice @_; } # Then, put back anything delayed - if ( @{$self->{delayed}} ) { - unshift @{$self->{buffer}}, splice @{$self->{delayed}}; + if ( @{ $self->{delayed} } ) { + unshift @{ $self->{buffer} }, splice @{ $self->{delayed} }; } 1; @@ -1497,17 +1476,13 @@ sub _buffer { my $self = shift; # Put any passed objects back - if ( @_ ) { - unshift @{$self->{buffer}}, splice @_; + if (@_) { + unshift @{ $self->{buffer} }, splice @_; } 1; } - - - - ##################################################################### # Error Handling @@ -1539,10 +1514,6 @@ sub errstr { $errstr; } - - - - ##################################################################### # PDOM Extensions # diff --git a/lib/PPI/Node.pm b/lib/PPI/Node.pm index 7d3f3d09..b8672e00 100644 --- a/lib/PPI/Node.pm +++ b/lib/PPI/Node.pm @@ -49,20 +49,16 @@ L objects also apply to C objects. =cut use strict; -use Carp (); -use Scalar::Util qw{refaddr}; -use Params::Util qw{_INSTANCE _CLASS _CODELIKE _NUMBER}; -use PPI::Element (); +use Carp (); +use Scalar::Util qw{refaddr}; +use Params::Util qw{_INSTANCE _CLASS _CODELIKE _NUMBER}; +use PPI::Element (); use PPI::Singletons '%_PARENT', '%_POSITION_CACHE'; our $VERSION = '1.282'; our @ISA = "PPI::Element"; - - - - ##################################################################### # The basic constructor @@ -71,10 +67,6 @@ sub new { bless { children => [] }, $class; } - - - - ##################################################################### # PDOM Methods @@ -108,14 +100,12 @@ sub add_element { my $self = shift; # Check the element - my $Element = _INSTANCE(shift, 'PPI::Element') or return undef; - $_PARENT{refaddr $Element} and return undef; + my $Element = _INSTANCE( shift, 'PPI::Element' ) or return undef; + $_PARENT{ refaddr $Element } and return undef; # Add the argument to the elements - push @{$self->{children}}, $Element; - Scalar::Util::weaken( - $_PARENT{refaddr $Element} = $self - ); + push @{ $self->{children} }, $Element; + Scalar::Util::weaken( $_PARENT{ refaddr $Element } = $self ); 1; } @@ -123,10 +113,8 @@ sub add_element { # In a typical run profile, add_element is the number 1 resource drain. # This is a highly optimised unsafe version, for internal use only. sub __add_element { - Scalar::Util::weaken( - $_PARENT{refaddr $_[1]} = $_[0] - ); - push @{$_[0]->{children}}, $_[1]; + Scalar::Util::weaken( $_PARENT{ refaddr $_[1] } = $_[0] ); + push @{ $_[0]->{children} }, $_[1]; } =pod @@ -146,10 +134,11 @@ returns a count of the number of elements. =cut sub elements { - if ( wantarray ) { - return @{$_[0]->{children}}; - } else { - return scalar @{$_[0]->{children}}; + if (wantarray) { + return @{ $_[0]->{children} }; + } + else { + return scalar @{ $_[0]->{children} }; } } @@ -207,7 +196,7 @@ returns a count of the number of lexical children. # In the default case, this is the same as for the elements method sub children { - wantarray ? @{$_[0]->{children}} : scalar @{$_[0]->{children}}; + wantarray ? @{ $_[0]->{children} } : scalar @{ $_[0]->{children} }; } =pod @@ -223,9 +212,9 @@ returns the number of significant children. =cut sub schildren { - return grep { $_->significant } @{$_[0]->{children}} if wantarray; + return grep { $_->significant } @{ $_[0]->{children} } if wantarray; my $count = 0; - foreach ( @{$_[0]->{children}} ) { + foreach ( @{ $_[0]->{children} } ) { $count++ if $_->significant; } return $count; @@ -245,7 +234,7 @@ element at that node. sub child { my ( $self, $index ) = @_; - PPI::Exception->throw( "method child() needs an index" ) + PPI::Exception->throw("method child() needs an index") if not defined _NUMBER $index; $self->{children}->[$index]; } @@ -273,12 +262,14 @@ sub schild { my $el = $self->{children}; if ( $idx < 0 ) { my $cursor = 0; - while ( exists $el->[--$cursor] ) { - return $el->[$cursor] if $el->[$cursor]->significant and ++$idx >= 0; + while ( exists $el->[ --$cursor ] ) { + return $el->[$cursor] + if $el->[$cursor]->significant and ++$idx >= 0; } - } else { + } + else { my $cursor = -1; - while ( exists $el->[++$cursor] ) { + while ( exists $el->[ ++$cursor ] ) { return $el->[$cursor] if $el->[$cursor]->significant and --$idx < 0; } } @@ -302,7 +293,7 @@ on error. sub contains { my $self = shift; - my $Element = _INSTANCE(shift, 'PPI::Element') or return undef; + my $Element = _INSTANCE( shift, 'PPI::Element' ) or return undef; # Iterate up the Element's parent chain until we either run out # of parents, or get to ourself. @@ -374,9 +365,9 @@ sub find { # Use a queue based search, rather than a recursive one my @found; - my @queue = @{$self->{children}}; - my $ok = eval { - while ( @queue ) { + my @queue = @{ $self->{children} }; + my $ok = eval { + while (@queue) { my $Element = shift @queue; my $rv = &$wanted( $self, $Element ); push @found, $Element if $rv; @@ -391,10 +382,11 @@ sub find { # better logical order. if ( $Element->isa('PPI::Structure') ) { unshift @queue, $Element->finish if $Element->finish; - unshift @queue, @{$Element->{children}}; + unshift @queue, @{ $Element->{children} }; unshift @queue, $Element->start if $Element->start; - } else { - unshift @queue, @{$Element->{children}}; + } + else { + unshift @queue, @{ $Element->{children} }; } } 1; @@ -431,14 +423,14 @@ sub find_first { my $wanted = $self->_wanted(shift) or return undef; # Use the same queue-based search as for ->find - my @queue = @{$self->{children}}; + my @queue = @{ $self->{children} }; my $rv; my $ok = eval { # The defined() here prevents a ton of calls to PPI::Util::TRUE - while ( @queue ) { - my $Element = shift @queue; + while (@queue) { + my $Element = shift @queue; my $element_rv = $wanted->( $self, $Element ); - if ( $element_rv ) { + if ($element_rv) { $rv = $Element; last; } @@ -452,11 +444,12 @@ sub find_first { # Depth-first keeps the queue size down and provides a # better logical order. if ( $Element->isa('PPI::Structure') ) { - unshift @queue, $Element->finish if defined($Element->finish); - unshift @queue, @{$Element->{children}}; - unshift @queue, $Element->start if defined($Element->start); - } else { - unshift @queue, @{$Element->{children}}; + unshift @queue, $Element->finish if defined( $Element->finish ); + unshift @queue, @{ $Element->{children} }; + unshift @queue, $Element->start if defined( $Element->start ); + } + else { + unshift @queue, @{ $Element->{children} }; } } 1; @@ -487,7 +480,7 @@ not, or C if given an invalid condition, or an error occurs. sub find_any { my $self = shift; my $rv = $self->find_first(@_); - $rv ? 1 : $rv; # false or undef + $rv ? 1 : $rv; # false or undef } =pod @@ -505,7 +498,7 @@ If successful, returns the removed element. Otherwise, returns C. sub remove_child { my $self = shift; - my $child = _INSTANCE(shift, 'PPI::Element') or return undef; + my $child = _INSTANCE( shift, 'PPI::Element' ) or return undef; # Find the position of the child my $key = refaddr $child; @@ -513,7 +506,7 @@ sub remove_child { return undef unless defined $p; # Splice it out, and remove the child's parent entry - splice( @{$self->{children}}, $p, 1 ); + splice( @{ $self->{children} }, $p, 1 ); delete $_PARENT{$key}; $child; @@ -528,8 +521,8 @@ If successful, returns the replace element. Otherwise, returns C. sub replace_child { my $self = shift; - my $child = _INSTANCE(shift, 'PPI::Element') or return undef; - my $replacement = _INSTANCE(shift, 'PPI::Element') or return undef; + my $child = _INSTANCE( shift, 'PPI::Element' ) or return undef; + my $replacement = _INSTANCE( shift, 'PPI::Element' ) or return undef; my $success = $self->__replace_child( $child, $replacement ); @@ -560,10 +553,10 @@ sub prune { # Use a depth-first queue search my $pruned = 0; my @queue = $self->children; - my $ok = eval { + my $ok = eval { while ( my $element = shift @queue ) { my $rv = &$wanted( $self, $element ); - if ( $rv ) { + if ($rv) { # Delete the child $element->delete or return undef; $pruned++; @@ -573,7 +566,7 @@ sub prune { # Support the undef == "don't descend" next unless defined $rv; - if ( _INSTANCE($element, 'PPI::Node') ) { + if ( _INSTANCE( $element, 'PPI::Node' ) ) { # Depth-first keeps the queue size down unshift @queue, $element->children; } @@ -582,7 +575,7 @@ sub prune { }; if ( !$ok ) { # Caught exception thrown from the wanted function - return undef; + return undef; } $pruned; @@ -594,7 +587,7 @@ sub prune { ### break File::Find::Rule::PPI sub _wanted { my $either = shift; - my $it = defined($_[0]) ? shift : do { + my $it = defined( $_[0] ) ? shift : do { Carp::carp('Undefined value passed as search condition') if $^W; return undef; }; @@ -603,15 +596,18 @@ sub _wanted { return $it if _CODELIKE($it); if ( ref $it ) { # No other ref types are supported - Carp::carp('Illegal non-CODE reference passed as search condition') if $^W; + Carp::carp('Illegal non-CODE reference passed as search condition') + if $^W; return undef; } # The first argument should be an Element class, possibly in shorthand - $it = "PPI::$it" unless substr($it, 0, 5) eq 'PPI::'; + $it = "PPI::$it" unless substr( $it, 0, 5 ) eq 'PPI::'; unless ( _CLASS($it) and $it->isa('PPI::Element') ) { # We got something, but it isn't an element - Carp::carp("Cannot create search condition for '$it': Not a PPI::Element") if $^W; + Carp::carp( + "Cannot create search condition for '$it': Not a PPI::Element") + if $^W; return undef; } @@ -624,45 +620,42 @@ sub _wanted { my $content = shift; if ( ref $content eq 'Regexp' ) { $content = "$content"; - } elsif ( ref $content ) { + } + elsif ( ref $content ) { # No other ref types are supported - Carp::carp("Cannot create search condition for '$it': Not a PPI::Element") if $^W; + Carp::carp( + "Cannot create search condition for '$it': Not a PPI::Element") + if $^W; return undef; - } else { + } + else { $content = quotemeta $content; } # Complete the content part of the wanted function $wanted_content .= "\n\treturn '' unless defined \$_[1]->{content};"; - $wanted_content .= "\n\treturn '' unless \$_[1]->{content} =~ /$content/;"; + $wanted_content .= + "\n\treturn '' unless \$_[1]->{content} =~ /$content/;"; } # Create the complete wanted function - my $code = "sub {" - . $wanted_class - . $wanted_content - . "\n\t1;" - . "\n}"; + my $code = "sub {" . $wanted_class . $wanted_content . "\n\t1;" . "\n}"; # Compile the wanted function $code = eval $code; - (ref $code eq 'CODE') ? $code : undef; + ( ref $code eq 'CODE' ) ? $code : undef; } - - - - #################################################################### # PPI::Element overloaded methods sub tokens { - map { $_->tokens } @{$_[0]->{children}}; + map { $_->tokens } @{ $_[0]->{children} }; } ### XS -> PPI/XS.xs:_PPI_Element__content 0.900+ sub content { - join '', map { $_->content } @{$_[0]->{children}}; + join '', map { $_->content } @{ $_[0]->{children} }; } # Clone as normal, but then go down and relink all the _PARENT entries @@ -679,10 +672,6 @@ sub location { $first->location; } - - - - ##################################################################### # Internal Methods @@ -690,8 +679,8 @@ sub DESTROY { local $_; if ( $_[0]->{children} ) { my @queue = $_[0]; - while ( defined($_ = shift @queue) ) { - unshift @queue, @{delete $_->{children}} if $_->{children}; + while ( defined( $_ = shift @queue ) ) { + unshift @queue, @{ delete $_->{children} } if $_->{children}; # Remove all internal/private weird crosslinking so that # the cascading DESTROY calls will get called properly. @@ -706,17 +695,18 @@ sub __position { my ( $self, $child ) = @_; my $key = refaddr $child; - return undef unless # - my $elements = $self->{children}; + return undef unless # + my $elements = $self->{children}; - if (defined (my $position = $_POSITION_CACHE{$key})) { + if ( defined( my $position = $_POSITION_CACHE{$key} ) ) { my $maybe_child = $elements->[$position]; - return $position if defined $maybe_child and refaddr $maybe_child == $key; + return $position + if defined $maybe_child and refaddr $maybe_child == $key; } delete $_POSITION_CACHE{$key}; - $_POSITION_CACHE{refaddr $elements->[$_]} = $_ for 0 .. $#{$elements}; + $_POSITION_CACHE{ refaddr $elements->[$_] } = $_ for 0 .. $#{$elements}; return $_POSITION_CACHE{$key}; } @@ -724,49 +714,43 @@ sub __position { # Insert one or more elements before a child sub __insert_before_child { my ( $self, $child, @insertions ) = @_; - my $key = refaddr $child; - my $p = $self->__position($child); - foreach ( @insertions ) { - Scalar::Util::weaken( - $_PARENT{refaddr $_} = $self - ); + my $key = refaddr $child; + my $p = $self->__position($child); + foreach (@insertions) { + Scalar::Util::weaken( $_PARENT{ refaddr $_ } = $self ); } - splice( @{$self->{children}}, $p, 0, @insertions ); + splice( @{ $self->{children} }, $p, 0, @insertions ); 1; } # Insert one or more elements after a child sub __insert_after_child { my ( $self, $child, @insertions ) = @_; - my $key = refaddr $child; - my $p = $self->__position($child); - foreach ( @insertions ) { - Scalar::Util::weaken( - $_PARENT{refaddr $_} = $self - ); + my $key = refaddr $child; + my $p = $self->__position($child); + foreach (@insertions) { + Scalar::Util::weaken( $_PARENT{ refaddr $_ } = $self ); } - splice( @{$self->{children}}, $p + 1, 0, @insertions ); + splice( @{ $self->{children} }, $p + 1, 0, @insertions ); 1; } # Replace a child sub __replace_child { my ( $self, $old_child, @replacements ) = @_; - my $old_child_addr = refaddr $old_child; + my $old_child_addr = refaddr $old_child; # Cache parent of new children my $old_child_index = $self->__position($old_child); return undef if !defined $old_child_index; - foreach ( @replacements ) { - Scalar::Util::weaken( - $_PARENT{refaddr $_} = $self - ); + foreach (@replacements) { + Scalar::Util::weaken( $_PARENT{ refaddr $_ } = $self ); } # Replace old child with new children - splice( @{$self->{children}}, $old_child_index, 1, @replacements ); + splice( @{ $self->{children} }, $old_child_index, 1, @replacements ); # Uncache parent of old child delete $_PARENT{$old_child_addr}; @@ -779,24 +763,20 @@ sub __link_children { my $self = shift; # Relink all our children ( depth first ) - my @queue = ( $self ); + my @queue = ($self); while ( my $Node = shift @queue ) { # Link our immediate children - foreach my $Element ( @{$Node->{children}} ) { - Scalar::Util::weaken( - $_PARENT{refaddr($Element)} = $Node - ); + foreach my $Element ( @{ $Node->{children} } ) { + Scalar::Util::weaken( $_PARENT{ refaddr($Element) } = $Node ); unshift @queue, $Element if $Element->isa('PPI::Node'); } # If it's a structure, relink the open/close braces next unless $Node->isa('PPI::Structure'); - Scalar::Util::weaken( - $_PARENT{refaddr($Node->start)} = $Node - ) if $Node->start; - Scalar::Util::weaken( - $_PARENT{refaddr($Node->finish)} = $Node - ) if $Node->finish; + Scalar::Util::weaken( $_PARENT{ refaddr( $Node->start ) } = $Node ) + if $Node->start; + Scalar::Util::weaken( $_PARENT{ refaddr( $Node->finish ) } = $Node ) + if $Node->finish; } 1; diff --git a/lib/PPI/Normal.pm b/lib/PPI/Normal.pm index c45c558d..2801baaf 100644 --- a/lib/PPI/Normal.pm +++ b/lib/PPI/Normal.pm @@ -35,12 +35,12 @@ process to be. =cut use strict; -use Carp (); -use List::Util 1.33 (); -use PPI::Util '_Document'; +use Carp (); +use List::Util 1.33 (); +use PPI::Util '_Document'; use PPI::Document::Normalized (); use PPI::Normal::Standard (); -use PPI::Singletons '%LAYER'; +use PPI::Singletons '%LAYER'; our $VERSION = '1.282'; @@ -48,9 +48,6 @@ our $VERSION = '1.282'; # normalization methods to initialize the store. PPI::Normal::Standard->import; - - - ##################################################################### # Configuration @@ -72,13 +69,13 @@ Returns true if all functions are registered, or C on error. sub register { my $class = shift; - while ( @_ ) { + while (@_) { # Check the function my $function = shift; - SCOPE: { + SCOPE: { no strict 'refs'; defined $function and defined &{"$function"} - or Carp::croak("Bad function name provided to PPI::Normal"); + or Carp::croak("Bad function name provided to PPI::Normal"); } # Has it already been added? @@ -89,7 +86,7 @@ sub register { # Check the layer to add it to my $layer = shift; defined $layer and $layer =~ /^(?:1|2)$/ - or Carp::croak("Bad layer provided to PPI::Normal"); + or Carp::croak("Bad layer provided to PPI::Normal"); # Add to the layer data store push @{ $LAYER{$layer} }, $function; @@ -98,10 +95,6 @@ sub register { 1; } - - - - ##################################################################### # Constructor and Accessors @@ -127,14 +120,15 @@ Returns a new C object, or C on error. sub new { my $class = shift; - my $layer = @_ ? - (defined $_[0] and ! ref $_[0] and $_[0] =~ /^[12]$/) ? shift : return undef - : 1; + my $layer = + @_ + ? ( defined $_[0] and !ref $_[0] and $_[0] =~ /^[12]$/ ) + ? shift + : return undef + : 1; # Create the object - my $object = bless { - layer => $layer, - }, $class; + my $object = bless { layer => $layer, }, $class; $object; } @@ -149,10 +143,6 @@ The C accessor returns the normalisation layer of the object. sub layer { $_[0]->{layer} } - - - - ##################################################################### # Main Methods @@ -181,7 +171,7 @@ sub process { my @functions = map { @{ $LAYER{$_} } } ( 1 .. $self->layer ); # Execute each function - foreach my $function ( @functions ) { + foreach my $function (@functions) { no strict 'refs'; &{"$function"}( $self->{Document} ); } diff --git a/lib/PPI/Normal/Standard.pm b/lib/PPI/Normal/Standard.pm index 24b34b08..d56c510c 100644 --- a/lib/PPI/Normal/Standard.pm +++ b/lib/PPI/Normal/Standard.pm @@ -20,10 +20,6 @@ use strict; our $VERSION = '1.282'; - - - - ##################################################################### # Configuration and Registration @@ -36,22 +32,18 @@ my @METHODS = ( ); sub import { - PPI::Normal->register( - map { /\D/ ? "PPI::Normal::Standard::$_" : $_ } @METHODS - ) or die "Failed to register PPI::Normal::Standard transforms"; + PPI::Normal->register( map { /\D/ ? "PPI::Normal::Standard::$_" : $_ } + @METHODS ) + or die "Failed to register PPI::Normal::Standard transforms"; } - - - - ##################################################################### # Level 1 Transforms # Remove all insignificant elements sub remove_insignificant_elements { my $Document = shift; - $Document->prune( sub { ! $_[1]->significant } ); + $Document->prune( sub { !$_[1]->significant } ); } # Remove custom attributes that are not relevant to normalization @@ -62,38 +54,39 @@ sub remove_useless_attributes { ### FIXME - Add support for more things } - - - - ##################################################################### # Level 2 Transforms # Remove version dependencies and pragma my $remove_pragma = map { $_ => 1 } qw{ - strict warnings diagnostics less - }; + strict warnings diagnostics less +}; + sub remove_useless_pragma { my $Document = shift; - $Document->prune( sub { - return '' unless $_[1]->isa('PPI::Statement::Include'); - return 1 if $_[1]->version; - return 1 if $remove_pragma->{$_[1]->pragma}; - ''; - } ); + $Document->prune( + sub { + return '' unless $_[1]->isa('PPI::Statement::Include'); + return 1 if $_[1]->version; + return 1 if $remove_pragma->{ $_[1]->pragma }; + ''; + } + ); } # Remove all semi-colons at the end of statements sub remove_statement_separator { my $Document = shift; - $Document->prune( sub { - $_[1]->isa('PPI::Token::Structure') or return ''; - $_[1]->content eq ';' or return ''; - my $stmt = $_[1]->parent or return ''; - $stmt->isa('PPI::Statement') or return ''; - $_[1]->next_sibling and return ''; - 1; - } ); + $Document->prune( + sub { + $_[1]->isa('PPI::Token::Structure') or return ''; + $_[1]->content eq ';' or return ''; + my $stmt = $_[1]->parent or return ''; + $stmt->isa('PPI::Statement') or return ''; + $_[1]->next_sibling and return ''; + 1; + } + ); } # In any block, the "return" in the last statement is not @@ -101,17 +94,19 @@ sub remove_statement_separator { # return. sub remove_useless_return { my $Document = shift; - $Document->prune( sub { - $_[1]->isa('PPI::Token::Word') or return ''; - $_[1]->content eq 'return' or return ''; - my $stmt = $_[1]->parent or return ''; - $stmt->isa('PPI::Statement::Break') or return ''; - $stmt->children == 2 or return ''; - $stmt->next_sibling and return ''; - my $block = $stmt->parent or return ''; - $block->isa('PPI::Structure::Block') or return ''; - 1; - } ); + $Document->prune( + sub { + $_[1]->isa('PPI::Token::Word') or return ''; + $_[1]->content eq 'return' or return ''; + my $stmt = $_[1]->parent or return ''; + $stmt->isa('PPI::Statement::Break') or return ''; + $stmt->children == 2 or return ''; + $stmt->next_sibling and return ''; + my $block = $stmt->parent or return ''; + $block->isa('PPI::Structure::Block') or return ''; + 1; + } + ); } 1; diff --git a/lib/PPI/Singletons.pm b/lib/PPI/Singletons.pm index 9e63dbb7..fbd9653f 100644 --- a/lib/PPI/Singletons.pm +++ b/lib/PPI/Singletons.pm @@ -3,45 +3,46 @@ package PPI::Singletons; # exports some singleton variables to avoid aliasing magic use strict; -use Exporter (); +use Exporter (); our $VERSION = '1.282'; -our @ISA = 'Exporter'; -our @EXPORT_OK = qw{ %_PARENT %_POSITION_CACHE %OPERATOR %MAGIC %LAYER $CURLY_SYMBOL %QUOTELIKE %KEYWORDS }; +our @ISA = 'Exporter'; +our @EXPORT_OK = + qw{ %_PARENT %_POSITION_CACHE %OPERATOR %MAGIC %LAYER $CURLY_SYMBOL %QUOTELIKE %KEYWORDS }; -our %_PARENT; # Master Child -> Parent index -our %_POSITION_CACHE; # cache for position in parent +our %_PARENT; # Master Child -> Parent index +our %_POSITION_CACHE; # cache for position in parent # operator index our %OPERATOR = map { $_ => 1 } ( qw{ - -> ++ -- ** ! ~ + - - =~ !~ * / % x . << >> - < > <= >= lt gt le ge - == != <=> eq ne cmp ~~ - & | ^ && || // .. ... - ? : - = **= += -= .= *= /= %= x= &= |= ^= <<= >>= &&= ||= //= - => <> <<>> - and or xor not - }, ',' # Avoids "comma in qw{}" warning + -> ++ -- ** ! ~ + - + =~ !~ * / % x . << >> + < > <= >= lt gt le ge + == != <=> eq ne cmp ~~ + & | ^ && || // .. ... + ? : + = **= += -= .= *= /= %= x= &= |= ^= <<= >>= &&= ||= //= + => <> <<>> + and or xor not + }, ',' # Avoids "comma in qw{}" warning ); # Magic variables taken from perlvar. # Several things added separately to avoid warnings. our %MAGIC = map { $_ => 1 } qw{ - $1 $2 $3 $4 $5 $6 $7 $8 $9 - $_ $& $` $' $+ @+ %+ $* $. $/ $| - $\\ $" $; $% $= $- @- %- $) - $~ $^ $: $? $! %! $@ $$ $< $> - $( $0 $[ $] @_ @* + $1 $2 $3 $4 $5 $6 $7 $8 $9 + $_ $& $` $' $+ @+ %+ $* $. $/ $| + $\\ $" $; $% $= $- @- %- $) + $~ $^ $: $? $! %! $@ $$ $< $> + $( $0 $[ $] @_ @* - $^L $^A $^E $^C $^D $^F $^H - $^I $^M $^N $^O $^P $^R $^S - $^T $^V $^W $^X %^H + $^L $^A $^E $^C $^D $^F $^H + $^I $^M $^N $^O $^P $^R $^S + $^T $^V $^W $^X %^H - $::| + $::| }, '$}', '$,', '$#', '$#+', '$#-'; our %LAYER = ( 1 => [], 2 => [] ); # Registered function store @@ -62,31 +63,31 @@ our %QUOTELIKE = ( # List of keywords is from regen/keywords.pl in the perl source. our %KEYWORDS = map { $_ => 1 } qw{ - abs accept alarm and atan2 bind binmode bless break caller chdir chmod - chomp chop chown chr chroot close closedir cmp connect continue cos - crypt dbmclose dbmopen default defined delete die do dump each else - elsif endgrent endhostent endnetent endprotoent endpwent endservent - eof eq eval evalbytes exec exists exit exp fc fcntl fileno flock for - foreach fork format formline ge getc getgrent getgrgid getgrnam - gethostbyaddr gethostbyname gethostent getlogin getnetbyaddr - getnetbyname getnetent getpeername getpgrp getppid getpriority - getprotobyname getprotobynumber getprotoent getpwent getpwnam - getpwuid getservbyname getservbyport getservent getsockname - getsockopt given glob gmtime goto grep gt hex if index int ioctl join - keys kill last lc lcfirst le length link listen local localtime lock - log lstat lt m map mkdir msgctl msgget msgrcv msgsnd my ne next no - not oct open opendir or ord our pack package pipe pop pos print - printf prototype push q qq qr quotemeta qw qx rand read readdir - readline readlink readpipe recv redo ref rename require reset return - reverse rewinddir rindex rmdir s say scalar seek seekdir select semctl - semget semop send setgrent sethostent setnetent setpgrp - setpriority setprotoent setpwent setservent setsockopt shift shmctl - shmget shmread shmwrite shutdown sin sleep socket socketpair sort - splice split sprintf sqrt srand stat state study sub substr symlink - syscall sysopen sysread sysseek system syswrite tell telldir tie tied - time times tr truncate uc ucfirst umask undef unless unlink unpack - unshift untie until use utime values vec wait waitpid wantarray warn - when while write x xor y + abs accept alarm and atan2 bind binmode bless break caller chdir chmod + chomp chop chown chr chroot close closedir cmp connect continue cos + crypt dbmclose dbmopen default defined delete die do dump each else + elsif endgrent endhostent endnetent endprotoent endpwent endservent + eof eq eval evalbytes exec exists exit exp fc fcntl fileno flock for + foreach fork format formline ge getc getgrent getgrgid getgrnam + gethostbyaddr gethostbyname gethostent getlogin getnetbyaddr + getnetbyname getnetent getpeername getpgrp getppid getpriority + getprotobyname getprotobynumber getprotoent getpwent getpwnam + getpwuid getservbyname getservbyport getservent getsockname + getsockopt given glob gmtime goto grep gt hex if index int ioctl join + keys kill last lc lcfirst le length link listen local localtime lock + log lstat lt m map mkdir msgctl msgget msgrcv msgsnd my ne next no + not oct open opendir or ord our pack package pipe pop pos print + printf prototype push q qq qr quotemeta qw qx rand read readdir + readline readlink readpipe recv redo ref rename require reset return + reverse rewinddir rindex rmdir s say scalar seek seekdir select semctl + semget semop send setgrent sethostent setnetent setpgrp + setpriority setprotoent setpwent setservent setsockopt shift shmctl + shmget shmread shmwrite shutdown sin sleep socket socketpair sort + splice split sprintf sqrt srand stat state study sub substr symlink + syscall sysopen sysread sysseek system syswrite tell telldir tie tied + time times tr truncate uc ucfirst umask undef unless unlink unpack + unshift untie until use utime values vec wait waitpid wantarray warn + when while write x xor y }; 1; diff --git a/lib/PPI/Statement.pm b/lib/PPI/Statement.pm index adccd88f..6e59c292 100644 --- a/lib/PPI/Statement.pm +++ b/lib/PPI/Statement.pm @@ -167,7 +167,7 @@ use PPI::Statement::Null (); use PPI::Statement::Package (); use PPI::Statement::Scheduled (); use PPI::Statement::Sub (); -use PPI::Statement::Given (); +use PPI::Statement::Given (); use PPI::Statement::UnmatchedBrace (); use PPI::Statement::Unknown (); use PPI::Statement::Variable (); @@ -178,10 +178,6 @@ use PPI::Statement::When (); # if we are at an implicit statement boundary. sub __LEXER__normal() { 1 } - - - - ##################################################################### # Constructor @@ -192,18 +188,15 @@ sub new { } # Create the object - my $self = bless { - children => [], - }, $class; + my $self = bless { children => [], }, $class; # If we have been passed what should be an initial token, add it my $token = shift; - if ( _INSTANCE($token, 'PPI::Token') ) { + if ( _INSTANCE( $token, 'PPI::Token' ) ) { # Inlined $self->__add_element(shift); - Scalar::Util::weaken( - $_PARENT{Scalar::Util::refaddr $token} = $self - ); - push @{$self->{children}}, $token; + Scalar::Util::weaken( $_PARENT{ Scalar::Util::refaddr $token } = + $self ); + push @{ $self->{children} }, $token; } $self; @@ -229,8 +222,8 @@ Returns false if the statement does not have a label. sub label { my $first = shift->schild(1) or return ''; $first->isa('PPI::Token::Label') - ? substr($first, 0, length($first) - 1) - : ''; + ? substr( $first, 0, length($first) - 1 ) + : ''; } =pod @@ -267,13 +260,9 @@ error. =cut sub stable { - die "The ->stable method has not yet been implemented"; + die "The ->stable method has not yet been implemented"; } - - - - ##################################################################### # PPI::Element Methods @@ -282,22 +271,19 @@ sub stable { sub _complete { my $self = shift; my $semi = $self->schild(-1); - return !! ( - defined $semi - and - $semi->isa('PPI::Token::Structure') - and - $semi->content eq ';' - ); + return !!( defined $semi + and $semi->isa('PPI::Token::Structure') + and $semi->content eq ';' ); } # You can insert either a statement or a non-significant token. sub insert_before { my $self = shift; - my $Element = _INSTANCE(shift, 'PPI::Element') or return undef; + my $Element = _INSTANCE( shift, 'PPI::Element' ) or return undef; if ( $Element->isa('PPI::Statement') ) { return $self->__insert_before($Element); - } elsif ( $Element->isa('PPI::Token') and ! $Element->significant ) { + } + elsif ( $Element->isa('PPI::Token') and !$Element->significant ) { return $self->__insert_before($Element); } ''; @@ -306,10 +292,11 @@ sub insert_before { # As above, you can insert a statement, or a non-significant token sub insert_after { my $self = shift; - my $Element = _INSTANCE(shift, 'PPI::Element') or return undef; + my $Element = _INSTANCE( shift, 'PPI::Element' ) or return undef; if ( $Element->isa('PPI::Statement') ) { return $self->__insert_after($Element); - } elsif ( $Element->isa('PPI::Token') and ! $Element->significant ) { + } + elsif ( $Element->isa('PPI::Token') and !$Element->significant ) { return $self->__insert_after($Element); } ''; diff --git a/lib/PPI/Statement/Compound.pm b/lib/PPI/Statement/Compound.pm index ea213949..d68c8607 100644 --- a/lib/PPI/Statement/Compound.pm +++ b/lib/PPI/Statement/Compound.pm @@ -70,10 +70,6 @@ my %TYPES = ( # Lexer clues sub __LEXER__normal() { '' } - - - - ##################################################################### # PPI::Statement::Compound analysis methods @@ -106,18 +102,18 @@ or C if the type cannot be determined. sub type { my $self = shift; - my $p = 0; # Child position + my $p = 0; # Child position my $Element = $self->schild($p) or return undef; # A labelled statement if ( $Element->isa('PPI::Token::Label') ) { - $Element = $self->schild(++$p) or return 'label'; + $Element = $self->schild( ++$p ) or return 'label'; } # Most simple cases my $content = $Element->content; if ( $content =~ /^for(?:each)?\z/ ) { - $Element = $self->schild(++$p) or return $content; + $Element = $self->schild( ++$p ) or return $content; if ( $Element->isa('PPI::Token') ) { return 'foreach' if $Element->content =~ /^my|our|state\z/; return 'foreach' if $Element->isa('PPI::Token::Symbol'); @@ -128,30 +124,20 @@ sub type { } return 'for'; } - return { - %TYPES, - ( try => 'try' ) x !!$self->presumed_features->{try}, - }->{$content} + return { %TYPES, ( try => 'try' ) x !!$self->presumed_features->{try}, } + ->{$content} if $Element->isa('PPI::Token::Word'); - return 'continue' if $Element->isa('PPI::Structure::Block'); + return 'continue' if $Element->isa('PPI::Structure::Block'); # Unknown (shouldn't exist?) undef; } - - - - ##################################################################### # PPI::Node Methods sub scope() { 1 } - - - - ##################################################################### # PPI::Element Methods @@ -169,9 +155,11 @@ sub _complete { $child->_complete or return ''; # It can STILL be - } elsif ( $type eq 'while' ) { + } + elsif ( $type eq 'while' ) { die "CODE INCOMPLETE"; - } else { + } + else { die "CODE INCOMPLETE"; } } diff --git a/lib/PPI/Statement/Given.pm b/lib/PPI/Statement/Given.pm index 60cefc15..fd215cc8 100644 --- a/lib/PPI/Statement/Given.pm +++ b/lib/PPI/Statement/Given.pm @@ -43,19 +43,11 @@ sub __LEXER__normal() { '' } sub _complete { my $child = $_[0]->schild(-1); - return !! ( - defined $child - and - $child->isa('PPI::Structure::Block') - and - $child->complete - ); + return !!( defined $child + and $child->isa('PPI::Structure::Block') + and $child->complete ); } - - - - ##################################################################### # PPI::Node Methods diff --git a/lib/PPI/Statement/Include.pm b/lib/PPI/Statement/Include.pm index 835e89eb..764b03c1 100644 --- a/lib/PPI/Statement/Include.pm +++ b/lib/PPI/Statement/Include.pm @@ -101,7 +101,7 @@ not specify a module name. =cut sub module { - my $self = shift; + my $self = shift; my $module = $self->schild(1) or return undef; $module->isa('PPI::Token::Word') and $module->content; } @@ -219,21 +219,19 @@ sub arguments { shift @args; # Remove the statement terminator - if ( - $args[-1]->isa('PPI::Token::Structure') - and - $args[-1]->content eq ';' - ) { + if ( $args[-1]->isa('PPI::Token::Structure') + and $args[-1]->content eq ';' ) + { pop @args; } # Remove the module or perl version. - shift @args; + shift @args; return unless @args; if ( $args[0]->isa('PPI::Token::Number') ) { - my $after = $args[1] or return; + my $after = $args[1] or return; $after->isa('PPI::Token::Operator') or shift @args; } @@ -324,14 +322,14 @@ sub _decompose_argument { sub _custom_feature_includes { my ($self) = @_; - return unless # + return unless # my $document = $self->document; return $document->custom_feature_includes || {}; } sub _custom_feature_include_cb { my ($self) = @_; - return unless # + return unless # my $document = $self->document; return $document->custom_feature_include_cb || sub { }; } diff --git a/lib/PPI/Statement/Package.pm b/lib/PPI/Statement/Package.pm index 91adced0..25f8b2bc 100644 --- a/lib/PPI/Statement/Package.pm +++ b/lib/PPI/Statement/Package.pm @@ -65,11 +65,11 @@ If the package statement is done any different way, it returns false. =cut sub namespace { - my $self = shift; + my $self = shift; my $namespace = $self->schild(1) or return ''; $namespace->isa('PPI::Token::Word') - ? $namespace->content - : ''; + ? $namespace->content + : ''; } =pod @@ -87,11 +87,11 @@ document (if any), otherwise the empty string. =cut sub version { - my $self = shift; + my $self = shift; my $version = $self->schild(2) or return ''; $version->isa('PPI::Token::Structure') - ? '' - : $version->content; + ? '' + : $version->content; } =pod @@ -113,11 +113,13 @@ a file does not represent a scope. =cut sub file_scoped { - my $self = shift; - my ($Parent, $Document) = ($self->parent, $self->top); - $Parent and $Document and $Parent == $Document - and $Document->isa('PPI::Document') - and ! $Document->isa('PPI::Document::Fragment'); + my $self = shift; + my ( $Parent, $Document ) = ( $self->parent, $self->top ); + $Parent + and $Document + and $Parent == $Document + and $Document->isa('PPI::Document') + and !$Document->isa('PPI::Document::Fragment'); } 1; diff --git a/lib/PPI/Statement/Scheduled.pm b/lib/PPI/Statement/Scheduled.pm index 24247a46..27c3e625 100644 --- a/lib/PPI/Statement/Scheduled.pm +++ b/lib/PPI/Statement/Scheduled.pm @@ -64,13 +64,9 @@ sub __LEXER__normal() { '' } sub _complete { my $child = $_[0]->schild(-1); - return !! ( - defined $child - and - $child->isa('PPI::Structure::Block') - and - $child->complete - ); + return !!( defined $child + and $child->isa('PPI::Structure::Block') + and $child->complete ); } =pod @@ -86,8 +82,8 @@ sub type { my $self = shift; my @children = $self->schildren or return undef; $children[0]->content eq 'sub' - ? $children[1]->content - : $children[0]->content; + ? $children[1]->content + : $children[0]->content; } # This is actually the same as Sub->name diff --git a/lib/PPI/Statement/Sub.pm b/lib/PPI/Statement/Sub.pm index 6a9ee2a9..744b7b71 100644 --- a/lib/PPI/Statement/Sub.pm +++ b/lib/PPI/Statement/Sub.pm @@ -44,19 +44,11 @@ sub __LEXER__normal() { '' } sub _complete { my $child = $_[0]->schild(-1); - return !! ( - defined $child - and - $child->isa('PPI::Structure::Block') - and - $child->complete - ); + return !!( defined $child + and $child->isa('PPI::Structure::Block') + and $child->complete ); } - - - - ##################################################################### # PPI::Statement::Sub Methods @@ -76,7 +68,7 @@ sub name { # Usually the second token is the name. # The third token is the name if this is a lexical subroutine. - my $token = $self->schild(defined $self->type ? 2 : 1); + my $token = $self->schild( defined $self->type ? 2 : 1 ); return $token->content if defined $token and $token->isa('PPI::Token::Word'); @@ -105,8 +97,9 @@ return is an empty string. sub prototype { my $self = shift; my $Prototype = List::Util::first { - _INSTANCE($_, 'PPI::Token::Prototype') - } $self->children; + _INSTANCE( $_, 'PPI::Token::Prototype' ) + } + $self->children; defined($Prototype) ? $Prototype->prototype : undef; } @@ -124,7 +117,7 @@ code block. =cut sub block { - my $self = shift; + my $self = shift; my $lastchild = $self->schild(-1) or return ''; $lastchild->isa('PPI::Structure::Block') and $lastchild; } @@ -142,7 +135,7 @@ if it does not. =cut sub forward { - ! shift->block; + !shift->block; } =pod @@ -189,12 +182,13 @@ sub type { my @schild = grep { $_->significant } $self->children; # Ignore labels - shift @schild if _INSTANCE($schild[0], 'PPI::Token::Label'); + shift @schild if _INSTANCE( $schild[0], 'PPI::Token::Label' ); # Get the type - (_INSTANCE($schild[0], 'PPI::Token::Word') and $schild[0]->content =~ /^(my|our|state)$/) - ? $schild[0]->content - : undef; + ( _INSTANCE( $schild[0], 'PPI::Token::Word' ) + and $schild[0]->content =~ /^(my|our|state)$/ ) + ? $schild[0]->content + : undef; } 1; diff --git a/lib/PPI/Statement/Variable.pm b/lib/PPI/Statement/Variable.pm index e9f3687d..59fb5010 100644 --- a/lib/PPI/Statement/Variable.pm +++ b/lib/PPI/Statement/Variable.pm @@ -65,12 +65,13 @@ sub type { my @schild = grep { $_->significant } $self->children; # Ignore labels - shift @schild if _INSTANCE($schild[0], 'PPI::Token::Label'); + shift @schild if _INSTANCE( $schild[0], 'PPI::Token::Label' ); # Get the type - (_INSTANCE($schild[0], 'PPI::Token::Word') and $schild[0]->content =~ /^(my|local|our|state)$/) - ? $schild[0]->content - : undef; + ( _INSTANCE( $schild[0], 'PPI::Token::Word' ) + and $schild[0]->content =~ /^(my|local|our|state)$/ ) + ? $schild[0]->content + : undef; } =pod @@ -104,30 +105,26 @@ sub symbols { # Get the children we care about my @schild = grep { $_->significant } $self->children; - shift @schild if _INSTANCE($schild[0], 'PPI::Token::Label'); + shift @schild if _INSTANCE( $schild[0], 'PPI::Token::Label' ); # If the second child is a symbol, return its name - if ( _INSTANCE($schild[1], 'PPI::Token::Symbol') ) { + if ( _INSTANCE( $schild[1], 'PPI::Token::Symbol' ) ) { return $schild[1]; } # If it's a list, return as a list - if ( _INSTANCE($schild[1], 'PPI::Structure::List') ) { + if ( _INSTANCE( $schild[1], 'PPI::Structure::List' ) ) { my $Expression = $schild[1]->schild(0); - $Expression and - $Expression->isa('PPI::Statement::Expression') or return (); + $Expression and $Expression->isa('PPI::Statement::Expression') + or return (); # my and our are simpler than local - if ( - $self->type eq 'my' - or - $self->type eq 'our' - or - $self->type eq 'state' - ) { - return grep { - $_->isa('PPI::Token::Symbol') - } $Expression->schildren; + if ( $self->type eq 'my' + or $self->type eq 'our' + or $self->type eq 'state' ) + { + return + grep { $_->isa('PPI::Token::Symbol') } $Expression->schildren; } # Local is much more icky (potentially). @@ -136,11 +133,8 @@ sub symbols { # for future bug reports about local() things. # This is a slightly better way to check. - return grep { - $self->_local_variable($_) - } grep { - $_->isa('PPI::Token::Symbol') - } $Expression->schildren; + return grep { $self->_local_variable($_) } + grep { $_->isa('PPI::Token::Symbol') } $Expression->schildren; } # erm... this is unexpected @@ -148,12 +142,12 @@ sub symbols { } sub _local_variable { - my ($self, $el) = @_; + my ( $self, $el ) = @_; # The last symbol should be a variable my $n = $el->snext_sibling or return 1; my $p = $el->sprevious_sibling; - if ( ! $p or $p eq ',' ) { + if ( !$p or $p eq ',' ) { # In the middle of a list return 1 if $n eq ','; diff --git a/lib/PPI/Statement/When.pm b/lib/PPI/Statement/When.pm index ae303c73..252fda17 100644 --- a/lib/PPI/Statement/When.pm +++ b/lib/PPI/Statement/When.pm @@ -51,19 +51,11 @@ sub __LEXER__normal() { '' } sub _complete { my $child = $_[0]->schild(-1); - return !! ( - defined $child - and - $child->isa('PPI::Structure::Block') - and - $child->complete - ); + return !!( defined $child + and $child->isa('PPI::Structure::Block') + and $child->complete ); } - - - - ##################################################################### # PPI::Node Methods diff --git a/lib/PPI/Structure.pm b/lib/PPI/Structure.pm index ad5fc54c..b4ba4e63 100644 --- a/lib/PPI/Structure.pm +++ b/lib/PPI/Structure.pm @@ -110,35 +110,25 @@ use PPI::Structure::Unknown (); use PPI::Structure::When (); use PPI::Structure::Signature (); - - - - ##################################################################### # Constructor sub new { my $class = shift; - my $Token = PPI::Token::__LEXER__opens($_[0]) ? shift : return undef; + my $Token = PPI::Token::__LEXER__opens( $_[0] ) ? shift : return undef; # Create the object my $self = bless { children => [], start => $Token, - }, $class; + }, $class; # Set the start braces parent link - Scalar::Util::weaken( - $_PARENT{Scalar::Util::refaddr $Token} = $self - ); + Scalar::Util::weaken( $_PARENT{ Scalar::Util::refaddr $Token } = $self ); $self; } - - - - ##################################################################### # PPI::Structure API methods @@ -162,7 +152,7 @@ due to manipulation of the PDOM tree. =cut -sub start { $_[0]->{start} } +sub start { $_[0]->{start} } =pod @@ -216,13 +206,9 @@ for the braces, and does not recurse downwards. =cut sub complete { - !! ($_[0]->{start} and $_[0]->{finish}); + !!( $_[0]->{start} and $_[0]->{finish} ); } - - - - ##################################################################### # PPI::Node overloaded methods @@ -230,15 +216,21 @@ sub complete { sub elements { my $self = shift; - if ( wantarray ) { + if (wantarray) { # Return a list in array context - return ( $self->{start} || (), @{$self->{children}}, $self->{finish} || () ); - } else { + return ( + $self->{start} || (), + @{ $self->{children} }, + $self->{finish} || () + ); + } + else { # Return the number of elements in scalar context. # This is memory-cheaper than creating another big array - return scalar(@{$self->{children}}) - + ($self->{start} ? 1 : 0) - + ($self->{finish} ? 1 : 0); + return + scalar( @{ $self->{children} } ) + + ( $self->{start} ? 1 : 0 ) + + ( $self->{finish} ? 1 : 0 ); } } @@ -263,21 +255,17 @@ sub location { $first->location; } - - - - ##################################################################### # PPI::Element overloaded methods # Get the full set of tokens, including start and finish sub tokens { - my $self = shift; + my $self = shift; my @tokens = ( - $self->{start} || (), + $self->{start} || (), $self->SUPER::tokens(@_), $self->{finish} || (), - ); + ); @tokens; } @@ -285,9 +273,9 @@ sub tokens { # This will recurse downwards through everything ### Reimplement this using List::Utils stuff sub content { - my $self = shift; + my $self = shift; my $content = $self->{start} ? $self->{start}->content : ''; - foreach my $child ( @{$self->{children}} ) { + foreach my $child ( @{ $self->{children} } ) { $content .= $child->content; } $content .= $self->{finish}->content if $self->{finish}; @@ -296,16 +284,17 @@ sub content { # Is the structure completed sub _complete { - !! ( defined $_[0]->{finish} ); + !!( defined $_[0]->{finish} ); } # You can insert either another structure, or a token sub insert_before { my $self = shift; - my $Element = _INSTANCE(shift, 'PPI::Element') or return undef; + my $Element = _INSTANCE( shift, 'PPI::Element' ) or return undef; if ( $Element->isa('PPI::Structure') ) { return $self->__insert_before($Element); - } elsif ( $Element->isa('PPI::Token') ) { + } + elsif ( $Element->isa('PPI::Token') ) { return $self->__insert_before($Element); } ''; @@ -314,10 +303,11 @@ sub insert_before { # As above, you can insert either another structure, or a token sub insert_after { my $self = shift; - my $Element = _INSTANCE(shift, 'PPI::Element') or return undef; + my $Element = _INSTANCE( shift, 'PPI::Element' ) or return undef; if ( $Element->isa('PPI::Structure') ) { return $self->__insert_after($Element); - } elsif ( $Element->isa('PPI::Token') ) { + } + elsif ( $Element->isa('PPI::Token') ) { return $self->__insert_after($Element); } ''; diff --git a/lib/PPI/Structure/Block.pm b/lib/PPI/Structure/Block.pm index 9efc75fd..bb4cb649 100644 --- a/lib/PPI/Structure/Block.pm +++ b/lib/PPI/Structure/Block.pm @@ -47,10 +47,6 @@ our $VERSION = '1.282'; our @ISA = "PPI::Structure"; - - - - ##################################################################### # PPI::Element Methods diff --git a/lib/PPI/Structure/For.pm b/lib/PPI/Structure/For.pm index 9db8056a..7ca58507 100644 --- a/lib/PPI/Structure/For.pm +++ b/lib/PPI/Structure/For.pm @@ -41,9 +41,10 @@ our @ISA = "PPI::Structure"; # Highly special custom isa method that will continue to respond # positively to ->isa('PPI::Structure::ForLoop') but warns. my $has_warned = 0; + sub isa { if ( $_[1] and $_[1] eq 'PPI::Structure::ForLoop' ) { - unless ( $has_warned ) { + unless ($has_warned) { warn("PPI::Structure::ForLoop has been deprecated"); $has_warned = 1; } diff --git a/lib/PPI/Structure/List.pm b/lib/PPI/Structure/List.pm index 212771de..fb5a1a80 100644 --- a/lib/PPI/Structure/List.pm +++ b/lib/PPI/Structure/List.pm @@ -44,14 +44,13 @@ our @ISA = "PPI::Structure"; # Highly special custom isa method that will continue to respond # positively to ->isa('PPI::Structure::ForLoop') but warns. my $has_warned = 0; + sub isa { if ( $_[1] and $_[1] eq 'PPI::Structure::ForLoop' ) { - if ( - $_[0]->parent->isa('PPI::Statement::Compound') - and - $_[0]->parent->type =~ /^for/ - ) { - unless ( $has_warned ) { + if ( $_[0]->parent->isa('PPI::Statement::Compound') + and $_[0]->parent->type =~ /^for/ ) + { + unless ($has_warned) { local $Carp::CarpLevel = $Carp::CarpLevel + 1; Carp::carp("PPI::Structure::ForLoop has been deprecated"); $has_warned = 1; diff --git a/lib/PPI/Structure/Signature.pm b/lib/PPI/Structure/Signature.pm index dfacdf0c..edf0e68d 100644 --- a/lib/PPI/Structure/Signature.pm +++ b/lib/PPI/Structure/Signature.pm @@ -1,61 +1,61 @@ -package PPI::Structure::Signature; - -=pod - -=head1 NAME - -PPI::Structure::Signature - List of subroutine signature elements - -=head1 SYNOPSIS - - sub do_thing( $param, $arg ) {} - -=head1 INHERITANCE - - PPI::Structure::Signature - isa PPI::Structure::List - isa PPI::Structure - isa PPI::Node - isa PPI::Element - -=head1 DESCRIPTION - -C is the class used for circular braces that -represent lists of signature elements. - -=head1 METHODS - -C has no methods beyond those provided by the -standard L, L, L and -L methods. - -=cut - -use strict; -use PPI::Structure (); - +package PPI::Structure::Signature; + +=pod + +=head1 NAME + +PPI::Structure::Signature - List of subroutine signature elements + +=head1 SYNOPSIS + + sub do_thing( $param, $arg ) {} + +=head1 INHERITANCE + + PPI::Structure::Signature + isa PPI::Structure::List + isa PPI::Structure + isa PPI::Node + isa PPI::Element + +=head1 DESCRIPTION + +C is the class used for circular braces that +represent lists of signature elements. + +=head1 METHODS + +C has no methods beyond those provided by the +standard L, L, L and +L methods. + +=cut + +use strict; +use PPI::Structure (); + our $VERSION = '1.282'; - -our @ISA = "PPI::Structure::List"; - -1; - -=head1 SUPPORT - -See the L in the main module. - -=head1 AUTHOR - -Adam Kennedy Eadamk@cpan.orgE - -=head1 COPYRIGHT - -Copyright 2001 - 2011 Adam Kennedy. - -This program is free software; you can redistribute -it and/or modify it under the same terms as Perl itself. - -The full text of the license can be found in the -LICENSE file included with this module. - -=cut + +our @ISA = "PPI::Structure::List"; + +1; + +=head1 SUPPORT + +See the L in the main module. + +=head1 AUTHOR + +Adam Kennedy Eadamk@cpan.orgE + +=head1 COPYRIGHT + +Copyright 2001 - 2011 Adam Kennedy. + +This program is free software; you can redistribute +it and/or modify it under the same terms as Perl itself. + +The full text of the license can be found in the +LICENSE file included with this module. + +=cut diff --git a/lib/PPI/Token.pm b/lib/PPI/Token.pm index 574a732c..989c381c 100644 --- a/lib/PPI/Token.pm +++ b/lib/PPI/Token.pm @@ -73,25 +73,22 @@ use PPI::Token::Prototype (); use PPI::Token::Attribute (); use PPI::Token::Unknown (); - - - - ##################################################################### # Constructor and Related sub new { - bless { content => (defined $_[1] ? "$_[1]" : '') }, $_[0]; + bless { content => ( defined $_[1] ? "$_[1]" : '' ) }, $_[0]; } sub set_class { - my $self = shift; + my $self = shift; # @_ or throw Exception("No arguments to set_class"); - my $class = substr( $_[0], 0, 12 ) eq 'PPI::Token::' ? shift : 'PPI::Token::' . shift; + my $class = + substr( $_[0], 0, 12 ) eq 'PPI::Token::' ? shift : 'PPI::Token::' . shift; # Find out if the current and new classes are complex - my $old_quote = (ref($self) =~ /\b(?:Quote|Regex)\b/o) ? 1 : 0; - my $new_quote = ($class =~ /\b(?:Quote|Regex)\b/o) ? 1 : 0; + my $old_quote = ( ref($self) =~ /\b(?:Quote|Regex)\b/o ) ? 1 : 0; + my $new_quote = ( $class =~ /\b(?:Quote|Regex)\b/o ) ? 1 : 0; # No matter what happens, we will have to rebless bless $self, $class; @@ -99,7 +96,7 @@ sub set_class { # If we are changing to or from a Quote style token, we # can't just rebless and need to do some extra thing # Otherwise, we have done enough - return $class if ($old_quote - $new_quote) == 0; + return $class if ( $old_quote - $new_quote ) == 0; # Make a new token from the old content, and overwrite the current # token's attributes with the new token's attributes. @@ -110,10 +107,6 @@ sub set_class { return $class; } - - - - ##################################################################### # PPI::Token Methods @@ -153,11 +146,7 @@ The C method returns the length of the string in a Token. =cut -sub length { CORE::length($_[0]->{content}) } - - - - +sub length { CORE::length( $_[0]->{content} ) } ##################################################################### # Overloaded PPI::Element methods @@ -169,10 +158,11 @@ sub content { # You can insert either a statement, or a non-significant token. sub insert_before { my $self = shift; - my $Element = _INSTANCE(shift, 'PPI::Element') or return undef; + my $Element = _INSTANCE( shift, 'PPI::Element' ) or return undef; if ( $Element->isa('PPI::Structure') ) { return $self->__insert_before($Element); - } elsif ( $Element->isa('PPI::Token') ) { + } + elsif ( $Element->isa('PPI::Token') ) { return $self->__insert_before($Element); } ''; @@ -181,19 +171,16 @@ sub insert_before { # As above, you can insert a statement, or a non-significant token sub insert_after { my $self = shift; - my $Element = _INSTANCE(shift, 'PPI::Element') or return undef; + my $Element = _INSTANCE( shift, 'PPI::Element' ) or return undef; if ( $Element->isa('PPI::Structure') ) { return $self->__insert_after($Element); - } elsif ( $Element->isa('PPI::Token') ) { + } + elsif ( $Element->isa('PPI::Token') ) { return $self->__insert_after($Element); } ''; } - - - - ##################################################################### # Tokenizer Methods @@ -201,23 +188,17 @@ sub __TOKENIZER__on_line_start() { 1 } sub __TOKENIZER__on_line_end() { 1 } sub __TOKENIZER__on_char() { 'Unknown' } - - - - ##################################################################### # Lexer Methods sub __LEXER__opens { - ref($_[0]) eq 'PPI::Token::Structure' - and - $_[0]->{content} =~ /(?:\(|\[|\{)/ + ref( $_[0] ) eq 'PPI::Token::Structure' + and $_[0]->{content} =~ /(?:\(|\[|\{)/; } sub __LEXER__closes { - ref($_[0]) eq 'PPI::Token::Structure' - and - $_[0]->{content} =~ /(?:\)|\]|\})/ + ref( $_[0] ) eq 'PPI::Token::Structure' + and $_[0]->{content} =~ /(?:\)|\]|\})/; } 1; diff --git a/lib/PPI/Token/ArrayIndex.pm b/lib/PPI/Token/ArrayIndex.pm index 1d9affce..575a3666 100644 --- a/lib/PPI/Token/ArrayIndex.pm +++ b/lib/PPI/Token/ArrayIndex.pm @@ -31,10 +31,6 @@ our $VERSION = '1.282'; our @ISA = "PPI::Token"; - - - - ##################################################################### # Tokenizer Methods @@ -49,7 +45,7 @@ sub __TOKENIZER__on_char { } # End of token - $t->_finalize_token->__TOKENIZER__on_char( $t ); + $t->_finalize_token->__TOKENIZER__on_char($t); } 1; diff --git a/lib/PPI/Token/Attribute.pm b/lib/PPI/Token/Attribute.pm index 6358d1e1..9021d5a9 100644 --- a/lib/PPI/Token/Attribute.pm +++ b/lib/PPI/Token/Attribute.pm @@ -37,9 +37,6 @@ our $VERSION = '1.282'; our @ISA = "PPI::Token"; - - - ##################################################################### # PPI::Token::Attribute Methods @@ -80,10 +77,6 @@ sub parameters { $self->{content} =~ /\((.*)\)$/ ? $1 : undef; } - - - - ##################################################################### # Tokenizer Methods @@ -95,13 +88,13 @@ sub __TOKENIZER__on_char { # Unless this is a '(', we are finished. unless ( $char eq '(' ) { # Finalise and recheck - return $t->_finalize_token->__TOKENIZER__on_char( $t ); + return $t->_finalize_token->__TOKENIZER__on_char($t); } # This is a bar(...) style attribute. # We are currently on the ( so scan in until the end. # We finish on the character AFTER our end - my $string = $class->__TOKENIZER__scan_for_end( $t ); + my $string = $class->__TOKENIZER__scan_for_end($t); if ( ref $string ) { # EOF $t->{token}->{content} .= $$string; @@ -111,7 +104,7 @@ sub __TOKENIZER__on_char { # Found the end of the attribute $t->{token}->{content} .= $string; - $t->_finalize_token->__TOKENIZER__on_char( $t ); + $t->_finalize_token->__TOKENIZER__on_char($t); } # Scan for a close braced, and take into account both escaping, @@ -122,7 +115,7 @@ sub __TOKENIZER__scan_for_end { # Loop as long as we can get new lines my $string = ''; - my $depth = 0; + my $depth = 0; while ( exists $t->{line} ) { # Get the search area pos $t->{line} = $t->{line_cursor}; @@ -141,7 +134,7 @@ sub __TOKENIZER__scan_for_end { $t->{line_cursor} += length $1; # Alter the depth and continue if we aren't at the end - $depth += ($1 =~ /\($/) ? 1 : -1 and next; + $depth += ( $1 =~ /\($/ ) ? 1 : -1 and next; # Found the end return $string; diff --git a/lib/PPI/Token/BOM.pm b/lib/PPI/Token/BOM.pm index b642b665..d35e6377 100644 --- a/lib/PPI/Token/BOM.pm +++ b/lib/PPI/Token/BOM.pm @@ -48,39 +48,38 @@ our @ISA = "PPI::Token"; sub significant() { '' } - - - - ##################################################################### # Parsing Methods my %bom_types = ( - "\x00\x00\xfe\xff" => 'UTF-32', - "\xff\xfe\x00\x00" => 'UTF-32', - "\xfe\xff" => 'UTF-16', - "\xff\xfe" => 'UTF-16', - "\xef\xbb\xbf" => 'UTF-8', + "\x00\x00\xfe\xff" => 'UTF-32', + "\xff\xfe\x00\x00" => 'UTF-32', + "\xfe\xff" => 'UTF-16', + "\xff\xfe" => 'UTF-16', + "\xef\xbb\xbf" => 'UTF-8', ); sub __TOKENIZER__on_line_start { my $t = $_[1]; $_ = $t->{line}; - if (m/^(\x00\x00\xfe\xff | # UTF-32, big-endian + if ( + m/^(\x00\x00\xfe\xff | # UTF-32, big-endian \xff\xfe\x00\x00 | # UTF-32, little-endian \xfe\xff | # UTF-16, big-endian \xff\xfe | # UTF-16, little-endian \xef\xbb\xbf) # UTF-8 - /xs) { - my $bom = $1; + /xs + ) + { + my $bom = $1; - if ($bom_types{$bom} ne 'UTF-8') { - return $t->_error("$bom_types{$bom} is not supported"); - } + if ( $bom_types{$bom} ne 'UTF-8' ) { + return $t->_error("$bom_types{$bom} is not supported"); + } - $t->_new_token('BOM', $bom) or return undef; - $t->{line_cursor} += length $bom; + $t->_new_token( 'BOM', $bom ) or return undef; + $t->{line_cursor} += length $bom; } # Continue just as if there was no BOM diff --git a/lib/PPI/Token/Cast.pm b/lib/PPI/Token/Cast.pm index ddf3ec0e..23a58f19 100644 --- a/lib/PPI/Token/Cast.pm +++ b/lib/PPI/Token/Cast.pm @@ -38,14 +38,11 @@ our @ISA = "PPI::Token"; our %POSTFIX = map { $_ => 1 } ( qw{ - %* @* $* + %* @* $* }, - '$#*' # throws warnings if it's inside a qw + '$#*' # throws warnings if it's inside a qw ); - - - ##################################################################### # Tokenizer Methods @@ -59,7 +56,7 @@ sub __TOKENIZER__on_char { my $content = $t->{token}->{content}; return 1 if $POSTFIX{ $content . $char }; - $t->_finalize_token->__TOKENIZER__on_char( $t ); + $t->_finalize_token->__TOKENIZER__on_char($t); } 1; diff --git a/lib/PPI/Token/Comment.pm b/lib/PPI/Token/Comment.pm index 565fb007..d347c633 100644 --- a/lib/PPI/Token/Comment.pm +++ b/lib/PPI/Token/Comment.pm @@ -75,7 +75,7 @@ sub __TOKENIZER__on_char { # Make sure not to include the trailing newline if ( substr( $t->{line}, $t->{line_cursor}, 1 ) eq "\n" ) { - return $t->_finalize_token->__TOKENIZER__on_char( $t ); + return $t->_finalize_token->__TOKENIZER__on_char($t); } 1; @@ -86,13 +86,14 @@ sub __TOKENIZER__commit { # Get the rest of the line my $rest = substr( $t->{line}, $t->{line_cursor} ); - if ( chomp $rest ) { # Include the newline separately - # Add the current token, and the newline - $t->_new_token('Comment', $rest); - $t->_new_token('Whitespace', "\n"); - } else { + if ( chomp $rest ) { # Include the newline separately + # Add the current token, and the newline + $t->_new_token( 'Comment', $rest ); + $t->_new_token( 'Whitespace', "\n" ); + } + else { # Add this token only - $t->_new_token('Comment', $rest); + $t->_new_token( 'Comment', $rest ); } # Advance the line cursor to the end diff --git a/lib/PPI/Token/DashedWord.pm b/lib/PPI/Token/DashedWord.pm index ebddd228..413c4882 100644 --- a/lib/PPI/Token/DashedWord.pm +++ b/lib/PPI/Token/DashedWord.pm @@ -44,8 +44,6 @@ C because C<-Foo'Bar> expands to C<-Foo::Bar>. *literal = *PPI::Token::Word::literal; - - ##################################################################### # Tokenizer Methods @@ -62,13 +60,14 @@ sub __TOKENIZER__on_char { # Are we a file test operator? if ( $t->{token}->{content} =~ /^\-[rwxoRWXOezsfdlpSbctugkTBMAC]$/ ) { # File test operator - $t->{class} = $t->{token}->set_class( 'Operator' ); - } else { + $t->{class} = $t->{token}->set_class('Operator'); + } + else { # No, normal dashed bareword - $t->{class} = $t->{token}->set_class( 'Word' ); + $t->{class} = $t->{token}->set_class('Word'); } - $t->_finalize_token->__TOKENIZER__on_char( $t ); + $t->_finalize_token->__TOKENIZER__on_char($t); } 1; diff --git a/lib/PPI/Token/Data.pm b/lib/PPI/Token/Data.pm index 5e8550a3..e03c7630 100644 --- a/lib/PPI/Token/Data.pm +++ b/lib/PPI/Token/Data.pm @@ -42,10 +42,6 @@ our $VERSION = '1.282'; our @ISA = "PPI::Token"; - - - - ##################################################################### # Methods diff --git a/lib/PPI/Token/End.pm b/lib/PPI/Token/End.pm index 2fedb322..40157076 100644 --- a/lib/PPI/Token/End.pm +++ b/lib/PPI/Token/End.pm @@ -47,10 +47,6 @@ our $VERSION = '1.282'; our @ISA = "PPI::Token"; - - - - ##################################################################### # Tokenizer Methods @@ -74,11 +70,13 @@ sub __TOKENIZER__on_line_start { # This is an error, but one we'll ignore # Don't go into Pod mode, since =cut normally # signals the end of Pod mode - } else { + } + else { if ( defined $t->{token} ) { # Add to existing token $t->{token}->{content} .= $t->{line}; - } else { + } + else { $t->_new_token( 'End', $t->{line} ); } } diff --git a/lib/PPI/Token/HereDoc.pm b/lib/PPI/Token/HereDoc.pm index b1615ece..039814b3 100644 --- a/lib/PPI/Token/HereDoc.pm +++ b/lib/PPI/Token/HereDoc.pm @@ -90,10 +90,6 @@ our $VERSION = '1.282'; our @ISA = "PPI::Token"; - - - - ##################################################################### # PPI::Token::HereDoc Methods @@ -110,7 +106,7 @@ the here-doc, B the terminator line. =cut -sub heredoc { @{shift->{_heredoc}} } +sub heredoc { @{ shift->{_heredoc} } } =pod @@ -143,9 +139,10 @@ sub terminator { sub _is_terminator { my ( $self, $terminator, $line, $indented ) = @_; - if ( $indented ) { + if ($indented) { return $line =~ /^\s*\Q$terminator\E$/; - } else { + } + else { return $line eq $terminator; } } @@ -158,12 +155,10 @@ sub _indent { sub _is_match_indent { my ( $self, $token, $indent ) = @_; - return (grep { /^$indent/ || $_ eq "\n" } @{$token->{_heredoc}}) == @{$token->{_heredoc}}; + return ( grep { /^$indent/ || $_ eq "\n" } @{ $token->{_heredoc} } ) == + @{ $token->{_heredoc} }; } - - - ##################################################################### # Tokenizer Methods @@ -179,10 +174,12 @@ sub __TOKENIZER__on_char { ### empty line. pos $t->{line} = $t->{line_cursor}; - if ( $t->{line} !~ m/\G( ~? \s* (?: "[^"]*" | '[^']*' | `[^`]*` | \\?\w+ ) )/gcx ) { + if ( $t->{line} !~ + m/\G( ~? \s* (?: "[^"]*" | '[^']*' | `[^`]*` | \\?\w+ ) )/gcx ) + { # Degenerate to a left-shift operation $t->{token}->set_class('Operator'); - return $t->_finalize_token->__TOKENIZER__on_char( $t ); + return $t->_finalize_token->__TOKENIZER__on_char($t); } # Add the rest of the token, work out what type it is, @@ -200,34 +197,39 @@ sub __TOKENIZER__on_char { $token->{_indented} = 1 if $1 eq '~'; $token->{_terminator} = $2; - } elsif ( $content =~ /^\<\<(~?)\s*\'(.*)\'$/ ) { + } + elsif ( $content =~ /^\<\<(~?)\s*\'(.*)\'$/ ) { # ''-quoted literal $token->{_mode} = 'literal'; $token->{_indented} = 1 if $1 eq '~'; $token->{_terminator} = $2; $token->{_terminator} =~ s/\\'/'/g; - } elsif ( $content =~ /^\<\<(~?)\s*\"(.*)\"$/ ) { + } + elsif ( $content =~ /^\<\<(~?)\s*\"(.*)\"$/ ) { # ""-quoted literal $token->{_mode} = 'interpolate'; $token->{_indented} = 1 if $1 eq '~'; $token->{_terminator} = $2; $token->{_terminator} =~ s/\\"/"/g; - } elsif ( $content =~ /^\<\<(~?)\s*\`(.*)\`$/ ) { + } + elsif ( $content =~ /^\<\<(~?)\s*\`(.*)\`$/ ) { # ``-quoted command $token->{_mode} = 'command'; $token->{_indented} = 1 if $1 eq '~'; $token->{_terminator} = $2; $token->{_terminator} =~ s/\\`/`/g; - } elsif ( $content =~ /^\<\<(~?)\\(\w+)$/ ) { + } + elsif ( $content =~ /^\<\<(~?)\\(\w+)$/ ) { # Legacy forward-slashed bareword $token->{_mode} = 'literal'; $token->{_indented} = 1 if $1 eq '~'; $token->{_terminator} = $2; - } else { + } + else { # WTF? return undef; } @@ -236,13 +238,14 @@ sub __TOKENIZER__on_char { $token->{_heredoc} = \my @heredoc; my $terminator = $token->{_terminator} . "\n"; while ( defined( my $line = $t->_get_line ) ) { - if ( $self->_is_terminator( $terminator, $line, $token->{_indented} ) ) { + if ( $self->_is_terminator( $terminator, $line, $token->{_indented} ) ) + { # Keep the actual termination line for consistency # when we are re-assembling the file $token->{_terminator_line} = $line; if ( $token->{_indented} ) { - my $indent = $self->_indent( $token ); + my $indent = $self->_indent($token); $token->{_indentation} = $indent; # Indentation of here-doc doesn't match delimiter unless ( $self->_is_match_indent( $token, $indent ) ) { @@ -254,7 +257,7 @@ sub __TOKENIZER__on_char { } # The HereDoc is now fully parsed - return $t->_finalize_token->__TOKENIZER__on_char( $t ); + return $t->_finalize_token->__TOKENIZER__on_char($t); } # Add the line @@ -281,11 +284,12 @@ sub __TOKENIZER__on_char { # newline at the end. If so, remove it from the content and set it as # the terminator line. $token->{_terminator_line} = pop @heredoc - if $self->_is_terminator( $token->{_terminator}, $heredoc[-1], $token->{_indented} ); + if $self->_is_terminator( $token->{_terminator}, $heredoc[-1], + $token->{_indented} ); } if ( $token->{_indented} && $token->{_terminator_line} ) { - my $indent = $self->_indent( $token ); + my $indent = $self->_indent($token); $token->{_indentation} = $indent; if ( $self->_is_match_indent( $token, $indent ) ) { # Remove indent from here-doc as much as possible @@ -300,7 +304,7 @@ sub __TOKENIZER__on_char { $token->{_damaged} = 1; # The HereDoc is not fully parsed - $t->_finalize_token->__TOKENIZER__on_char( $t ); + $t->_finalize_token->__TOKENIZER__on_char($t); } 1; diff --git a/lib/PPI/Token/Magic.pm b/lib/PPI/Token/Magic.pm index 6b907344..ab433a7e 100644 --- a/lib/PPI/Token/Magic.pm +++ b/lib/PPI/Token/Magic.pm @@ -42,9 +42,9 @@ L, L and L. =cut use strict; -use PPI::Token::Symbol (); +use PPI::Token::Symbol (); use PPI::Token::Unknown (); -use PPI::Singletons qw' %MAGIC $CURLY_SYMBOL '; +use PPI::Singletons qw' %MAGIC $CURLY_SYMBOL '; our $VERSION = '1.282'; @@ -69,19 +69,19 @@ sub __TOKENIZER__on_char { if ( $c =~ /^\$\'\d$/ ) { # In this case, we have a magic plus a digit. # Save the CURRENT token, and rerun the on_char - return $t->_finalize_token->__TOKENIZER__on_char( $t ); + return $t->_finalize_token->__TOKENIZER__on_char($t); } # A symbol in the style $_foo or $::foo or $'foo. # Overwrite the current token $t->{class} = $t->{token}->set_class('Symbol'); - return PPI::Token::Symbol->__TOKENIZER__on_char( $t ); + return PPI::Token::Symbol->__TOKENIZER__on_char($t); } if ( $c =~ /^\$\$\w/ ) { # This is really a scalar dereference. ( $$foo ) # Add the current token as the cast... - $t->{token} = PPI::Token::Cast->new( '$' ); + $t->{token} = PPI::Token::Cast->new('$'); $t->_finalize_token; # ... and create a new token for the symbol @@ -95,7 +95,7 @@ sub __TOKENIZER__on_char { if ( $t->{line} =~ m/$CURLY_SYMBOL/gc ) { # This is really a dereference. ( $${^_foo} ) # Add the current token as the cast... - $t->{token} = PPI::Token::Cast->new( '$' ); + $t->{token} = PPI::Token::Cast->new('$'); $t->_finalize_token; # ... and create a new token for the symbol @@ -107,22 +107,24 @@ sub __TOKENIZER__on_char { # This is really an index dereferencing cast, although # it has the same two chars as the magic variable $#. $t->{class} = $t->{token}->set_class('Cast'); - return $t->_finalize_token->__TOKENIZER__on_char( $t ); + return $t->_finalize_token->__TOKENIZER__on_char($t); } if ( $c =~ /^(\$\#)\w/ ) { # This is really an array index thingy ( $#array ) - $t->{token} = PPI::Token::ArrayIndex->new( "$1" ); - return PPI::Token::ArrayIndex->__TOKENIZER__on_char( $t ); + $t->{token} = PPI::Token::ArrayIndex->new("$1"); + return PPI::Token::ArrayIndex->__TOKENIZER__on_char($t); } if ( $c =~ /^\$\^\w+$/o ) { # It's an escaped char magic... maybe ( like $^M ) - my $next = substr( $t->{line}, $t->{line_cursor}+1, 1 ); # Peek ahead - if ($MAGIC{$c} && (!$next || $next !~ /\w/)) { + my $next = + substr( $t->{line}, $t->{line_cursor} + 1, 1 ); # Peek ahead + if ( $MAGIC{$c} && ( !$next || $next !~ /\w/ ) ) { $t->{token}->{content} = $c; $t->{line_cursor}++; - } else { + } + else { # Maybe it's a long magic variable like $^WIDE_SYSTEM_CALLS return 1; } @@ -131,19 +133,21 @@ sub __TOKENIZER__on_char { if ( $c =~ /^\$\#\{/ ) { # The $# is actually a cast, and { is its block # Add the current token as the cast... - $t->{token} = PPI::Token::Cast->new( '$#' ); + $t->{token} = PPI::Token::Cast->new('$#'); $t->_finalize_token; # ... and create a new token for the block return $t->_new_token( 'Structure', '{' ); } - } elsif ($c =~ /^%\^/) { + } + elsif ( $c =~ /^%\^/ ) { return 1 if $c eq '%^'; # It's an escaped char magic... maybe ( like %^H ) - if ($MAGIC{$c}) { + if ( $MAGIC{$c} ) { $t->{token}->{content} = $c; $t->{line_cursor}++; - } else { + } + else { # Back off, treat '%' as an operator chop $t->{token}->{content}; bless $t->{token}, $t->{class} = 'PPI::Token::Operator'; @@ -153,15 +157,17 @@ sub __TOKENIZER__on_char { if ( $MAGIC{$c} ) { # $#+ and $#- - $t->{line_cursor} += length( $c ) - length( $t->{token}->{content} ); + $t->{line_cursor} += length($c) - length( $t->{token}->{content} ); $t->{token}->{content} = $c; - } else { + } + else { pos $t->{line} = $t->{line_cursor}; if ( $t->{line} =~ m/($CURLY_SYMBOL)/gc ) { # control character symbol (e.g. ${^MATCH}) $t->{token}->{content} .= $1; - $t->{line_cursor} += length $1; - } elsif ( $c =~ /^\$\d+$/ and $t->{line} =~ /\G(\d+)/gc ) { + $t->{line_cursor} += length $1; + } + elsif ( $c =~ /^\$\d+$/ and $t->{line} =~ /\G(\d+)/gc ) { # Grab trailing digits of regex capture variables. $t->{token}{content} .= $1; $t->{line_cursor} += length $1; @@ -169,7 +175,7 @@ sub __TOKENIZER__on_char { } # End the current magic token, and recheck - $t->_finalize_token->__TOKENIZER__on_char( $t ); + $t->_finalize_token->__TOKENIZER__on_char($t); } # Our version of canonical is plain simple diff --git a/lib/PPI/Token/Number.pm b/lib/PPI/Token/Number.pm index 5f2c1778..17d664fa 100644 --- a/lib/PPI/Token/Number.pm +++ b/lib/PPI/Token/Number.pm @@ -68,10 +68,6 @@ sub _literal { return $string; } - - - - ##################################################################### # Tokenizer Methods @@ -89,20 +85,23 @@ sub __TOKENIZER__on_char { if ( $token->{content} =~ /^-?0_*$/ ) { # This could be special if ( $char eq 'x' || $char eq 'X' ) { - $t->{class} = $t->{token}->set_class( 'Number::Hex' ); + $t->{class} = $t->{token}->set_class('Number::Hex'); return 1; - } elsif ( $char eq 'b' || $char eq 'B' ) { - $t->{class} = $t->{token}->set_class( 'Number::Binary' ); + } + elsif ( $char eq 'b' || $char eq 'B' ) { + $t->{class} = $t->{token}->set_class('Number::Binary'); return 1; - } elsif ( $char eq 'o' || $char eq 'O' ) { - $t->{class} = $t->{token}->set_class( 'Number::Octal' ); + } + elsif ( $char eq 'o' || $char eq 'O' ) { + $t->{class} = $t->{token}->set_class('Number::Octal'); return 1; - } elsif ( $char =~ /\d/ ) { + } + elsif ( $char =~ /\d/ ) { # You cannot have 8s and 9s on octals if ( $char eq '8' or $char eq '9' ) { $token->{_error} = "Illegal character in octal number '$char'"; } - $t->{class} = $t->{token}->set_class( 'Number::Octal' ); + $t->{class} = $t->{token}->set_class('Number::Octal'); return 1; } } @@ -111,17 +110,17 @@ sub __TOKENIZER__on_char { return 1 if $char =~ /\d/o; if ( $char eq '.' ) { - $t->{class} = $t->{token}->set_class( 'Number::Float' ); + $t->{class} = $t->{token}->set_class('Number::Float'); return 1; } if ( $char eq 'e' || $char eq 'E' ) { - $t->{class} = $t->{token}->set_class( 'Number::Exp' ); + $t->{class} = $t->{token}->set_class('Number::Exp'); return 1; } # Doesn't fit a special case, or is after the end of the token # End of token. - $t->_finalize_token->__TOKENIZER__on_char( $t ); + $t->_finalize_token->__TOKENIZER__on_char($t); } 1; diff --git a/lib/PPI/Token/Number/Binary.pm b/lib/PPI/Token/Number/Binary.pm index 1345f569..a13d5d30 100644 --- a/lib/PPI/Token/Number/Binary.pm +++ b/lib/PPI/Token/Number/Binary.pm @@ -64,10 +64,6 @@ sub literal { return $neg ? -$val : $val; } - - - - ##################################################################### # Tokenizer Methods @@ -82,14 +78,15 @@ sub __TOKENIZER__on_char { if ( $char =~ /[\w\d]/ ) { unless ( $char eq '1' or $char eq '0' ) { # Add a warning if it contains non-binary chars - $t->{token}->{_error} = "Illegal character in binary number '$char'"; + $t->{token}->{_error} = + "Illegal character in binary number '$char'"; } return 1; } # Doesn't fit a special case, or is after the end of the token # End of token. - $t->_finalize_token->__TOKENIZER__on_char( $t ); + $t->_finalize_token->__TOKENIZER__on_char($t); } 1; diff --git a/lib/PPI/Token/Number/Exp.pm b/lib/PPI/Token/Number/Exp.pm index 34ddb1af..66ab42d0 100644 --- a/lib/PPI/Token/Number/Exp.pm +++ b/lib/PPI/Token/Number/Exp.pm @@ -46,28 +46,28 @@ Return the numeric value of this token. sub literal { my $self = shift; return if $self->{_error}; - my ($mantissa, $exponent) = split m/e/i, $self->_literal; + my ( $mantissa, $exponent ) = split m/e/i, $self->_literal; my $neg = $mantissa =~ s/^\-//; $mantissa =~ s/^\./0./; $exponent =~ s/^\+//; - # Must cast exponent as numeric type, due to string type '00' exponent - # creating false positive condition in for() loop below, causing infinite loop + # Must cast exponent as numeric type, due to string type '00' exponent + # creating false positive condition in for() loop below, causing infinite loop $exponent += 0; # This algorithm is reasonably close to the S_mulexp10() # algorithm from the Perl source code, so it should arrive # at the same answer as Perl most of the time. my $negpow = 0; - if ($exponent < 0) { + if ( $exponent < 0 ) { $negpow = 1; $exponent *= -1; } my $result = 1; - my $power = 10; - for (my $bit = 1; $exponent; $bit = $bit << 1) { - if ($exponent & $bit) { + my $power = 10; + for ( my $bit = 1 ; $exponent ; $bit = $bit << 1 ) { + if ( $exponent & $bit ) { $exponent = $exponent ^ $bit; $result *= $power; } @@ -78,10 +78,6 @@ sub literal { return $negpow ? $val / $result : $val * $result; } - - - - ##################################################################### # Tokenizer Methods @@ -90,7 +86,7 @@ sub __TOKENIZER__on_char { my $t = shift; my $char = substr( $t->{line}, $t->{line_cursor}, 1 ); - # To get here, the token must have already encountered an 'E' + # To get here, the token must have already encountered an 'E' # Allow underscores straight through return 1 if $char eq '_'; @@ -107,9 +103,9 @@ sub __TOKENIZER__on_char { if ( $t->{token}->{content} =~ s/\.(e)$//i ) { my $word = $1; $t->{class} = $t->{token}->set_class('Number'); - $t->_new_token('Operator', '.'); - $t->_new_token('Word', $word); - return $t->{class}->__TOKENIZER__on_char( $t ); + $t->_new_token( 'Operator', '.' ); + $t->_new_token( 'Word', $word ); + return $t->{class}->__TOKENIZER__on_char($t); } else { $t->{token}->{_error} = "Illegal character in exponent '$char'"; @@ -118,7 +114,7 @@ sub __TOKENIZER__on_char { # Doesn't fit a special case, or is after the end of the token # End of token. - $t->_finalize_token->__TOKENIZER__on_char( $t ); + $t->_finalize_token->__TOKENIZER__on_char($t); } 1; diff --git a/lib/PPI/Token/Number/Float.pm b/lib/PPI/Token/Number/Float.pm index c409bc0e..130cb7d4 100644 --- a/lib/PPI/Token/Number/Float.pm +++ b/lib/PPI/Token/Number/Float.pm @@ -55,17 +55,13 @@ Return the numeric value of this token. sub literal { my $self = shift; - my $str = $self->_literal; - my $neg = $str =~ s/^\-//; + my $str = $self->_literal; + my $neg = $str =~ s/^\-//; $str =~ s/^\./0./; - my $val = 0+$str; + my $val = 0 + $str; return $neg ? -$val : $val; } - - - - ##################################################################### # Tokenizer Methods @@ -80,39 +76,41 @@ sub __TOKENIZER__on_char { # Allow digits return 1 if $char =~ /\d/o; - if ( $char eq '.' ) { # A second decimal point? That gets complicated. + if ( $char eq '.' ) { # A second decimal point? That gets complicated. if ( $t->{token}{content} =~ /\.$/ ) { # We have a .., which is an operator. Take the . off the end of the # token and finish it, then make the .. operator. chop $t->{token}{content}; - $t->{class} = $t->{token}->set_class( 'Number' ); - $t->_new_token('Operator', '..'); + $t->{class} = $t->{token}->set_class('Number'); + $t->_new_token( 'Operator', '..' ); return 0; - } elsif ( $t->{token}{content} =~ /\._/ ) { - ($t->{token}{content}, my $bareword) - = split /\./, $t->{token}{content}; - $t->{class} = $t->{token}->set_class( 'Number' ); - $t->_new_token('Operator', '.'); - $t->_new_token('Word', $bareword); - $t->_new_token('Operator', '.'); + } + elsif ( $t->{token}{content} =~ /\._/ ) { + ( $t->{token}{content}, my $bareword ) = split /\./, + $t->{token}{content}; + $t->{class} = $t->{token}->set_class('Number'); + $t->_new_token( 'Operator', '.' ); + $t->_new_token( 'Word', $bareword ); + $t->_new_token( 'Operator', '.' ); return 0; - } else { - $t->{class} = $t->{token}->set_class( 'Number::Version' ); + } + else { + $t->{class} = $t->{token}->set_class('Number::Version'); return 1; } } # perl seems to regard pretty much anything that's not strictly an exp num # as float + stuff - my $char2 = substr $t->{line}, $t->{line_cursor}+1, 1; - if ("$char$char2" =~ /[eE][0-9+-]/) { - $t->{class} = $t->{token}->set_class( 'Number::Exp' ); + my $char2 = substr $t->{line}, $t->{line_cursor} + 1, 1; + if ( "$char$char2" =~ /[eE][0-9+-]/ ) { + $t->{class} = $t->{token}->set_class('Number::Exp'); return 1; } # Doesn't fit a special case, or is after the end of the token # End of token. - $t->_finalize_token->__TOKENIZER__on_char( $t ); + $t->_finalize_token->__TOKENIZER__on_char($t); } 1; diff --git a/lib/PPI/Token/Number/Hex.pm b/lib/PPI/Token/Number/Hex.pm index 4c0c852e..d1f591fc 100644 --- a/lib/PPI/Token/Number/Hex.pm +++ b/lib/PPI/Token/Number/Hex.pm @@ -53,16 +53,12 @@ Return the numeric value of this token. sub literal { my $self = shift; - my $str = $self->_literal; - my $neg = $str =~ s/^\-//; - my $val = hex lc( $str ); # lc for compatibility with perls before 5.14 + my $str = $self->_literal; + my $neg = $str =~ s/^\-//; + my $val = hex lc($str); # lc for compatibility with perls before 5.14 return $neg ? -$val : $val; } - - - - ##################################################################### # Tokenizer Methods @@ -80,7 +76,7 @@ sub __TOKENIZER__on_char { # Doesn't fit a special case, or is after the end of the token # End of token. - $t->_finalize_token->__TOKENIZER__on_char( $t ); + $t->_finalize_token->__TOKENIZER__on_char($t); } 1; diff --git a/lib/PPI/Token/Number/Octal.pm b/lib/PPI/Token/Number/Octal.pm index cd349994..0a209b69 100644 --- a/lib/PPI/Token/Number/Octal.pm +++ b/lib/PPI/Token/Number/Octal.pm @@ -62,10 +62,6 @@ sub literal { return $neg ? -$val : $val; } - - - - ##################################################################### # Tokenizer Methods @@ -87,7 +83,7 @@ sub __TOKENIZER__on_char { # Doesn't fit a special case, or is after the end of the token # End of token. - $t->_finalize_token->__TOKENIZER__on_char( $t ); + $t->_finalize_token->__TOKENIZER__on_char($t); } 1; diff --git a/lib/PPI/Token/Number/Version.pm b/lib/PPI/Token/Number/Version.pm index ad335fdb..ff904cfe 100644 --- a/lib/PPI/Token/Number/Version.pm +++ b/lib/PPI/Token/Number/Version.pm @@ -62,10 +62,6 @@ sub literal { return join '', map { chr $_ } ( split /\./, $content ); } - - - - ##################################################################### # Tokenizer Methods @@ -77,14 +73,14 @@ sub __TOKENIZER__on_char { # Allow digits return 1 if $char =~ /\d/o; - if( $char eq '_' ) { + if ( $char eq '_' ) { return 1 if $t->{token}{content} !~ /\.$/; chop $t->{token}->{content}; - $t->{class} = $t->{token}->set_class( 'Number::Float' ) - if $t->{token}{content} !~ /\..+\./; - $t->_new_token('Operator', '.'); - $t->_new_token('Word', '_'); + $t->{class} = $t->{token}->set_class('Number::Float') + if $t->{token}{content} !~ /\..+\./; + $t->_new_token( 'Operator', '.' ); + $t->_new_token( 'Word', '_' ); return 0; } @@ -95,18 +91,19 @@ sub __TOKENIZER__on_char { # Take the . off the end of the token.. # and finish it, then make the .. operator. chop $t->{token}->{content}; - $t->{class} = $t->{token}->set_class( 'Number::Float' ) - if $t->{token}{content} !~ /\..+\./; - $t->_new_token('Operator', '..'); + $t->{class} = $t->{token}->set_class('Number::Float') + if $t->{token}{content} !~ /\..+\./; + $t->_new_token( 'Operator', '..' ); return 0; - } else { + } + else { return 1; } } # Doesn't fit a special case, or is after the end of the token # End of token. - $t->_finalize_token->__TOKENIZER__on_char( $t ); + $t->_finalize_token->__TOKENIZER__on_char($t); } sub __TOKENIZER__commit { @@ -116,14 +113,14 @@ sub __TOKENIZER__commit { pos $t->{line} = $t->{line_cursor}; # This was not a v-string after all (it's a word); return PPI::Token::Word->__TOKENIZER__commit($t) - if $t->{line} !~ m/\G(v\d[_\d]*(?:\.\d[_\d]*)+|v\d[_\d]*\b)/gc; + if $t->{line} !~ m/\G(v\d[_\d]*(?:\.\d[_\d]*)+|v\d[_\d]*\b)/gc; my $content = $1; # If there are no periods this could be a word starting with v\d # Forced to be a word. Done. return PPI::Token::Word->__TOKENIZER__commit($t) - if $content !~ /\./ and $t->__current_token_is_forced_word($content); + if $content !~ /\./ and $t->__current_token_is_forced_word($content); # This is a v-string $t->{line_cursor} += length $content; diff --git a/lib/PPI/Token/Operator.pm b/lib/PPI/Token/Operator.pm index 809d8ed7..95c0bc6c 100644 --- a/lib/PPI/Token/Operator.pm +++ b/lib/PPI/Token/Operator.pm @@ -47,10 +47,6 @@ our $VERSION = '1.282'; our @ISA = "PPI::Token"; - - - - ##################################################################### # Tokenizer Methods @@ -61,8 +57,11 @@ sub __TOKENIZER__on_char { # Are we still an operator if we add the next character my $content = $t->{token}->{content}; # special case for <<>> operator - if(length($content) < 4 && - $content . substr( $t->{line}, $t->{line_cursor}, 4 - length($content) ) eq '<<>>') { + if ( length($content) < 4 + && $content + . substr( $t->{line}, $t->{line_cursor}, 4 - length($content) ) eq + '<<>>' ) + { return 1; } return 1 if $OPERATOR{ $content . $char }; @@ -72,7 +71,7 @@ sub __TOKENIZER__on_char { if ( $char =~ /^[0-9]$/ ) { # This is a decimal number $t->{class} = $t->{token}->set_class('Number::Float'); - return $t->{class}->__TOKENIZER__on_char( $t ); + return $t->{class}->__TOKENIZER__on_char($t); } } @@ -84,19 +83,19 @@ sub __TOKENIZER__on_char { ### Is the zero-width look-ahead assertion really ### supposed to be there? if ( $t->{line} =~ m/\G ~? (?: (?!\d)\w | \s*['"`] | \\\w ) /gcx ) { - # This is a here-doc. - # Change the class and move to the HereDoc's own __TOKENIZER__on_char method. + # This is a here-doc. + # Change the class and move to the HereDoc's own __TOKENIZER__on_char method. $t->{class} = $t->{token}->set_class('HereDoc'); - return $t->{class}->__TOKENIZER__on_char( $t ); + return $t->{class}->__TOKENIZER__on_char($t); } } # Handle the special case of the null Readline $t->{class} = $t->{token}->set_class('QuoteLike::Readline') - if $content eq '<>' or $content eq '<<>>'; + if $content eq '<>' or $content eq '<<>>'; # Finalize normally - $t->_finalize_token->__TOKENIZER__on_char( $t ); + $t->_finalize_token->__TOKENIZER__on_char($t); } 1; diff --git a/lib/PPI/Token/Pod.pm b/lib/PPI/Token/Pod.pm index 8f322d98..1ed1406b 100644 --- a/lib/PPI/Token/Pod.pm +++ b/lib/PPI/Token/Pod.pm @@ -32,10 +32,6 @@ our $VERSION = '1.282'; our @ISA = "PPI::Token"; - - - - ##################################################################### # PPI::Token::Pod Methods @@ -52,10 +48,10 @@ Returns a new C object, or C on error. =cut sub merge { - my $class = (! ref $_[0]) ? shift : return undef; + my $class = ( !ref $_[0] ) ? shift : return undef; # Check there are no bad arguments - if ( grep { ! _INSTANCE($_, 'PPI::Token::Pod') } @_ ) { + if ( grep { !_INSTANCE( $_, 'PPI::Token::Pod' ) } @_ ) { return undef; } @@ -64,7 +60,7 @@ sub merge { # Remove the leading =pod tags, trailing =cut tags, and any empty lines # between them and the pod contents. - foreach my $pod ( @content ) { + foreach my $pod (@content) { # Leading =pod tag if ( @$pod and $pod->[0] =~ /^=pod\b/o ) { shift @$pod; @@ -76,13 +72,13 @@ sub merge { } # Leading and trailing empty lines - while ( @$pod and $pod->[0] eq '' ) { shift @$pod } - while ( @$pod and $pod->[-1] eq '' ) { pop @$pod } + while ( @$pod and $pod->[0] eq '' ) { shift @$pod } + while ( @$pod and $pod->[-1] eq '' ) { pop @$pod } } # Remove any empty pod sections, and add the =pod and =cut tags # for the merged pod back to it. - @content = ( [ '=pod' ], grep { @$_ } @content, [ '=cut' ] ); + @content = ( ['=pod'], grep { @$_ } @content, ['=cut'] ); # Create the new object $class->new( join "\n", map { join( "\n", @$_ ) . "\n" } @content ); @@ -101,21 +97,12 @@ sub lines { split /(?:\015{1,2}\012|\015|\012)/, $_[0]->{content}; } - - - - - ##################################################################### # PPI::Element Methods ### XS -> PPI/XS.xs:_PPI_Token_Pod__significant 0.900+ sub significant() { '' } - - - - ##################################################################### # Tokenizer Methods diff --git a/lib/PPI/Token/Quote.pm b/lib/PPI/Token/Quote.pm index 5a1a56fd..d3bf2131 100644 --- a/lib/PPI/Token/Quote.pm +++ b/lib/PPI/Token/Quote.pm @@ -53,10 +53,6 @@ our $VERSION = '1.282'; our @ISA = "PPI::Token"; - - - - ##################################################################### # PPI::Token::Quote Methods diff --git a/lib/PPI/Token/Quote/Double.pm b/lib/PPI/Token/Quote/Double.pm index fa5d1364..3e0f839d 100644 --- a/lib/PPI/Token/Quote/Double.pm +++ b/lib/PPI/Token/Quote/Double.pm @@ -37,14 +37,10 @@ use PPI::Token::_QuoteEngine::Simple (); our $VERSION = '1.282'; our @ISA = qw{ - PPI::Token::_QuoteEngine::Simple - PPI::Token::Quote + PPI::Token::_QuoteEngine::Simple + PPI::Token::Quote }; - - - - ##################################################################### # PPI::Token::Quote::Double Methods @@ -63,7 +59,7 @@ Returns true if the string contains interpolations, or false if not. # Upgrade: Returns parsed expressions. sub interpolations { # Are there any unescaped $things in the string - !! ($_[0]->content =~ /(?content =~ /(?content; - my $value = substr($content, 1, length($content) - 2); + my $value = substr( $content, 1, length($content) - 2 ); return $self if $value =~ /[\\\$@\'\"]/; # Change the token to a single string @@ -99,12 +95,6 @@ sub simplify { bless $self, 'PPI::Token::Quote::Single'; } - - - - - - ##################################################################### # PPI::Token::Quote Methods diff --git a/lib/PPI/Token/Quote/Interpolate.pm b/lib/PPI/Token/Quote/Interpolate.pm index 02279f74..3e5edf4d 100644 --- a/lib/PPI/Token/Quote/Interpolate.pm +++ b/lib/PPI/Token/Quote/Interpolate.pm @@ -27,20 +27,16 @@ L classes. =cut use strict; -use PPI::Token::Quote (); +use PPI::Token::Quote (); use PPI::Token::_QuoteEngine::Full (); our $VERSION = '1.282'; our @ISA = qw{ - PPI::Token::_QuoteEngine::Full - PPI::Token::Quote + PPI::Token::_QuoteEngine::Full + PPI::Token::Quote }; - - - - ##################################################################### # PPI::Token::Quote Methods @@ -48,7 +44,7 @@ sub string { my $self = shift; my @sections = $self->_sections; my $str = $sections[0]; - substr( $self->{content}, $str->{position}, $str->{size} ); + substr( $self->{content}, $str->{position}, $str->{size} ); } 1; diff --git a/lib/PPI/Token/Quote/Literal.pm b/lib/PPI/Token/Quote/Literal.pm index 7970fd3e..470c5e76 100644 --- a/lib/PPI/Token/Quote/Literal.pm +++ b/lib/PPI/Token/Quote/Literal.pm @@ -33,14 +33,10 @@ use PPI::Token::_QuoteEngine::Full (); our $VERSION = '1.282'; our @ISA = qw{ - PPI::Token::_QuoteEngine::Full - PPI::Token::Quote + PPI::Token::_QuoteEngine::Full + PPI::Token::Quote }; - - - - ##################################################################### # PPI::Token::Quote Methods @@ -52,7 +48,6 @@ sub string { substr( $self->{content}, $str->{position}, $str->{size} ); } - # Use the same implementation as another module *literal = *PPI::Token::Quote::Single::literal; diff --git a/lib/PPI/Token/Quote/Single.pm b/lib/PPI/Token/Quote/Single.pm index 1d4ceb6f..c664c25c 100644 --- a/lib/PPI/Token/Quote/Single.pm +++ b/lib/PPI/Token/Quote/Single.pm @@ -33,20 +33,16 @@ L classes. =cut use strict; -use PPI::Token::Quote (); +use PPI::Token::Quote (); use PPI::Token::_QuoteEngine::Simple (); our $VERSION = '1.282'; our @ISA = qw{ - PPI::Token::_QuoteEngine::Simple - PPI::Token::Quote + PPI::Token::_QuoteEngine::Simple + PPI::Token::Quote }; - - - - ##################################################################### # PPI::Token::Quote Methods @@ -55,7 +51,6 @@ sub string { substr( $str, 1, length($str) - 2 ); } - my %UNESCAPE = ( "\\'" => "'", "\\\\" => "\\", diff --git a/lib/PPI/Token/QuoteLike/Backtick.pm b/lib/PPI/Token/QuoteLike/Backtick.pm index 8fb115d7..ec4c3fb2 100644 --- a/lib/PPI/Token/QuoteLike/Backtick.pm +++ b/lib/PPI/Token/QuoteLike/Backtick.pm @@ -33,8 +33,8 @@ use PPI::Token::_QuoteEngine::Simple (); our $VERSION = '1.282'; our @ISA = qw{ - PPI::Token::_QuoteEngine::Simple - PPI::Token::QuoteLike + PPI::Token::_QuoteEngine::Simple + PPI::Token::QuoteLike }; 1; diff --git a/lib/PPI/Token/QuoteLike/Command.pm b/lib/PPI/Token/QuoteLike/Command.pm index d83853e4..a18e4c1a 100644 --- a/lib/PPI/Token/QuoteLike/Command.pm +++ b/lib/PPI/Token/QuoteLike/Command.pm @@ -33,8 +33,8 @@ use PPI::Token::_QuoteEngine::Full (); our $VERSION = '1.282'; our @ISA = qw{ - PPI::Token::_QuoteEngine::Full - PPI::Token::QuoteLike + PPI::Token::_QuoteEngine::Full + PPI::Token::QuoteLike }; 1; diff --git a/lib/PPI/Token/QuoteLike/Readline.pm b/lib/PPI/Token/QuoteLike/Readline.pm index 5ba887e6..9017aa26 100644 --- a/lib/PPI/Token/QuoteLike/Readline.pm +++ b/lib/PPI/Token/QuoteLike/Readline.pm @@ -42,8 +42,8 @@ use PPI::Token::_QuoteEngine::Full (); our $VERSION = '1.282'; our @ISA = qw{ - PPI::Token::_QuoteEngine::Full - PPI::Token::QuoteLike + PPI::Token::_QuoteEngine::Full + PPI::Token::QuoteLike }; 1; diff --git a/lib/PPI/Token/QuoteLike/Regexp.pm b/lib/PPI/Token/QuoteLike/Regexp.pm index 452e81e3..1ef99ab1 100644 --- a/lib/PPI/Token/QuoteLike/Regexp.pm +++ b/lib/PPI/Token/QuoteLike/Regexp.pm @@ -36,14 +36,10 @@ use PPI::Token::_QuoteEngine::Full (); our $VERSION = '1.282'; our @ISA = qw{ - PPI::Token::_QuoteEngine::Full - PPI::Token::QuoteLike + PPI::Token::_QuoteEngine::Full + PPI::Token::QuoteLike }; - - - - ##################################################################### # PPI::Token::QuoteLike::Regexp Methods @@ -57,7 +53,7 @@ will be compiled into the match portion of the regexp. =cut sub get_match_string { - return $_[0]->_section_content( 0 ); + return $_[0]->_section_content(0); } =pod diff --git a/lib/PPI/Token/QuoteLike/Words.pm b/lib/PPI/Token/QuoteLike/Words.pm index ee44bb2a..d0ec00f8 100644 --- a/lib/PPI/Token/QuoteLike/Words.pm +++ b/lib/PPI/Token/QuoteLike/Words.pm @@ -32,8 +32,8 @@ use PPI::Token::_QuoteEngine::Full (); our $VERSION = '1.282'; our @ISA = qw{ - PPI::Token::_QuoteEngine::Full - PPI::Token::QuoteLike + PPI::Token::_QuoteEngine::Full + PPI::Token::QuoteLike }; =pod @@ -47,7 +47,7 @@ the last element if the token is in scalar context. =cut sub literal { - my ( $self ) = @_; + my ($self) = @_; my $content = $self->_section_content(0); return if !defined $content; diff --git a/lib/PPI/Token/Regexp.pm b/lib/PPI/Token/Regexp.pm index 1d146285..97e75f51 100644 --- a/lib/PPI/Token/Regexp.pm +++ b/lib/PPI/Token/Regexp.pm @@ -49,10 +49,6 @@ our $VERSION = '1.282'; our @ISA = "PPI::Token"; - - - - ##################################################################### # PPI::Token::Regexp Methods @@ -66,7 +62,7 @@ performs the match. =cut sub get_match_string { - return $_[0]->_section_content( 0 ); + return $_[0]->_section_content(0); } =pod @@ -80,7 +76,7 @@ substitute, C is returned. =cut sub get_substitute_string { - return $_[0]->_section_content( 1 ); + return $_[0]->_section_content(1); } =pod @@ -110,7 +106,6 @@ sub get_delimiters { return $_[0]->_delimiters(); } - 1; =pod diff --git a/lib/PPI/Token/Regexp/Match.pm b/lib/PPI/Token/Regexp/Match.pm index 4a5384b9..c978d4ad 100644 --- a/lib/PPI/Token/Regexp/Match.pm +++ b/lib/PPI/Token/Regexp/Match.pm @@ -47,8 +47,8 @@ use PPI::Token::_QuoteEngine::Full (); our $VERSION = '1.282'; our @ISA = qw{ - PPI::Token::_QuoteEngine::Full - PPI::Token::Regexp + PPI::Token::_QuoteEngine::Full + PPI::Token::Regexp }; 1; diff --git a/lib/PPI/Token/Regexp/Substitute.pm b/lib/PPI/Token/Regexp/Substitute.pm index a39fe06b..c450193e 100644 --- a/lib/PPI/Token/Regexp/Substitute.pm +++ b/lib/PPI/Token/Regexp/Substitute.pm @@ -37,8 +37,8 @@ use PPI::Token::_QuoteEngine::Full (); our $VERSION = '1.282'; our @ISA = qw{ - PPI::Token::_QuoteEngine::Full - PPI::Token::Regexp + PPI::Token::_QuoteEngine::Full + PPI::Token::Regexp }; 1; diff --git a/lib/PPI/Token/Regexp/Transliterate.pm b/lib/PPI/Token/Regexp/Transliterate.pm index c6c4b45b..cf96d66c 100644 --- a/lib/PPI/Token/Regexp/Transliterate.pm +++ b/lib/PPI/Token/Regexp/Transliterate.pm @@ -41,8 +41,8 @@ use PPI::Token::_QuoteEngine::Full (); our $VERSION = '1.282'; our @ISA = qw{ - PPI::Token::_QuoteEngine::Full - PPI::Token::Regexp + PPI::Token::_QuoteEngine::Full + PPI::Token::Regexp }; 1; diff --git a/lib/PPI/Token/Structure.pm b/lib/PPI/Token/Structure.pm index e10eb9de..883106b1 100644 --- a/lib/PPI/Token/Structure.pm +++ b/lib/PPI/Token/Structure.pm @@ -56,10 +56,6 @@ my %CLOSES = ( ord ')' => 1, ); - - - - ##################################################################### # Tokenizer Methods @@ -76,22 +72,14 @@ sub __TOKENIZER__commit { 0; } - - - - ##################################################################### # Lexer Methods # For a given brace, find its opposing pair sub __LEXER__opposite { - $MATCH{ord $_[0]->{content}}; + $MATCH{ ord $_[0]->{content} }; } - - - - ##################################################################### # PPI::Element Methods @@ -137,10 +125,11 @@ sub next_token { # structure, if it has children. if ( $OPENS{ ord $self->{content} } ) { my $child = $structure->child(0); - if ( $child ) { + if ($child) { # Decend deeper, or return if it is a token return $child->isa('PPI::Token') ? $child : $child->first_token; - } elsif ( $structure->finish ) { + } + elsif ( $structure->finish ) { # Empty structure, so next is closing brace return $structure->finish; } @@ -165,10 +154,11 @@ sub previous_token { # structure, if it has children. if ( $CLOSES{ ord $self->{content} } ) { my $child = $structure->child(-1); - if ( $child ) { + if ($child) { # Decend deeper, or return if it is a token return $child->isa('PPI::Token') ? $child : $child->last_token; - } elsif ( $structure->start ) { + } + elsif ( $structure->start ) { # Empty structure, so next is closing brace return $structure->start; } diff --git a/lib/PPI/Token/Symbol.pm b/lib/PPI/Token/Symbol.pm index 99e7ca88..f4f62c32 100644 --- a/lib/PPI/Token/Symbol.pm +++ b/lib/PPI/Token/Symbol.pm @@ -26,7 +26,7 @@ Most methods are provided to help work out what the object is actually pointing at, rather than what it might appear to be pointing at. =cut - + use strict; use Params::Util qw{_INSTANCE}; use PPI::Token (); @@ -35,10 +35,6 @@ our $VERSION = '1.282'; our @ISA = "PPI::Token"; - - - - ##################################################################### # PPI::Token::Symbol Methods @@ -91,8 +87,8 @@ sub symbol { return $symbol if $type eq '&'; # Unless the next significant Element is a structure, it's correct. - my $after = $self->snext_sibling; - return $symbol unless _INSTANCE($after, 'PPI::Structure'); + my $after = $self->snext_sibling; + return $symbol unless _INSTANCE( $after, 'PPI::Structure' ); # Process the rest for cases where it might actually be something else my $braces = $after->braces; @@ -101,18 +97,21 @@ sub symbol { # If it is cast to '$' or '@', that trumps any braces my $before = $self->sprevious_sibling; - return $symbol if $before && - $before->isa( 'PPI::Token::Cast' ) && - $cast_which_trumps_braces{ $before->content }; + return $symbol + if $before + && $before->isa('PPI::Token::Cast') + && $cast_which_trumps_braces{ $before->content }; # Otherwise the braces rule substr( $symbol, 0, 1, '@' ) if $braces eq '[]'; substr( $symbol, 0, 1, '%' ) if $braces eq '{}'; - } elsif ( $type eq '@' ) { + } + elsif ( $type eq '@' ) { substr( $symbol, 0, 1, '%' ) if $braces eq '{}'; - } elsif ( $type eq '%' ) { + } + elsif ( $type eq '%' ) { substr( $symbol, 0, 1, '@' ) if $braces eq '[]'; } @@ -150,10 +149,6 @@ sub symbol_type { substr( $_[0]->symbol, 0, 1 ); } - - - - ##################################################################### # Tokenizer Methods @@ -164,14 +159,14 @@ sub __TOKENIZER__on_char { pos $t->{line} = $t->{line_cursor}; if ( $t->{line} =~ m/\G([\w:\']+)/gc ) { $t->{token}->{content} .= $1; - $t->{line_cursor} += length $1; + $t->{line_cursor} += length $1; } # Handle magic things - my $content = $t->{token}->{content}; + my $content = $t->{token}->{content}; if ( $content eq '@_' or $content eq '$_' ) { - $t->{class} = $t->{token}->set_class( 'Magic' ); - return $t->_finalize_token->__TOKENIZER__on_char( $t ); + $t->{class} = $t->{token}->set_class('Magic'); + return $t->_finalize_token->__TOKENIZER__on_char($t); } # Shortcut for most of the X:: symbols @@ -181,19 +176,19 @@ sub __TOKENIZER__on_char { if ( $nextchar eq '|' ) { $t->{token}->{content} .= $nextchar; $t->{line_cursor}++; - $t->{class} = $t->{token}->set_class( 'Magic' ); + $t->{class} = $t->{token}->set_class('Magic'); } - return $t->_finalize_token->__TOKENIZER__on_char( $t ); + return $t->_finalize_token->__TOKENIZER__on_char($t); } if ( $content =~ /^[\$%*@&]::(?:[^\w]|$)/ ) { my $current = substr( $content, 0, 3, '' ); $t->{token}->{content} = $current; - $t->{line_cursor} -= length( $content ); - return $t->_finalize_token->__TOKENIZER__on_char( $t ); + $t->{line_cursor} -= length($content); + return $t->_finalize_token->__TOKENIZER__on_char($t); } if ( $content =~ /^(?:\$|\@)\d+/ ) { - $t->{class} = $t->{token}->set_class( 'Magic' ); - return $t->_finalize_token->__TOKENIZER__on_char( $t ); + $t->{class} = $t->{token}->set_class('Magic'); + return $t->_finalize_token->__TOKENIZER__on_char($t); } # Verify and extract actual full symbol name from sigil to end @@ -219,7 +214,7 @@ sub __TOKENIZER__on_char { $t->{token}->{content} = $1; } - $t->_finalize_token->__TOKENIZER__on_char( $t ); + $t->_finalize_token->__TOKENIZER__on_char($t); } 1; diff --git a/lib/PPI/Token/Unknown.pm b/lib/PPI/Token/Unknown.pm index 86db997b..480018cb 100644 --- a/lib/PPI/Token/Unknown.pm +++ b/lib/PPI/Token/Unknown.pm @@ -28,41 +28,36 @@ object as a bug. =cut use strict; -use PPI::Token (); -use PPI::Exception (); +use PPI::Token (); +use PPI::Exception (); use PPI::Singletons qw' %MAGIC $CURLY_SYMBOL '; our $VERSION = '1.282'; our @ISA = "PPI::Token"; - - - - - ##################################################################### # Tokenizer Methods sub __TOKENIZER__on_char { - my ( $self, $t ) = @_; # Self and Tokenizer - my $c = $t->{token}->{content}; # Current token - my $char = substr( $t->{line}, $t->{line_cursor}, 1 ); # Current character + my ( $self, $t ) = @_; # Self and Tokenizer + my $c = $t->{token}->{content}; # Current token + my $char = substr( $t->{line}, $t->{line_cursor}, 1 ); # Current character # Now, we split on the different values of the current content if ( $c eq '*' ) { # Is it a number? if ( $char =~ /\d/ ) { # bitwise operator - $t->{class} = $t->{token}->set_class( 'Operator' ); - return $t->_finalize_token->__TOKENIZER__on_char( $t ); + $t->{class} = $t->{token}->set_class('Operator'); + return $t->_finalize_token->__TOKENIZER__on_char($t); } if ( $char =~ /[\w:]/ ) { # Symbol (unless the thing before it is a number - my ( $prev ) = $t->_previous_significant_tokens(1); + my ($prev) = $t->_previous_significant_tokens(1); if ( not $prev or not $prev->isa('PPI::Token::Number') ) { - $t->{class} = $t->{token}->set_class( 'Symbol' ); + $t->{class} = $t->{token}->set_class('Symbol'); return 1; } } @@ -72,46 +67,51 @@ sub __TOKENIZER__on_char { pos $t->{line} = $t->{line_cursor} + 1; if ( $t->{line} =~ m/$CURLY_SYMBOL/gc ) { # control-character symbol (e.g. *{^_Foo}) - $t->{class} = $t->{token}->set_class( 'Magic' ); + $t->{class} = $t->{token}->set_class('Magic'); return 1; } } # Postfix dereference: ->** if ( $char eq '*' ) { - my ( $prev ) = $t->_previous_significant_tokens(1); - if ( $prev and $prev->isa('PPI::Token::Operator') and $prev->content eq '->' ) { - $t->{class} = $t->{token}->set_class( 'Cast' ); + my ($prev) = $t->_previous_significant_tokens(1); + if ( $prev + and $prev->isa('PPI::Token::Operator') + and $prev->content eq '->' ) + { + $t->{class} = $t->{token}->set_class('Cast'); return 1; } } if ( $char eq '*' || $char eq '=' ) { # Power operator '**' or mult-assign '*=' - $t->{class} = $t->{token}->set_class( 'Operator' ); + $t->{class} = $t->{token}->set_class('Operator'); return 1; } return $self->_as_cast_or_op($t) if $self->_is_cast_or_op($char); - $t->{class} = $t->{token}->set_class( 'Operator' ); - return $t->_finalize_token->__TOKENIZER__on_char( $t ); - + $t->{class} = $t->{token}->set_class('Operator'); + return $t->_finalize_token->__TOKENIZER__on_char($t); - - } elsif ( $c eq '$' ) { + } + elsif ( $c eq '$' ) { # Postfix dereference: ->$* ->$#* if ( $char eq '*' || $char eq '#' ) { - my ( $prev ) = $t->_previous_significant_tokens(1); - if ( $prev and $prev->isa('PPI::Token::Operator') and $prev->content eq '->' ) { - $t->{class} = $t->{token}->set_class( 'Cast' ); + my ($prev) = $t->_previous_significant_tokens(1); + if ( $prev + and $prev->isa('PPI::Token::Operator') + and $prev->content eq '->' ) + { + $t->{class} = $t->{token}->set_class('Cast'); return 1; } } if ( $char =~ /[a-z_]/i ) { # Symbol - $t->{class} = $t->{token}->set_class( 'Symbol' ); + $t->{class} = $t->{token}->set_class('Symbol'); return 1; } @@ -126,7 +126,7 @@ sub __TOKENIZER__on_char { if ( $MAGIC{ $c . $char } ) { # Magic variable - $t->{class} = $t->{token}->set_class( 'Magic' ); + $t->{class} = $t->{token}->set_class('Magic'); return 1; } @@ -135,30 +135,32 @@ sub __TOKENIZER__on_char { pos $t->{line} = $t->{line_cursor} + 1; if ( $t->{line} =~ m/$CURLY_SYMBOL/gc ) { # control-character symbol (e.g. ${^MATCH}) - $t->{class} = $t->{token}->set_class( 'Magic' ); + $t->{class} = $t->{token}->set_class('Magic'); return 1; } } # Must be a cast - $t->{class} = $t->{token}->set_class( 'Cast' ); - return $t->_finalize_token->__TOKENIZER__on_char( $t ); - + $t->{class} = $t->{token}->set_class('Cast'); + return $t->_finalize_token->__TOKENIZER__on_char($t); - - } elsif ( $c eq '@' ) { + } + elsif ( $c eq '@' ) { # Postfix dereference: ->@* if ( $char eq '*' ) { - my ( $prev ) = $t->_previous_significant_tokens(1); - if ( $prev and $prev->isa('PPI::Token::Operator') and $prev->content eq '->' ) { - $t->{class} = $t->{token}->set_class( 'Cast' ); + my ($prev) = $t->_previous_significant_tokens(1); + if ( $prev + and $prev->isa('PPI::Token::Operator') + and $prev->content eq '->' ) + { + $t->{class} = $t->{token}->set_class('Cast'); return 1; } } if ( $char =~ /[\w:]/ ) { # Symbol - $t->{class} = $t->{token}->set_class( 'Symbol' ); + $t->{class} = $t->{token}->set_class('Symbol'); return 1; } @@ -173,7 +175,7 @@ sub __TOKENIZER__on_char { if ( $MAGIC{ $c . $char } ) { # Magic variable - $t->{class} = $t->{token}->set_class( 'Magic' ); + $t->{class} = $t->{token}->set_class('Magic'); return 1; } @@ -182,29 +184,31 @@ sub __TOKENIZER__on_char { pos $t->{line} = $t->{line_cursor} + 1; if ( $t->{line} =~ m/$CURLY_SYMBOL/gc ) { # control-character symbol (e.g. @{^_Foo}) - $t->{class} = $t->{token}->set_class( 'Magic' ); + $t->{class} = $t->{token}->set_class('Magic'); return 1; } } # Must be a cast - $t->{class} = $t->{token}->set_class( 'Cast' ); - return $t->_finalize_token->__TOKENIZER__on_char( $t ); - + $t->{class} = $t->{token}->set_class('Cast'); + return $t->_finalize_token->__TOKENIZER__on_char($t); - - } elsif ( $c eq '%' ) { + } + elsif ( $c eq '%' ) { # Postfix dereference: ->%* ->%[...] if ( $char eq '*' || $char eq '[' ) { - my ( $prev ) = $t->_previous_significant_tokens(1); - if ( $prev and $prev->isa('PPI::Token::Operator') and $prev->content eq '->' ) { + my ($prev) = $t->_previous_significant_tokens(1); + if ( $prev + and $prev->isa('PPI::Token::Operator') + and $prev->content eq '->' ) + { if ( $char eq '*' ) { - $t->{class} = $t->{token}->set_class( 'Cast' ); + $t->{class} = $t->{token}->set_class('Cast'); return 1; } if ( $char eq '[' ) { - $t->{class} = $t->{token}->set_class( 'Cast' ); - return $t->_finalize_token->__TOKENIZER__on_char( $t ); + $t->{class} = $t->{token}->set_class('Cast'); + return $t->_finalize_token->__TOKENIZER__on_char($t); } } } @@ -212,8 +216,8 @@ sub __TOKENIZER__on_char { # Is it a number? if ( $char =~ /\d/ ) { # bitwise operator - $t->{class} = $t->{token}->set_class( 'Operator' ); - return $t->_finalize_token->__TOKENIZER__on_char( $t ); + $t->{class} = $t->{token}->set_class('Operator'); + return $t->_finalize_token->__TOKENIZER__on_char($t); } # Is it a nameless arg in a signature? @@ -227,15 +231,15 @@ sub __TOKENIZER__on_char { # Is it a magic variable? if ( $char eq '^' || $MAGIC{ $c . $char } ) { - $t->{class} = $t->{token}->set_class( 'Magic' ); + $t->{class} = $t->{token}->set_class('Magic'); return 1; } if ( $char =~ /[\w:]/ ) { # Symbol (unless the thing before it is a number - my ( $prev ) = $t->_previous_significant_tokens(1); + my ($prev) = $t->_previous_significant_tokens(1); if ( not $prev or not $prev->isa('PPI::Token::Number') ) { - $t->{class} = $t->{token}->set_class( 'Symbol' ); + $t->{class} = $t->{token}->set_class('Symbol'); return 1; } } @@ -245,7 +249,7 @@ sub __TOKENIZER__on_char { pos $t->{line} = $t->{line_cursor} + 1; if ( $t->{line} =~ m/$CURLY_SYMBOL/gc ) { # control-character symbol (e.g. %{^_Foo}) - $t->{class} = $t->{token}->set_class( 'Magic' ); + $t->{class} = $t->{token}->set_class('Magic'); return 1; } } @@ -253,17 +257,19 @@ sub __TOKENIZER__on_char { return $self->_as_cast_or_op($t) if $self->_is_cast_or_op($char); # Probably the mod operator - $t->{class} = $t->{token}->set_class( 'Operator' ); - return $t->{class}->__TOKENIZER__on_char( $t ); - + $t->{class} = $t->{token}->set_class('Operator'); + return $t->{class}->__TOKENIZER__on_char($t); - - } elsif ( $c eq '&' ) { + } + elsif ( $c eq '&' ) { # Postfix dereference: ->&* if ( $char eq '*' ) { - my ( $prev ) = $t->_previous_significant_tokens(1); - if ( $prev and $prev->isa('PPI::Token::Operator') and $prev->content eq '->' ) { - $t->{class} = $t->{token}->set_class( 'Cast' ); + my ($prev) = $t->_previous_significant_tokens(1); + if ( $prev + and $prev->isa('PPI::Token::Operator') + and $prev->content eq '->' ) + { + $t->{class} = $t->{token}->set_class('Cast'); return 1; } } @@ -271,15 +277,15 @@ sub __TOKENIZER__on_char { # Is it a number? if ( $char =~ /\d/ ) { # bitwise operator - $t->{class} = $t->{token}->set_class( 'Operator' ); - return $t->_finalize_token->__TOKENIZER__on_char( $t ); + $t->{class} = $t->{token}->set_class('Operator'); + return $t->_finalize_token->__TOKENIZER__on_char($t); } if ( $char =~ /[\w:]/ ) { # Symbol (unless the thing before it is a number - my ( $prev ) = $t->_previous_significant_tokens(1); + my ($prev) = $t->_previous_significant_tokens(1); if ( not $prev or not $prev->isa('PPI::Token::Number') ) { - $t->{class} = $t->{token}->set_class( 'Symbol' ); + $t->{class} = $t->{token}->set_class('Symbol'); return 1; } } @@ -287,55 +293,53 @@ sub __TOKENIZER__on_char { return $self->_as_cast_or_op($t) if $self->_is_cast_or_op($char); # Probably the binary and operator - $t->{class} = $t->{token}->set_class( 'Operator' ); - return $t->{class}->__TOKENIZER__on_char( $t ); + $t->{class} = $t->{token}->set_class('Operator'); + return $t->{class}->__TOKENIZER__on_char($t); - - - } elsif ( $c eq '-' ) { + } + elsif ( $c eq '-' ) { if ( $char =~ /\d/o ) { # Number - $t->{class} = $t->{token}->set_class( 'Number' ); + $t->{class} = $t->{token}->set_class('Number'); return 1; } if ( $char eq '.' ) { # Number::Float - $t->{class} = $t->{token}->set_class( 'Number::Float' ); + $t->{class} = $t->{token}->set_class('Number::Float'); return 1; } if ( $char =~ /[a-zA-Z]/ ) { - $t->{class} = $t->{token}->set_class( 'DashedWord' ); + $t->{class} = $t->{token}->set_class('DashedWord'); return 1; } # The numeric negative operator - $t->{class} = $t->{token}->set_class( 'Operator' ); - return $t->{class}->__TOKENIZER__on_char( $t ); + $t->{class} = $t->{token}->set_class('Operator'); + return $t->{class}->__TOKENIZER__on_char($t); - - - } elsif ( $c eq ':' ) { + } + elsif ( $c eq ':' ) { if ( $char eq ':' ) { # ::foo style bareword - $t->{class} = $t->{token}->set_class( 'Word' ); + $t->{class} = $t->{token}->set_class('Word'); return 1; } # Now, : acts very very differently in different contexts. # Mainly, we need to find out if this is a subroutine attribute. # We'll leave a hint in the token to indicate that, if it is. - if ( $self->__TOKENIZER__is_an_attribute( $t ) ) { + if ( $self->__TOKENIZER__is_an_attribute($t) ) { # This : is an attribute indicator - $t->{class} = $t->{token}->set_class( 'Operator' ); + $t->{class} = $t->{token}->set_class('Operator'); $t->{token}->{_attribute} = 1; - return $t->_finalize_token->__TOKENIZER__on_char( $t ); + return $t->_finalize_token->__TOKENIZER__on_char($t); } # It MIGHT be a label, but it's probably the ?: trinary operator - $t->{class} = $t->{token}->set_class( 'Operator' ); - return $t->{class}->__TOKENIZER__on_char( $t ); + $t->{class} = $t->{token}->set_class('Operator'); + return $t->{class}->__TOKENIZER__on_char($t); } # erm... @@ -354,9 +358,9 @@ sub _is_cast_or_op { sub _as_cast_or_op { my ( $self, $t ) = @_; - my $class = _cast_or_op( $t ); - $t->{class} = $t->{token}->set_class( $class ); - return $t->_finalize_token->__TOKENIZER__on_char( $t ); + my $class = _cast_or_op($t); + $t->{class} = $t->{token}->set_class($class); + return $t->_finalize_token->__TOKENIZER__on_char($t); } sub _prev_significant_w_cursor { @@ -372,16 +376,16 @@ sub _prev_significant_w_cursor { # Operator/operand-sensitive, multiple or GLOB cast sub _cast_or_op { - my ( $t ) = @_; + my ($t) = @_; my $tokens = $t->{tokens}; - my $cursor = scalar( @$tokens ) - 1; + my $cursor = scalar(@$tokens) - 1; my $token; ( $token, $cursor ) = _prev_significant_w_cursor( $tokens, $cursor ); return 'Cast' if !$token; # token was first in the document - if ( $token->isa( 'PPI::Token::Structure' ) and $token->content eq '}' ) { + if ( $token->isa('PPI::Token::Structure') and $token->content eq '}' ) { # Scan the token stream backwards an arbitrarily long way, # looking for the matching opening curly brace. @@ -389,8 +393,8 @@ sub _cast_or_op { ( $token, $cursor ) = _prev_significant_w_cursor( $tokens, $cursor, sub { - my ( $token ) = @_; - return if !$token->isa( 'PPI::Token::Structure' ); + my ($token) = @_; + return if !$token->isa('PPI::Token::Structure'); if ( $token eq '}' ) { $structure_depth++; return; @@ -402,30 +406,35 @@ sub _cast_or_op { return 1; } ); - return 'Operator' if !$token; # no matching '{', probably an unbalanced '}' + return 'Operator' + if !$token; # no matching '{', probably an unbalanced '}' # Scan past any whitespace ( $token, $cursor ) = _prev_significant_w_cursor( $tokens, $cursor ); - return 'Operator' if !$token; # Document began with what must be a hash constructor. - return 'Operator' if $token->isa( 'PPI::Token::Symbol' ); # subscript + return 'Operator' + if !$token; # Document began with what must be a hash constructor. + return 'Operator' if $token->isa('PPI::Token::Symbol'); # subscript my %meth_or_subscript_end = map { $_ => 1 } qw@ -> } ] @; - return 'Operator' if $meth_or_subscript_end{ $token->content }; # subscript + return 'Operator' + if $meth_or_subscript_end{ $token->content }; # subscript my $content = $token->content; my $produces_or_wants_value = - ( $token->isa( 'PPI::Token::Word' ) and ( $content eq 'do' or $content eq 'eval' ) ); + ( $token->isa('PPI::Token::Word') + and ( $content eq 'do' or $content eq 'eval' ) ); return $produces_or_wants_value ? 'Operator' : 'Cast'; } my %list_start_or_term_end = map { $_ => 1 } qw@ ; ( { [ @; return 'Cast' - if $token->isa( 'PPI::Token::Structure' ) and $list_start_or_term_end{ $token->content } - or $token->isa( 'PPI::Token::Cast' ) - or $token->isa( 'PPI::Token::Operator' ) - or $token->isa( 'PPI::Token::Label' ); + if $token->isa('PPI::Token::Structure') + and $list_start_or_term_end{ $token->content } + or $token->isa('PPI::Token::Cast') + or $token->isa('PPI::Token::Operator') + or $token->isa('PPI::Token::Label'); - return 'Operator' if !$token->isa( 'PPI::Token::Word' ); + return 'Operator' if !$token->isa('PPI::Token::Word'); ( $token, $cursor ) = _prev_significant_w_cursor( $tokens, $cursor ); return 'Cast' if !$token || $token->content ne '->'; @@ -435,7 +444,7 @@ sub _cast_or_op { # Are we at a location where a ':' would indicate a subroutine attribute sub __TOKENIZER__is_an_attribute { - my $t = $_[1]; # Tokenizer object + my $t = $_[1]; # Tokenizer object my @tokens = $t->_previous_significant_tokens(3); my $p0 = $tokens[0]; return '' if not $p0; @@ -458,27 +467,22 @@ sub __TOKENIZER__is_an_attribute { my $p1 = $tokens[1]; my $p2 = $tokens[2]; if ( - $p1 - and - $p1->isa('PPI::Token::Word') - and - $p1->content eq 'sub' + $p1 + and $p1->isa('PPI::Token::Word') + and $p1->content eq 'sub' and ( - not $p2 - or - $p2->isa('PPI::Token::Structure') - or ( - $p2->isa('PPI::Token::Whitespace') - and - $p2->content eq '' - ) + not $p2 + or $p2->isa('PPI::Token::Structure') + or ( $p2->isa('PPI::Token::Whitespace') + and $p2->content eq '' ) ) - ) { + ) + { return 1; } # We aren't an attribute - ''; + ''; } 1; diff --git a/lib/PPI/Token/Whitespace.pm b/lib/PPI/Token/Whitespace.pm index 841da67c..bf334441 100644 --- a/lib/PPI/Token/Whitespace.pm +++ b/lib/PPI/Token/Whitespace.pm @@ -100,16 +100,14 @@ sub tidy { 1; } - - - - ##################################################################### # Parsing Methods # Build the class and commit maps my %COMMITMAP = ( - map( { ord $_ => 'PPI::Token::Word' } 'a' .. 'u', 'A' .. 'Z', qw" w y z _ " ), # no v or x + map( { ord $_ => 'PPI::Token::Word' } 'a' .. 'u', + 'A' .. 'Z', + qw" w y z _ " ), # no v or x map( { ord $_ => 'PPI::Token::Structure' } qw" ; [ ] { } ) " ), ord '#' => 'PPI::Token::Comment', ord 'v' => 'PPI::Token::Number::Version', @@ -152,30 +150,34 @@ sub __TOKENIZER__on_line_start { $t->_new_token( 'Whitespace', $line ); return 0; - } elsif ( $line =~ /^\s*#/ ) { + } + elsif ( $line =~ /^\s*#/ ) { # A comment line $t->_new_token( 'Comment', $line ); $t->_finalize_token; return 0; - } elsif ( $line =~ /^=(\w+)/ ) { + } + elsif ( $line =~ /^=(\w+)/ ) { # A Pod tag... change to pod mode $t->_new_token( 'Pod', $line ); if ( $1 eq 'cut' ) { # This is an error, but one we'll ignore # Don't go into Pod mode, since =cut normally # signals the end of Pod mode - } else { + } + else { $t->{class} = 'PPI::Token::Pod'; } return 0; - } elsif ( $line =~ /^use v6\-alpha\;/ ) { + } + elsif ( $line =~ /^use v6\-alpha\;/ ) { # Indicates a Perl 6 block. Make the initial # implementation just suck in the entire rest of the # file. my @perl6; - while ( 1 ) { + while (1) { my $line6 = $t->_get_line; last unless defined $line6; push @perl6, $line6; @@ -193,7 +195,7 @@ sub __TOKENIZER__on_line_start { sub __TOKENIZER__on_char { my $t = $_[1]; - my $c = substr $t->{line}, $t->{line_cursor}, 1; + my $c = substr $t->{line}, $t->{line_cursor}, 1; my $char = ord $c; # Do we definitely know what something is? @@ -202,8 +204,8 @@ sub __TOKENIZER__on_char { # Handle the simple option first return $CLASSMAP{$char} if $CLASSMAP{$char}; - if ( $char == 40 ) { # $char eq '(' - # Finalise any whitespace token... + if ( $char == 40 ) { # $char eq '(' + # Finalise any whitespace token... $t->_finalize_token if $t->{token}; # Is this the beginning of a sub prototype? @@ -219,50 +221,49 @@ sub __TOKENIZER__on_char { my $p1 = $tokens[1]; my $p2 = $tokens[2]; if ( - $tokens[0] - and - $tokens[0]->isa('PPI::Token::Word') - and - $p1 - and - $p1->isa('PPI::Token::Word') - and - $p1->content eq 'sub' + $tokens[0] + and $tokens[0]->isa('PPI::Token::Word') + and $p1 + and $p1->isa('PPI::Token::Word') + and $p1->content eq 'sub' and ( - not $p2 - or - $p2->isa('PPI::Token::Structure') - or ( - $p2->isa('PPI::Token::Whitespace') - and - $p2->content eq '' - ) + not $p2 + or $p2->isa('PPI::Token::Structure') + or ( $p2->isa('PPI::Token::Whitespace') + and $p2->content eq '' ) or ( # Lexical subroutine $p2->isa('PPI::Token::Word') - and - $p2->content =~ /^(?:my|our|state)$/ + and $p2->content =~ /^(?:my|our|state)$/ ) ) - ) { + ) + { # This is a sub prototype return 'Prototype'; } # A prototyped anonymous subroutine my $p0 = $tokens[0]; - if ( $p0 and $p0->isa('PPI::Token::Word') and $p0->content eq 'sub' + if ( + $p0 + and $p0->isa('PPI::Token::Word') + and $p0->content eq 'sub' # Maybe it's invoking a method named 'sub' - and not ( $p1 and $p1->isa('PPI::Token::Operator') and $p1->content eq '->') - ) { + and not($p1 + and $p1->isa('PPI::Token::Operator') + and $p1->content eq '->' ) + ) + { return 'Prototype'; } # This is a normal open bracket return 'Structure'; - } elsif ( $char == 60 ) { # $char eq '<' - # Finalise any whitespace token... + } + elsif ( $char == 60 ) { # $char eq '<' + # Finalise any whitespace token... $t->_finalize_token if $t->{token}; # This is either "less than" or "readline quote-like" @@ -289,11 +290,11 @@ sub __TOKENIZER__on_char { # while <>; my $prec = $prev->content; return 'QuoteLike::Readline' - if ( $prev->isa('PPI::Token::Structure') and $prec eq '(' ) - or ( $prev->isa('PPI::Token::Structure') and $prec eq ';' ) - or ( $prev->isa('PPI::Token::Word') and $prec eq 'while' ) - or ( $prev->isa('PPI::Token::Operator') and $prec eq '=' ) - or ( $prev->isa('PPI::Token::Operator') and $prec eq ',' ); + if ( $prev->isa('PPI::Token::Structure') and $prec eq '(' ) + or ( $prev->isa('PPI::Token::Structure') and $prec eq ';' ) + or ( $prev->isa('PPI::Token::Word') and $prec eq 'while' ) + or ( $prev->isa('PPI::Token::Operator') and $prec eq '=' ) + or ( $prev->isa('PPI::Token::Operator') and $prec eq ',' ); if ( $prev->isa('PPI::Token::Structure') and $prec eq '}' ) { # Could go either way... do a regex check @@ -310,8 +311,9 @@ sub __TOKENIZER__on_char { # until this more comprehensive section was created. return 'Operator'; - } elsif ( $char == 47 ) { # $char eq '/' - # Finalise any whitespace token... + } + elsif ( $char == 47 ) { # $char eq '/' + # Finalise any whitespace token... $t->_finalize_token if $t->{token}; # This is either a "divided by" or a "start regex" @@ -326,7 +328,7 @@ sub __TOKENIZER__on_char { # Most times following an operator, we are a regex. # This includes cases such as: - # , - As an argument in a list + # , - As an argument in a list # .. - The second condition in a flip flop # =~ - A bound regex # !~ - Ditto @@ -344,23 +346,18 @@ sub __TOKENIZER__on_char { # After going into scope/brackets if ( $prev->isa('PPI::Token::Structure') - and ( - $prec eq '(' - or - $prec eq '{' - or - $prec eq ';' - ) - ) { + and ( $prec eq '(' + or $prec eq '{' + or $prec eq ';' ) + ) + { return 'Regexp::Match'; } # Functions and keywords - if ( - $MATCHWORD{$prec} - and - $prev->isa('PPI::Token::Word') - ) { + if ( $MATCHWORD{$prec} + and $prev->isa('PPI::Token::Word') ) + { return 'Regexp::Match'; } @@ -377,14 +374,15 @@ sub __TOKENIZER__on_char { # Add more tests here as potential cases come to light return 'Operator'; - } elsif ( $char == 120 ) { # $char eq 'x' - # Could be a word, the x= operator, the x operator - # followed by whitespace, or the x operator without any - # space between itself and its operand, e.g.: '$a x3', - # which is the same as '$a x 3'. _current_x_is_operator - # assumes we have a complete 'x' token, but we don't - # yet. We may need to split this x character apart from - # what follows it. + } + elsif ( $char == 120 ) { # $char eq 'x' + # Could be a word, the x= operator, the x operator + # followed by whitespace, or the x operator without any + # space between itself and its operand, e.g.: '$a x3', + # which is the same as '$a x 3'. _current_x_is_operator + # assumes we have a complete 'x' token, but we don't + # yet. We may need to split this x character apart from + # what follows it. if ( $t->_current_x_is_operator ) { pos $t->{line} = $t->{line_cursor} + 1; return 'Operator' if $t->{line} =~ m/\G(?: @@ -404,22 +402,24 @@ sub __TOKENIZER__on_char { # operator followed by whitespace. return PPI::Token::Word->__TOKENIZER__commit($t); - } elsif ( $char == 45 ) { # $char eq '-' - # Look for an obvious operator operand context + } + elsif ( $char == 45 ) { # $char eq '-' + # Look for an obvious operator operand context my $context = $t->_opcontext; if ( $context eq 'operator' ) { return 'Operator'; - } else { + } + else { # More logic needed return 'Unknown'; } - } elsif ( $char >= 128 ) { # Outside ASCII + } + elsif ( $char >= 128 ) { # Outside ASCII return 'PPI::Token::Word'->__TOKENIZER__commit($t) if $c =~ /\w/; - return 'Whitespace' if $c =~ /\s/; + return 'Whitespace' if $c =~ /\s/; } - # All the whitespaces are covered, so what to do ### For now, die PPI::Exception->throw("Encountered unexpected character '$char'"); diff --git a/lib/PPI/Token/Word.pm b/lib/PPI/Token/Word.pm index 7f773e52..93675f76 100644 --- a/lib/PPI/Token/Word.pm +++ b/lib/PPI/Token/Word.pm @@ -36,7 +36,7 @@ now, look at L. =cut use strict; -use PPI::Token (); +use PPI::Token (); use PPI::Singletons qw' %OPERATOR %QUOTELIKE %KEYWORDS '; our $VERSION = '1.282'; @@ -77,13 +77,10 @@ sub method_call { my $self = shift; my $previous = $self->sprevious_sibling; - if ( - $previous - and - $previous->isa('PPI::Token::Operator') - and - $previous->content eq '->' - ) { + if ( $previous + and $previous->isa('PPI::Token::Operator') + and $previous->content eq '->' ) + { return 1; } @@ -91,32 +88,24 @@ sub method_call { return 0 unless $snext; if ( - $snext->isa('PPI::Structure::List') - or - $snext->isa('PPI::Token::Structure') - or - $snext->isa('PPI::Token::Operator') - and ( - $snext->content eq ',' - or - $snext->content eq '=>' - ) - ) { + $snext->isa('PPI::Structure::List') + or $snext->isa('PPI::Token::Structure') + or $snext->isa('PPI::Token::Operator') and ( $snext->content eq ',' + or $snext->content eq '=>' ) + ) + { return 0; } - if ( - $snext->isa('PPI::Token::Word') - and - $snext->content =~ m< \w :: \z >xms - ) { + if ( $snext->isa('PPI::Token::Word') + and $snext->content =~ m< \w :: \z >xms ) + { return 1; } return; } - sub __TOKENIZER__on_char { my $class = shift; my $t = shift; @@ -129,7 +118,7 @@ sub __TOKENIZER__on_char { # the word "eq'foo", then just make 'eq' (or whatever # else is in the %KEYWORDS hash. if ( $word =~ /^(\w+)'/ && $KEYWORDS{$1} ) { - $word = $1; + $word = $1; } $t->{token}->{content} .= $word; $t->{line_cursor} += length $word; @@ -138,35 +127,35 @@ sub __TOKENIZER__on_char { # We might be a subroutine attribute. if ( __current_token_is_attribute($t) ) { - $t->{class} = $t->{token}->set_class( 'Attribute' ); - return $t->{class}->__TOKENIZER__commit( $t ); + $t->{class} = $t->{token}->set_class('Attribute'); + return $t->{class}->__TOKENIZER__commit($t); } my $word = $t->{token}->{content}; if ( $KEYWORDS{$word} ) { # Check for a Perl keyword that is forced to be a normal word instead if ( $t->__current_token_is_forced_word ) { - $t->{class} = $t->{token}->set_class( 'Word' ); - return $t->{class}->__TOKENIZER__on_char( $t ); + $t->{class} = $t->{token}->set_class('Word'); + return $t->{class}->__TOKENIZER__on_char($t); } - # Check for a quote like operator. %QUOTELIKE must be subset of %KEYWORDS + # Check for a quote like operator. %QUOTELIKE must be subset of %KEYWORDS if ( $QUOTELIKE{$word} ) { $t->{class} = $t->{token}->set_class( $QUOTELIKE{$word} ); - return $t->{class}->__TOKENIZER__on_char( $t ); + return $t->{class}->__TOKENIZER__on_char($t); } # Or one of the word operators. %OPERATOR must be subset of %KEYWORDS if ( $OPERATOR{$word} ) { - $t->{class} = $t->{token}->set_class( 'Operator' ); - return $t->_finalize_token->__TOKENIZER__on_char( $t ); + $t->{class} = $t->{token}->set_class('Operator'); + return $t->_finalize_token->__TOKENIZER__on_char($t); } } # Unless this is a simple identifier, at this point # it has to be a normal bareword if ( $word =~ /\:/ ) { - return $t->_finalize_token->__TOKENIZER__on_char( $t ); + return $t->_finalize_token->__TOKENIZER__on_char($t); } # If the NEXT character in the line is a colon, this @@ -175,38 +164,39 @@ sub __TOKENIZER__on_char { if ( $char eq ':' ) { $t->{token}->{content} .= ':'; $t->{line_cursor}++; - $t->{class} = $t->{token}->set_class( 'Label' ); + $t->{class} = $t->{token}->set_class('Label'); - # If not a label, '_' on its own is the magic filehandle - } elsif ( $word eq '_' ) { - $t->{class} = $t->{token}->set_class( 'Magic' ); + # If not a label, '_' on its own is the magic filehandle + } + elsif ( $word eq '_' ) { + $t->{class} = $t->{token}->set_class('Magic'); } # Finalise and process the character again - $t->_finalize_token->__TOKENIZER__on_char( $t ); + $t->_finalize_token->__TOKENIZER__on_char($t); } - - # We are committed to being a bareword. # Or so we would like to believe. sub __TOKENIZER__commit { - my ($class, $t) = @_; + my ( $class, $t ) = @_; # Our current position is the first character of the bareword. # Capture the bareword. pos $t->{line} = $t->{line_cursor}; unless ( $t->{line} =~ m/\G((?!\d)\w+(?:(?:\'|::)\w+)*(?:::)?)/gc ) { # Programmer error - die sprintf "Fatal error... regex failed to match in '%s' when expected", substr $t->{line}, $t->{line_cursor}; + die sprintf + "Fatal error... regex failed to match in '%s' when expected", + substr $t->{line}, $t->{line_cursor}; } # Special Case: If we accidentally treat eq'foo' like the word "eq'foo", # then unwind it and just make it 'eq' (or the other stringy comparitors) my $word = $1; if ( $word =~ /^(\w+)'/ && $KEYWORDS{$1} ) { - $word = $1; + $word = $1; } # Advance the position one after the end of the bareword @@ -215,8 +205,9 @@ sub __TOKENIZER__commit { # We might be a subroutine attribute. if ( __current_token_is_attribute($t) ) { $t->_new_token( 'Attribute', $word ); - return ($t->{line_cursor} >= $t->{line_length}) ? 0 - : $t->{class}->__TOKENIZER__on_char($t); + return ( $t->{line_cursor} >= $t->{line_length} ) + ? 0 + : $t->{class}->__TOKENIZER__on_char($t); } # Check for the end of the file @@ -235,9 +226,10 @@ sub __TOKENIZER__commit { $t->{line_cursor} = length $t->{line}; if ( $end_rest =~ /\n$/ ) { chomp $end_rest; - $t->_new_token( 'Comment', $end_rest ) if length $end_rest; + $t->_new_token( 'Comment', $end_rest ) if length $end_rest; $t->_new_token( 'Whitespace', "\n" ); - } else { + } + else { $t->_new_token( 'Comment', $end_rest ) if length $end_rest; } $t->_finalize_token; @@ -259,9 +251,10 @@ sub __TOKENIZER__commit { $t->{line_cursor} = length $t->{line}; if ( $data_rest =~ /\n$/ ) { chomp $data_rest; - $t->_new_token( 'Comment', $data_rest ) if length $data_rest; + $t->_new_token( 'Comment', $data_rest ) if length $data_rest; $t->_new_token( 'Whitespace', "\n" ); - } else { + } + else { $t->_new_token( 'Comment', $data_rest ) if length $data_rest; } $t->_finalize_token; @@ -274,21 +267,27 @@ sub __TOKENIZER__commit { # Since it's not a simple identifier... $token_class = 'Word'; - } elsif ( $KEYWORDS{$word} and $t->__current_token_is_forced_word ) { + } + elsif ( $KEYWORDS{$word} and $t->__current_token_is_forced_word ) { $token_class = 'Word'; - } elsif ( $QUOTELIKE{$word} ) { + } + elsif ( $QUOTELIKE{$word} ) { # Special Case: A Quote-like operator $t->_new_token( $QUOTELIKE{$word}, $word ); - return ($t->{line_cursor} >= $t->{line_length}) ? 0 - : $t->{class}->__TOKENIZER__on_char( $t ); + return ( $t->{line_cursor} >= $t->{line_length} ) + ? 0 + : $t->{class}->__TOKENIZER__on_char($t); - } elsif ( $OPERATOR{$word} && ($word ne 'x' || $t->_current_x_is_operator) ) { + } + elsif ( $OPERATOR{$word} && ( $word ne 'x' || $t->_current_x_is_operator ) ) + { # Word operator $token_class = 'Operator'; - } else { - # Get tokens early to be sure to not disturb state set up by pos and m//gc. + } + else { + # Get tokens early to be sure to not disturb state set up by pos and m//gc. my @tokens = $t->_previous_significant_tokens(1); # If the next character is a ':' then it's a label... @@ -305,16 +304,22 @@ sub __TOKENIZER__commit { # attribute operator doesn't directly # touch the object name already works. $token_class = 'Word'; - } elsif ( !($tokens[0] and $tokens[0]->isa('PPI::Token::Operator')) ) { + } + elsif ( + !( $tokens[0] and $tokens[0]->isa('PPI::Token::Operator') ) ) + { $word .= $1; $t->{line_cursor} += length($1); $token_class = 'Label'; - } else { + } + else { $token_class = 'Word'; } - } elsif ( $word eq '_' ) { + } + elsif ( $word eq '_' ) { $token_class = 'Magic'; - } else { + } + else { $token_class = 'Word'; } } @@ -329,20 +334,18 @@ sub __TOKENIZER__commit { $t->_finalize_token->__TOKENIZER__on_char($t); } - - # Is the current Word really a subroutine attribute? sub __current_token_is_attribute { - my ( $t ) = @_; + my ($t) = @_; my @tokens = $t->_previous_significant_tokens(1); return ( $tokens[0] - and ( + and ( # hint from tokenizer $tokens[0]->{_attribute} # nothing between attribute and us except whitespace or $tokens[0]->isa('PPI::Token::Attribute') - ) + ) ); } diff --git a/lib/PPI/Token/_QuoteEngine.pm b/lib/PPI/Token/_QuoteEngine.pm index 8d48f40e..a7084c88 100644 --- a/lib/PPI/Token/_QuoteEngine.pm +++ b/lib/PPI/Token/_QuoteEngine.pm @@ -35,17 +35,13 @@ use Carp (); our $VERSION = '1.282'; - - - - # Hook for the __TOKENIZER__on_char token call sub __TOKENIZER__on_char { my $class = shift; my $t = $_[0]->{token} ? shift : return undef; # Call the fill method to process the quote - my $rv = $t->{token}->_fill( $t ); + my $rv = $t->{token}->_fill($t); return undef unless defined $rv; ## Doesn't support "end of file" indicator @@ -57,10 +53,6 @@ sub __TOKENIZER__on_char { 0; } - - - - ##################################################################### # Optimised character processors, used for quotes # and quote like stuff, and accessible to the child classes @@ -74,7 +66,7 @@ sub __TOKENIZER__on_char { sub _scan_for_unescaped_character { my $class = shift; my $t = shift; - my $char = (length $_[0] == 1) ? quotemeta shift : return undef; + my $char = ( length $_[0] == 1 ) ? quotemeta shift : return undef; # Create the search regex. # Same as above but with a negative look-behind assertion. @@ -95,13 +87,15 @@ sub _scan_for_unescaped_character { # Load in the next line $string .= substr $t->{line}, $t->{line_cursor}; my $rv = $t->_fill_line('inscan'); - if ( $rv ) { + if ($rv) { # Push to first character $t->{line_cursor} = 0; - } elsif ( defined $rv ) { + } + elsif ( defined $rv ) { # We hit the End of File return \$string; - } else { + } + else { # Unexpected error return undef; } @@ -115,20 +109,21 @@ sub _scan_for_unescaped_character { # and open close bracket pairs in the string. When complete, the # method leaves the line cursor on the LAST character found. sub _scan_for_brace_character { - my $class = shift; - my $t = shift; - my $close_brace = $_[0] =~ /^(?:\>|\)|\}|\])$/ ? shift : Carp::confess(''); # return undef; - my $open_brace = $close_brace; + my $class = shift; + my $t = shift; + my $close_brace = + $_[0] =~ /^(?:\>|\)|\}|\])$/ ? shift : Carp::confess(''); # return undef; + my $open_brace = $close_brace; $open_brace =~ tr/\>\)\}\]/\<\(\{\[/; # Create the search string $close_brace = quotemeta $close_brace; - $open_brace = quotemeta $open_brace; + $open_brace = quotemeta $open_brace; my $search = qr/\G(.*?(?{line} ) { # Get the search area pos $t->{line} = $t->{line_cursor}; @@ -138,7 +133,7 @@ sub _scan_for_brace_character { # Load in the next line $string .= substr( $t->{line}, $t->{line_cursor} ); my $rv = $t->_fill_line('inscan'); - if ( $rv ) { + if ($rv) { # Push to first character $t->{line_cursor} = 0; next; @@ -157,7 +152,7 @@ sub _scan_for_brace_character { $t->{line_cursor} += length $1; # Alter the depth and continue if we aren't at the end - $depth += ($1 =~ /$open_brace$/) ? 1 : -1 and next; + $depth += ( $1 =~ /$open_brace$/ ) ? 1 : -1 and next; # Rewind the cursor by one character ( cludgy hack ) $t->{line_cursor} -= 1; @@ -200,13 +195,15 @@ sub _scan_quote_like_operator_gap { # If we reach the EOF, $t->{line} gets deleted, # which is caught by the while. my $rv = $t->_fill_line('inscan'); - if ( $rv ) { + if ($rv) { # Set the cursor to the first character $t->{line_cursor} = 0; - } elsif ( defined $rv ) { + } + elsif ( defined $rv ) { # Returning the string as a reference indicates EOF return \$string; - } else { + } + else { return undef; } } diff --git a/lib/PPI/Token/_QuoteEngine/Full.pm b/lib/PPI/Token/_QuoteEngine/Full.pm index e281e747..cabc8787 100644 --- a/lib/PPI/Token/_QuoteEngine/Full.pm +++ b/lib/PPI/Token/_QuoteEngine/Full.pm @@ -22,38 +22,86 @@ my %SECTIONS = ( # For each quote type, the extra fields that should be set. # This should give us faster initialization. my %QUOTES = ( - 'q' => { operator => 'q', braced => undef, separator => undef, _sections => 1 }, - 'qq' => { operator => 'qq', braced => undef, separator => undef, _sections => 1 }, - 'qx' => { operator => 'qx', braced => undef, separator => undef, _sections => 1 }, - 'qw' => { operator => 'qw', braced => undef, separator => undef, _sections => 1 }, - 'qr' => { operator => 'qr', braced => undef, separator => undef, _sections => 1, modifiers => 1 }, - 'm' => { operator => 'm', braced => undef, separator => undef, _sections => 1, modifiers => 1 }, - 's' => { operator => 's', braced => undef, separator => undef, _sections => 2, modifiers => 1 }, - 'tr' => { operator => 'tr', braced => undef, separator => undef, _sections => 2, modifiers => 1 }, + 'q' => + { operator => 'q', braced => undef, separator => undef, _sections => 1 }, + 'qq' => + { operator => 'qq', braced => undef, separator => undef, _sections => 1 }, + 'qx' => + { operator => 'qx', braced => undef, separator => undef, _sections => 1 }, + 'qw' => + { operator => 'qw', braced => undef, separator => undef, _sections => 1 }, + 'qr' => { + operator => 'qr', + braced => undef, + separator => undef, + _sections => 1, + modifiers => 1 + }, + 'm' => { + operator => 'm', + braced => undef, + separator => undef, + _sections => 1, + modifiers => 1 + }, + 's' => { + operator => 's', + braced => undef, + separator => undef, + _sections => 2, + modifiers => 1 + }, + 'tr' => { + operator => 'tr', + braced => undef, + separator => undef, + _sections => 2, + modifiers => 1 + }, # Y is the little-used variant of tr - 'y' => { operator => 'y', braced => undef, separator => undef, _sections => 2, modifiers => 1 }, - - '/' => { operator => undef, braced => 0, separator => '/', _sections => 1, modifiers => 1 }, + 'y' => { + operator => 'y', + braced => undef, + separator => undef, + _sections => 2, + modifiers => 1 + }, + + '/' => { + operator => undef, + braced => 0, + separator => '/', + _sections => 1, + modifiers => 1 + }, # Angle brackets quotes mean "readline(*FILEHANDLE)" - '<' => { operator => undef, braced => 1, separator => undef, _sections => 1, }, + '<' => + { operator => undef, braced => 1, separator => undef, _sections => 1, }, # The final ( and kind of depreciated ) "first match only" one is not # used yet, since I'm not sure on the context differences between # this and the trinary operator, but it's here for completeness. - '?' => { operator => undef, braced => 0, separator => '?', _sections => 1, modifiers => 1 }, + '?' => { + operator => undef, + braced => 0, + separator => '?', + _sections => 1, + modifiers => 1 + }, # parse prototypes as a literal quote - '(' => { operator => undef, braced => 1, separator => undef, _sections => 1, }, + '(' => + { operator => undef, braced => 1, separator => undef, _sections => 1, }, ); - sub new { my $class = shift; - my $init = defined $_[0] - ? shift - : Carp::croak("::Full->new called without init string"); + my $init = + defined $_[0] + ? shift + : Carp::croak("::Full->new called without init string"); # Create the token ### This manual SUPER'ing ONLY works because none of @@ -62,9 +110,8 @@ sub new { my $self = PPI::Token::new( $class, $init ) or return undef; # Do we have a prototype for the initializer? If so, add the extra fields - my $options = $QUOTES{$init} or return $self->_error( - "Unknown quote type '$init'" - ); + my $options = $QUOTES{$init} + or return $self->_error("Unknown quote type '$init'"); foreach ( keys %$options ) { $self->{$_} = $options->{$_}; } @@ -83,7 +130,7 @@ sub _fill { my $class = shift; my $t = shift; my $self = $t->{token} - or Carp::croak("::Full->_fill called without current token"); + or Carp::croak("::Full->_fill called without current token"); # Load in the operator stuff if needed if ( $self->{operator} ) { @@ -91,7 +138,7 @@ sub _fill { # operator and the opening separator. if ( substr( $t->{line}, $t->{line_cursor}, 1 ) =~ /\s/ ) { # Go past the gap - my $gap = $self->_scan_quote_like_operator_gap( $t ); + my $gap = $self->_scan_quote_like_operator_gap($t); return undef unless defined $gap; if ( ref $gap ) { # End of file @@ -108,18 +155,20 @@ sub _fill { # Determine if these are normal or braced type sections if ( my $section = $SECTIONS{$sep} ) { - $self->{braced} = 1; + $self->{braced} = 1; $self->{sections}->[0] = Clone::clone($section); - } else { - $self->{braced} = 0; - $self->{separator} = $sep; + } + else { + $self->{braced} = 0; + $self->{separator} = $sep; } } # Parse different based on whether we are normal or braced - my $rv = $self->{braced} - ? $self->_fill_braced($t) - : $self->_fill_normal($t); + my $rv = + $self->{braced} + ? $self->_fill_braced($t) + : $self->_fill_normal($t); return $rv if !$rv; # Return now unless it has modifiers ( i.e. s/foo//eieio ) @@ -128,10 +177,12 @@ sub _fill { # Check for modifiers my $char; my $len = 0; - while ( ($char = substr( $t->{line}, $t->{line_cursor} + 1, 1 )) =~ /[^\W\d_]/ ) { + while ( ( $char = substr( $t->{line}, $t->{line_cursor} + 1, 1 ) ) =~ + /[^\W\d_]/ ) + { $len++; $self->{content} .= $char; - $self->{modifiers}->{lc $char} = 1; + $self->{modifiers}->{ lc $char } = 1; $t->{line_cursor}++; } } @@ -146,19 +197,20 @@ sub _fill_normal { return undef unless defined $string; if ( ref $string ) { # End of file - if ( length($$string) > 1 ) { + if ( length($$string) > 1 ) { # Complete the properties for the first section my $str = $$string; chop $str; $self->{sections}->[0] = { - position => length($self->{content}), + position => length( $self->{content} ), size => length($$string) - 1, type => "$self->{separator}$self->{separator}", }; $self->{_sections} = 1; - } else { + } + else { # No sections at all - $self->{sections} = [ ]; + $self->{sections} = []; $self->{_sections} = 0; } $self->{content} .= $$string; @@ -186,16 +238,17 @@ sub _fill_normal { return undef unless defined $string; if ( ref $string ) { # End of file - if ( length($$string) > 1 ) { + if ( length($$string) > 1 ) { # Complete the properties for the second section my $str = $$string; chop $str; $self->{sections}->[1] = { - position => length($self->{content}), + position => length( $self->{content} ), size => length($$string) - 1, type => "$self->{separator}$self->{separator}", }; - } else { + } + else { # No sections at all $self->{_sections} = 1; } @@ -205,7 +258,7 @@ sub _fill_normal { # Complete the properties of the second section $self->{sections}->[1] = { - position => length($self->{content}), + position => length( $self->{content} ), size => length($string) - 1 }; $self->{content} .= $string; @@ -224,19 +277,20 @@ sub _fill_braced { return undef unless defined $brace_str; if ( ref $brace_str ) { # End of file - if ( length($$brace_str) > 1 ) { + if ( length($$brace_str) > 1 ) { # Complete the properties for the first section my $str = $$brace_str; chop $str; $self->{sections}->[0] = { - position => length($self->{content}), + position => length( $self->{content} ), size => length($$brace_str) - 1, type => $section->{type}, }; $self->{_sections} = 1; - } else { + } + else { # No sections at all - $self->{sections} = [ ]; + $self->{sections} = []; $self->{_sections} = 0; } $self->{content} .= $$brace_str; @@ -258,7 +312,7 @@ sub _fill_braced { my $char = substr( $t->{line}, ++$t->{line_cursor}, 1 ); if ( $char =~ /\s/ ) { # Go past the gap - my $gap_str = $self->_scan_quote_like_operator_gap( $t ); + my $gap_str = $self->_scan_quote_like_operator_gap($t); return undef unless defined $gap_str; if ( ref $gap_str ) { # End of file @@ -271,12 +325,12 @@ sub _fill_braced { $section = $SECTIONS{$char}; - if ( $section ) { + if ($section) { # It's a brace # Initialize the second section $self->{content} .= $char; - $section = { %$section }; + $section = {%$section}; # Advance into the second section $t->{line_cursor}++; @@ -286,32 +340,35 @@ sub _fill_braced { return undef unless defined $brace_str; if ( ref $brace_str ) { # End of file - if ( length($$brace_str) > 1 ) { + if ( length($$brace_str) > 1 ) { # Complete the properties for the second section my $str = $$brace_str; chop $str; $self->{sections}->[1] = { - position => length($self->{content}), + position => length( $self->{content} ), size => length($$brace_str) - 1, type => $section->{type}, }; $self->{_sections} = 2; - } else { + } + else { # No sections at all $self->{_sections} = 1; } $self->{content} .= $$brace_str; return 0; - } else { + } + else { # Complete the properties for the second section $self->{sections}->[1] = { - position => length($self->{content}), + position => length( $self->{content} ), size => length($brace_str) - 1, type => $section->{type}, }; $self->{content} .= $brace_str; } - } elsif ( $char =~ m/ \A [^\w\s] \z /smx ) { + } + elsif ( $char =~ m/ \A [^\w\s] \z /smx ) { # It is some other delimiter (weird, but possible) # Add the delimiter to the content. @@ -325,16 +382,17 @@ sub _fill_braced { return undef unless defined $string; if ( ref $string ) { # End of file - if ( length($$string) > 1 ) { + if ( length($$string) > 1 ) { # Complete the properties for the second section my $str = $$string; chop $str; $self->{sections}->[1] = { - position => length($self->{content}), + position => length( $self->{content} ), size => length($$string) - 1, type => "$char$char", }; - } else { + } + else { # Only the one section $self->{_sections} = 1; } @@ -344,13 +402,14 @@ sub _fill_braced { # Complete the properties of the second section $self->{sections}->[1] = { - position => length($self->{content}), + position => length( $self->{content} ), size => length($string) - 1, - type => "$char$char", + type => "$char$char", }; $self->{content} .= $string; - } else { + } + else { # Error, it has to be a delimiter of some sort. # Although this will result in a REALLY illegal regexp, @@ -358,13 +417,14 @@ sub _fill_braced { # Create a null second section $self->{sections}->[1] = { - position => length($self->{content}), + position => length( $self->{content} ), size => 0, type => '', }; # Attach an error to the token and move on - $self->{_error} = "No second section of regexp, or does not start with a balanced character"; + $self->{_error} = +"No second section of regexp, or does not start with a balanced character"; # Roll back the cursor one char and return signalling end of regexp $t->{line_cursor}--; @@ -374,24 +434,20 @@ sub _fill_braced { 1; } - - - - ##################################################################### # Additional methods to find out about the quote # In a scalar context, get the number of sections # In an array context, get the section information sub _sections { - wantarray ? @{$_[0]->{sections}} : scalar @{$_[0]->{sections}} + wantarray ? @{ $_[0]->{sections} } : scalar @{ $_[0]->{sections} }; } # Get a section's content sub _section_content { my $self = shift; my $i = shift; - $self->{sections} or return; + $self->{sections} or return; my $section = $self->{sections}->[$i] or return; return substr( $self->content, $section->{position}, $section->{size} ); } @@ -415,11 +471,12 @@ sub _delimiters { foreach my $sect ( @{ $self->{sections} } ) { if ( exists $sect->{type} ) { push @delims, $sect->{type}; - } else { + } + else { my $content = $self->content; push @delims, - substr( $content, $sect->{position} - 1, 1 ) . - substr( $content, $sect->{position} + $sect->{size}, 1 ); + substr( $content, $sect->{position} - 1, 1 ) + . substr( $content, $sect->{position} + $sect->{size}, 1 ); } } return @delims; diff --git a/lib/PPI/Token/_QuoteEngine/Simple.pm b/lib/PPI/Token/_QuoteEngine/Simple.pm index 34d31165..decc4985 100644 --- a/lib/PPI/Token/_QuoteEngine/Simple.pm +++ b/lib/PPI/Token/_QuoteEngine/Simple.pm @@ -35,7 +35,8 @@ sub _fill { # End of file $self->{content} .= $$string; return 0; - } else { + } + else { # End of string $self->{content} .= $string; return $self; diff --git a/lib/PPI/Tokenizer.pm b/lib/PPI/Tokenizer.pm index e524eb8c..3d07e14f 100644 --- a/lib/PPI/Tokenizer.pm +++ b/lib/PPI/Tokenizer.pm @@ -79,14 +79,14 @@ in private methods. # Make sure everything we need is loaded so # we don't have to go and load all of PPI. use strict; -use Params::Util qw{_INSTANCE _SCALAR0 _ARRAY0}; -use List::Util 1.33 (); -use PPI::Util (); -use PPI::Element (); -use PPI::Token (); -use PPI::Exception (); +use Params::Util qw{_INSTANCE _SCALAR0 _ARRAY0}; +use List::Util 1.33 (); +use PPI::Util (); +use PPI::Element (); +use PPI::Token (); +use PPI::Exception (); use PPI::Exception::ParserRejection (); -use PPI::Document (); +use PPI::Document (); our $VERSION = '1.282'; @@ -106,32 +106,30 @@ my %X_CAN_FOLLOW_STRUCTURE = map { $_ => 1 } qw( } ] \) ); # chop, chomp, dump are ambiguous because they can have either parms # or no parms. my %X_CAN_FOLLOW_WORD = map { $_ => 1 } qw( - endgrent - endhostent - endnetent - endprotoent - endpwent - endservent - fork - getgrent - gethostent - getlogin - getnetent - getppid - getprotoent - getpwent - getservent - setgrent - setpwent - time - times - wait - wantarray - __SUB__ + endgrent + endhostent + endnetent + endprotoent + endpwent + endservent + fork + getgrent + gethostent + getlogin + getnetent + getppid + getprotoent + getpwent + getservent + setgrent + setpwent + time + times + wait + wantarray + __SUB__ ); - - ##################################################################### # Creation and Initialization @@ -153,7 +151,7 @@ L exception on error. =cut sub new { - my $class = ref($_[0]) || $_[0]; + my $class = ref( $_[0] ) || $_[0]; # Create the empty tokenizer struct my $self = bless { @@ -163,15 +161,15 @@ sub new { document => undef, # Line buffer - line => undef, - line_length => undef, - line_cursor => undef, - line_count => 0, + line => undef, + line_length => undef, + line_cursor => undef, + line_count => 0, # Parse state - token => undef, - class => 'PPI::Token::BOM', - zone => 'PPI::Token::Whitespace', + token => undef, + class => 'PPI::Token::BOM', + zone => 'PPI::Token::Whitespace', # Output token buffer tokens => [], @@ -179,32 +177,38 @@ sub new { token_eof => 0, # Perl 6 blocks - perl6 => [], + perl6 => [], }, $class; - if ( ! defined $_[1] ) { + if ( !defined $_[1] ) { # We weren't given anything PPI::Exception->throw("No source provided to Tokenizer"); - } elsif ( ! ref $_[1] ) { - my $source = PPI::Util::_slurp($_[1]); + } + elsif ( !ref $_[1] ) { + my $source = PPI::Util::_slurp( $_[1] ); if ( ref $source ) { # Content returned by reference $self->{source} = $$source; - } else { + } + else { # Errors returned as a string - return( $source ); + return ($source); } - } elsif ( _SCALAR0($_[1]) ) { - $self->{source} = ${$_[1]}; + } + elsif ( _SCALAR0( $_[1] ) ) { + $self->{source} = ${ $_[1] }; - } elsif ( _ARRAY0($_[1]) ) { - $self->{source} = join '', map { "\n" } @{$_[1]}; + } + elsif ( _ARRAY0( $_[1] ) ) { + $self->{source} = join '', map { "\n" } @{ $_[1] }; - } else { + } + else { # We don't support whatever this is - PPI::Exception->throw(ref($_[1]) . " is not supported as a source provider"); + PPI::Exception->throw( + ref( $_[1] ) . " is not supported as a source provider" ); } # We can't handle a null string @@ -214,8 +218,9 @@ sub new { $self->{source} =~ s/(?:\015{1,2}\012|\015|\012)/\n/g; $self->{source} = [ split /(?<=\n)/, $self->{source} ]; - } else { - $self->{source} = [ ]; + } + else { + $self->{source} = []; } ### EVIL @@ -236,13 +241,16 @@ sub new { # once the tokenizer hits end of file, it examines the last token to # manually either remove the ' ' token, or chop it off the end of # a longer one in which the space would be valid. - if ( List::Util::any { /^__(?:DATA|END)__\s*$/ } @{$self->{source}} ) { + if ( List::Util::any { /^__(?:DATA|END)__\s*$/ } @{ $self->{source} } ) { $self->{source_eof_chop} = ''; - } elsif ( ! defined $self->{source}->[0] ) { + } + elsif ( !defined $self->{source}->[0] ) { $self->{source_eof_chop} = ''; - } elsif ( $self->{source}->[-1] =~ /\s$/ ) { + } + elsif ( $self->{source}->[-1] =~ /\s$/ ) { $self->{source_eof_chop} = ''; - } else { + } + else { $self->{source_eof_chop} = 1; $self->{source}->[-1] .= ' '; } @@ -255,10 +263,6 @@ sub _document { return @_ ? $self->{document} = shift : $self->{document}; } - - - - ##################################################################### # Main Public Methods @@ -289,9 +293,9 @@ sub get_token { my $self = shift; # Shortcut for EOF - if ( $self->{token_eof} - and $self->{token_cursor} > scalar @{$self->{tokens}} - ) { + if ( $self->{token_eof} + and $self->{token_cursor} > scalar @{ $self->{tokens} } ) + { return 0; } @@ -310,22 +314,29 @@ sub get_token { while ( $line_rv = $self->_process_next_line ) { # If there is something in the buffer, return it # The defined() prevents a ton of calls to PPI::Util::TRUE - if ( defined( my $token = $self->{tokens}->[ $self->{token_cursor} ] ) ) { + if ( + defined( + my $token = $self->{tokens}->[ $self->{token_cursor} ] + ) + ) + { $self->{token_cursor}++; return $token; } } return undef; }; - if ( $@ ) { - if ( _INSTANCE($@, 'PPI::Exception') ) { + if ($@) { + if ( _INSTANCE( $@, 'PPI::Exception' ) ) { $@->throw; - } else { + } + else { my $errstr = $@; $errstr =~ s/^(.*) at line .+$/$1/; - PPI::Exception->throw( $errstr ); + PPI::Exception->throw($errstr); } - } elsif ( $rv ) { + } + elsif ($rv) { return $rv; } @@ -371,7 +382,7 @@ sub all_tokens { # Process lines until we get EOF unless ( $self->{token_eof} ) { my $rv; - while ( $rv = $self->_process_next_line ) {} + while ( $rv = $self->_process_next_line ) { } unless ( defined $rv ) { PPI::Exception->throw("Error while processing source"); } @@ -384,11 +395,11 @@ sub all_tokens { if ( !$ok ) { my $errstr = $@; $errstr =~ s/^(.*) at line .+$/$1/; - PPI::Exception->throw( $errstr ); + PPI::Exception->throw($errstr); } # End of file, return a copy of the token array. - return [ @{$self->{tokens}} ]; + return [ @{ $self->{tokens} } ]; } =pod @@ -443,10 +454,6 @@ sub decrement_cursor { --$self->{token_cursor}; } - - - - ##################################################################### # Working With Source @@ -454,16 +461,16 @@ sub decrement_cursor { # Returns undef at EOF. sub _get_line { my $self = shift; - return undef unless $self->{source}; # EOF hit previously + return undef unless $self->{source}; # EOF hit previously # Pull off the next line - my $line = shift @{$self->{source}}; + my $line = shift @{ $self->{source} }; # Flag EOF if we hit it $self->{source} = undef unless defined $line; # Return the line (or EOF flag) - return $line; # string or undef + return $line; # string or undef } # Fetches the next line, ready to process @@ -477,7 +484,7 @@ sub _fill_line { my $line = $self->_get_line; unless ( defined $line ) { # End of file - unless ( $inscan ) { + unless ($inscan) { delete $self->{line}; delete $self->{line_cursor}; delete $self->{line_length}; @@ -505,10 +512,6 @@ sub _char { substr( $self->{line}, $self->{line_cursor}, 1 ); } - - - - #################################################################### # Per line processing methods @@ -530,10 +533,10 @@ sub _process_next_line { } # Run the __TOKENIZER__on_line_start - $rv = $self->{class}->__TOKENIZER__on_line_start( $self ); - unless ( $rv ) { + $rv = $self->{class}->__TOKENIZER__on_line_start($self); + unless ($rv) { # If there are no more source lines, then clean up - if ( ref $self->{source} eq 'ARRAY' and ! @{$self->{source}} ) { + if ( ref $self->{source} eq 'ARRAY' and !@{ $self->{source} } ) { $self->_clean_eof; } @@ -543,26 +546,24 @@ sub _process_next_line { } # If we can't deal with the entire line, process char by char - while ( $rv = $self->_process_next_char ) {} + while ( $rv = $self->_process_next_char ) { } unless ( defined $rv ) { - PPI::Exception->throw("Error at line $self->{line_count}, character $self->{line_cursor}"); + PPI::Exception->throw( + "Error at line $self->{line_count}, character $self->{line_cursor}" + ); } # Trigger any action that needs to happen at the end of a line - $self->{class}->__TOKENIZER__on_line_end( $self ); + $self->{class}->__TOKENIZER__on_line_end($self); # If there are no more source lines, then clean up - unless ( ref($self->{source}) eq 'ARRAY' and @{$self->{source}} ) { + unless ( ref( $self->{source} ) eq 'ARRAY' and @{ $self->{source} } ) { return $self->_clean_eof; } return 1; } - - - - ##################################################################### # Per-character processing methods @@ -575,18 +576,18 @@ sub _process_next_char { ### FIXME - This checks for a screwed up condition that triggers ### several warnings, amongst other things. - if ( ! defined $self->{line_cursor} or ! defined $self->{line_length} ) { + if ( !defined $self->{line_cursor} or !defined $self->{line_length} ) { # $DB::single = 1; return undef; } - $self->{line_cursor}++; - return 0 if $self->_at_line_end; + $self->{line_cursor}++; + return 0 if $self->_at_line_end; # Pass control to the token class my $result; - unless ( $result = $self->{class}->__TOKENIZER__on_char( $self ) ) { - # undef is error. 0 is "Did stuff ourself, you don't have to do anything" + unless ( $result = $self->{class}->__TOKENIZER__on_char($self) ) { + # undef is error. 0 is "Did stuff ourself, you don't have to do anything" return defined $result ? 1 : undef; } @@ -599,8 +600,10 @@ sub _process_next_char { # Add the character if ( defined $self->{token} ) { $self->{token}->{content} .= $char; - } else { - defined($self->{token} = $self->{class}->new($char)) or return undef; + } + else { + defined( $self->{token} = $self->{class}->new($char) ) + or return undef; } return 1; @@ -610,26 +613,24 @@ sub _process_next_char { if ( $self->{class} ne "PPI::Token::$result" ) { # New class $self->_new_token( $result, $char ); - } elsif ( defined $self->{token} ) { + } + elsif ( defined $self->{token} ) { # Same class as current $self->{token}->{content} .= $char; - } else { + } + else { # Same class, but no current - defined($self->{token} = $self->{class}->new($char)) or return undef; + defined( $self->{token} = $self->{class}->new($char) ) or return undef; } 1; } sub _at_line_end { - my ($self) = @_; - return $self->{line_cursor} >= $self->{line_length}; + my ($self) = @_; + return $self->{line_cursor} >= $self->{line_length}; } - - - - ##################################################################### # Altering Tokens in Tokenizer @@ -652,14 +653,14 @@ sub _finalize_token { sub _new_token { my $self = shift; # throw PPI::Exception() unless @_; - my $class = substr( $_[0], 0, 12 ) eq 'PPI::Token::' - ? shift : 'PPI::Token::' . shift; + my $class = + substr( $_[0], 0, 12 ) eq 'PPI::Token::' ? shift : 'PPI::Token::' . shift; # Finalize any existing token $self->_finalize_token if defined $self->{token}; # Create the new token and update the parse class - defined($self->{token} = $class->new($_[0])) or PPI::Exception->throw; + defined( $self->{token} = $class->new( $_[0] ) ) or PPI::Exception->throw; $self->{class} = $class; 1; @@ -676,19 +677,19 @@ sub _clean_eof { # Find the last token, and if it has no content, kill it. # There appears to be some evidence that such "null tokens" are # somehow getting created accidentally. - my $last_token = $self->{tokens}->[ -1 ]; + my $last_token = $self->{tokens}->[-1]; unless ( length $last_token->{content} ) { - pop @{$self->{tokens}}; + pop @{ $self->{tokens} }; } # Now, if the last character of the last token is a space we added, # chop it off, deleting the token if there's nothing else left. if ( $self->{source_eof_chop} ) { - $last_token = $self->{tokens}->[ -1 ]; + $last_token = $self->{tokens}->[-1]; $last_token->{content} =~ s/ $//; unless ( length $last_token->{content} ) { # Popping token - pop @{$self->{tokens}}; + pop @{ $self->{tokens} }; } # The hack involving adding an extra space is now reversed, and @@ -699,10 +700,6 @@ sub _clean_eof { 1; } - - - - ##################################################################### # Utility Methods @@ -715,7 +712,7 @@ sub _last_significant_token { my $self = shift; my $cursor = $#{ $self->{tokens} }; while ( $cursor >= 0 ) { - my $token = $self->{tokens}->[$cursor--]; + my $token = $self->{tokens}->[ $cursor-- ]; return $token if $token->significant; } return; @@ -731,7 +728,7 @@ sub _previous_significant_tokens { my @tokens; while ( $cursor >= 0 ) { - my $token = $self->{tokens}->[$cursor--]; + my $token = $self->{tokens}->[ $cursor-- ]; next if not $token->significant; push @tokens, $token; last if @tokens >= $count; @@ -764,7 +761,6 @@ my %OBVIOUS_CONTENT = ( '}' => 'operator', ); - my %USUALLY_FORCES = map { $_ => 1 } qw( sub package use no ); # Try to determine operator/operand context, if possible. @@ -774,7 +770,7 @@ sub _opcontext { my @tokens = $self->_previous_significant_tokens(1); my $p0 = $tokens[0]; return '' if not $p0; - my $c0 = ref $p0; + my $c0 = ref $p0; # Map the obvious cases return $OBVIOUS_CLASS{$c0} if defined $OBVIOUS_CLASS{$c0}; @@ -787,27 +783,28 @@ sub _opcontext { return 'operand' if $p0->content eq ''; # Otherwise, we don't know - return '' + return ''; } # Assuming we are currently parsing the word 'x', return true # if previous tokens imply the x is an operator, false otherwise. sub _current_x_is_operator { - my ( $self ) = @_; - return if !@{$self->{tokens}}; + my ($self) = @_; + return if !@{ $self->{tokens} }; - my ($prev, $prevprev) = $self->_previous_significant_tokens(2); + my ( $prev, $prevprev ) = $self->_previous_significant_tokens(2); return if !$prev; - return !$self->__current_token_is_forced_word if $prev->isa('PPI::Token::Word'); + return !$self->__current_token_is_forced_word + if $prev->isa('PPI::Token::Word'); - return (!$prev->isa('PPI::Token::Operator') || $X_CAN_FOLLOW_OPERATOR{$prev}) - && (!$prev->isa('PPI::Token::Structure') || $X_CAN_FOLLOW_STRUCTURE{$prev}) - && !$prev->isa('PPI::Token::Label') - ; + return (!$prev->isa('PPI::Token::Operator') + || $X_CAN_FOLLOW_OPERATOR{$prev} ) + && (!$prev->isa('PPI::Token::Structure') + || $X_CAN_FOLLOW_STRUCTURE{$prev} ) + && !$prev->isa('PPI::Token::Label'); } - # Assuming we are at the end of parsing the current token that could be a word, # a wordlike operator, or a version string, try to determine whether context # before or after it forces it to be a bareword. This method is only useful @@ -841,10 +838,15 @@ sub __current_token_is_forced_word { # preceded by 'package sub', in which case we're a version string. # We also have to make sure that the sub/package/etc doing the forcing # is not a method call. - if( $USUALLY_FORCES{$content}) { - return if defined $word and $word =~ /^v[0-9]+$/ and ( $content eq "use" or $content eq "no" ); + if ( $USUALLY_FORCES{$content} ) { + return + if defined $word + and $word =~ /^v[0-9]+$/ + and ( $content eq "use" or $content eq "no" ); return 1 if not $prevprev; - return 1 if not $USUALLY_FORCES{$prevprev->content} and $prevprev->content ne '->'; + return 1 + if not $USUALLY_FORCES{ $prevprev->content } + and $prevprev->content ne '->'; return; } } @@ -873,7 +875,8 @@ sub _current_token_has_signatures_active { } my ($closest_parented_token) = grep $_->parent, @tokens; - $closest_parented_token ||= $t->_document || $t->_document(PPI::Document->new); + $closest_parented_token ||= + $t->_document || $t->_document( PPI::Document->new ); return $closest_parented_token->presumed_features->{signatures}, @tokens; } diff --git a/lib/PPI/Transform.pm b/lib/PPI/Transform.pm index c85af3db..170c2b5f 100644 --- a/lib/PPI/Transform.pm +++ b/lib/PPI/Transform.pm @@ -23,10 +23,6 @@ use Params::Util qw{_INSTANCE _CLASS _CODE _SCALAR0}; our $VERSION = '1.282'; - - - - ##################################################################### # Apply Handler Registration @@ -37,9 +33,12 @@ my @ORDER; # I'm just leaving it undocumented for now. sub register_apply_handler { my $class = shift; - my $handler = _CLASS(shift) or Carp::croak("Invalid PPI::Transform->register_apply_handler param"); - my $get = _CODE(shift) or Carp::croak("Invalid PPI::Transform->register_apply_handler param"); - my $set = _CODE(shift) or Carp::croak("Invalid PPI::Transform->register_apply_handler param"); + my $handler = _CLASS(shift) + or Carp::croak("Invalid PPI::Transform->register_apply_handler param"); + my $get = _CODE(shift) + or Carp::croak("Invalid PPI::Transform->register_apply_handler param"); + my $set = _CODE(shift) + or Carp::croak("Invalid PPI::Transform->register_apply_handler param"); if ( $HANDLER{$handler} ) { Carp::croak("PPI::Transform->apply handler '$handler' already exists"); } @@ -51,11 +50,11 @@ sub register_apply_handler { # Register the default handlers __PACKAGE__->register_apply_handler( 'SCALAR', \&_SCALAR_get, \&_SCALAR_set ); -__PACKAGE__->register_apply_handler( 'PPI::Document', sub { $_[0] }, sub() { 1 } ); - - - - +__PACKAGE__->register_apply_handler( + 'PPI::Document', + sub { $_[0] }, + sub() { 1 } +); ##################################################################### # Constructor @@ -83,7 +82,7 @@ C on error. sub new { my $class = shift; - bless { @_ }, $class; + bless {@_}, $class; } =pod @@ -137,19 +136,23 @@ sub apply { my $it = defined $_[0] ? shift : return undef; # Try to find an apply handler - my $class = _SCALAR0($it) ? 'SCALAR' - : List::Util::first { _INSTANCE($it, $_) } @ORDER - or return undef; + my $class = + _SCALAR0($it) + ? 'SCALAR' + : List::Util::first { _INSTANCE( $it, $_ ) } @ORDER + or return undef; my $handler = $HANDLER{$class} - or die("->apply handler for $class missing! Panic"); + or die("->apply handler for $class missing! Panic"); # Get, change, set - my $Document = _INSTANCE($handler->[0]->($it), 'PPI::Document') - or Carp::croak("->apply handler for $class failed to get a PPI::Document"); - $self->document( $Document ) or return undef; - $handler->[1]->($it, $Document) - or Carp::croak("->apply handler for $class failed to save the changed document"); - 1; + my $Document = _INSTANCE( $handler->[0]->($it), 'PPI::Document' ) + or + Carp::croak("->apply handler for $class failed to get a PPI::Document"); + $self->document($Document) or return undef; + $handler->[1]->( $it, $Document ) + or Carp::croak( + "->apply handler for $class failed to save the changed document"); + 1; } =pod @@ -178,15 +181,11 @@ sub file { my $output = @_ ? defined $_[0] ? "$_[0]" : undef : $input or return undef; # Process the file - my $Document = PPI::Document->new( "$input" ) or return undef; - $self->document( $Document ) or return undef; - $Document->save( $output ); + my $Document = PPI::Document->new("$input") or return undef; + $self->document($Document) or return undef; + $Document->save($output); } - - - - ##################################################################### # Apply Hander Methods @@ -200,18 +199,13 @@ sub _SCALAR_set { 1; } - - - - ##################################################################### # Support Functions sub _SELF { return shift if ref $_[0]; - my $self = $_[0]->new or Carp::croak( - "Failed to auto-instantiate new $_[0] object" - ); + my $self = $_[0]->new + or Carp::croak("Failed to auto-instantiate new $_[0] object"); $self; } diff --git a/lib/PPI/Transform/UpdateCopyright.pm b/lib/PPI/Transform/UpdateCopyright.pm index 1b4484ba..8c01a566 100644 --- a/lib/PPI/Transform/UpdateCopyright.pm +++ b/lib/PPI/Transform/UpdateCopyright.pm @@ -33,10 +33,6 @@ use PPI::Transform (); our $VERSION = '1.282'; - - - - ##################################################################### # Constructor and Accessors @@ -61,7 +57,7 @@ sub new { my $self = shift->SUPER::new(@_); # Must provide a name - unless ( defined _STRING($self->name) ) { + unless ( defined _STRING( $self->name ) ) { PPI::Exception->throw("Did not provide a valid name param"); } @@ -81,33 +77,31 @@ sub name { $_[0]->{name}; } - - - - ##################################################################### # Transform sub document { my $self = shift; - my $document = _INSTANCE(shift, 'PPI::Document') or return undef; + my $document = _INSTANCE( shift, 'PPI::Document' ) or return undef; # Find things to transform my $name = quotemeta $self->name; my $regexp = qr/\bcopyright\b.*$name/m; - my $elements = $document->find( sub { - $_[1]->isa('PPI::Token::Pod') or return ''; - $_[1]->content =~ $regexp or return ''; - return 1; - } ); + my $elements = $document->find( + sub { + $_[1]->isa('PPI::Token::Pod') or return ''; + $_[1]->content =~ $regexp or return ''; + return 1; + } + ); return undef unless defined $elements; - return 0 unless $elements; + return 0 unless $elements; # Try to transform any elements my $changes = 0; my $change = sub { my $copyright = shift; - my $thisyear = (localtime time)[5] + 1900; + my $thisyear = ( localtime time )[5] + 1900; my @year = $copyright =~ m/(\d{4})/g; if ( @year == 1 ) { @@ -115,7 +109,8 @@ sub document { if ( $year[0] == $thisyear ) { # No change return $copyright; - } else { + } + else { # Convert from single year to multiple year $changes++; $copyright =~ s/(\d{4})/$1 - $thisyear/; @@ -128,7 +123,8 @@ sub document { if ( $year[1] == $thisyear ) { # No change return $copyright; - } else { + } + else { # Change the second year to the current one $changes++; $copyright =~ s/$year[1]/$thisyear/; @@ -142,7 +138,7 @@ sub document { # Attempt to transform each element my $pattern = qr/\b(copyright.*\d)({4}(?:\s*-\s*\d{4})?)(.*$name)/mi; - foreach my $element ( @$elements ) { + foreach my $element (@$elements) { $element =~ s/$pattern/$1 . $change->($2) . $2/eg; } diff --git a/lib/PPI/Util.pm b/lib/PPI/Util.pm index 50f705f2..87ed8272 100644 --- a/lib/PPI/Util.pm +++ b/lib/PPI/Util.pm @@ -14,17 +14,13 @@ our @EXPORT_OK = qw{ _Document _slurp }; # 5.8.7 was the first version to resolve the notorious # "unicode length caching" bug. -use constant HAVE_UNICODE => !! ( $] >= 5.008007 ); +use constant HAVE_UNICODE => !!( $] >= 5.008007 ); # Common reusable true and false functions # This makes it easy to upgrade many places in PPI::XS -sub TRUE () { 1 } +sub TRUE () { 1 } sub FALSE () { '' } - - - - ##################################################################### # Functions @@ -35,9 +31,9 @@ sub _Document { return undef unless defined $_[0]; require PPI::Document; return PPI::Document->new(shift) unless ref $_[0]; - return PPI::Document->new(shift) if _SCALAR0($_[0]); - return PPI::Document->new(shift) if _ARRAY0($_[0]); - return shift if _INSTANCE($_[0], 'PPI::Document'); + return PPI::Document->new(shift) if _SCALAR0( $_[0] ); + return PPI::Document->new(shift) if _ARRAY0( $_[0] ); + return shift if _INSTANCE( $_[0], 'PPI::Document' ); return undef; } @@ -48,7 +44,7 @@ sub _slurp { local *FILE; open( FILE, '<', $file ) or return "open($file) failed: $!"; my $source = ; - close( FILE ) or return "close($file) failed: $!"; + close(FILE) or return "close($file) failed: $!"; return \$source; } diff --git a/lib/PPI/XSAccessor.pm b/lib/PPI/XSAccessor.pm index 39d1cb04..e46fba6c 100644 --- a/lib/PPI/XSAccessor.pm +++ b/lib/PPI/XSAccessor.pm @@ -9,157 +9,116 @@ use PPI (); our $VERSION = '1.282'; - - - - ###################################################################### # Replacement Methods # Packages are implemented here in alphabetical order -package #hide from indexer - PPI::Document; +package #hide from indexer + PPI::Document; use Class::XSAccessor - replace => 1, - getters => { - readonly => 'readonly', - }, - true => [ - 'scope' - ]; + replace => 1, + getters => { readonly => 'readonly', }, + true => ['scope']; -package #hide from indexer - PPI::Document::File; +package #hide from indexer + PPI::Document::File; use Class::XSAccessor - replace => 1, - getters => { - filename => 'filename', - }; + replace => 1, + getters => { filename => 'filename', }; -package #hide from indexer - PPI::Document::Fragment; +package #hide from indexer + PPI::Document::Fragment; use Class::XSAccessor - replace => 1, - false => [ - 'scope', - ]; + replace => 1, + false => [ 'scope', ]; -package #hide from indexer - PPI::Document::Normalized; +package #hide from indexer + PPI::Document::Normalized; use Class::XSAccessor - replace => 1, - getters => { - '_Document' => 'Document', - 'version' => 'version', - 'functions' => 'functions', - }; + replace => 1, + getters => { + '_Document' => 'Document', + 'version' => 'version', + 'functions' => 'functions', + }; -package #hide from indexer - PPI::Element; +package #hide from indexer + PPI::Element; use Class::XSAccessor - replace => 1, - true => [ - 'significant', - ]; + replace => 1, + true => [ 'significant', ]; -package #hide from indexer - PPI::Exception; +package #hide from indexer + PPI::Exception; use Class::XSAccessor - replace => 1, - getters => { - message => 'message', - }; + replace => 1, + getters => { message => 'message', }; -package #hide from indexer - PPI::Node; +package #hide from indexer + PPI::Node; use Class::XSAccessor - replace => 1, - false => [ - 'scope', - ]; + replace => 1, + false => [ 'scope', ]; -package #hide from indexer - PPI::Normal; +package #hide from indexer + PPI::Normal; use Class::XSAccessor - replace => 1, - getters => { - 'layer' => 'layer', - }; + replace => 1, + getters => { 'layer' => 'layer', }; -package #hide from indexer - PPI::Statement; +package #hide from indexer + PPI::Statement; use Class::XSAccessor - replace => 1, - true => [ - '__LEXER__normal', - ]; + replace => 1, + true => [ '__LEXER__normal', ]; -package #hide from indexer - PPI::Statement::Compound; +package #hide from indexer + PPI::Statement::Compound; use Class::XSAccessor - replace => 1, - true => [ - 'scope', - ], - false => [ - '__LEXER__normal', - ]; + replace => 1, + true => [ 'scope', ], + false => [ '__LEXER__normal', ]; -package #hide from indexer - PPI::Statement::Data; +package #hide from indexer + PPI::Statement::Data; use Class::XSAccessor - replace => 1, - false => [ - '_complete', - ]; + replace => 1, + false => [ '_complete', ]; -package #hide from indexer - PPI::Statement::End; +package #hide from indexer + PPI::Statement::End; use Class::XSAccessor - replace => 1, - true => [ - '_complete', - ]; + replace => 1, + true => [ '_complete', ]; -package #hide from indexer - PPI::Statement::Given; +package #hide from indexer + PPI::Statement::Given; use Class::XSAccessor - replace => 1, - true => [ - 'scope', - ], - false => [ - '__LEXER__normal', - ]; + replace => 1, + true => [ 'scope', ], + false => [ '__LEXER__normal', ]; -package #hide from indexer - PPI::Token; +package #hide from indexer + PPI::Token; use Class::XSAccessor - replace => 1, - getters => { - content => 'content', - }, - setters => { - set_content => 'content', - }, - true => [ - '__TOKENIZER__on_line_start', - '__TOKENIZER__on_line_end', - ]; + replace => 1, + getters => { content => 'content', }, + setters => { set_content => 'content', }, + true => [ '__TOKENIZER__on_line_start', '__TOKENIZER__on_line_end', ]; 1; diff --git a/t/01_compile.t b/t/01_compile.t index d4b777e3..1c80684c 100644 --- a/t/01_compile.t +++ b/t/01_compile.t @@ -4,21 +4,22 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 9 + ($ENV{AUTHOR_TESTING} ? 1 : 0); - +use Test::More tests => 9 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); # Do the modules load -use_all_ok( qw{ - PPI - PPI::Tokenizer - PPI::Lexer - PPI::Dumper - PPI::Find - PPI::Normal - PPI::Util - PPI::Cache - } ); +use_all_ok( + qw{ + PPI + PPI::Tokenizer + PPI::Lexer + PPI::Dumper + PPI::Find + PPI::Normal + PPI::Util + PPI::Cache + } +); sub use_all_ok { use_ok $_ for @_ } -ok( ! $PPI::XS::VERSION, 'PPI::XS is correctly NOT loaded' ); +ok( !$PPI::XS::VERSION, 'PPI::XS is correctly NOT loaded' ); diff --git a/t/03_document.t b/t/03_document.t index ce9c26da..f7f32cc4 100644 --- a/t/03_document.t +++ b/t/03_document.t @@ -4,20 +4,19 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 19 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 19 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use File::Spec::Functions qw( catfile ); -use PPI (); +use PPI (); use Helper 'safe_new'; - ##################################################################### # Test a basic document # Parse a simple document in all possible ways NEW: { - my $file = catfile(qw{ t data 03_document test.dat }); - ok( -f $file, 'Found test.dat' ); + my $file = catfile(qw{ t data 03_document test.dat }); + ok( -f $file, 'Found test.dat' ); my $doc1 = safe_new $file; @@ -32,16 +31,13 @@ END_PERL my $doc2 = safe_new \$script; my $doc3 = safe_new [ - "#!/usr/bin/perl", - "", - "# A simple test script", - "", + "#!/usr/bin/perl", "", "# A simple test script", "", "print \"Hello World!\\n\";", ]; # Compare the three forms is_deeply( $doc1, $doc2, 'Stringref form matches file form' ); - is_deeply( $doc1, $doc3, 'Arrayref form matches file form' ); + is_deeply( $doc1, $doc3, 'Arrayref form matches file form' ); } # Repeat the above with a null document @@ -51,11 +47,11 @@ NEW_EMPTY: { my $doc1 = safe_new $empty; my $doc2 = safe_new \''; - my $doc3 = safe_new [ ]; + my $doc3 = safe_new []; # Compare the three forms is_deeply( $doc1, $doc2, 'Stringref form matches file form' ); - is_deeply( $doc1, $doc3, 'Arrayref form matches file form' ); + is_deeply( $doc1, $doc3, 'Arrayref form matches file form' ); # Make sure the null document round-trips my $string = $doc1->serialize; diff --git a/t/04_element.t b/t/04_element.t index a987b1cd..a9bf6b6f 100644 --- a/t/04_element.t +++ b/t/04_element.t @@ -7,41 +7,48 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 227 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 227 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); -use PPI (); +use PPI (); use PPI::Singletons qw( %_PARENT ); -use PPI::Test qw( pause ); -use Scalar::Util qw( refaddr ); +use PPI::Test qw( pause ); +use Scalar::Util qw( refaddr ); use Helper 'safe_new'; my $RE_IDENTIFIER = qr/[^\W\d]\w*/; sub is_object { - my ($left, $right, $message) = @_; + my ( $left, $right, $message ) = @_; $message ||= "Objects match"; my $condition = ( - defined $left - and ref $left, + defined $left and ref $left, and defined $right, and ref $right, and refaddr($left) == refaddr($right) - ); + ); ok( $condition, $message ); } sub omethod_fails { - my $object = ref($_[0])->isa('UNIVERSAL') ? shift : die "Failed to pass method_fails test an object"; - my $method = (defined $_[0] and $_[0] =~ /$RE_IDENTIFIER/o) ? shift : die "Failed to pass method_fails an identifier"; - my $arg_set = ( ref $_[0] eq 'ARRAY' and scalar(@{$_[0]}) ) ? shift : die "Failed to pass method_fails a set of arguments"; - - foreach my $args ( @$arg_set ) { - is( $object->$method( $args ), undef, ref($object) . "->$method fails correctly" ); + my $object = + ref( $_[0] )->isa('UNIVERSAL') + ? shift + : die "Failed to pass method_fails test an object"; + my $method = + ( defined $_[0] and $_[0] =~ /$RE_IDENTIFIER/o ) + ? shift + : die "Failed to pass method_fails an identifier"; + my $arg_set = + ( ref $_[0] eq 'ARRAY' and scalar( @{ $_[0] } ) ) + ? shift + : die "Failed to pass method_fails a set of arguments"; + + foreach my $args (@$arg_set) { + is( $object->$method($args), + undef, ref($object) . "->$method fails correctly" ); } } - - ##################################################################### # Miscellaneous @@ -52,30 +59,30 @@ SCOPE: { my %hash; my $counter = 0; - SCOPE: { - my $object1 = bless { }, 'My::WeakenTest'; - my $object2 = bless { }, 'My::WeakenTest'; - my $object3 = bless { }, 'My::WeakenTest'; + SCOPE: { + my $object1 = bless {}, 'My::WeakenTest'; + my $object2 = bless {}, 'My::WeakenTest'; + my $object3 = bless {}, 'My::WeakenTest'; isa_ok( $object1, 'My::WeakenTest' ); isa_ok( $object2, 'My::WeakenTest' ); isa_ok( $object3, 'My::WeakenTest' ); # Do nothing for object1. - + # Add object2 to a has index normally $hash{foo} = $object2; # Add object2 and weaken - Scalar::Util::weaken($hash{bar} = $object3); + Scalar::Util::weaken( $hash{bar} = $object3 ); ok( Scalar::Util::isweak( $hash{bar} ), 'index entry is weak' ); - ok( ! Scalar::Util::isweak( $object3 ), 'original is not weak' ); + ok( !Scalar::Util::isweak($object3), 'original is not weak' ); pause(); # Do all the objects still exist - isa_ok( $object1, 'My::WeakenTest' ); - isa_ok( $object2, 'My::WeakenTest' ); - isa_ok( $object3, 'My::WeakenTest' ); + isa_ok( $object1, 'My::WeakenTest' ); + isa_ok( $object2, 'My::WeakenTest' ); + isa_ok( $object3, 'My::WeakenTest' ); isa_ok( $hash{foo}, 'My::WeakenTest' ); isa_ok( $hash{bar}, 'My::WeakenTest' ); } @@ -87,18 +94,15 @@ SCOPE: { isa_ok( $hash{foo}, 'My::WeakenTest' ); # bar should ->exists, but be undefined - ok( exists $hash{bar}, 'weakened object hash slot exists' ); - ok( ! defined $hash{bar}, 'weakened object hash slot is undefined' ); + ok( exists $hash{bar}, 'weakened object hash slot exists' ); + ok( !defined $hash{bar}, 'weakened object hash slot is undefined' ); package My::WeakenTest; - + sub DESTROY { $counter++; } } - - - # Test interaction between weaken and Clone SCOPE: { @@ -107,52 +111,61 @@ SCOPE: { my $object2 = $object; Scalar::Util::weaken($object2); my $clone = Clone::clone($object); - is_deeply( $clone, $object, 'Object is cloned OK when a different reference is weakened' ); + is_deeply( $clone, $object, + 'Object is cloned OK when a different reference is weakened' ); } - - - - ##################################################################### # Prepare # Build a basic source tree to test with my $source = 'my@foo = (1, 2);'; -my $Document = PPI::Lexer->lex_source( $source ); +my $Document = PPI::Lexer->lex_source($source); isa_ok( $Document, 'PPI::Document' ); is( $Document->content, $source, "Document round-trips ok" ); -is( scalar($Document->tokens), 12, "Basic source contains the correct number of tokens" ); -is( scalar(@{$Document->{children}}), 1, "Document contains one element" ); +is( scalar( $Document->tokens ), + 12, "Basic source contains the correct number of tokens" ); +is( scalar( @{ $Document->{children} } ), 1, "Document contains one element" ); my $Statement = $Document->{children}->[0]; isa_ok( $Statement, 'PPI::Statement' ); isa_ok( $Statement, 'PPI::Statement::Variable' ); -is( scalar(@{$Statement->{children}}), 7, "Statement contains the correct number of elements" ); +is( scalar( @{ $Statement->{children} } ), + 7, "Statement contains the correct number of elements" ); my $Token1 = $Statement->{children}->[0]; my $Token2 = $Statement->{children}->[1]; my $Token3 = $Statement->{children}->[2]; my $Braces = $Statement->{children}->[5]; my $Token7 = $Statement->{children}->[6]; -isa_ok( $Token1, 'PPI::Token::Word' ); -isa_ok( $Token2, 'PPI::Token::Symbol' ); +isa_ok( $Token1, 'PPI::Token::Word' ); +isa_ok( $Token2, 'PPI::Token::Symbol' ); isa_ok( $Token3, 'PPI::Token::Whitespace' ); -isa_ok( $Braces, 'PPI::Structure::List' ); -isa_ok( $Token7, 'PPI::Token::Structure' ); -ok( ($Token1->isa('PPI::Token::Word') and $Token1->content eq 'my'), 'First token is correct' ); -ok( ($Token2->isa('PPI::Token::Symbol') and $Token2->content eq '@foo'), 'Second token is correct' ); -ok( ($Token3->isa('PPI::Token::Whitespace') and $Token3->content eq ' '), 'Third token is correct' ); +isa_ok( $Braces, 'PPI::Structure::List' ); +isa_ok( $Token7, 'PPI::Token::Structure' ); +ok( ( $Token1->isa('PPI::Token::Word') and $Token1->content eq 'my' ), + 'First token is correct' ); +ok( ( $Token2->isa('PPI::Token::Symbol') and $Token2->content eq '@foo' ), + 'Second token is correct' ); +ok( ( $Token3->isa('PPI::Token::Whitespace') and $Token3->content eq ' ' ), + 'Third token is correct' ); is( $Braces->braces, '()', 'Braces seem correct' ); -ok( ($Token7->isa('PPI::Token::Structure') and $Token7->content eq ';'), 'Seventh token is correct' ); +ok( ( $Token7->isa('PPI::Token::Structure') and $Token7->content eq ';' ), + 'Seventh token is correct' ); isa_ok( $Braces->start, 'PPI::Token::Structure' ); -ok( ($Braces->start->isa('PPI::Token::Structure') and $Braces->start->content eq '('), - 'Start brace token matches expected' ); +ok( + ( + $Braces->start->isa('PPI::Token::Structure') + and $Braces->start->content eq '(' + ), + 'Start brace token matches expected' +); isa_ok( $Braces->finish, 'PPI::Token::Structure' ); -ok( ($Braces->finish->isa('PPI::Token::Structure') and $Braces->finish->content eq ')'), - 'Finish brace token matches expected' ); - - - - +ok( + ( + $Braces->finish->isa('PPI::Token::Structure') + and $Braces->finish->content eq ')' + ), + 'Finish brace token matches expected' +); ##################################################################### # Testing of PPI::Element basic information methods @@ -167,13 +180,13 @@ is( $Braces->content, '(1, 2)', "Token content is correct" ); is( $Token7->content, ';', "Token content is correct" ); # Testing the ->tokens method -is( scalar($Document->tokens), 12, "Document token count is correct" ); -is( scalar($Statement->tokens), 12, "Statement token count is correct" ); -isa_ok( $Token1->tokens, 'PPI::Token', "Token token count is correct" ); -isa_ok( $Token2->tokens, 'PPI::Token', "Token token count is correct" ); -isa_ok( $Token3->tokens, 'PPI::Token', "Token token count is correct" ); -is( scalar($Braces->tokens), 6, "Token token count is correct" ); -isa_ok( $Token7->tokens, 'PPI::Token', "Token token count is correct" ); +is( scalar( $Document->tokens ), 12, "Document token count is correct" ); +is( scalar( $Statement->tokens ), 12, "Statement token count is correct" ); +isa_ok( $Token1->tokens, 'PPI::Token', "Token token count is correct" ); +isa_ok( $Token2->tokens, 'PPI::Token', "Token token count is correct" ); +isa_ok( $Token3->tokens, 'PPI::Token', "Token token count is correct" ); +is( scalar( $Braces->tokens ), 6, "Token token count is correct" ); +isa_ok( $Token7->tokens, 'PPI::Token', "Token token count is correct" ); # Testing the ->significant method is( $Document->significant, 1, 'Document is significant' ); @@ -184,25 +197,23 @@ is( $Token3->significant, '', 'Token is significant' ); is( $Braces->significant, 1, 'Token is significant' ); is( $Token7->significant, 1, 'Token is significant' ); - - - - ##################################################################### # Testing of PPI::Element navigation # Test the ->parent method is( $Document->parent, undef, "Document does not have a parent" ); -is_object( $Statement->parent, $Document, "Statement sees document as parent" ); -is_object( $Token1->parent, $Statement, "Token sees statement as parent" ); -is_object( $Token2->parent, $Statement, "Token sees statement as parent" ); -is_object( $Token3->parent, $Statement, "Token sees statement as parent" ); -is_object( $Braces->parent, $Statement, "Braces sees statement as parent" ); -is_object( $Token7->parent, $Statement, "Token sees statement as parent" ); +is_object( $Statement->parent, $Document, "Statement sees document as parent" ); +is_object( $Token1->parent, $Statement, "Token sees statement as parent" ); +is_object( $Token2->parent, $Statement, "Token sees statement as parent" ); +is_object( $Token3->parent, $Statement, "Token sees statement as parent" ); +is_object( $Braces->parent, $Statement, "Braces sees statement as parent" ); +is_object( $Token7->parent, $Statement, "Token sees statement as parent" ); # Test the special case of parents for the Braces opening and closing braces -is_object( $Braces->start->parent, $Braces, "Start brace sees the PPI::Structure as its parent" ); -is_object( $Braces->finish->parent, $Braces, "Finish brace sees the PPI::Structure as its parent" ); +is_object( $Braces->start->parent, + $Braces, "Start brace sees the PPI::Structure as its parent" ); +is_object( $Braces->finish->parent, + $Braces, "Finish brace sees the PPI::Structure as its parent" ); # Test the ->top method is_object( $Document->top, $Document, "Document sees itself as top" ); @@ -214,20 +225,24 @@ is_object( $Braces->top, $Document, "Braces sees document as top" ); is_object( $Token7->top, $Document, "Token sees document as top" ); # Test the ->document method -is_object( $Document->document, $Document, "Document sees itself as document" ); -is_object( $Statement->document, $Document, "Statement sees document correctly" ); -is_object( $Token1->document, $Document, "Token sees document correctly" ); -is_object( $Token2->document, $Document, "Token sees document correctly" ); -is_object( $Token3->document, $Document, "Token sees document correctly" ); -is_object( $Braces->document, $Document, "Braces sees document correctly" ); -is_object( $Token7->document, $Document, "Token sees document correctly" ); +is_object( $Document->document, $Document, "Document sees itself as document" ); +is_object( $Statement->document, $Document, + "Statement sees document correctly" ); +is_object( $Token1->document, $Document, "Token sees document correctly" ); +is_object( $Token2->document, $Document, "Token sees document correctly" ); +is_object( $Token3->document, $Document, "Token sees document correctly" ); +is_object( $Braces->document, $Document, "Braces sees document correctly" ); +is_object( $Token7->document, $Document, "Token sees document correctly" ); # Test the ->next_sibling method -is( $Document->next_sibling, '', "Document returns false for next_sibling" ); +is( $Document->next_sibling, '', "Document returns false for next_sibling" ); is( $Statement->next_sibling, '', "Statement returns false for next_sibling" ); -is_object( $Token1->next_sibling, $Token2, "First token sees second token as next_sibling" ); -is_object( $Token2->next_sibling, $Token3, "Second token sees third token as next_sibling" ); -is_object( $Braces->next_sibling, $Token7, "Braces sees seventh token as next_sibling" ); +is_object( $Token1->next_sibling, $Token2, + "First token sees second token as next_sibling" ); +is_object( $Token2->next_sibling, $Token3, + "Second token sees third token as next_sibling" ); +is_object( $Braces->next_sibling, $Token7, + "Braces sees seventh token as next_sibling" ); is( $Token7->next_sibling, '', 'Last token returns false for next_sibling' ); # More extensive test for next_sibling @@ -236,59 +251,83 @@ SCOPE: { my $end = $doc->last_token; isa_ok( $end, 'PPI::Token::Structure' ); is( $end->content, '}', 'Got end token' ); - is( $end->next_sibling, '', '->next_sibling for an end closing brace returns false' ); - my $braces = $doc->find_first( sub { - $_[1]->isa('PPI::Structure') and $_[1]->braces eq '()' - } ); - isa_ok( $braces, 'PPI::Structure' ); + is( $end->next_sibling, '', + '->next_sibling for an end closing brace returns false' ); + my $braces = $doc->find_first( + sub { + $_[1]->isa('PPI::Structure') and $_[1]->braces eq '()'; + } + ); + isa_ok( $braces, 'PPI::Structure' ); isa_ok( $braces->next_token, 'PPI::Token::Structure' ); - is( $braces->next_token->content, ';', 'Got the correct next_token for structure' ); + is( $braces->next_token->content, + ';', 'Got the correct next_token for structure' ); } # Test the ->previous_sibling method -is( $Document->previous_sibling, '', "Document returns false for previous_sibling" ); -is( $Statement->previous_sibling, '', "Statement returns false for previous_sibling" ); -is( $Token1->previous_sibling, '', "First token returns false for previous_sibling" ); -is_object( $Token2->previous_sibling, $Token1, "Second token sees first token as previous_sibling" ); -is_object( $Token3->previous_sibling, $Token2, "Third token sees second token as previous_sibling" ); -is_object( $Token7->previous_sibling, $Braces, "Last token sees braces as previous_sibling" ); +is( $Document->previous_sibling, + '', "Document returns false for previous_sibling" ); +is( $Statement->previous_sibling, + '', "Statement returns false for previous_sibling" ); +is( $Token1->previous_sibling, '', + "First token returns false for previous_sibling" ); +is_object( $Token2->previous_sibling, $Token1, + "Second token sees first token as previous_sibling" ); +is_object( $Token3->previous_sibling, $Token2, + "Third token sees second token as previous_sibling" ); +is_object( $Token7->previous_sibling, $Braces, + "Last token sees braces as previous_sibling" ); # More extensive test for next_sibling SCOPE: { - my $doc = safe_new \"{ no strict; bar(); }"; + my $doc = safe_new \"{ no strict; bar(); }"; my $start = $doc->first_token; isa_ok( $start, 'PPI::Token::Structure' ); is( $start->content, '{', 'Got start token' ); - is( $start->previous_sibling, '', '->previous_sibling for a start opening brace returns false' ); - my $braces = $doc->find_first( sub { - $_[1]->isa('PPI::Structure') and $_[1]->braces eq '()' - } ); - isa_ok( $braces, 'PPI::Structure' ); + is( $start->previous_sibling, '', + '->previous_sibling for a start opening brace returns false' ); + my $braces = $doc->find_first( + sub { + $_[1]->isa('PPI::Structure') and $_[1]->braces eq '()'; + } + ); + isa_ok( $braces, 'PPI::Structure' ); isa_ok( $braces->previous_token, 'PPI::Token::Word' ); - is( $braces->previous_token->content, 'bar', 'Got the correct previous_token for structure' ); + is( $braces->previous_token->content, + 'bar', 'Got the correct previous_token for structure' ); } # Test the ->snext_sibling method my $Token4 = $Statement->{children}->[3]; is( $Document->snext_sibling, '', "Document returns false for snext_sibling" ); -is( $Statement->snext_sibling, '', "Statement returns false for snext_sibling" ); -is_object( $Token1->snext_sibling, $Token2, "First token sees second token as snext_sibling" ); -is_object( $Token2->snext_sibling, $Token4, "Second token sees third token as snext_sibling" ); -is_object( $Braces->snext_sibling, $Token7, "Braces sees seventh token as snext_sibling" ); +is( $Statement->snext_sibling, '', + "Statement returns false for snext_sibling" ); +is_object( $Token1->snext_sibling, $Token2, + "First token sees second token as snext_sibling" ); +is_object( $Token2->snext_sibling, $Token4, + "Second token sees third token as snext_sibling" ); +is_object( $Braces->snext_sibling, $Token7, + "Braces sees seventh token as snext_sibling" ); is( $Token7->snext_sibling, '', 'Last token returns false for snext_sibling' ); # Test the ->sprevious_sibling method -is( $Document->sprevious_sibling, '', "Document returns false for sprevious_sibling" ); -is( $Statement->sprevious_sibling, '', "Statement returns false for sprevious_sibling" ); -is( $Token1->sprevious_sibling, '', "First token returns false for sprevious_sibling" ); -is_object( $Token2->sprevious_sibling, $Token1, "Second token sees first token as sprevious_sibling" ); -is_object( $Token3->sprevious_sibling, $Token2, "Third token sees second token as sprevious_sibling" ); -is_object( $Token7->sprevious_sibling, $Braces, "Last token sees braces as sprevious_sibling" ); +is( $Document->sprevious_sibling, + '', "Document returns false for sprevious_sibling" ); +is( $Statement->sprevious_sibling, + '', "Statement returns false for sprevious_sibling" ); +is( $Token1->sprevious_sibling, + '', "First token returns false for sprevious_sibling" ); +is_object( $Token2->sprevious_sibling, + $Token1, "Second token sees first token as sprevious_sibling" ); +is_object( $Token3->sprevious_sibling, + $Token2, "Third token sees second token as sprevious_sibling" ); +is_object( $Token7->sprevious_sibling, + $Braces, "Last token sees braces as sprevious_sibling" ); # Test snext_sibling and sprevious_sibling cases when inside a parent block SCOPE: { my $cpan13454 = safe_new \'{ 1 }'; - my $num = $cpan13454->find_first('Token::Number'); + my $num = $cpan13454->find_first('Token::Number'); isa_ok( $num, 'PPI::Token::Number' ); my $prev = $num->sprevious_sibling; is( $prev, '', '->sprevious_sibling returns false' ); @@ -296,26 +335,23 @@ SCOPE: { is( $next, '', '->snext_sibling returns false' ); } - - - - ##################################################################### # Test the PPI::Element and PPI::Node analysis methods # Test the find method SCOPE: { - is( $Document->find('PPI::Token::End'), '', '->find returns false if nothing found' ); + is( $Document->find('PPI::Token::End'), + '', '->find returns false if nothing found' ); isa_ok( $Document->find('PPI::Structure')->[0], 'PPI::Structure' ); my $found = $Document->find('PPI::Token::Number'); ok( $found, 'Multiple find succeeded' ); - is( ref $found, 'ARRAY', '->find returned an array' ); + is( ref $found, 'ARRAY', '->find returned an array' ); is( scalar(@$found), 2, 'Multiple find returned expected number of items' ); # Test for the ability to shorten the names $found = $Document->find('Token::Number'); ok( $found, 'Multiple find succeeded' ); - is( ref $found, 'ARRAY', '->find returned an array' ); + is( ref $found, 'ARRAY', '->find returned an array' ); is( scalar(@$found), 2, 'Multiple find returned expected number of items' ); } @@ -325,13 +361,14 @@ SCOPE: { SCOPE: { local $^W = 0; is( $Document->find(undef), undef, '->find(undef) failed' ); - is( $Document->find([]), undef, '->find([]) failed' ); - is( $Document->find('Foo'), undef, '->find(BAD) failed' ); + is( $Document->find( [] ), undef, '->find([]) failed' ); + is( $Document->find('Foo'), undef, '->find(BAD) failed' ); } # Test the find_first method SCOPE: { - is( $Document->find_first('PPI::Token::End'), '', '->find_first returns false if nothing found' ); + is( $Document->find_first('PPI::Token::End'), + '', '->find_first returns false if nothing found' ); isa_ok( $Document->find_first('PPI::Structure'), 'PPI::Structure' ); my $found = $Document->find_first('PPI::Token::Number'); ok( $found, 'Multiple find_first succeeded' ); @@ -345,31 +382,37 @@ SCOPE: { # Test the find_any method SCOPE: { - is( $Document->find_any('PPI::Token::End'), '', '->find_any returns false if nothing found' ); - is( $Document->find_any('PPI::Structure'), 1, '->find_any returns true is something found' ); - is( $Document->find_any('PPI::Token::Number'), 1, '->find_any returns true for multiple find' ); - is( $Document->find_any('Token::Number'), 1, '->find_any returns true for shortened multiple find' ); + is( $Document->find_any('PPI::Token::End'), + '', '->find_any returns false if nothing found' ); + is( $Document->find_any('PPI::Structure'), + 1, '->find_any returns true is something found' ); + is( $Document->find_any('PPI::Token::Number'), + 1, '->find_any returns true for multiple find' ); + is( $Document->find_any('Token::Number'), + 1, '->find_any returns true for shortened multiple find' ); } # Test the contains method SCOPE: { - omethod_fails( $Document, 'contains', [ undef, '', 1, [], bless( {}, 'Foo') ] ); + omethod_fails( $Document, 'contains', + [ undef, '', 1, [], bless( {}, 'Foo' ) ] ); my $found = $Document->find('PPI::Element'); - is( ref $found, 'ARRAY', '(preparing for contains tests) ->find returned an array' ); - is( scalar(@$found), 15, '(preparing for contains tests) ->find returns correctly for all elements' ); - foreach my $Element ( @$found ) { - is( $Document->contains( $Element ), 1, 'Document contains ' . ref($Element) . ' known to be in it' ); + is( ref $found, 'ARRAY', + '(preparing for contains tests) ->find returned an array' ); + is( scalar(@$found), 15, +'(preparing for contains tests) ->find returns correctly for all elements' + ); + foreach my $Element (@$found) { + is( $Document->contains($Element), + 1, 'Document contains ' . ref($Element) . ' known to be in it' ); } shift @$found; - foreach my $Element ( @$found ) { - is( $Document->contains( $Element ), 1, 'Statement contains ' . ref($Element) . ' known to be in it' ); + foreach my $Element (@$found) { + is( $Document->contains($Element), + 1, 'Statement contains ' . ref($Element) . ' known to be in it' ); } } - - - - ##################################################################### # Test the PPI::Element manipulation methods @@ -377,41 +420,44 @@ SCOPE: { SCOPE: { my $Doc2 = $Document->clone; isa_ok( $Doc2->schild(0), 'PPI::Statement' ); - is_object( $Doc2->schild(0)->parent, $Doc2, 'Basic parent links stay intact after ->clone' ); - is_object( $Doc2->schild(0)->schild(3)->start->document, $Doc2, - 'Clone goes deep, and Structure braces get relinked properly' ); + is_object( $Doc2->schild(0)->parent, + $Doc2, 'Basic parent links stay intact after ->clone' ); + is_object( $Doc2->schild(0)->schild(3)->start->document, + $Doc2, 'Clone goes deep, and Structure braces get relinked properly' ); isnt( refaddr($Document), refaddr($Doc2), 'Cloned Document has a different memory location' ); - isnt( refaddr($Document->schild(0)), refaddr($Doc2->schild(0)), - 'Cloned Document has children at different memory locations' ); + isnt( + refaddr( $Document->schild(0) ), + refaddr( $Doc2->schild(0) ), + 'Cloned Document has children at different memory locations' + ); } # Delete the second token ok( $Token2->delete, "Deletion of token 2 returns true" ); is( $Document->content, 'my = (1, 2);', "Content is modified correctly" ); -is( scalar($Document->tokens), 11, "Modified source contains the correct number of tokens" ); -ok( ! defined $Token2->parent, "Token 2 is detached from parent" ); +is( scalar( $Document->tokens ), + 11, "Modified source contains the correct number of tokens" ); +ok( !defined $Token2->parent, "Token 2 is detached from parent" ); # Delete the braces ok( $Braces->delete, "Deletion of braces returns true" ); is( $Document->content, 'my = ;', "Content is modified correctly" ); -is( scalar($Document->tokens), 5, "Modified source contains the correct number of tokens" ); -ok( ! defined $Braces->parent, "Braces are detached from parent" ); - - - - +is( scalar( $Document->tokens ), + 5, "Modified source contains the correct number of tokens" ); +ok( !defined $Braces->parent, "Braces are detached from parent" ); ##################################################################### # Test DESTROY # Start with DESTROY for an element that never has a parent SCOPE: { - my $Token = PPI::Token::Whitespace->new( ' ' ); - my $k1 = scalar keys %_PARENT; + my $Token = PPI::Token::Whitespace->new(' '); + my $k1 = scalar keys %_PARENT; $Token->DESTROY; my $k2 = scalar keys %_PARENT; - is( $k1, $k2, '_PARENT key count remains unchanged after naked Element DESTROY' ); + is( $k1, $k2, + '_PARENT key count remains unchanged after naked Element DESTROY' ); } # Next, a single element within a parent @@ -419,10 +465,10 @@ SCOPE: { my $k1 = scalar keys %_PARENT; my $k2; my $k3; - SCOPE: { - my $Token = PPI::Token::Number->new( '1' ); + SCOPE: { + my $Token = PPI::Token::Number->new('1'); my $Statement = PPI::Statement->new; - $Statement->add_element( $Token ); + $Statement->add_element($Token); $k2 = scalar keys %_PARENT; is( $k2, $k1 + 1, 'PARENT keys increases after adding element' ); $Statement->DESTROY; @@ -437,15 +483,17 @@ SCOPE: { my $k1 = scalar keys %_PARENT; my $k2; my $k3; - SCOPE: { + SCOPE: { my $NodeDocument = safe_new $INC{"PPI/Node.pm"}; $k2 = scalar keys %_PARENT; - ok( $k2 > ($k1 + 3000), 'PARENT keys increases after loading document' ); + ok( $k2 > ( $k1 + 3000 ), + 'PARENT keys increases after loading document' ); $NodeDocument->DESTROY; } pause(); $k3 = scalar keys %_PARENT; - is( $k3, $k1, 'PARENT keys returns to original on explicit Document DESTROY' ); + is( $k3, $k1, + 'PARENT keys returns to original on explicit Document DESTROY' ); } # Repeat again, but with an implicit DESTROY @@ -453,26 +501,24 @@ SCOPE: { my $k1 = scalar keys %_PARENT; my $k2; my $k3; - SCOPE: { + SCOPE: { my $NodeDocument = safe_new $INC{"PPI/Node.pm"}; $k2 = scalar keys %_PARENT; - ok( $k2 > ($k1 + 3000), 'PARENT keys increases after loading document' ); + ok( $k2 > ( $k1 + 3000 ), + 'PARENT keys increases after loading document' ); } pause(); $k3 = scalar keys %_PARENT; - is( $k3, $k1, 'PARENT keys returns to original on implicit Document DESTROY' ); + is( $k3, $k1, + 'PARENT keys returns to original on implicit Document DESTROY' ); } - - - - ##################################################################### # Token-related methods # Test first_token, last_token, next_token and previous_token SCOPE: { -my $code = <<'END_PERL'; + my $code = <<'END_PERL'; my $foo = bar(); sub foo { @@ -497,31 +543,41 @@ END_PERL # Test next_token is( $last_token->next_token, '', 'last->next_token returns false' ); - is( $doc->next_token, '', 'doc->next_token returns false' ); + is( $doc->next_token, '', 'doc->next_token returns false' ); my $next_token = $first_token->next_token; isa_ok( $next_token, 'PPI::Token::Whitespace' ); is( $next_token->content, ' ', 'Trivial ->next_token works as expected' ); my $counter = 1; my $token = $first_token; + while ( $token = $token->next_token ) { $counter++; } - is( $counter, scalar($doc->tokens), - '->next_token iterated the expected number of times for a sample document' ); + is( + $counter, + scalar( $doc->tokens ), +'->next_token iterated the expected number of times for a sample document' + ); # Test previous_token - is( $first_token->previous_token, '', 'last->previous_token returns false' ); - is( $doc->previous_token, '', 'doc->previous_token returns false' ); + is( $first_token->previous_token, '', + 'last->previous_token returns false' ); + is( $doc->previous_token, '', 'doc->previous_token returns false' ); my $previous_token = $last_token->previous_token; isa_ok( $previous_token, 'PPI::Token::Whitespace' ); - is( $previous_token->content, "\n", 'Trivial ->previous_token works as expected' ); + is( $previous_token->content, "\n", + 'Trivial ->previous_token works as expected' ); $counter = 1; $token = $last_token; + while ( $token = $token->previous_token ) { $counter++; } - is( $counter, scalar($doc->tokens), - '->previous_token iterated the expected number of times for a sample document' ); + is( + $counter, + scalar( $doc->tokens ), +'->previous_token iterated the expected number of times for a sample document' + ); } ##################################################################### @@ -530,11 +586,11 @@ END_PERL # Make sure the 'use overload' is working on Element subclasses SCOPE: { - my $source = '1;'; - my $Document = PPI::Lexer->lex_source( $source ); - isa_ok( $Document, 'PPI::Document' ); - ok($Document eq $source, 'overload eq'); - ok($Document ne 'foo', 'overload ne'); - ok($Document == $Document, 'overload =='); - ok($Document != $Document->schild(0), 'overload !='); + my $source = '1;'; + my $Document = PPI::Lexer->lex_source($source); + isa_ok( $Document, 'PPI::Document' ); + ok( $Document eq $source, 'overload eq' ); + ok( $Document ne 'foo', 'overload ne' ); + ok( $Document == $Document, 'overload ==' ); + ok( $Document != $Document->schild(0), 'overload !=' ); } diff --git a/t/05_lexer.t b/t/05_lexer.t index 03f29e17..c7b5e325 100644 --- a/t/05_lexer.t +++ b/t/05_lexer.t @@ -5,10 +5,10 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 236 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 236 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use File::Spec::Functions qw( catdir ); -use PPI::Test::Run (); +use PPI::Test::Run (); ##################################################################### # Code/Dump Testing diff --git a/t/06_round_trip.t b/t/06_round_trip.t index 43b29359..12e6b0ae 100644 --- a/t/06_round_trip.t +++ b/t/06_round_trip.t @@ -5,61 +5,50 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More; # Plan comes later +use Test::More; # Plan comes later use File::Spec::Functions qw( catdir ); -use PPI (); -use PPI::Test qw( find_files ); +use PPI (); +use PPI::Test qw( find_files ); use Helper 'safe_new'; - - ##################################################################### # Prepare # Find all of the files to be checked -my %tests = map { $_ => $INC{$_} } grep { ! /\bXS\.pm/ } grep { /^PPI\b/ } keys %INC; +my %tests = + map { $_ => $INC{$_} } grep { !/\bXS\.pm/ } grep { /^PPI\b/ } keys %INC; my @files = sort values %tests; -unless ( @files ) { - Test::More::plan( tests => ($ENV{AUTHOR_TESTING} ? 1 : 0) + 1 ); +unless (@files) { + Test::More::plan( tests => ( $ENV{AUTHOR_TESTING} ? 1 : 0 ) + 1 ); ok( undef, "Failed to find any files to test" ); exit(); } # Find all the testable perl files in t/data foreach my $dir ( - '05_lexer', - '07_token', - '08_regression', - '11_util', - '13_data', - '15_transform' -) { + '05_lexer', '07_token', '08_regression', '11_util', + '13_data', '15_transform' + ) +{ my @perl = find_files( catdir( 't', 'data', $dir ) ); push @files, @perl; } # Add the test scripts themselves -push @files, find_files( 't' ); +push @files, find_files('t'); # Declare our plan -Test::More::plan( tests => ($ENV{AUTHOR_TESTING} ? 1 : 0) + scalar(@files) * 10 - 1 ); - - - - +Test::More::plan( + tests => ( $ENV{AUTHOR_TESTING} ? 1 : 0 ) + scalar(@files) * 10 - 1 ); ##################################################################### # Run the Tests -foreach my $file ( @files ) { - roundtrip_ok( $file ); +foreach my $file (@files) { + roundtrip_ok($file); } - - - - ##################################################################### # Test Functions @@ -68,7 +57,7 @@ sub roundtrip_ok { local *FILE; my $rv = open( FILE, '<', $file ); ok( $rv, "$file: Found file " ); - SKIP: { + SKIP: { skip "No file to test", 7 unless $rv; my $source = do { local $/ = undef; }; close FILE; @@ -76,25 +65,26 @@ sub roundtrip_ok { $source =~ s/(?:\015{1,2}\012|\015|\012)/\n/g; # Load the file as a Document - SKIP: { + SKIP: { skip( 'Ignoring 14_charset.t', 7 ) if $file =~ /14_charset/; my $Document = safe_new $file; ok( $Document, "$file: ->new returned true" ); # Serialize it back out, and compare with the raw version - skip( "Ignoring failed parse of $file", 5 ) unless defined $Document; + skip( "Ignoring failed parse of $file", 5 ) + unless defined $Document; my $content = $Document->serialize; ok( length($content), "$file: PPI::Document serializes" ); is( $content, $source, "$file: Round trip was successful" ); # Are there any unknown things? - is( $Document->find_any('Token::Unknown'), '', - "$file: Contains no PPI::Token::Unknown elements" ); - is( $Document->find_any('Structure::Unknown'), '', - "$file: Contains no PPI::Structure::Unknown elements" ); - is( $Document->find_any('Statement::Unknown'), '', - "$file: Contains no PPI::Statement::Unknown elements" ); + is( $Document->find_any('Token::Unknown'), + '', "$file: Contains no PPI::Token::Unknown elements" ); + is( $Document->find_any('Structure::Unknown'), + '', "$file: Contains no PPI::Structure::Unknown elements" ); + is( $Document->find_any('Statement::Unknown'), + '', "$file: Contains no PPI::Statement::Unknown elements" ); } - } + } } diff --git a/t/07_token.t b/t/07_token.t index 65e33c5a..ab56756d 100644 --- a/t/07_token.t +++ b/t/07_token.t @@ -3,30 +3,23 @@ # Formal unit tests for specific PPI::Token classes sub warns_on_misplaced_underscore { $] >= 5.006 and $] < 5.008 } -sub dies_on_incomplete_bx { $] >= 5.031002 } +sub dies_on_incomplete_bx { $] >= 5.031002 } -use if !(-e 'META.yml'), "Test::InDistDir"; +use if !( -e 'META.yml' ), "Test::InDistDir"; use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 594 + (warns_on_misplaced_underscore() ? 2 : 0 ) + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 594 + ( warns_on_misplaced_underscore() ? 2 : 0 ) + + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use File::Spec::Functions qw( catdir ); -use PPI (); -use PPI::Test::Run (); - - - - +use PPI (); +use PPI::Test::Run (); ##################################################################### # Code/Dump Testing PPI::Test::Run->run_testdir( catdir( 't', 'data', '07_token' ) ); - - - - ##################################################################### # PPI::Token::Number Unit Tests @@ -75,141 +68,163 @@ SCOPE: { '1.1.1.1.1.1' => 256, ); - while ( @examples ) { - my $code = shift @examples; - my $base = shift @examples; - if ( warns_on_misplaced_underscore() and ($code eq '1_0e1_0' or $code eq '1_0' or $code eq '1_0.') ) { - SKIP: { + while (@examples) { + my $code = shift @examples; + my $base = shift @examples; + if ( warns_on_misplaced_underscore() + and ( $code eq '1_0e1_0' or $code eq '1_0' or $code eq '1_0.' ) ) + { + SKIP: { skip( 'Ignoring known-bad cases on Perl 5.6.2', 5 ); } next; } - my $is_exp = $base =~ s/e//; + my $is_exp = $base =~ s/e//; my $is_float = $is_exp || $base =~ s/f//; - my $T = PPI::Tokenizer->new( \$code ); - my $token = $T->get_token; - is("$token", $code, "'$code' is a single token"); - is($token->base, $base, "base of '$code' is $base"); - is($token->isa('PPI::Token::Number::Float'), $is_float, - "'$code' ".($is_float ? "is" : "not")." ::Float"); - is($token->isa('PPI::Token::Number::Exp'), $is_exp, - "'$code' ".($is_float ? "is" : "not")." ::Exp"); + my $T = PPI::Tokenizer->new( \$code ); + my $token = $T->get_token; + is( "$token", $code, "'$code' is a single token" ); + is( $token->base, $base, "base of '$code' is $base" ); + is( $token->isa('PPI::Token::Number::Float'), + $is_float, "'$code' " . ( $is_float ? "is" : "not" ) . " ::Float" ); + is( $token->isa('PPI::Token::Number::Exp'), + $is_exp, "'$code' " . ( $is_float ? "is" : "not" ) . " ::Exp" ); next if $base == 256; $^W = 0; - my $underscore_incompatible = warns_on_misplaced_underscore() && $code =~ /^1_0[.]?$/; - my $incomplete_incompatible = dies_on_incomplete_bx() && $code =~ /^0[bx]$/; + my $underscore_incompatible = + warns_on_misplaced_underscore() && $code =~ /^1_0[.]?$/; + my $incomplete_incompatible = + dies_on_incomplete_bx() && $code =~ /^0[bx]$/; my $literal = eval $code; - my $err = $@; - $literal = undef if $underscore_incompatible || $incomplete_incompatible; + my $err = $@; + $literal = undef + if $underscore_incompatible || $incomplete_incompatible; warning_is { $literal = eval $code } "Misplaced _ in number", - "$] warns about misplaced underscore" - if $underscore_incompatible; - like($err, qr/No digits found for (binary|hexadecimal) literal/, - "$] dies on incomplete binary/hexadecimal literals") - if $underscore_incompatible; + "$] warns about misplaced underscore" + if $underscore_incompatible; + like( + $err, + qr/No digits found for (binary|hexadecimal) literal/, + "$] dies on incomplete binary/hexadecimal literals" + ) if $underscore_incompatible; no warnings qw{ uninitialized }; - cmp_ok($token->literal, '==', $err ? undef : $literal, - "literal('$code'), eval error: " . ($err || "none")); + cmp_ok( + $token->literal, '==', + $err ? undef : $literal, + "literal('$code'), eval error: " . ( $err || "none" ) + ); } } -for my $code ( '1.0._0' ) { +for my $code ('1.0._0') { my $token = PPI::Tokenizer->new( \$code )->get_token; - isnt("$token", $code, 'tokenize bad version'); + isnt( "$token", $code, 'tokenize bad version' ); } -for my $code ( '1.0.0.0_0' ) { +for my $code ('1.0.0.0_0') { my $token = PPI::Tokenizer->new( \$code )->get_token; - is("$token", $code, 'tokenize good version'); + is( "$token", $code, 'tokenize good version' ); } foreach my $code ( '08', '09', '0778', '0779' ) { - my $T = PPI::Tokenizer->new( \$code ); + my $T = PPI::Tokenizer->new( \$code ); my $token = $T->get_token; - isa_ok($token, 'PPI::Token::Number::Octal'); - is("$token", $code, "tokenize bad octal '$code'"); - ok($token->{_error} && $token->{_error} =~ m/octal/i, - 'invalid octal number should trigger parse error'); - is($token->literal, undef, "literal('$code') is undef"); + isa_ok( $token, 'PPI::Token::Number::Octal' ); + is( "$token", $code, "tokenize bad octal '$code'" ); + ok( + $token->{_error} && $token->{_error} =~ m/octal/i, + 'invalid octal number should trigger parse error' + ); + is( $token->literal, undef, "literal('$code') is undef" ); } BINARY: { my @tests = ( # Good binary numbers - { code => '0b0', error => 0, value => 0 }, - { code => '0b1', error => 0, value => 1 }, - { code => '0B1', error => 0, value => 1 }, - { code => '0b101', error => 0, value => 5 }, - { code => '0b1_1', error => 0, value => 3 }, - { code => '0b1__1', error => 0, value => 3 }, # perl warns, but parses it - { code => '0b1__1_', error => 0, value => 3 }, # perl warns, but parses it - # Bad binary numbers - { code => '0b2', error => 1, value => 0 }, - { code => '0B2', error => 1, value => 0 }, - { code => '0b012', error => 1, value => 0 }, - { code => '0B012', error => 1, value => 0 }, - { code => '0B0121', error => 1, value => 0 }, - ); - foreach my $test ( @tests ) { - my $code = $test->{code}; - my $T = PPI::Tokenizer->new( \$code ); + { code => '0b0', error => 0, value => 0 }, + { code => '0b1', error => 0, value => 1 }, + { code => '0B1', error => 0, value => 1 }, + { code => '0b101', error => 0, value => 5 }, + { code => '0b1_1', error => 0, value => 3 }, + { code => '0b1__1', error => 0, value => 3 } + , # perl warns, but parses it + { code => '0b1__1_', error => 0, value => 3 } + , # perl warns, but parses it + # Bad binary numbers + { code => '0b2', error => 1, value => 0 }, + { code => '0B2', error => 1, value => 0 }, + { code => '0b012', error => 1, value => 0 }, + { code => '0B012', error => 1, value => 0 }, + { code => '0B0121', error => 1, value => 0 }, + ); + foreach my $test (@tests) { + my $code = $test->{code}; + my $T = PPI::Tokenizer->new( \$code ); my $token = $T->get_token; - isa_ok($token, 'PPI::Token::Number::Binary'); - if ( $test->{error} ) { - ok($token->{_error} && $token->{_error} =~ m/binary/i, - 'invalid binary number should trigger parse error'); - is($token->literal, undef, "literal('$code') is undef"); - } - else { - ok(!$token->{_error}, "no error for '$code'"); - is($token->literal, $test->{value}, "literal('$code') is $test->{value}"); - } - is($token->content, $code, "parsed everything"); + isa_ok( $token, 'PPI::Token::Number::Binary' ); + if ( $test->{error} ) { + ok( + $token->{_error} && $token->{_error} =~ m/binary/i, + 'invalid binary number should trigger parse error' + ); + is( $token->literal, undef, "literal('$code') is undef" ); + } + else { + ok( !$token->{_error}, "no error for '$code'" ); + is( $token->literal, $test->{value}, + "literal('$code') is $test->{value}" ); + } + is( $token->content, $code, "parsed everything" ); } } HEX: { my @tests = ( # Good hex numbers--entire thing goes in the token - { code => '0x0', parsed => '0x0', value => 0 }, - { code => '0X1', parsed => '0X1', value => 1 }, - { code => '0x1', parsed => '0x1', value => 1 }, - { code => '0x_1', parsed => '0x_1', value => 1 }, - { code => '0x__1', parsed => '0x__1', value => 1 }, # perl warns, but parses it - { code => '0x__1_', parsed => '0x__1_', value => 1 }, # perl warns, but parses it - { code => '0X1', parsed => '0X1', value => 1 }, - { code => '0xc', parsed => '0xc', value => 12 }, - { code => '0Xc', parsed => '0Xc', value => 12 }, - { code => '0XC', parsed => '0XC', value => 12 }, - { code => '0xbeef', parsed => '0xbeef', value => 48879 }, - { code => '0XbeEf', parsed => '0XbeEf', value => 48879 }, - { code => '0x0e', parsed => '0x0e', value => 14 }, - { code => '0x00000e', parsed => '0x00000e', value => 14 }, - { code => '0x000_00e', parsed => '0x000_00e', value => 14 }, - { code => '0x000__00e', parsed => '0x000__00e', value => 14 }, # perl warns, but parses it - # Bad hex numbers--tokenizing stops when bad digit seen - { code => '0x', parsed => '0x', value => 0 }, - { code => '0X', parsed => '0X', value => 0 }, - { code => '0xg', parsed => '0x', value => 0 }, - { code => '0Xg', parsed => '0X', value => 0 }, - { code => '0XG', parsed => '0X', value => 0 }, - { code => '0x0g', parsed => '0x0', value => 0 }, - { code => '0X0g', parsed => '0X0', value => 0 }, - { code => '0X0G', parsed => '0X0', value => 0 }, - { code => '0x1g', parsed => '0x1', value => 1 }, - { code => '0x1g2', parsed => '0x1', value => 1 }, + { code => '0x0', parsed => '0x0', value => 0 }, + { code => '0X1', parsed => '0X1', value => 1 }, + { code => '0x1', parsed => '0x1', value => 1 }, + { code => '0x_1', parsed => '0x_1', value => 1 }, + { code => '0x__1', parsed => '0x__1', value => 1 } + , # perl warns, but parses it + { code => '0x__1_', parsed => '0x__1_', value => 1 } + , # perl warns, but parses it + { code => '0X1', parsed => '0X1', value => 1 }, + { code => '0xc', parsed => '0xc', value => 12 }, + { code => '0Xc', parsed => '0Xc', value => 12 }, + { code => '0XC', parsed => '0XC', value => 12 }, + { code => '0xbeef', parsed => '0xbeef', value => 48879 }, + { code => '0XbeEf', parsed => '0XbeEf', value => 48879 }, + { code => '0x0e', parsed => '0x0e', value => 14 }, + { code => '0x00000e', parsed => '0x00000e', value => 14 }, + { code => '0x000_00e', parsed => '0x000_00e', value => 14 }, + { code => '0x000__00e', parsed => '0x000__00e', value => 14 } + , # perl warns, but parses it + # Bad hex numbers--tokenizing stops when bad digit seen + { code => '0x', parsed => '0x', value => 0 }, + { code => '0X', parsed => '0X', value => 0 }, + { code => '0xg', parsed => '0x', value => 0 }, + { code => '0Xg', parsed => '0X', value => 0 }, + { code => '0XG', parsed => '0X', value => 0 }, + { code => '0x0g', parsed => '0x0', value => 0 }, + { code => '0X0g', parsed => '0X0', value => 0 }, + { code => '0X0G', parsed => '0X0', value => 0 }, + { code => '0x1g', parsed => '0x1', value => 1 }, + { code => '0x1g2', parsed => '0x1', value => 1 }, { code => '0x1_g', parsed => '0x1_', value => 1 }, ); - foreach my $test ( @tests ) { - my $code = $test->{code}; - my $T = PPI::Tokenizer->new( \$code ); + foreach my $test (@tests) { + my $code = $test->{code}; + my $T = PPI::Tokenizer->new( \$code ); my $token = $T->get_token; - isa_ok($token, 'PPI::Token::Number::Hex'); - ok(!$token->{_error}, "no error for '$code' even on invalid digits"); - is($token->content, $test->{parsed}, "correctly parsed everything expected"); - is($token->literal, $test->{value}, "literal('$code') is $test->{value}"); + isa_ok( $token, 'PPI::Token::Number::Hex' ); + ok( !$token->{_error}, "no error for '$code' even on invalid digits" ); + is( $token->content, $test->{parsed}, + "correctly parsed everything expected" ); + is( $token->literal, $test->{value}, + "literal('$code') is $test->{value}" ); } } @@ -219,13 +234,15 @@ OCTAL: { { code => '0O10', parsed => '0O10', value => 8 }, ); - foreach my $test ( @tests ) { - my $code = $test->{code}; - my $T = PPI::Tokenizer->new( \$code ); + foreach my $test (@tests) { + my $code = $test->{code}; + my $T = PPI::Tokenizer->new( \$code ); my $token = $T->get_token; - isa_ok($token, 'PPI::Token::Number::Octal'); - is($token->content, $test->{parsed}, "correctly parsed everything expected"); - is($token->literal, $test->{value}, "literal('$code') is $test->{value}"); + isa_ok( $token, 'PPI::Token::Number::Octal' ); + is( $token->content, $test->{parsed}, + "correctly parsed everything expected" ); + is( $token->literal, $test->{value}, + "literal('$code') is $test->{value}" ); } } diff --git a/t/08_regression.t b/t/08_regression.t index b6026afd..b5ec8162 100644 --- a/t/08_regression.t +++ b/t/08_regression.t @@ -4,52 +4,42 @@ # Some other regressions tests are included here for simplicity. -use if !(-e 'META.yml'), "Test::InDistDir"; +use if !( -e 'META.yml' ), "Test::InDistDir"; use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 1085 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 1085 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); -use PPI (); -use PPI::Test qw( pause ); -use PPI::Test::Run (); +use PPI (); +use PPI::Test qw( pause ); +use PPI::Test::Run (); use PPI::Singletons qw( %_PARENT ); use Helper 'safe_new'; - - ##################################################################### # Code/Dump Testing PPI::Test::Run->run_testdir(qw{ t data 08_regression }); - - - - ##################################################################### # Regression Test for rt.cpan.org #11522 # Check that objects created in a foreach don't leak circulars. foreach ( 1 .. 3 ) { pause(); - is( scalar(keys(%_PARENT)), 0, "No parent links at start of loop $_" ); + is( scalar( keys(%_PARENT) ), 0, "No parent links at start of loop $_" ); # Keep the document from going out of scope before the _PARENT test below. - my $Document = safe_new \q[print "Foo!"]; ## no critic ( Variables::ProhibitUnusedVarsStricter ) - is( scalar(keys(%_PARENT)), 4, 'Correct number of keys created' ); + my $Document = safe_new \q[print "Foo!"]; ## no critic ( Variables::ProhibitUnusedVarsStricter ) + is( scalar( keys(%_PARENT) ), 4, 'Correct number of keys created' ); } - - - - ##################################################################### -# A number of things picked up during exhaustive testing I want to +# A number of things picked up during exhaustive testing I want to # watch for regressions on # Create a document with a complete braced regexp SCOPE: { my $Document = safe_new \"s {foo} i"; - my $stmt = $Document->first_element; + my $stmt = $Document->first_element; isa_ok( $stmt, 'PPI::Statement' ); my $regexp = $stmt->first_element; isa_ok( $regexp, 'PPI::Token::Regexp::Substitute' ); @@ -62,24 +52,27 @@ SCOPE: { content => 's {foo} i', modifiers => { i => 1 }, operator => 's', - sections => [ { - position => 3, - size => 3, - type => '{}', - }, { - position => 9, - size => 3, - type => '<>', - } ], + sections => [ + { + position => 3, + size => 3, + type => '{}', + }, + { + position => 9, + size => 3, + type => '<>', + } + ], separator => undef, }; - is_deeply( { %$regexp }, $expected, 'Complex regexp matches expected' ); + is_deeply( {%$regexp}, $expected, 'Complex regexp matches expected' ); } # Also test the handling of a screwed up single part multi-regexp SCOPE: { my $Document = safe_new \"s {foo}_"; - my $stmt = $Document->first_element; + my $stmt = $Document->first_element; isa_ok( $stmt, 'PPI::Statement' ); my $regexp = $stmt->first_element; isa_ok( $regexp, 'PPI::Token::Regexp::Substitute' ); @@ -87,33 +80,37 @@ SCOPE: { # Check the internal details as before my $expected = { _sections => 2, - _error => "No second section of regexp, or does not start with a balanced character", + _error => +"No second section of regexp, or does not start with a balanced character", braced => 1, content => 's {foo}', modifiers => {}, operator => 's', - sections => [ { - position => 3, - size => 3, - type => '{}', - }, { - position => 7, - size => 0, - type => '', - } ], + sections => [ + { + position => 3, + size => 3, + type => '{}', + }, + { + position => 7, + size => 0, + type => '', + } + ], separator => undef, }; - is_deeply( { %$regexp }, $expected, 'Badly short regexp matches expected' ); + is_deeply( {%$regexp}, $expected, 'Badly short regexp matches expected' ); } # Encode an assumption that the value of a zero-length substr one char # after the end of the string returns ''. This assumption is used to make # the decision on the sections->[1]->{position} value being one char after # the end of the current string -is( substr('foo', 3, 0), '', 'substr one char after string end returns ""' ); +is( substr( 'foo', 3, 0 ), '', 'substr one char after string end returns ""' ); -# rt.cpan.org: Ticket #16671 $_ is not localized -# Apparently I DID fix the localisation during parsing, but I forgot to +# rt.cpan.org: Ticket #16671 $_ is not localized +# Apparently I DID fix the localisation during parsing, but I forgot to # localise in PPI::Node::DESTROY (ack). $_ = 1234; is( $_, 1234, 'Set $_ to 1234' ); @@ -122,23 +119,18 @@ SCOPE: { } is( $_, 1234, 'Remains after document creation and destruction' ); - - - - ##################################################################### # Bug 16815: location of Structure::List is not defined. SCOPE: { my $code = '@foo = (1,2)'; - my $doc = safe_new \$code; - ok( $doc->find_first('Structure::List')->location, '->location for a ::List returns true' ); + my $doc = safe_new \$code; + ok( + $doc->find_first('Structure::List')->location, + '->location for a ::List returns true' + ); } - - - - ##################################################################### # Bug 18413: PPI::Node prune() implementation broken @@ -162,10 +154,6 @@ END_PERL ok( defined $doc->prune('PPI::Statement::Sub'), '->prune ok' ); } - - - - ##################################################################### # Bug 19883: 'package' bareword used as hash key is detected as package statement @@ -175,10 +163,6 @@ SCOPE: { isa_ok( $doc->child(0)->child(0)->child(0), 'PPI::Statement::Expression' ); } - - - - ##################################################################### # Bug 19629: End of list mistakenly seen as end of statement @@ -197,71 +181,61 @@ SCOPE: { isa_ok( $doc->child(0), 'PPI::Statement' ); } - - - - ##################################################################### # Bug 21575: PPI::Statement::Variable::variables breaks for lists # with leading whitespace SCOPE: { - my $doc = safe_new \'my ( $self, $param ) = @_;'; + my $doc = safe_new \'my ( $self, $param ) = @_;'; my $stmt = $doc->child(0); isa_ok( $stmt, 'PPI::Statement::Variable' ); - is_deeply( [$stmt->variables], ['$self', '$param'], 'variables() for my list with whitespace' ); + is_deeply( + [ $stmt->variables ], + [ '$self', '$param' ], + 'variables() for my list with whitespace' + ); } - - - - ##################################################################### # Bug #23788: PPI::Statement::location() returns undef for C<({})>. SCOPE: { my $doc = safe_new \'({})'; - my $bad = $doc->find( sub { - not defined $_[1]->location - } ); + my $bad = $doc->find( + sub { + not defined $_[1]->location; + } + ); is( $bad, '', 'All elements return defined for ->location' ); } - - - - ##################################################################### # Chris Laco on users@perlcritic.tigris.org (sorry no direct URL...) # http://perlcritic.tigris.org/servlets/SummarizeList?listName=users # Empty constructor has no location SCOPE: { - my $doc = safe_new \'$h={};'; + my $doc = safe_new \'$h={};'; my $hash = $doc->find('PPI::Structure::Constructor')->[0]; - ok($hash, 'location for empty constructor - fetched a constructor'); - is_deeply( $hash->location, [1,4,4,1,undef], 'location for empty constructor'); + ok( $hash, 'location for empty constructor - fetched a constructor' ); + is_deeply( + $hash->location, + [ 1, 4, 4, 1, undef ], + 'location for empty constructor' + ); } - - - - ##################################################################### # Perl::MinimumVersion regression SCOPE: { - my $doc = safe_new \'use utf8;'; + my $doc = safe_new \'use utf8;'; my $stmt = $doc->child(0); isa_ok( $stmt, 'PPI::Statement::Include' ); is( $stmt->pragma, 'utf8', 'pragma() with numbers' ); } - - - - ##################################################################### # Proof that _new_token must return "1" @@ -271,91 +245,84 @@ $$content =~ s/(?:\015{1,2}\012|\015|\012)/\n/gs; END_PERL } - - - ###################################################################### # Check quoteengine token behaviour at end of file SCOPE: { - my $doc = safe_new \'s/'; + my $doc = safe_new \'s/'; my $regexp = $doc->child(0)->child(0); isa_ok( $regexp, 'PPI::Token::Regexp::Substitute' ); is( $regexp->_sections, 0, 'Found 0 section' ); } SCOPE: { - my $doc = safe_new \'s{'; + my $doc = safe_new \'s{'; my $regexp = $doc->child(0)->child(0); isa_ok( $regexp, 'PPI::Token::Regexp::Substitute' ); is( $regexp->_sections, 0, 'Found 0 section' ); } SCOPE: { - my $doc = safe_new \'s/foo'; + my $doc = safe_new \'s/foo'; my $regexp = $doc->child(0)->child(0); isa_ok( $regexp, 'PPI::Token::Regexp::Substitute' ); - is( $regexp->_sections, 1, 'Found 1 section' ); + is( $regexp->_sections, 1, 'Found 1 section' ); is( $regexp->_section_content(0), 'foo', 's/foo correct at EOL' ); } SCOPE: { - my $doc = safe_new \'s{foo'; + my $doc = safe_new \'s{foo'; my $regexp = $doc->child(0)->child(0); isa_ok( $regexp, 'PPI::Token::Regexp::Substitute' ); - is( $regexp->_sections, 1, 'Found 1 section' ); + is( $regexp->_sections, 1, 'Found 1 section' ); is( $regexp->_section_content(0), 'foo', 's{foo correct at EOL' ); } SCOPE: { - my $doc = safe_new \'s/foo/'; + my $doc = safe_new \'s/foo/'; my $regexp = $doc->child(0)->child(0); isa_ok( $regexp, 'PPI::Token::Regexp::Substitute' ); is( $regexp->_sections, 1, 'Found 1 section' ); } SCOPE: { - my $doc = safe_new \'s{foo}{'; + my $doc = safe_new \'s{foo}{'; my $regexp = $doc->child(0)->child(0); isa_ok( $regexp, 'PPI::Token::Regexp::Substitute' ); is( $regexp->_sections, 1, 'Found 1 section' ); } SCOPE: { - my $doc = safe_new \'s{foo}/'; + my $doc = safe_new \'s{foo}/'; my $regexp = $doc->child(0)->child(0); isa_ok( $regexp, 'PPI::Token::Regexp::Substitute' ); is( $regexp->_sections, 1, 'Found 1 section' ); } SCOPE: { - my $doc = safe_new \'s/foo/bar'; + my $doc = safe_new \'s/foo/bar'; my $regexp = $doc->child(0)->child(0); isa_ok( $regexp, 'PPI::Token::Regexp::Substitute' ); - is( $regexp->_sections, 2, 'Found 2 sections' ); + is( $regexp->_sections, 2, 'Found 2 sections' ); is( $regexp->_section_content(1), 'bar', 's/foo/bar correct at EOL' ); } SCOPE: { - my $doc = safe_new \'s{foo}{bar'; + my $doc = safe_new \'s{foo}{bar'; my $regexp = $doc->child(0)->child(0); isa_ok( $regexp, 'PPI::Token::Regexp::Substitute' ); - is( $regexp->_sections, 2, 'Found 2 sections' ); + is( $regexp->_sections, 2, 'Found 2 sections' ); is( $regexp->_section_content(1), 'bar', 's{foo}{bar correct at EOL' ); } SCOPE: { - my $doc = safe_new \'s{foo}/bar'; + my $doc = safe_new \'s{foo}/bar'; my $regexp = $doc->child(0)->child(0); isa_ok( $regexp, 'PPI::Token::Regexp::Substitute' ); - is( $regexp->_sections, 2, 'Found 2 sections' ); + is( $regexp->_sections, 2, 'Found 2 sections' ); is( $regexp->_section_content(1), 'bar', 's{foo}/bar correct at EOL' ); } - - - - ###################################################################### # Confirmation of cases where we special case / to a regex diff --git a/t/09_normal.t b/t/09_normal.t index c8070ce3..a6a551b2 100644 --- a/t/09_normal.t +++ b/t/09_normal.t @@ -5,16 +5,12 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 21 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 21 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); -use PPI (); +use PPI (); use PPI::Singletons qw( %LAYER ); use Helper 'safe_new'; - - - - ##################################################################### # Creation and Manipulation @@ -29,8 +25,6 @@ SCOPE: { ok( scalar(@$functions), '->functions returns at least 1 function' ); } - - ##################################################################### # Basic Empiric Tests @@ -40,14 +34,16 @@ SCOPE: { my $Document1 = safe_new \'my $foo = 1; # comment'; my $Document2 = safe_new \'my $foo=1 ;# different comment'; my $Document3 = safe_new \'sub foo { print "Hello World!\n"; }'; - my $Normal1 = $Document1->normalized; - my $Normal2 = $Document2->normalized; - my $Normal3 = $Document3->normalized; + my $Normal1 = $Document1->normalized; + my $Normal2 = $Document2->normalized; + my $Normal3 = $Document3->normalized; isa_ok( $Normal1, 'PPI::Document::Normalized' ); isa_ok( $Normal2, 'PPI::Document::Normalized' ); isa_ok( $Normal3, 'PPI::Document::Normalized' ); - is( $Normal1->equal( $Normal2 ), 1, '->equal returns true for equivalent code' ); - is( $Normal1->equal( $Normal3 ), '', '->equal returns false for different code' ); + is( $Normal1->equal($Normal2), + 1, '->equal returns true for equivalent code' ); + is( $Normal1->equal($Normal3), + '', '->equal returns false for different code' ); } NO_DOUBLE_REG: { @@ -55,6 +51,7 @@ NO_DOUBLE_REG: { ok( PPI::Normal->register( "main::just_a_test_sub", 2 ), "can add subs" ); is $LAYER{2}[-1], "main::just_a_test_sub", "and find subs at right layer"; my $size = @{ $LAYER{2} }; - ok( PPI::Normal->register( "main::just_a_test_sub", 2 ), "can add subs again" ); + ok( PPI::Normal->register( "main::just_a_test_sub", 2 ), + "can add subs again" ); is scalar @{ $LAYER{2} }, $size, "but sub isn't added twice"; } diff --git a/t/10_statement.t b/t/10_statement.t index 90cebca4..96003624 100644 --- a/t/10_statement.t +++ b/t/10_statement.t @@ -4,15 +4,11 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 7 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 7 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); use Helper 'safe_new'; - - - - ##################################################################### # Basic subroutine test @@ -21,10 +17,6 @@ SCOPE: { isa_ok( $doc->child(0), 'PPI::Statement::Sub' ); } - - - - ##################################################################### # Regression test, make sure utf8 is a pragma diff --git a/t/11_util.t b/t/11_util.t index ab8d36a4..3f6a8b4f 100644 --- a/t/11_util.t +++ b/t/11_util.t @@ -4,11 +4,11 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 11 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 11 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use File::Spec::Functions qw( catfile ); -use PPI (); -use PPI::Util qw( _Document _slurp ); +use PPI (); +use PPI::Util qw( _Document _slurp ); use Helper 'safe_new'; # Execute the tests @@ -18,7 +18,7 @@ my $slurpfile = catfile( 't', 'data', 'basic.pl' ); my $slurpcode = <<'END_FILE'; #!/usr/bin/perl -if ( 1 ) { +if (1) { print "Hello World!\n"; } @@ -26,9 +26,6 @@ if ( 1 ) { END_FILE - - - ##################################################################### # Test PPI::Util::_Document @@ -36,7 +33,7 @@ my $Document = safe_new \$testsource; # Good things foreach my $thing ( $testfile, \$testsource, $Document, [] ) { - isa_ok( _Document( $thing ), 'PPI::Document' ); + isa_ok( _Document($thing), 'PPI::Document' ); } # Bad things @@ -44,25 +41,18 @@ foreach my $thing ( $testfile, \$testsource, $Document, [] ) { # Evil things foreach my $thing ( {}, sub () { 1 } ) { - is( _Document( $thing ), undef, '_Document(evil) returns undef' ); + is( _Document($thing), undef, '_Document(evil) returns undef' ); } - - - ##################################################################### # Test PPI::Util::_slurp -my $source = _slurp( $slurpfile ); +my $source = _slurp($slurpfile); is_deeply( $source, \$slurpcode, '_slurp loads file as expected' ); - - - - ##################################################################### # Check the capability flags my $have_unicode = PPI::Util::HAVE_UNICODE(); ok( defined $have_unicode, 'HAVE_UNICODE defined' ); -is( $have_unicode, !! $have_unicode, 'HAVE_UNICODE is a boolean' ); +is( $have_unicode, !!$have_unicode, 'HAVE_UNICODE is a boolean' ); diff --git a/t/12_location.t b/t/12_location.t index fab64661..418c611c 100644 --- a/t/12_location.t +++ b/t/12_location.t @@ -4,12 +4,11 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 683 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 683 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); use Helper 'safe_new'; - my $test_source = <<'END_PERL'; my $foo = 'bar'; @@ -116,270 +115,272 @@ $o 1; END_PERL my @test_locations = ( - [ 1, 1, 1, 1, undef ], # my - [ 1, 3, 3, 1, undef ], # ' ' - [ 1, 4, 4, 1, undef ], # $foo - [ 1, 8, 8, 1, undef ], # ' ' - [ 1, 9, 9, 1, undef ], # = - [ 1, 10, 10, 1, undef ], # ' ' - [ 1, 11, 11, 1, undef ], # 'bar' - [ 1, 16, 16, 1, undef ], # ; - [ 1, 17, 17, 1, undef ], # \n - - [ 2, 1, 1, 2, undef ], # \n - - [ 3, 1, 1, 3, undef ], # # comment - - [ 4, 1, 1, 4, undef ], # sub - [ 4, 4, 4, 4, undef ], # ' ' - [ 4, 5, 5, 4, undef ], # foo - [ 4, 8, 8, 4, undef ], # ' ' - [ 4, 9, 9, 4, undef ], # { - [ 4, 10, 10, 4, undef ], # \n - - [ 5, 1, 1, 5, undef ], # ' ' - [ 5, 5, 5, 5, undef ], # my - [ 5, 7, 7, 5, undef ], # ' ' - [ 5, 8, 8, 5, undef ], # ( - [ 5, 9, 9, 5, undef ], # $this - [ 5, 14, 14, 5, undef ], # , - [ 5, 15, 15, 5, undef ], # ' ' - [ 5, 16, 16, 5, undef ], # $that - [ 5, 21, 21, 5, undef ], # ) - [ 5, 22, 22, 5, undef ], # ' ' - [ 5, 23, 23, 5, undef ], # = - [ 5, 24, 24, 5, undef ], # ' ' - [ 5, 25, 25, 5, undef ], # ( - [ 5, 26, 26, 5, undef ], # <<'THIS' - [ 5, 34, 34, 5, undef ], # , - [ 5, 35, 35, 5, undef ], # ' ' - [ 5, 36, 36, 5, undef ], # <<"THAT" - [ 5, 44, 44, 5, undef ], # ) - [ 5, 45, 45, 5, undef ], # ; - [ 5, 46, 46, 5, undef ], # \n - - [ 13, 1, 1, 13, undef ], # } - [ 13, 2, 2, 13, undef ], # \n - - [ 14, 1, 1, 14, undef ], # \n - - [ 15, 1, 1, 15, undef ], # sub - [ 15, 4, 4, 15, undef ], # ' ' - [ 15, 5, 5, 15, undef ], # baz - [ 15, 8, 8, 15, undef ], # ' ' - [ 15, 9, 9, 15, undef ], # { - [ 15, 10, 10, 15, undef ], # \n - - [ 16, 1, 1, 16, undef ], # tab# sub baz contains *tabs* - [ 17, 1, 1, 17, undef ], # tab - [ 17, 2, 5, 17, undef ], # my - [ 17, 4, 7, 17, undef ], # ' ' - [ 17, 5, 8, 17, undef ], # ( - [ 17, 6, 9, 17, undef ], # $one - [ 17, 10, 13, 17, undef ], # , - [ 17, 11, 14, 17, undef ], # ' ' - [ 17, 12, 15, 17, undef ], # $other - [ 17, 18, 21, 17, undef ], # ) - [ 17, 19, 22, 17, undef ], # ' ' - [ 17, 20, 23, 17, undef ], # = - [ 17, 21, 24, 17, undef ], # ' tab' - [ 17, 23, 29, 17, undef ], # ( - [ 17, 24, 30, 17, undef ], # "one" - [ 17, 29, 35, 17, undef ], # , - [ 17, 30, 36, 17, undef ], # tab - [ 17, 31, 37, 17, undef ], # "other" - [ 17, 38, 44, 17, undef ], # ) - [ 17, 39, 45, 17, undef ], # ; - [ 17, 40, 46, 17, undef ], # tab - [ 17, 41, 49, 17, undef ], # # contains 3 tabs - [ 17, 58, 66, 17, undef ], # \n - - [ 18, 1, 1, 18, undef ], # \n\t - - [ 19, 2, 5, 19, undef ], # foo - [ 19, 5, 8, 19, undef ], # ( - [ 19, 6, 9, 19, undef ], # ) - [ 19, 7, 10, 19, undef ], # tab - [ 19, 8, 13, 19, undef ], # ; - [ 19, 9, 14, 19, undef ], # \n - - [ 20, 1, 1, 20, undef ], # { - [ 20, 2, 2, 20, undef ], # \n - - [ 21, 1, 1, 21, undef ], # \n - - [ 22, 1, 1, 22, undef ], # sub - [ 22, 4, 4, 22, undef ], # ' ' - [ 22, 5, 5, 22, undef ], # bar - [ 22, 8, 8, 22, undef ], # ' ' - [ 22, 9, 9, 22, undef ], # { - [ 22, 10, 10, 22, undef ], # \n - - [ 23, 1, 1, 23, undef ], # ' ' - [ 23, 5, 5, 23, undef ], # baz - [ 23, 8, 8, 23, undef ], # ( - [ 23, 9, 9, 23, undef ], # ) - [ 23, 10, 10, 23, undef ], # ; - [ 23, 11, 11, 23, undef ], # \n - - [ 24, 1, 1, 24, undef ], # \n - - [ 25, 1, 1, 25, undef ], # #Note that there are leading 4 x space, ... - - [ 26, 1, 1, 26, undef ], # '\n ' - - [ 27, 5, 5, 27, undef ], # bas - [ 27, 8, 8, 27, undef ], # ( - [ 27, 9, 9, 27, undef ], # ) - [ 27, 10, 10, 27, undef ], # ; - [ 27, 11, 11, 27, undef ], # \n - - [ 28, 1, 1, 28, undef ], # } - [ 28, 2, 2, 28, undef ], # \n - - [ 29, 1, 1, 29, undef ], # \n - - [ 30, 1, 1, 30, undef ], # =head2 fluzz() ... - - [ 35, 1, 1, 35, undef ], # sub - [ 35, 4, 4, 35, undef ], # ' ' - [ 35, 5, 5, 35, undef ], # fluzz - [ 35, 10, 10, 35, undef ], # ' ' - [ 35, 11, 11, 35, undef ], # { - [ 35, 12, 12, 35, undef ], # \n - - [ 36, 1, 1, 36, undef ], # ' ' - [ 36, 5, 5, 36, undef ], # print - [ 36, 10, 10, 36, undef ], # ' ' - [ 36, 11, 11, 36, undef ], # "fluzz" - [ 36, 18, 18, 36, undef ], # ; - [ 36, 19, 19, 36, undef ], # # line 300 not_at_start_of_line - [ 36, 50, 50, 36, undef ], # \n - - [ 37, 1, 1, 37, undef ], # } - [ 37, 2, 2, 37, undef ], # \n - - [ 38, 1, 1, 38, undef ], # \n - - [ 39, 1, 1, 39, undef ], # #line 400 - - [ 40, 1, 1, 400, undef ], # $a - [ 40, 3, 3, 400, undef ], # \n - - [ 41, 1, 1, 401, undef ], # # line 500 - - [ 42, 1, 1, 500, undef ], # $b - [ 42, 3, 3, 500, undef ], # \n + [ 1, 1, 1, 1, undef ], # my + [ 1, 3, 3, 1, undef ], # ' ' + [ 1, 4, 4, 1, undef ], # $foo + [ 1, 8, 8, 1, undef ], # ' ' + [ 1, 9, 9, 1, undef ], # = + [ 1, 10, 10, 1, undef ], # ' ' + [ 1, 11, 11, 1, undef ], # 'bar' + [ 1, 16, 16, 1, undef ], # ; + [ 1, 17, 17, 1, undef ], # \n + + [ 2, 1, 1, 2, undef ], # \n + + [ 3, 1, 1, 3, undef ], # # comment + + [ 4, 1, 1, 4, undef ], # sub + [ 4, 4, 4, 4, undef ], # ' ' + [ 4, 5, 5, 4, undef ], # foo + [ 4, 8, 8, 4, undef ], # ' ' + [ 4, 9, 9, 4, undef ], # { + [ 4, 10, 10, 4, undef ], # \n + + [ 5, 1, 1, 5, undef ], # ' ' + [ 5, 5, 5, 5, undef ], # my + [ 5, 7, 7, 5, undef ], # ' ' + [ 5, 8, 8, 5, undef ], # ( + [ 5, 9, 9, 5, undef ], # $this + [ 5, 14, 14, 5, undef ], # , + [ 5, 15, 15, 5, undef ], # ' ' + [ 5, 16, 16, 5, undef ], # $that + [ 5, 21, 21, 5, undef ], # ) + [ 5, 22, 22, 5, undef ], # ' ' + [ 5, 23, 23, 5, undef ], # = + [ 5, 24, 24, 5, undef ], # ' ' + [ 5, 25, 25, 5, undef ], # ( + [ 5, 26, 26, 5, undef ], # <<'THIS' + [ 5, 34, 34, 5, undef ], # , + [ 5, 35, 35, 5, undef ], # ' ' + [ 5, 36, 36, 5, undef ], # <<"THAT" + [ 5, 44, 44, 5, undef ], # ) + [ 5, 45, 45, 5, undef ], # ; + [ 5, 46, 46, 5, undef ], # \n + + [ 13, 1, 1, 13, undef ], # } + [ 13, 2, 2, 13, undef ], # \n + + [ 14, 1, 1, 14, undef ], # \n + + [ 15, 1, 1, 15, undef ], # sub + [ 15, 4, 4, 15, undef ], # ' ' + [ 15, 5, 5, 15, undef ], # baz + [ 15, 8, 8, 15, undef ], # ' ' + [ 15, 9, 9, 15, undef ], # { + [ 15, 10, 10, 15, undef ], # \n + + [ 16, 1, 1, 16, undef ], # tab# sub baz contains *tabs* + [ 17, 1, 1, 17, undef ], # tab + [ 17, 2, 5, 17, undef ], # my + [ 17, 4, 7, 17, undef ], # ' ' + [ 17, 5, 8, 17, undef ], # ( + [ 17, 6, 9, 17, undef ], # $one + [ 17, 10, 13, 17, undef ], # , + [ 17, 11, 14, 17, undef ], # ' ' + [ 17, 12, 15, 17, undef ], # $other + [ 17, 18, 21, 17, undef ], # ) + [ 17, 19, 22, 17, undef ], # ' ' + [ 17, 20, 23, 17, undef ], # = + [ 17, 21, 24, 17, undef ], # ' tab' + [ 17, 23, 29, 17, undef ], # ( + [ 17, 24, 30, 17, undef ], # "one" + [ 17, 29, 35, 17, undef ], # , + [ 17, 30, 36, 17, undef ], # tab + [ 17, 31, 37, 17, undef ], # "other" + [ 17, 38, 44, 17, undef ], # ) + [ 17, 39, 45, 17, undef ], # ; + [ 17, 40, 46, 17, undef ], # tab + [ 17, 41, 49, 17, undef ], # # contains 3 tabs + [ 17, 58, 66, 17, undef ], # \n + + [ 18, 1, 1, 18, undef ], # \n\t + + [ 19, 2, 5, 19, undef ], # foo + [ 19, 5, 8, 19, undef ], # ( + [ 19, 6, 9, 19, undef ], # ) + [ 19, 7, 10, 19, undef ], # tab + [ 19, 8, 13, 19, undef ], # ; + [ 19, 9, 14, 19, undef ], # \n + + [ 20, 1, 1, 20, undef ], # { + [ 20, 2, 2, 20, undef ], # \n + + [ 21, 1, 1, 21, undef ], # \n + + [ 22, 1, 1, 22, undef ], # sub + [ 22, 4, 4, 22, undef ], # ' ' + [ 22, 5, 5, 22, undef ], # bar + [ 22, 8, 8, 22, undef ], # ' ' + [ 22, 9, 9, 22, undef ], # { + [ 22, 10, 10, 22, undef ], # \n + + [ 23, 1, 1, 23, undef ], # ' ' + [ 23, 5, 5, 23, undef ], # baz + [ 23, 8, 8, 23, undef ], # ( + [ 23, 9, 9, 23, undef ], # ) + [ 23, 10, 10, 23, undef ], # ; + [ 23, 11, 11, 23, undef ], # \n + + [ 24, 1, 1, 24, undef ], # \n + + [ 25, 1, 1, 25, undef ], # #Note that there are leading 4 x space, ... + + [ 26, 1, 1, 26, undef ], # '\n ' + + [ 27, 5, 5, 27, undef ], # bas + [ 27, 8, 8, 27, undef ], # ( + [ 27, 9, 9, 27, undef ], # ) + [ 27, 10, 10, 27, undef ], # ; + [ 27, 11, 11, 27, undef ], # \n + + [ 28, 1, 1, 28, undef ], # } + [ 28, 2, 2, 28, undef ], # \n + + [ 29, 1, 1, 29, undef ], # \n + + [ 30, 1, 1, 30, undef ], # =head2 fluzz() ... + + [ 35, 1, 1, 35, undef ], # sub + [ 35, 4, 4, 35, undef ], # ' ' + [ 35, 5, 5, 35, undef ], # fluzz + [ 35, 10, 10, 35, undef ], # ' ' + [ 35, 11, 11, 35, undef ], # { + [ 35, 12, 12, 35, undef ], # \n + + [ 36, 1, 1, 36, undef ], # ' ' + [ 36, 5, 5, 36, undef ], # print + [ 36, 10, 10, 36, undef ], # ' ' + [ 36, 11, 11, 36, undef ], # "fluzz" + [ 36, 18, 18, 36, undef ], # ; + [ 36, 19, 19, 36, undef ], # # line 300 not_at_start_of_line + [ 36, 50, 50, 36, undef ], # \n + + [ 37, 1, 1, 37, undef ], # } + [ 37, 2, 2, 37, undef ], # \n + + [ 38, 1, 1, 38, undef ], # \n + + [ 39, 1, 1, 39, undef ], # #line 400 + + [ 40, 1, 1, 400, undef ], # $a + [ 40, 3, 3, 400, undef ], # \n + + [ 41, 1, 1, 401, undef ], # # line 500 + + [ 42, 1, 1, 500, undef ], # $b + [ 42, 3, 3, 500, undef ], # \n # No space between "line" and number causes it to not work. - [ 43, 1, 1, 501, undef ], # #line600 + [ 43, 1, 1, 501, undef ], # #line600 - [ 44, 1, 1, 502, undef ], # $c - [ 44, 3, 3, 502, undef ], # \n + [ 44, 1, 1, 502, undef ], # $c + [ 44, 3, 3, 502, undef ], # \n - [ 45, 1, 1, 503, undef ], # #line 700 filename + [ 45, 1, 1, 503, undef ], # #line 700 filename - [ 46, 1, 1, 700, 'filename' ], # $d - [ 46, 3, 3, 700, 'filename' ], # \n + [ 46, 1, 1, 700, 'filename' ], # $d + [ 46, 3, 3, 700, 'filename' ], # \n - [ 47, 1, 1, 701, 'filename' ], # #line 800another-filename + [ 47, 1, 1, 701, 'filename' ], # #line 800another-filename - [ 48, 1, 1, 800, 'another-filename' ], # $e - [ 48, 3, 3, 800, 'another-filename' ], # \n + [ 48, 1, 1, 800, 'another-filename' ], # $e + [ 48, 3, 3, 800, 'another-filename' ], # \n - [ 49, 1, 1, 801, 'another-filename' ], # #line 900 yet-another-filename + [ 49, 1, 1, 801, 'another-filename' ], # #line 900 yet-another-filename - [ 50, 1, 1, 900, 'yet-another-filename' ], # $f - [ 50, 3, 3, 900, 'yet-another-filename' ], # \n + [ 50, 1, 1, 900, 'yet-another-filename' ], # $f + [ 50, 3, 3, 900, 'yet-another-filename' ], # \n - [ 51, 1, 1, 901, 'yet-another-filename' ], # #line 1000"quoted-filename" + [ 51, 1, 1, 901, 'yet-another-filename' ], # #line 1000"quoted-filename" - [ 52, 1, 1, 1000, 'quoted-filename' ], # $g - [ 52, 3, 3, 1000, 'quoted-filename' ], # \n + [ 52, 1, 1, 1000, 'quoted-filename' ], # $g + [ 52, 3, 3, 1000, 'quoted-filename' ], # \n - [ 53, 1, 1, 1001, 'quoted-filename' ], # \n + [ 53, 1, 1, 1001, 'quoted-filename' ], # \n - [ 54, 1, 1, 1002, 'quoted-filename' ], # =pod #line 1100 (not in column 1) + [ 54, 1, 1, 1002, 'quoted-filename' ], # =pod #line 1100 (not in column 1) - [ 59, 1, 1, 1007, 'quoted-filename' ], # $h - [ 59, 3, 3, 1007, 'quoted-filename' ], # \n + [ 59, 1, 1, 1007, 'quoted-filename' ], # $h + [ 59, 3, 3, 1007, 'quoted-filename' ], # \n - [ 60, 1, 1, 1008, 'quoted-filename' ], # =pod #line 1200 + [ 60, 1, 1, 1008, 'quoted-filename' ], # =pod #line 1200 - [ 65, 1, 1, 1202, 'quoted-filename' ], # $i - [ 65, 3, 3, 1202, 'quoted-filename' ], # \n + [ 65, 1, 1, 1202, 'quoted-filename' ], # $i + [ 65, 3, 3, 1202, 'quoted-filename' ], # \n - [ 66, 1, 1, 1203, 'quoted-filename' ], # =pod # line 1300 + [ 66, 1, 1, 1203, 'quoted-filename' ], # =pod # line 1300 - [ 71, 1, 1, 1302, 'quoted-filename' ], # $j - [ 71, 3, 3, 1302, 'quoted-filename' ], # \n + [ 71, 1, 1, 1302, 'quoted-filename' ], # $j + [ 71, 3, 3, 1302, 'quoted-filename' ], # \n # No space between "line" and number causes it to not work. - [ 72, 1, 1, 1303, 'quoted-filename' ], # =pod #line1400 + [ 72, 1, 1, 1303, 'quoted-filename' ], # =pod #line1400 - [ 77, 1, 1, 1308, 'quoted-filename' ], # $k - [ 77, 3, 3, 1308, 'quoted-filename' ], # \n + [ 77, 1, 1, 1308, 'quoted-filename' ], # $k + [ 77, 3, 3, 1308, 'quoted-filename' ], # \n - [ 78, 1, 1, 1309, 'quoted-filename' ], # =pod #line 1500 filename + [ 78, 1, 1, 1309, 'quoted-filename' ], # =pod #line 1500 filename - [ 83, 1, 1, 1502, 'filename' ], # $l - [ 83, 3, 3, 1502, 'filename' ], # \n + [ 83, 1, 1, 1502, 'filename' ], # $l + [ 83, 3, 3, 1502, 'filename' ], # \n - [ 84, 1, 1, 1503, 'filename' ], # =pod #line 1600another-filename + [ 84, 1, 1, 1503, 'filename' ], # =pod #line 1600another-filename - [ 89, 1, 1, 1602, 'another-filename' ], # $m - [ 89, 3, 3, 1602, 'another-filename' ], # \n + [ 89, 1, 1, 1602, 'another-filename' ], # $m + [ 89, 3, 3, 1602, 'another-filename' ], # \n - [ 90, 1, 1, 1603, 'another-filename' ], # =pod #line 1700 yet-another-filename + [ 90, 1, 1, 1603, 'another-filename' ] + , # =pod #line 1700 yet-another-filename - [ 95, 1, 1, 1702, 'yet-another-filename' ], # $n - [ 95, 3, 3, 1702, 'yet-another-filename' ], # \n + [ 95, 1, 1, 1702, 'yet-another-filename' ], # $n + [ 95, 3, 3, 1702, 'yet-another-filename' ], # \n - [ 96, 1, 1, 1703, 'yet-another-filename' ], # =pod #line 1800"quoted-filename" + [ 96, 1, 1, 1703, 'yet-another-filename' ] + , # =pod #line 1800"quoted-filename" - [ 101, 1, 1, 1802, 'quoted-filename' ], # $o - [ 101, 3, 3, 1802, 'quoted-filename' ], # \n + [ 101, 1, 1, 1802, 'quoted-filename' ], # $o + [ 101, 3, 3, 1802, 'quoted-filename' ], # \n - [ 102, 1, 1, 1803, 'quoted-filename' ], # \n + [ 102, 1, 1, 1803, 'quoted-filename' ], # \n - [ 103, 1, 1, 1804, 'quoted-filename' ], # 1 - [ 103, 2, 2, 1804, 'quoted-filename' ], # ; - [ 103, 3, 3, 1804, 'quoted-filename' ], # \n + [ 103, 1, 1, 1804, 'quoted-filename' ], # 1 + [ 103, 2, 2, 1804, 'quoted-filename' ], # ; + [ 103, 3, 3, 1804, 'quoted-filename' ], # \n ); - - ##################################################################### # Test the locations of everything in the test code # Prepare my $Document = safe_new \$test_source; $Document->tab_width(4); -is($Document->tab_width, 4, 'Tab width set correctly'); +is( $Document->tab_width, 4, 'Tab width set correctly' ); ok( $Document->index_locations, '->index_locations returns true' ); # Now check the locations of every token my @tokens = $Document->tokens; -is( scalar(@tokens), scalar(@test_locations), 'Number of tokens matches expected' ); +is( scalar(@tokens), scalar(@test_locations), + 'Number of tokens matches expected' ); foreach my $i ( 0 .. $#test_locations ) { my $location = $tokens[$i]->location; is( ref($location), 'ARRAY', "Token $i: ->location returns an ARRAY ref" ); - is( scalar(@$location), 5, "Token $i: ->location returns a 5 element ARRAY ref" ); + is( scalar(@$location), 5, + "Token $i: ->location returns a 5 element ARRAY ref" ); ok( ( - $location->[0] > 0 - and $location->[1] > 0 - and $location->[2] > 0 - and $location->[3] > 0 + $location->[0] > 0 + and $location->[1] > 0 + and $location->[2] > 0 + and $location->[3] > 0 ), "Token $i: ->location returns four positive positions" ); is_deeply( $tokens[$i]->location, - $test_locations[$i], - "Token $i: ->location matches expected", + $test_locations[$i], "Token $i: ->location matches expected", ); } ok( $Document->flush_locations, '->flush_locations returns true' ); -is( scalar(grep { defined $_->{_location} } $Document->tokens), 0, 'All _location attributes removed' ); +is( scalar( grep { defined $_->{_location} } $Document->tokens ), + 0, 'All _location attributes removed' ); diff --git a/t/13_data.t b/t/13_data.t index bd07b20c..6a2c2938 100755 --- a/t/13_data.t +++ b/t/13_data.t @@ -4,20 +4,19 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 8 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 8 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use File::Spec::Functions qw( catfile ); -use PPI (); +use PPI (); use Helper 'safe_new'; - -my $module = catfile('t', 'data', '13_data', 'Foo.pm'); +my $module = catfile( 't', 'data', '13_data', 'Foo.pm' ); ok( -f $module, 'Test file exists' ); my $Document = safe_new $module; # Get the data token -my $Token = $Document->find_first( 'Token::Data' ); +my $Token = $Document->find_first('Token::Data'); isa_ok( $Token, 'PPI::Token::Data' ); # Get the handle @@ -30,5 +29,8 @@ is( $line, "This is data\n", "Reading off a handle works as expected" ); # Print to the handle ok( $handle->print("Foo bar\n"), "handle->print returns ok" ); -is( $Token->content, "This is data\nFoo bar\nis\n", - "handle->print modifies the content as expected" ); +is( + $Token->content, + "This is data\nFoo bar\nis\n", + "handle->print modifies the content as expected" +); diff --git a/t/14_charsets.t b/t/14_charsets.t index 0df7d105..6168eb63 100644 --- a/t/14_charsets.t +++ b/t/14_charsets.t @@ -1,35 +1,32 @@ -#!/usr/bin/perl +#!/usr/bin/perl use lib 't/lib'; use PPI::Test::pragmas; use Test::More; + BEGIN { - if ($] < 5.008007) { + if ( $] < 5.008007 ) { Test::More->import( skip_all => "Unicode support requires perl 5.8.7" ); exit(0); } - plan( tests => 44 + ($ENV{AUTHOR_TESTING} ? 1 : 0) ); + plan( tests => 44 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ) ); } -use utf8; # perl version check above says this is okay +use utf8; # perl version check above says this is okay use Params::Util qw( _INSTANCE ); -use PPI (); +use PPI (); use Helper 'safe_new'; sub good_ok { my $source = shift; my $message = shift; - my $doc = safe_new \$source; - ok( _INSTANCE($doc, 'PPI::Document'), $message ); - if ( ! _INSTANCE($doc, 'PPI::Document') ) { + my $doc = safe_new \$source; + ok( _INSTANCE( $doc, 'PPI::Document' ), $message ); + if ( !_INSTANCE( $doc, 'PPI::Document' ) ) { diag($PPI::Document::errstr); } } - - - - ##################################################################### # Begin Tests @@ -51,15 +48,15 @@ SKIP: { # Testing accented characters in UTF-8 good_ok( 'sub func { }', "Parsed code without accented chars" ); - good_ok( 'rätselhaft();', "Function with umlaut" ); - good_ok( 'ätselhaft()', "Starting with umlaut" ); - good_ok( '"rätselhaft"', "In double quotes" ); - good_ok( "'rätselhaft'", "In single quotes" ); - good_ok( 'sub func { s/a/ä/g; }', "Regex with umlaut" ); - good_ok( 'sub func { $ä=1; }', "Variable with umlaut" ); - good_ok( '$一 = "壹";', "Variables with Chinese characters" ); - good_ok( '$a=1; # ä is an umlaut', "Comment with umlaut" ); - good_ok( <<'END_CODE', "POD with umlaut" ); + good_ok( 'rätselhaft();', "Function with umlaut" ); + good_ok( 'ätselhaft()', "Starting with umlaut" ); + good_ok( '"rätselhaft"', "In double quotes" ); + good_ok( "'rätselhaft'", "In single quotes" ); + good_ok( 'sub func { s/a/ä/g; }', "Regex with umlaut" ); + good_ok( 'sub func { $ä=1; }', "Variable with umlaut" ); + good_ok( '$一 = "壹";', "Variables with Chinese characters" ); + good_ok( '$a=1; # ä is an umlaut', "Comment with umlaut" ); + good_ok( <<'END_CODE', "POD with umlaut" ); sub func { } =pod @@ -69,13 +66,15 @@ sub func { } } END_CODE - ok(utf8::is_utf8('κλειδί'), "utf8 flag set on source string"); - good_ok( 'my %h = ( κλειδί => "Clé" );', "Hash with greek key in character string" ); + ok( utf8::is_utf8('κλειδί'), "utf8 flag set on source string" ); + good_ok( 'my %h = ( κλειδί => "Clé" );', + "Hash with greek key in character string" ); use Encode (); - my $bytes = Encode::encode('utf8', 'use utf8; my %h = ( κλειδί => "Clé" );'); - ok(!utf8::is_utf8($bytes), "utf8 flag not set on byte string"); + my $bytes = + Encode::encode( 'utf8', 'use utf8; my %h = ( κλειδί => "Clé" );' ); + ok( !utf8::is_utf8($bytes), "utf8 flag not set on byte string" ); { - local $TODO = "Fix CRASH"; - good_ok( $bytes, "Hash with greek key in bytes string" ); + local $TODO = "Fix CRASH"; + good_ok( $bytes, "Hash with greek key in bytes string" ); } } diff --git a/t/15_transform.t b/t/15_transform.t index a59dad8f..9513e652 100644 --- a/t/15_transform.t +++ b/t/15_transform.t @@ -3,13 +3,13 @@ use lib 't/lib'; use PPI::Test::pragmas; -use File::Copy qw( copy ); +use File::Copy qw( copy ); use File::Spec::Functions qw( catdir catfile ); -use File::Temp qw( tempdir ); -use PPI (); -use PPI::Transform (); -use Scalar::Util qw( refaddr ); -use Test::More 0.86 tests => 26 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use File::Temp qw( tempdir ); +use PPI (); +use PPI::Transform (); +use Scalar::Util qw( refaddr ); +use Test::More 0.86 tests => 26 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); ##################################################################### # Begin Tests @@ -19,7 +19,8 @@ APPLY: { my $rv = MyCleaner->apply( \$code ); ok( $rv, 'MyCleaner->apply( \$code ) returns true' ); - is( $code, 'my$foo="bar";', 'MyCleaner->apply( \$code ) modifies code as expected' ); + is( $code, 'my$foo="bar";', + 'MyCleaner->apply( \$code ) modifies code as expected' ); ok( PPI::Transform->register_apply_handler( 'Foo', \&Foo::get, \&Foo::set ), @@ -28,96 +29,87 @@ APPLY: { $Foo::VALUE = 'my $foo = "bar";'; my $Foo = Foo->new; isa_ok( $Foo, 'Foo' ); - ok( MyCleaner->apply( $Foo ), 'MyCleaner->apply( $Foo ) returns true' ); - is( $Foo::VALUE, 'my$foo="bar";', 'MyCleaner->apply( $Foo ) modifies code as expected' ); + ok( MyCleaner->apply($Foo), 'MyCleaner->apply( $Foo ) returns true' ); + is( $Foo::VALUE, 'my$foo="bar";', + 'MyCleaner->apply( $Foo ) modifies code as expected' ); } - - - - ##################################################################### # File transforms -my $testdir = catdir( 't', 'data', '15_transform'); +my $testdir = catdir( 't', 'data', '15_transform' ); # Does the test directory exist? -ok( (-e $testdir and -d $testdir and -r $testdir), "Test directory $testdir found" ); +ok( ( -e $testdir and -d $testdir and -r $testdir ), + "Test directory $testdir found" ); # Find the .pm test files opendir( TESTDIR, $testdir ) or die "opendir: $!"; my @files = sort grep { /\.pm$/ } readdir(TESTDIR); -closedir( TESTDIR ) or die "closedir: $!"; +closedir(TESTDIR) or die "closedir: $!"; ok( scalar @files, 'Found at least one .pm file' ); - - - - ##################################################################### # Testing -my $tempdir = tempdir(CLEANUP => 1); -foreach my $input ( @files ) { +my $tempdir = tempdir( CLEANUP => 1 ); +foreach my $input (@files) { # Prepare - my $copy = catfile($tempdir, "${input}_copy"); - my $copy2 = catfile($tempdir, "${input}_copy2"); + my $copy = catfile( $tempdir, "${input}_copy" ); + my $copy2 = catfile( $tempdir, "${input}_copy2" ); - $input = catfile($testdir, $input); + $input = catfile( $testdir, $input ); my $output = "${input}_out"; ok( copy( $input, $copy ), "Copied $input to $copy" ); - my $Original = new_ok( 'PPI::Document' => [ $input ] ); - my $Input = new_ok( 'PPI::Document' => [ $input ] ); - my $Output = new_ok( 'PPI::Document' => [ $output ] ); + my $Original = new_ok( 'PPI::Document' => [$input] ); + my $Input = new_ok( 'PPI::Document' => [$input] ); + my $Output = new_ok( 'PPI::Document' => [$output] ); # Process the file - my $rv = MyCleaner->document( $Input ); + my $rv = MyCleaner->document($Input); isa_ok( $rv, 'PPI::Document' ); is( refaddr($rv), refaddr($Input), '->document returns original document' ); is_deeply( $Input, $Output, 'Transform works as expected' ); # Squish to another location ok( MyCleaner->file( $copy, $copy2 ), '->file returned true' ); - my $Copy = new_ok( 'PPI::Document' => [ $copy ] ); - is_deeply( $Copy, $Original, 'targeted transform leaves original unchanged' ); - my $Copy2 = new_ok( 'PPI::Document' => [ $copy2 ] ); + my $Copy = new_ok( 'PPI::Document' => [$copy] ); + is_deeply( $Copy, $Original, + 'targeted transform leaves original unchanged' ); + my $Copy2 = new_ok( 'PPI::Document' => [$copy2] ); is_deeply( $Copy2, $Output, 'targeted transform works as expected' ); # Copy the file and process in-place - ok( MyCleaner->file( $copy ), '->file returned true' ); - $Copy = new_ok( 'PPI::Document' => [ $copy ] ); + ok( MyCleaner->file($copy), '->file returned true' ); + $Copy = new_ok( 'PPI::Document' => [$copy] ); is_deeply( $Copy, $Output, 'In-place transform works as expected' ); } - eval { PPI::Transform->document }; like $@, qr/PPI::Transform does not implement the required ->document method/, "transform classes need to implement ->document"; - - - - ##################################################################### # Support Code # Test Transform class package MyCleaner; -use Params::Util qw( _INSTANCE ); +use Params::Util qw( _INSTANCE ); use PPI::Transform (); our @ISA; + BEGIN { - @ISA = 'PPI::Transform'; # in a BEGIN block due to being an inline package + @ISA = 'PPI::Transform'; # in a BEGIN block due to being an inline package } sub document { my $self = shift; - my $Document = _INSTANCE(shift, 'PPI::Document') or return undef; - $Document->prune( 'Token::Whitespace' ); + my $Document = _INSTANCE( shift, 'PPI::Document' ) or return undef; + $Document->prune('Token::Whitespace'); $Document; } @@ -126,7 +118,7 @@ package Foo; use Helper 'safe_new'; sub new { - bless { }, 'Foo'; + bless {}, 'Foo'; } our $VALUE = ''; diff --git a/t/16_xml.t b/t/16_xml.t index 477150b5..51842f32 100644 --- a/t/16_xml.t +++ b/t/16_xml.t @@ -3,45 +3,37 @@ use lib 't/lib'; use PPI::Test::pragmas; use PPI::Document (); -use Test::More 0.86 tests => 16 + ($ENV{AUTHOR_TESTING} ? 1 : 0); - - - - - +use Test::More 0.86 tests => 16 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); ##################################################################### # Begin Tests -my $code = 'print "Hello World";'; +my $code = 'print "Hello World";'; my $document = new_ok( PPI::Document:: => [ \$code ] ); my @elements = $document->elements; push @elements, $elements[0]->elements; my @expected = ( - [ 'statement', {}, '' ], - [ 'token_word', {}, 'print' ], - [ 'token_whitespace', {}, ' ' ], + [ 'statement', {}, '' ], + [ 'token_word', {}, 'print' ], + [ 'token_whitespace', {}, ' ' ], [ 'token_quote_double', {}, '"Hello World"' ], - [ 'token_structure', {}, ';' ], - ); + [ 'token_structure', {}, ';' ], +); my $i = 0; -foreach my $expect ( @expected ) { +foreach my $expect (@expected) { is( $elements[$i]->_xml_name, - $expect->[0], - "Got _xml_name '$expect->[0]' as expected", + $expect->[0], "Got _xml_name '$expect->[0]' as expected", ); is_deeply( $elements[$i]->_xml_attr, - $expect->[1], - "Got _xml_attr as expected", + $expect->[1], "Got _xml_attr as expected", ); is( $elements[$i]->_xml_content, - $expect->[2], - "Got _xml_content '$expect->[2]' as expected", + $expect->[2], "Got _xml_content '$expect->[2]' as expected", ); $i++; } diff --git a/t/17_storable.t b/t/17_storable.t index 5e0516e4..cb700dba 100644 --- a/t/17_storable.t +++ b/t/17_storable.t @@ -5,25 +5,21 @@ use lib 't/lib'; use PPI::Test::pragmas; -use PPI (); +use PPI (); use Scalar::Util qw( refaddr ); use Test::More; BEGIN { # Is Storable installed? if ( eval { require Storable; 1 } ) { - plan( tests => 9 + ($ENV{AUTHOR_TESTING} ? 1 : 0) ); - } else { - plan( 'skip_all' ); + plan( tests => 9 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ) ); + } + else { + plan('skip_all'); exit(0); } } - - - - - ##################################################################### # Test freeze/thaw of PPI::Document objects @@ -37,19 +33,28 @@ package Foo; END_PERL Test::More::isa_ok( $Document, 'PPI::Document' ); { - my $isa = $Document->find_first(sub { $_[1] eq '@ISA'; }); - Test::More::ok( $isa, "Found ISA var"); - Test::More::is( $isa->parent, q|@ISA = (qw/File::Spec/);|, "Got parent ok"); + my $isa = $Document->find_first( sub { $_[1] eq '@ISA'; } ); + Test::More::ok( $isa, "Found ISA var" ); + Test::More::is( + $isa->parent, + q|@ISA = (qw/File::Spec/);|, + "Got parent ok" + ); } my $clone = Storable::dclone($Document); - Test::More::ok($clone, "dclone ok"); - Test::More::isnt( refaddr($Document), refaddr($clone), "Not the same object" ); - Test::More::is(ref($Document), ref($clone), "Same class"); + Test::More::ok( $clone, "dclone ok" ); + Test::More::isnt( refaddr($Document), refaddr($clone), + "Not the same object" ); + Test::More::is( ref($Document), ref($clone), "Same class" ); Test::More::is_deeply( $Document, $clone, "Deeply equal" ); { - my $isa = $clone->find_first(sub { $_[1] eq '@ISA'; }); - Test::More::ok($isa, "Found ISA var"); - Test::More::is($isa->parent, q|@ISA = (qw/File::Spec/);|, "Got parent ok"); # <-- this one fails + my $isa = $clone->find_first( sub { $_[1] eq '@ISA'; } ); + Test::More::ok( $isa, "Found ISA var" ); + Test::More::is( + $isa->parent, + q|@ISA = (qw/File::Spec/);|, + "Got parent ok" + ); # <-- this one fails } } diff --git a/t/18_cache.t b/t/18_cache.t index 76975c99..2cded2d6 100644 --- a/t/18_cache.t +++ b/t/18_cache.t @@ -4,101 +4,102 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 44 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 44 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use File::Spec::Functions qw( catfile ); -use File::Temp qw( tempdir ); -use Scalar::Util qw( refaddr ); -use PPI::Document (); -use PPI::Cache (); -use Test::SubCalls 1.07 (); +use File::Temp qw( tempdir ); +use Scalar::Util qw( refaddr ); +use PPI::Document (); +use PPI::Cache (); +use Test::SubCalls 1.07 (); -use constant VMS => !! ( $^O eq 'VMS' ); +use constant VMS => !!( $^O eq 'VMS' ); use constant FILE => VMS ? 'File::Spec::Unix' : 'File::Spec'; use Helper 'safe_new'; -my $this_file = FILE->catdir( 't', 'data', '03_document', 'test.dat' ); -my $cache_dir = tempdir(CLEANUP => 1); +my $this_file = FILE->catdir( 't', 'data', '03_document', 'test.dat' ); +my $cache_dir = tempdir( CLEANUP => 1 ); ok( -d $cache_dir, 'Verified the cache path exists' ); -ok( -w $cache_dir, 'Can write to the cache path' ); +ok( -w $cache_dir, 'Can write to the cache path' ); my $sample_document = \'print "Hello World!\n";'; - - - - ##################################################################### # Basic Testing # Create a basic cache object -my $Cache = PPI::Cache->new( - path => $cache_dir, - ); +my $Cache = PPI::Cache->new( path => $cache_dir, ); isa_ok( $Cache, 'PPI::Cache' ); -is( scalar($Cache->path), $cache_dir, '->path returns the original path' ); -is( scalar($Cache->readonly), '', '->readonly returns false by default' ); +is( scalar( $Cache->path ), $cache_dir, '->path returns the original path' ); +is( scalar( $Cache->readonly ), '', '->readonly returns false by default' ); # Create a test document -my $doc = safe_new $sample_document; -my $doc_md5 = '64568092e7faba16d99fa04706c46517'; -is( $doc->hex_id, $doc_md5, '->hex_id specifically matches the UNIX newline md5' ); -my $doc_file = catfile($cache_dir, '6', '64', '64568092e7faba16d99fa04706c46517.ppi'); -my $bad_md5 = 'abcdef1234567890abcdef1234567890'; -my $bad_file = catfile($cache_dir, 'a', 'ab', 'abcdef1234567890abcdef1234567890.ppi'); +my $doc = safe_new $sample_document; +my $doc_md5 = '64568092e7faba16d99fa04706c46517'; +is( $doc->hex_id, $doc_md5, + '->hex_id specifically matches the UNIX newline md5' ); +my $doc_file = + catfile( $cache_dir, '6', '64', '64568092e7faba16d99fa04706c46517.ppi' ); +my $bad_md5 = 'abcdef1234567890abcdef1234567890'; +my $bad_file = + catfile( $cache_dir, 'a', 'ab', 'abcdef1234567890abcdef1234567890.ppi' ); # Save to an arbitrary location -ok( $Cache->_store($bad_md5, $doc), '->_store returns true' ); -ok( -f $bad_file, 'Created file where expected' ); +ok( $Cache->_store( $bad_md5, $doc ), '->_store returns true' ); +ok( -f $bad_file, 'Created file where expected' ); my $loaded = $Cache->_load($bad_md5); isa_ok( $loaded, 'PPI::Document' ); is_deeply( $doc, $loaded, '->_load loads the same document back in' ); # Store the test document in the cache in its proper place -is( scalar( $Cache->store_document($doc) ), 1, - '->store_document(Document) returns true' ); +is( scalar( $Cache->store_document($doc) ), + 1, '->store_document(Document) returns true' ); ok( -f $doc_file, 'The document was stored in the expected location' ); # Check the _md5hex method -is( PPI::Cache->_md5hex($sample_document), $doc_md5, - '->_md5hex returns as expected for sample document' ); -is( PPI::Cache->_md5hex($doc_md5), $doc_md5, - '->_md5hex null transform works as expected' ); -is( $Cache->_md5hex($sample_document), $doc_md5, - '->_md5hex returns as expected for sample document' ); -is( $Cache->_md5hex($doc_md5), $doc_md5, - '->_md5hex null transform works as expected' ); +is( PPI::Cache->_md5hex($sample_document), + $doc_md5, '->_md5hex returns as expected for sample document' ); +is( PPI::Cache->_md5hex($doc_md5), + $doc_md5, '->_md5hex null transform works as expected' ); +is( $Cache->_md5hex($sample_document), + $doc_md5, '->_md5hex returns as expected for sample document' ); +is( $Cache->_md5hex($doc_md5), + $doc_md5, '->_md5hex null transform works as expected' ); # Retrieve the Document by content -$loaded = $Cache->get_document( $sample_document ); +$loaded = $Cache->get_document($sample_document); isa_ok( $loaded, 'PPI::Document' ); -is_deeply( $doc, $loaded, '->get_document(\$source) loads the same document back in' ); +is_deeply( $doc, $loaded, + '->get_document(\$source) loads the same document back in' ); # Retrieve the Document by md5 directly -$loaded = $Cache->get_document( $doc_md5 ); +$loaded = $Cache->get_document($doc_md5); isa_ok( $loaded, 'PPI::Document' ); -is_deeply( $doc, $loaded, '->get_document($md5hex) loads the same document back in' ); - - - - - +is_deeply( $doc, $loaded, + '->get_document($md5hex) loads the same document back in' ); ##################################################################### # Empiric Testing # Load a test document twice, and see how many tokenizer objects get # created internally. -is( PPI::Document->get_cache, undef, 'PPI::Document cache initially undef' ); -ok( PPI::Document->set_cache( $Cache ), 'PPI::Document->set_cache returned true' ); +is( PPI::Document->get_cache, undef, 'PPI::Document cache initially undef' ); +ok( PPI::Document->set_cache($Cache), + 'PPI::Document->set_cache returned true' ); isa_ok( PPI::Document->get_cache, 'PPI::Cache' ); -is( refaddr($Cache), refaddr(PPI::Document->get_cache), - '->get_cache returns the same cache object' ); +is( + refaddr($Cache), + refaddr( PPI::Document->get_cache ), + '->get_cache returns the same cache object' +); SCOPE: { # Set the tracking on the Tokenizer constructor - ok( Test::SubCalls::sub_track( 'PPI::Tokenizer::new' ), 'Tracking calls to PPI::Tokenizer::new' ); + ok( + Test::SubCalls::sub_track('PPI::Tokenizer::new'), + 'Tracking calls to PPI::Tokenizer::new' + ); Test::SubCalls::sub_calls( 'PPI::Tokenizer::new', 0 ); my $doc1 = safe_new $this_file; my $doc2 = safe_new $this_file; @@ -107,9 +108,12 @@ SCOPE: { skip( "Skipping due to previous failures", 3 ); } Test::SubCalls::sub_calls( 'PPI::Tokenizer::new', 1, - 'Two calls to PPI::Document->new results in one Tokenizer object creation' ); - ok( refaddr($doc1) != refaddr($doc2), - 'PPI::Document->new with cache enabled does NOT return the same object' ); +'Two calls to PPI::Document->new results in one Tokenizer object creation' + ); + ok( + refaddr($doc1) != refaddr($doc2), + 'PPI::Document->new with cache enabled does NOT return the same object' + ); is_deeply( $doc1, $doc2, 'PPI::Document->new with cache enabled returns two identical objects' ); } @@ -117,16 +121,19 @@ SCOPE: { SCOPE: { # Done now, can we clear the cache? is( PPI::Document->set_cache(undef), 1, '->set_cache(undef) returns true' ); - is( PPI::Document->get_cache, undef, '->get_cache returns undef' ); + is( PPI::Document->get_cache, undef, '->get_cache returns undef' ); # Next, test the import mechanism - is( eval "use PPI::Cache path => '$cache_dir'; 1", 1, 'use PPI::Cache path => ...; succeeded' ); + is( eval "use PPI::Cache path => '$cache_dir'; 1", + 1, 'use PPI::Cache path => ...; succeeded' ); isa_ok( PPI::Document->get_cache, 'PPI::Cache' ); - is( scalar(PPI::Document->get_cache->path), $cache_dir, '->path returns the original path' ); - is( scalar(PPI::Document->get_cache->readonly), '', '->readonly returns false by default' ); + is( scalar( PPI::Document->get_cache->path ), + $cache_dir, '->path returns the original path' ); + is( scalar( PPI::Document->get_cache->readonly ), + '', '->readonly returns false by default' ); # Does it still keep the previously cached documents - Test::SubCalls::sub_reset( 'PPI::Tokenizer::new' ); + Test::SubCalls::sub_reset('PPI::Tokenizer::new'); my $doc3 = safe_new $this_file; Test::SubCalls::sub_calls( 'PPI::Tokenizer::new', 0, 'Tokenizer was not created. Previous cache used ok' ); diff --git a/t/19_selftesting.t b/t/19_selftesting.t index 94492923..72259878 100644 --- a/t/19_selftesting.t +++ b/t/19_selftesting.t @@ -10,44 +10,41 @@ use PPI::Test::pragmas; use Class::Inspector 1.22 (); use File::Spec::Functions qw( catdir ); -use Params::Util qw( _CLASS _ARRAY _INSTANCE _IDENTIFIER ); -use PPI (); -use PPI::Test qw( find_files ); -use PPI::Test::Object (); ## no perlimports -use Test::More; # Plan comes later +use Params::Util qw( _CLASS _ARRAY _INSTANCE _IDENTIFIER ); +use PPI (); +use PPI::Test qw( find_files ); +use PPI::Test::Object (); ## no perlimports +use Test::More; # Plan comes later use Test::Object qw( object_ok ); use constant CI => Class::Inspector::; use Helper 'safe_new'; - - - ##################################################################### # Prepare # Find all of the files to be checked -my %tests = map { $_ => $INC{$_} } grep { ! /\bXS\.pm/ } grep { /^PPI\b/ } keys %INC; -unless ( %tests ) { - Test::More::plan( tests => 1 + ($ENV{AUTHOR_TESTING} ? 1 : 0) ); +my %tests = + map { $_ => $INC{$_} } grep { !/\bXS\.pm/ } grep { /^PPI\b/ } keys %INC; +unless (%tests) { + Test::More::plan( tests => 1 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ) ); ok( undef, "Failed to find any files to test" ); exit(); } my @files = sort values %tests; # Find all the testable perl files in t/data -foreach my $dir ( '05_lexer', '08_regression', '11_util', '13_data', '15_transform' ) { - my @perl = find_files( catdir('t', 'data', $dir) ); +foreach + my $dir ( '05_lexer', '08_regression', '11_util', '13_data', '15_transform' ) +{ + my @perl = find_files( catdir( 't', 'data', $dir ) ); push @files, @perl; } # Declare our plan -Test::More::plan( tests => scalar(@files) * 16 + 4 + ($ENV{AUTHOR_TESTING} ? 1 : 0) ); - - - - +Test::More::plan( + tests => scalar(@files) * 16 + 4 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ) ); ##################################################################### # Self-test the search functions before we use them @@ -66,24 +63,23 @@ END_PERL my $bad = $sample->find( \&bug_bad_isa_class_name ); ok( _ARRAY($bad), 'Found bad things' ); @$bad = map { $_->string } @$bad; -is_deeply( $bad, [ 'Bad::Class1', 'Bad::Class2', 'Bad::Class3', 'Bad::Class4' ], - 'Found all found known bad things' ); - - - - +is_deeply( + $bad, + [ 'Bad::Class1', 'Bad::Class2', 'Bad::Class3', 'Bad::Class4' ], + 'Found all found known bad things' +); ##################################################################### # Run the Tests -foreach my $file ( @files ) { +foreach my $file (@files) { # MD5 the raw file my $md5a = PPI::Util::md5hex_file($file); like( $md5a, qr/^[[:xdigit:]]{32}\z/, 'md5hex_file ok' ); # Load the file my $Document = safe_new $file; - ok( _INSTANCE($Document, 'PPI::Document'), "$file: Parsed ok" ); + ok( _INSTANCE( $Document, 'PPI::Document' ), "$file: Parsed ok" ); # Compare the preload signature to the post-load value my $md5b = $Document->hex_id; @@ -91,40 +87,35 @@ foreach my $file ( @files ) { # By this point, everything should have parsed properly at least # once, so no need to skip. - SCOPE: { + SCOPE: { my $rv = $Document->find( \&bug_bad_isa_class_name ); - if ( $rv ) { + if ($rv) { $Document->index_locations; - foreach ( @$rv ) { - print "# $file: Found bad class " - . $_->content - . "\n"; + foreach (@$rv) { + print "# $file: Found bad class " . $_->content . "\n"; } } is_deeply( $rv, '', "$file: All class names in ->isa calls exist" ); } - SCOPE: { + SCOPE: { my $rv = $Document->find( \&bad_static_method ); - if ( $rv ) { + if ($rv) { $Document->index_locations; - foreach ( @$rv ) { + foreach (@$rv) { my $c = $_->sprevious_sibling->content; my $m = $_->snext_sibling->content; my $l = $_->location; - print "# $file: Found bad call ${c}->${m} at line $l->[0], col $l->[1]\n"; + print +"# $file: Found bad call ${c}->${m} at line $l->[0], col $l->[1]\n"; } } is_deeply( $rv, '', "$file: All class names in static method calls" ); } # Test with Test::Object stuff - object_ok( $Document ); + object_ok($Document); } - - - - ##################################################################### # Test Functions @@ -132,11 +123,11 @@ foreach my $file ( @files ) { # ->isa calls. This has happened at least once, presumably because # PPI has a LOT of classes and it can get confusing. sub bug_bad_isa_class_name { - my ($Document, $Element) = @_; + my ( $Document, $Element ) = @_; # Find a quote containing a class name - $Element->isa('PPI::Token::Quote') or return ''; - _CLASS($Element->string) or return ''; + $Element->isa('PPI::Token::Quote') or return ''; + _CLASS( $Element->string ) or return ''; if ( $Element->string =~ /^(?:ARRAY|HASH|CODE|SCALAR|REF|GLOB)$/ ) { return ''; } @@ -146,17 +137,17 @@ sub bug_bad_isa_class_name { $Expression->isa('PPI::Statement::Expression') or return ''; $Element == $Expression->schild(-1) or return ''; - my $List = $Expression->parent or return ''; - $List->isa('PPI::Structure::List') or return ''; - $List->schildren == 1 or return ''; + my $List = $Expression->parent or return ''; + $List->isa('PPI::Structure::List') or return ''; + $List->schildren == 1 or return ''; # The list should be the params list for an isa call - my $Word = $List->sprevious_sibling or return ''; - $Word->isa('PPI::Token::Word') or return ''; - $Word->content =~ /^(?:UNIVERSAL::)?isa\z/s or return ''; + my $Word = $List->sprevious_sibling or return ''; + $Word->isa('PPI::Token::Word') or return ''; + $Word->content =~ /^(?:UNIVERSAL::)?isa\z/s or return ''; # Is the class real and loaded? - CI->loaded($Element->string) and return ''; + CI->loaded( $Element->string ) and return ''; # Looks like we found a class that doesn't exist in # an isa call. @@ -165,29 +156,29 @@ sub bug_bad_isa_class_name { # Check for the use of a method that doesn't exist sub bad_static_method { - my ($document, $element) = @_; + my ( $document, $element ) = @_; # Find a quote containing a class name - $element->isa('PPI::Token::Operator') or return ''; - $element->content eq '->' or return ''; + $element->isa('PPI::Token::Operator') or return ''; + $element->content eq '->' or return ''; # Check the method - my $method = $element->snext_sibling or return ''; - $method->isa('PPI::Token::Word') or return ''; - _IDENTIFIER($method->content) or return ''; + my $method = $element->snext_sibling or return ''; + $method->isa('PPI::Token::Word') or return ''; + _IDENTIFIER( $method->content ) or return ''; # Check the class my $class = $element->sprevious_sibling or return ''; $class->isa('PPI::Token::Word') or return ''; - _CLASS($class->content) or return ''; + _CLASS( $class->content ) or return ''; # It's usually a deep class $class = $class->content; $method = $method->content; - $class =~ /::/ or return ''; + $class =~ /::/ or return ''; # Check the method exists - $class->can($method) and return ''; + $class->can($method) and return ''; # special case IO::String as it will normally not be loaded, and the call # to it is also conditional. diff --git a/t/21_exhaustive.t b/t/21_exhaustive.t index c834bf84..ab8a78ca 100644 --- a/t/21_exhaustive.t +++ b/t/21_exhaustive.t @@ -4,23 +4,23 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More; # Plan comes later +use Test::More; # Plan comes later use Params::Util qw( _INSTANCE ); -use PPI (); -use PPI::Test qw( quotable ); +use PPI (); +use PPI::Test qw( quotable ); use Helper 'safe_new'; # When distributing, keep this in to verify the test script # is working correctly, but limit to 2 (maaaaybe 3) so we # don't slow the install process down too much. -my ( $MAX_CHARS, $ITERATIONS, $LENGTH ) = ( 2, 1000, 190 ); +my ( $MAX_CHARS, $ITERATIONS, $LENGTH ) = ( 2, 1000, 190 ); my @ALL_CHARS = ( qw{a b c f g m q r s t w x y z V W X 0 1 8 9}, - ';', '[', ']', '{', '}', '(', ')', '=', '?', '|', '+', '<', - '>', '.', '!', '~', '^', '*', '$', '@', '&', ':', '%', ',', + ';', '[', ']', '{', '}', '(', ')', '=', '?', '|', '+', '<', + '>', '.', '!', '~', '^', '*', '$', '@', '&', ':', '%', ',', '\\', '/', '_', ' ', "\n", "\t", '-', - "'", '"', '`', '#', # Comment out to make parsing more intense + "'", '"', '`', '#', # Comment out to make parsing more intense ); # Cases known to have failed in the past. @@ -46,16 +46,13 @@ my @FAILURES = ( '@::0', '@::1', '@:::', '&::0', '&::\'', '%:::', '%::\'', # More-specific single cases thrown up during the heavy testing - '$:::z', '*:::z', "\\\@::'9:!", "} mz}~<\nV", "( {8", ); -plan tests => (9722 + ($ENV{AUTHOR_TESTING} ? 1 : 0)); - - - +plan tests => ( 9722 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ) ); ##################################################################### # Code/Dump Testing @@ -64,21 +61,21 @@ my $last_index = scalar(@ALL_CHARS) - 1; LENGTHLOOP: foreach my $len ( 1 .. $MAX_CHARS ) { # Initialise the char array - my @chars = (0) x $len; + my @chars = (0) x $len; # The main test loop - my $failures = 0; # simulate subtests - CHARLOOP: - while ( 1 ) { + my $failures = 0; # simulate subtests + CHARLOOP: + while (1) { # Test the current set of chars my $code = join '', map { $ALL_CHARS[$_] } @chars; unless ( length($code) == $len ) { die "Failed sanity check. Error in the code generation mechanism"; } - $failures += 1 if !compare_code( $code ); + $failures += 1 if !compare_code($code); # Increment the last character - $chars[$len - 1]++; + $chars[ $len - 1 ]++; # Cascade the wrapping as needed foreach ( reverse( 0 .. $len - 1 ) ) { @@ -90,16 +87,12 @@ foreach my $len ( 1 .. $MAX_CHARS ) { # Carry to the previous char $chars[$_] = 0; - $chars[$_ - 1]++; + $chars[ $_ - 1 ]++; } } is( $failures, 0, "No tokenizer failures for all $len-length programs" ); } - - - - ##################################################################### # Test a series of random strings @@ -107,78 +100,67 @@ for ( 1 .. $ITERATIONS ) { # Generate a random string my $code = join( '', map { $ALL_CHARS[$_] } - map { int(rand($last_index) + 1) } - (1 .. $LENGTH) - ); + map { int( rand($last_index) + 1 ) } ( 1 .. $LENGTH ) ); ok( compare_code($code), "round trip successful" ); } - - - ##################################################################### # Test all the failures -foreach my $code ( @FAILURES ) { +foreach my $code (@FAILURES) { ok( compare_code($code), "round trip of old failure successful" ); } - exit(0); - - - - ##################################################################### # Support Functions sub compare_code { - my ( $code ) = @_; + my ($code) = @_; my $round_tripped = round_trip_code($code); - my $ok = ($code eq $round_tripped); + my $ok = ( $code eq $round_tripped ); if ( !$ok ) { my $code_quoted = quotable($code); - diag( qq{input: "$code_quoted"} ); + diag(qq{input: "$code_quoted"}); my $round_tripped_quoted = quotable($round_tripped); - diag( qq{output: "$round_tripped_quoted"} ); - my $shortest = quotable(quickcheck($code)); - diag( qq{shorted failing substring: "$shortest"} ); + diag(qq{output: "$round_tripped_quoted"}); + my $shortest = quotable( quickcheck($code) ); + diag(qq{shorted failing substring: "$shortest"}); } - if ( scalar(keys %PPI::Element::PARENT) != 0 ) { + if ( scalar( keys %PPI::Element::PARENT ) != 0 ) { $ok = 0; my $code_quoted = quotable($code); - diag( qq{ Stale \%PARENT entries at the end of testing of "$code_quoted"} ); + diag(qq{ Stale \%PARENT entries at the end of testing of "$code_quoted"} + ); } %PPI::Element::PARENT = %PPI::Element::PARENT; return $ok; } - sub round_trip_code { - my ( $code ) = @_; + my ($code) = @_; my $result; - my $Document = eval { - # use Carp 'croak'; $SIG{__WARN__} = sub { croak('Triggered a warning') }; + my $Document = eval { + # use Carp 'croak'; $SIG{__WARN__} = sub { croak('Triggered a warning') }; safe_new \$code; }; - if ( _INSTANCE($Document, 'PPI::Document') ) { + if ( _INSTANCE( $Document, 'PPI::Document' ) ) { $result = $Document->serialize; } return $result; } - # Find the shortest failing substring of known bad string sub quickcheck { - my $code = shift; - my $fails = $code; + my $code = shift; + my $fails = $code; # use Carp 'croak'; $SIG{__WARN__} = sub { croak('Triggered a warning') }; while ( length $fails ) { diff --git a/t/22_readonly.t b/t/22_readonly.t index a1945487..8892a3e2 100644 --- a/t/22_readonly.t +++ b/t/22_readonly.t @@ -4,26 +4,22 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 12 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 12 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI::Document (); use Helper 'safe_new'; - - - - ##################################################################### # Creating Documents SCOPE: { - # Blank document + # Blank document my $empty = safe_new; is( $empty->readonly, '', '->readonly is false for blank' ); # From source my $source = 'print "Hello World!\n"'; - my $doc1 = safe_new \$source; + my $doc1 = safe_new \$source; is( $doc1->readonly, '', '->readonly is false by default' ); # With explicit false diff --git a/t/23_file.t b/t/23_file.t index e27ad681..1ca91bf6 100644 --- a/t/23_file.t +++ b/t/23_file.t @@ -4,26 +4,22 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 4 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 4 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use File::Spec::Functions qw( catfile ); -use PPI::Document::File (); - - - - +use PPI::Document::File (); ##################################################################### # Creating Documents SCOPE: { # From a specific file - my $file = catfile('t', 'data', 'basic.pl'); + my $file = catfile( 't', 'data', 'basic.pl' ); ok( -f $file, 'Found test file' ); # Load from the file - my $doc = PPI::Document::File->new( $file ); + my $doc = PPI::Document::File->new($file); isa_ok( $doc, 'PPI::Document::File' ); - isa_ok( $doc, 'PPI::Document' ); + isa_ok( $doc, 'PPI::Document' ); is( $doc->filename, $file, '->filename ok' ); } diff --git a/t/24_v6.t b/t/24_v6.t index 5a72ec3f..264976ec 100644 --- a/t/24_v6.t +++ b/t/24_v6.t @@ -5,26 +5,29 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 10 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 10 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use File::Spec::Functions qw( catfile ); -use PPI (); +use PPI (); use Helper 'safe_new'; -foreach my $file ( qw{ +foreach my $file ( + qw{ Simple.pm Grammar.pm -} ) { + } + ) +{ my $path = catfile( qw{ t data 24_v6 }, $file ); ok( -f $path, "Found test file $file" ); my $doc = safe_new $path; # Find the first Perl6 include - my $include = $doc->find_first( 'PPI::Statement::Include::Perl6' ); + my $include = $doc->find_first('PPI::Statement::Include::Perl6'); isa_ok( $include, 'PPI::Statement::Include::Perl6' ); ok( - scalar($include->perl6), + scalar( $include->perl6 ), 'use v6 statement has a working ->perl6 method', ); } diff --git a/t/25_increment.t b/t/25_increment.t index 8942ff6a..3d9865d8 100644 --- a/t/25_increment.t +++ b/t/25_increment.t @@ -7,14 +7,10 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 9554 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 9554 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI::Test::Run (); - - - - ##################################################################### # Code/Dump Testing diff --git a/t/26_bom.t b/t/26_bom.t index 3fb1399d..db7bc6fb 100644 --- a/t/26_bom.t +++ b/t/26_bom.t @@ -2,14 +2,10 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 20 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 20 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI::Test::Run (); - - - - ##################################################################### # Code/Dump Testing diff --git a/t/27_complete.t b/t/27_complete.t index 4b355cd4..266c6207 100644 --- a/t/27_complete.t +++ b/t/27_complete.t @@ -4,35 +4,31 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More; # Plan comes later +use Test::More; # Plan comes later use File::Spec::Functions qw( catdir ); -use PPI (); -use PPI::Test qw( find_files ); +use PPI (); +use PPI::Test qw( find_files ); use Helper 'safe_new'; # This test uses a series of ordered files, containing test code. # The letter after the number acts as a boolean yes/no answer to # "Is this code complete" my @files = find_files( catdir( 't', 'data', '27_complete' ) ); -my $tests = (scalar(@files) * 3) + 1 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +my $tests = ( scalar(@files) * 3 ) + 1 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); plan( tests => $tests ); - - - - ##################################################################### # Resource Location ok( scalar(@files), 'Found at least one ->complete test file' ); -foreach my $file ( @files ) { +foreach my $file (@files) { # Load the document my $document = safe_new $file; # Test if complete or not - my $got = !! ($document->complete); - my $expected = !! ($file =~ /\d+y\w+\.code$/); - my $isnot = ($got == $expected) ? 'is' : 'is NOT'; + my $got = !!( $document->complete ); + my $expected = !!( $file =~ /\d+y\w+\.code$/ ); + my $isnot = ( $got == $expected ) ? 'is' : 'is NOT'; is( $got, $expected, "File $file $isnot complete" ); } diff --git a/t/28_foreach_qw.t b/t/28_foreach_qw.t index ec899f62..7efdb0f1 100644 --- a/t/28_foreach_qw.t +++ b/t/28_foreach_qw.t @@ -4,42 +4,38 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 15 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 15 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); #use File::Spec::Functions ':ALL'; use PPI (); use Helper 'safe_new'; - - - - ##################################################################### # Parse the canonical cases SCOPE: { - my $string = 'for qw{foo} {} foreach'; - my $document = safe_new \$string; + my $string = 'for qw{foo} {} foreach'; + my $document = safe_new \$string; my $statements = $document->find('Statement::Compound'); - is( scalar(@$statements), 2, 'Found 2 statements' ); + is( scalar(@$statements), 2, 'Found 2 statements' ); is( $statements->[0]->type, 'foreach', '->type ok' ); is( $statements->[1]->type, 'foreach', '->type ok' ); } SCOPE: { - my $string = 'foreach qw{foo} {} foreach'; - my $document = safe_new \$string; + my $string = 'foreach qw{foo} {} foreach'; + my $document = safe_new \$string; my $statements = $document->find('Statement::Compound'); - is( scalar(@$statements), 2, 'Found 2 statements' ); + is( scalar(@$statements), 2, 'Found 2 statements' ); is( $statements->[0]->type, 'foreach', '->type ok' ); is( $statements->[1]->type, 'foreach', '->type ok' ); } SCOPE: { - my $string = 'for my $foo qw{bar} {} foreach'; - my $document = safe_new \$string; + my $string = 'for my $foo qw{bar} {} foreach'; + my $document = safe_new \$string; my $statements = $document->find('Statement::Compound'); - is( scalar(@$statements), 2, 'Found 2 statements' ); + is( scalar(@$statements), 2, 'Found 2 statements' ); is( $statements->[0]->type, 'foreach', '->type ok' ); is( $statements->[1]->type, 'foreach', '->type ok' ); } diff --git a/t/29_logical_filename.t b/t/29_logical_filename.t index 168d93fc..43d28653 100644 --- a/t/29_logical_filename.t +++ b/t/29_logical_filename.t @@ -3,49 +3,50 @@ # Testing of PPI::Element->logical_filename use strict; + BEGIN { no warnings 'once'; - $| = 1; + $| = 1; $PPI::XS_DISABLE = 1; $PPI::Lexer::X_TOKENIZER ||= $ENV{X_TOKENIZER}; } use File::Spec::Functions qw( catfile ); -use PPI::Document (); -use PPI::Document::File (); -use PPI::Util (); -use Test::More tests => 20 + 1; # Test::NoWarnings -use Test::NoWarnings; ## no perlimports +use PPI::Document (); +use PPI::Document::File (); +use PPI::Util (); +use Test::More tests => 20 + 1; # Test::NoWarnings +use Test::NoWarnings; ## no perlimports for my $class ( ( PPI::Document::, PPI::Document::File:: ) ) { ##################################################################### # Actual filename is used until #line directive - SCOPE: { - my $file = catfile('t', 'data', 'filename.pl'); + SCOPE: { + my $file = catfile( 't', 'data', 'filename.pl' ); ok( -f $file, "$class, test file" ); - my $doc = $class->new( $file ); - my $items = $doc->find( 'Token::Quote' ); - is( @$items + 0, 2, "$class, number of items" ); - is( $items->[ 0 ]->logical_filename, "$file", "$class, filename" ); - is( $items->[ 1 ]->logical_filename, "moo.pl", "$class, filename" ); + my $doc = $class->new($file); + my $items = $doc->find('Token::Quote'); + is( @$items + 0, 2, "$class, number of items" ); + is( $items->[0]->logical_filename, "$file", "$class, filename" ); + is( $items->[1]->logical_filename, "moo.pl", "$class, filename" ); } ##################################################################### # filename attribute overrides actual filename - SCOPE: { - my $file = catfile('t', 'data', 'filename.pl'); + SCOPE: { + my $file = catfile( 't', 'data', 'filename.pl' ); ok( -f $file, "$class, test file" ); - my $doc = $class->new( $file, filename => 'assa.pl' ); - my $items = $doc->find( 'Token::Quote' ); + my $doc = $class->new( $file, filename => 'assa.pl' ); + my $items = $doc->find('Token::Quote'); is( @$items + 0, 2, "$class, number of items" ); - my $str = $items->[ 0 ]; - is( $items->[ 0 ]->logical_filename, "assa.pl", "$class, filename" ); - is( $items->[ 1 ]->logical_filename, "moo.pl", "$class, filename" ); + my $str = $items->[0]; + is( $items->[0]->logical_filename, "assa.pl", "$class, filename" ); + is( $items->[1]->logical_filename, "moo.pl", "$class, filename" ); } } @@ -55,14 +56,14 @@ for my $class ( ( PPI::Document::, PPI::Document::File:: ) ) { SCOPE: { my $class = 'PPI::Document'; - my $file = catfile('t', 'data', 'filename.pl'); + my $file = catfile( 't', 'data', 'filename.pl' ); ok( -f $file, "$class, test file" ); - my $text = PPI::Util::_slurp( $file ); + my $text = PPI::Util::_slurp($file); - my $doc = $class->new( $text, filename => 'tadam.pl' ); - my $items = $doc->find( 'Token::Quote' ); + my $doc = $class->new( $text, filename => 'tadam.pl' ); + my $items = $doc->find('Token::Quote'); is( @$items + 0, 2, "$class, number of items" ); - my $str = $items->[ 0 ]; - is( $items->[ 0 ]->logical_filename, "tadam.pl", "$class, filename" ); - is( $items->[ 1 ]->logical_filename, "moo.pl", "$class, filename" ); + my $str = $items->[0]; + is( $items->[0]->logical_filename, "tadam.pl", "$class, filename" ); + is( $items->[1]->logical_filename, "moo.pl", "$class, filename" ); } diff --git a/t/data/15_transform/sample1.pm b/t/data/15_transform/sample1.pm index 27cc3f60..c29c3f3e 100644 --- a/t/data/15_transform/sample1.pm +++ b/t/data/15_transform/sample1.pm @@ -1,3 +1,2 @@ my $foo = 'bar'; - diff --git a/t/data/basic.pl b/t/data/basic.pl index 191bd1b4..00fb6bdc 100644 --- a/t/data/basic.pl +++ b/t/data/basic.pl @@ -1,6 +1,6 @@ #!/usr/bin/perl -if ( 1 ) { +if (1) { print "Hello World!\n"; } diff --git a/t/data/filename.pl b/t/data/filename.pl index 48dfcea4..ddce2b38 100644 --- a/t/data/filename.pl +++ b/t/data/filename.pl @@ -1,6 +1,6 @@ #!/usr/bin/perl -if ( 1 ) { +if (1) { print "Hello World!\n"; } diff --git a/t/feature_tracking.t b/t/feature_tracking.t index 009cc60b..9c5424a5 100644 --- a/t/feature_tracking.t +++ b/t/feature_tracking.t @@ -415,8 +415,10 @@ END_PERL "simple custom boilerplate modules"; } - -ok( PPI::Tokenizer->new( \"d()" )->all_tokens, "bare tokenizer auto-vivifies document object" ); +ok( + PPI::Tokenizer->new( \"d()" )->all_tokens, + "bare tokenizer auto-vivifies document object" +); ### TODO from ppi_token_unknown.t , deduplicate diff --git a/t/interactive.t b/t/interactive.t index b4c4a610..44bcc227 100644 --- a/t/interactive.t +++ b/t/interactive.t @@ -6,22 +6,17 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 3 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 3 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); use Helper 'safe_new'; - # Define the test code my $code = 'sub f:f('; - - - - ##################################################################### # Run the actual tests my $document = eval { safe_new \$code }; -$DB::single = $DB::single = 1 if $@; # Catch exceptions +$DB::single = $DB::single = 1 if $@; # Catch exceptions is( $@, '', 'Parsed without error' ); diff --git a/t/lib/PPI/Test.pm b/t/lib/PPI/Test.pm index d60efbbf..31536e9f 100644 --- a/t/lib/PPI/Test.pm +++ b/t/lib/PPI/Test.pm @@ -5,28 +5,31 @@ use strict; use File::Spec::Functions (); -our @ISA = 'Exporter'; +our @ISA = 'Exporter'; our @EXPORT_OK = qw( find_files quotable pause ); our %EXPORT_TAGS; # Find file names in named t/data dirs sub find_files { - my ( $testdir ) = @_; + my ($testdir) = @_; # Does the test directory exist? - die "Failed to find test directory $testdir" if !-e $testdir or !-d $testdir or !-r $testdir; + die "Failed to find test directory $testdir" + if !-e $testdir + or !-d $testdir + or !-r $testdir; # Find the .code test files opendir my $TESTDIR, $testdir or die "opendir: $!"; - my @perl = map { File::Spec::Functions::catfile( $testdir, $_ ) } sort grep { /\.(?:code|pm|t)$/ } readdir $TESTDIR; + my @perl = map { File::Spec::Functions::catfile( $testdir, $_ ) } + sort grep { /\.(?:code|pm|t)$/ } readdir $TESTDIR; closedir $TESTDIR or die "closedir: $!"; return @perl; } - sub quotable { - my ( $quotable ) = @_; + my ($quotable) = @_; $quotable =~ s|\\|\\\\|g; $quotable =~ s|\t|\\t|g; $quotable =~ s|\n|\\n|g; @@ -36,11 +39,9 @@ sub quotable { return $quotable; } - sub pause { local $@; sleep 1 if !eval { require Time::HiRes; Time::HiRes::sleep(0.1); 1 }; } - 1; diff --git a/t/lib/PPI/Test/Object.pm b/t/lib/PPI/Test/Object.pm index 1b0cbb4e..c7d52806 100755 --- a/t/lib/PPI/Test/Object.pm +++ b/t/lib/PPI/Test/Object.pm @@ -24,12 +24,10 @@ sub document_ok { # A document should have zero or more children that are either # a statement or a non-significant child. my @children = $doc->children; - my $good = grep { - _INSTANCE($_, 'PPI::Statement') - or ( - _INSTANCE($_, 'PPI::Token') and ! $_->significant - ) - } @children; + my $good = grep { + _INSTANCE( $_, 'PPI::Statement' ) + or ( _INSTANCE( $_, 'PPI::Token' ) and !$_->significant ) + } @children; is( $good, scalar(@children), 'Document contains only statements and non-significant tokens' ); @@ -37,10 +35,6 @@ sub document_ok { 1; } - - - - ##################################################################### # Are there an unknowns @@ -55,27 +49,20 @@ sub unknown_objects { is( $doc->find_any('Token::Unknown'), - '', - "Contains no PPI::Token::Unknown elements", + '', "Contains no PPI::Token::Unknown elements", ); is( $doc->find_any('Structure::Unknown'), - '', - "Contains no PPI::Structure::Unknown elements", + '', "Contains no PPI::Structure::Unknown elements", ); is( $doc->find_any('Statement::Unknown'), - '', - "Contains no PPI::Statement::Unknown elements", + '', "Contains no PPI::Statement::Unknown elements", ); 1; } - - - - ##################################################################### # Are there any invalid nestings? @@ -89,13 +76,14 @@ sub nested_statements { my $doc = shift; ok( - ! $doc->find_any( sub { - _INSTANCE($_[1], 'PPI::Statement') - and - any { _INSTANCE($_, 'PPI::Statement') } $_[1]->children - } ), + !$doc->find_any( + sub { + _INSTANCE( $_[1], 'PPI::Statement' ) + and any { _INSTANCE( $_, 'PPI::Statement' ) } $_[1]->children; + } + ), 'Document contains no nested statements', - ); + ); } Test::Object->register( @@ -108,11 +96,12 @@ sub nested_structures { my $doc = shift; ok( - ! $doc->find_any( sub { - _INSTANCE($_[1], 'PPI::Structure') - and - any { _INSTANCE($_, 'PPI::Structure') } $_[1]->children - } ), + !$doc->find_any( + sub { + _INSTANCE( $_[1], 'PPI::Structure' ) + and any { _INSTANCE( $_, 'PPI::Structure' ) } $_[1]->children; + } + ), 'Document contains no nested structures', ); } @@ -127,19 +116,16 @@ sub no_attribute_in_attribute { my $doc = shift; ok( - ! $doc->find_any( sub { - _INSTANCE($_[1], 'PPI::Token::Attribute') - and - ! exists $_[1]->{_attribute} - } ), + !$doc->find_any( + sub { + _INSTANCE( $_[1], 'PPI::Token::Attribute' ) + and !exists $_[1]->{_attribute}; + } + ), 'No ->{_attribute} in PPI::Token::Attributes', ); } - - - - ##################################################################### # PPI::Statement Tests @@ -158,10 +144,6 @@ sub valid_compound_type { ); } - - - - ##################################################################### # Does ->location work properly # As an aside, fixes #23788: PPI::Statement::location() returns undef for C<({})>. @@ -169,14 +151,16 @@ sub valid_compound_type { Test::Object->register( class => 'PPI::Document', tests => 1, - code => \&defined_location, + code => \&defined_location, ); sub defined_location { my $document = shift; - my $bad = $document->find( sub { - not defined $_[1]->location - } ); + my $bad = $document->find( + sub { + not defined $_[1]->location; + } + ); is( $bad, '', '->location always defined' ); } diff --git a/t/lib/PPI/Test/Run.pm b/t/lib/PPI/Test/Run.pm index 72ce39af..bd176f7a 100644 --- a/t/lib/PPI/Test/Run.pm +++ b/t/lib/PPI/Test/Run.pm @@ -19,7 +19,8 @@ sub run_testdir { my $testdir = catdir(@_); # Does the test directory exist? - ok( (-e $testdir and -d $testdir and -r $testdir), "Test directory $testdir found" ); + ok( ( -e $testdir and -d $testdir and -r $testdir ), + "Test directory $testdir found" ); # Find the .code test files my @code = do { @@ -28,7 +29,7 @@ sub run_testdir { }; ok( scalar @code, 'Found at least one code file' ); - foreach my $codefile ( @code ) { + foreach my $codefile (@code) { # Does the .code file have a matching .dump file my $dumpfile = $codefile; $dumpfile =~ s/\.code$/\.dump/; @@ -41,18 +42,21 @@ sub run_testdir { my $document = safe_new $codefile; ok( $document, "$codename: Lexer->Document returns true" ); - SKIP: { + SKIP: { skip "No Document to test", 12 unless $document; # Index locations ok( $document->index_locations, "$codename: ->index_locations ok" ); # Check standard things - object_ok( $document ); # 7 tests contained within + object_ok($document); # 7 tests contained within # Get the dump array ref for the Document object - my $Dumper = PPI::Dumper->new( $document ); - ok( _INSTANCE($Dumper, 'PPI::Dumper'), "$codename: Object isa PPI::Dumper" ); + my $Dumper = PPI::Dumper->new($document); + ok( + _INSTANCE( $Dumper, 'PPI::Dumper' ), + "$codename: Object isa PPI::Dumper" + ); my @dump_list = $Dumper->list; ok( scalar @dump_list, "$codename: Got dump content from dumper" ); @@ -66,28 +70,33 @@ sub run_testdir { # Compare the two { - local $TODO = $ENV{TODO} if $ENV{TODO}; - is_deeply( \@dump_list, \@content, "$codename: Generated dump matches stored dump" ) - or diag map "$_\n", @dump_list; + local $TODO = $ENV{TODO} if $ENV{TODO}; + is_deeply( \@dump_list, \@content, + "$codename: Generated dump matches stored dump" ) + or diag map "$_\n", @dump_list; } } - SKIP: { + SKIP: { # Also, do a round-trip check - skip "No roundtrip check: Couldn't parse code file before", 1 if !$document; - skip "No roundtrip check: Couldn't open code file '$codename', $!", 1 unless # - my $source = do { open my $CODEFILE, '<', $codefile; binmode $CODEFILE; local $/; <$CODEFILE> }; + skip "No roundtrip check: Couldn't parse code file before", 1 + if !$document; + skip "No roundtrip check: Couldn't open code file '$codename', $!", + 1 + unless # + my $source = do { + open my $CODEFILE, '<', $codefile; + binmode $CODEFILE; + local $/; + <$CODEFILE>; + }; $source =~ s/(?:\015{1,2}\012|\015|\012)/\n/g; - is( $document->serialize, $source, "$codename: Round-trip back to source was ok" ); + is( $document->serialize, $source, + "$codename: Round-trip back to source was ok" ); } } } - - - - - ##################################################################### # Process a .code/.dump file pair # plan: 2 + 14 * npairs @@ -97,7 +106,8 @@ sub increment_testdir { my $testdir = catdir(@_); # Does the test directory exist? - ok( (-e $testdir and -d $testdir and -r $testdir), "Test directory $testdir found" ); + ok( ( -e $testdir and -d $testdir and -r $testdir ), + "Test directory $testdir found" ); # Find the .code test files my @code = do { @@ -106,7 +116,7 @@ sub increment_testdir { }; ok( scalar @code, 'Found at least one code file' ); - for my $codefile ( @code ) { + for my $codefile (@code) { # Does the .code file have a matching .dump file my $codename = $codefile; $codename =~ s/\.code$//; @@ -125,7 +135,10 @@ sub increment_testdir { my $string = substr $buffer, 0, $chars; my $document = eval { safe_new \$string }; is( $@ => '', "$codename: $chars chars ok" ); - is( $document->serialize => $string, "$codename: $chars char roundtrip" ); + is( + $document->serialize => $string, + "$codename: $chars char roundtrip" + ); } } } diff --git a/t/lib/PPI/Test/pragmas.pm b/t/lib/PPI/Test/pragmas.pm index ffcfc032..eb52d653 100644 --- a/t/lib/PPI/Test/pragmas.pm +++ b/t/lib/PPI/Test/pragmas.pm @@ -21,13 +21,14 @@ use Test::More 0.88; use if $ENV{AUTHOR_TESTING}, 'Test::Warnings', ':no_end_test'; BEGIN { - select STDERR; ## no critic ( InputOutput::ProhibitOneArgSelect ) + select STDERR; ## no critic ( InputOutput::ProhibitOneArgSelect ) $| = 1; - select STDOUT; ## no critic ( InputOutput::ProhibitOneArgSelect ) + select STDOUT; ## no critic ( InputOutput::ProhibitOneArgSelect ) - $^W++; # throw -w at runtime to try and catch warnings in un-warning-ed modules + $^W++ + ; # throw -w at runtime to try and catch warnings in un-warning-ed modules - no warnings 'once'; ## no critic ( TestingAndDebugging::ProhibitNoWarnings ) + no warnings 'once'; ## no critic ( TestingAndDebugging::ProhibitNoWarnings ) $PPI::XS_DISABLE = 1; $PPI::Lexer::X_TOKENIZER ||= $ENV{X_TOKENIZER}; } @@ -39,7 +40,7 @@ sub import { } END { - Test::Warnings::had_no_warnings() if $ENV{AUTHOR_TESTING}; + Test::Warnings::had_no_warnings() if $ENV{AUTHOR_TESTING}; } 1; diff --git a/t/marpa.t b/t/marpa.t index 72852444..abc1bb8c 100644 --- a/t/marpa.t +++ b/t/marpa.t @@ -11,335 +11,335 @@ use PPI (); use Helper 'safe_new'; test_statement( - 'use v5 ;', - [ - 'PPI::Statement::Include' => 'use v5 ;', - 'PPI::Token::Word' => 'use', - 'PPI::Token::Number::Version' => 'v5', - 'PPI::Token::Structure' => ';' - ] + 'use v5 ;', + [ + 'PPI::Statement::Include' => 'use v5 ;', + 'PPI::Token::Word' => 'use', + 'PPI::Token::Number::Version' => 'v5', + 'PPI::Token::Structure' => ';' + ] ); test_statement( - 'use 5 ;', - [ - 'PPI::Statement::Include' => 'use 5 ;', - 'PPI::Token::Word' => 'use', - 'PPI::Token::Number' => '5', - 'PPI::Token::Structure' => ';' - ] + 'use 5 ;', + [ + 'PPI::Statement::Include' => 'use 5 ;', + 'PPI::Token::Word' => 'use', + 'PPI::Token::Number' => '5', + 'PPI::Token::Structure' => ';' + ] ); test_statement( - 'use 5.1 ;', - [ - 'PPI::Statement::Include' => 'use 5.1 ;', - 'PPI::Token::Word' => 'use', - 'PPI::Token::Number::Float' => '5.1', - 'PPI::Token::Structure' => ';' - ] + 'use 5.1 ;', + [ + 'PPI::Statement::Include' => 'use 5.1 ;', + 'PPI::Token::Word' => 'use', + 'PPI::Token::Number::Float' => '5.1', + 'PPI::Token::Structure' => ';' + ] ); test_statement( - 'use xyz () ;', - [ - 'PPI::Statement::Include' => 'use xyz () ;', - 'PPI::Token::Word' => 'use', - 'PPI::Token::Word' => 'xyz', - 'PPI::Structure::List' => '()', - 'PPI::Token::Structure' => '(', - 'PPI::Token::Structure' => ')', - 'PPI::Token::Structure' => ';' - ] + 'use xyz () ;', + [ + 'PPI::Statement::Include' => 'use xyz () ;', + 'PPI::Token::Word' => 'use', + 'PPI::Token::Word' => 'xyz', + 'PPI::Structure::List' => '()', + 'PPI::Token::Structure' => '(', + 'PPI::Token::Structure' => ')', + 'PPI::Token::Structure' => ';' + ] ); test_statement( - 'use v5 xyz () ;', - [ - 'PPI::Statement::Include' => 'use v5 xyz () ;', - 'PPI::Token::Word' => 'use', - 'PPI::Token::Number::Version' => 'v5', - 'PPI::Token::Word' => 'xyz', - 'PPI::Structure::List' => '()', - 'PPI::Token::Structure' => '(', - 'PPI::Token::Structure' => ')', - 'PPI::Token::Structure' => ';' - ] + 'use v5 xyz () ;', + [ + 'PPI::Statement::Include' => 'use v5 xyz () ;', + 'PPI::Token::Word' => 'use', + 'PPI::Token::Number::Version' => 'v5', + 'PPI::Token::Word' => 'xyz', + 'PPI::Structure::List' => '()', + 'PPI::Token::Structure' => '(', + 'PPI::Token::Structure' => ')', + 'PPI::Token::Structure' => ';' + ] ); test_statement( - 'use 5 xyz () ;', - [ - 'PPI::Statement::Include' => 'use 5 xyz () ;', - 'PPI::Token::Word' => 'use', - 'PPI::Token::Number' => '5', - 'PPI::Token::Word' => 'xyz', - 'PPI::Structure::List' => '()', - 'PPI::Token::Structure' => '(', - 'PPI::Token::Structure' => ')', - 'PPI::Token::Structure' => ';' - ] + 'use 5 xyz () ;', + [ + 'PPI::Statement::Include' => 'use 5 xyz () ;', + 'PPI::Token::Word' => 'use', + 'PPI::Token::Number' => '5', + 'PPI::Token::Word' => 'xyz', + 'PPI::Structure::List' => '()', + 'PPI::Token::Structure' => '(', + 'PPI::Token::Structure' => ')', + 'PPI::Token::Structure' => ';' + ] ); test_statement( - 'use 5.1 xyz () ;', - [ - 'PPI::Statement::Include' => 'use 5.1 xyz () ;', - 'PPI::Token::Word' => 'use', - 'PPI::Token::Number::Float' => '5.1', - 'PPI::Token::Word' => 'xyz', - 'PPI::Structure::List' => '()', - 'PPI::Token::Structure' => '(', - 'PPI::Token::Structure' => ')', - 'PPI::Token::Structure' => ';' - ] + 'use 5.1 xyz () ;', + [ + 'PPI::Statement::Include' => 'use 5.1 xyz () ;', + 'PPI::Token::Word' => 'use', + 'PPI::Token::Number::Float' => '5.1', + 'PPI::Token::Word' => 'xyz', + 'PPI::Structure::List' => '()', + 'PPI::Token::Structure' => '(', + 'PPI::Token::Structure' => ')', + 'PPI::Token::Structure' => ';' + ] ); test_statement( - 'use xyz v5 () ;', - [ - 'PPI::Statement::Include' => 'use xyz v5 () ;', - 'PPI::Token::Word' => 'use', - 'PPI::Token::Word' => 'xyz', - 'PPI::Token::Number::Version' => 'v5', - 'PPI::Structure::List' => '()', - 'PPI::Token::Structure' => '(', - 'PPI::Token::Structure' => ')', - 'PPI::Token::Structure' => ';' - ] + 'use xyz v5 () ;', + [ + 'PPI::Statement::Include' => 'use xyz v5 () ;', + 'PPI::Token::Word' => 'use', + 'PPI::Token::Word' => 'xyz', + 'PPI::Token::Number::Version' => 'v5', + 'PPI::Structure::List' => '()', + 'PPI::Token::Structure' => '(', + 'PPI::Token::Structure' => ')', + 'PPI::Token::Structure' => ';' + ] ); test_statement( - 'use xyz 5 () ;', - [ - 'PPI::Statement::Include' => 'use xyz 5 () ;', - 'PPI::Token::Word' => 'use', - 'PPI::Token::Word' => 'xyz', - 'PPI::Token::Number' => '5', - 'PPI::Structure::List' => '()', - 'PPI::Token::Structure' => '(', - 'PPI::Token::Structure' => ')', - 'PPI::Token::Structure' => ';' - ] + 'use xyz 5 () ;', + [ + 'PPI::Statement::Include' => 'use xyz 5 () ;', + 'PPI::Token::Word' => 'use', + 'PPI::Token::Word' => 'xyz', + 'PPI::Token::Number' => '5', + 'PPI::Structure::List' => '()', + 'PPI::Token::Structure' => '(', + 'PPI::Token::Structure' => ')', + 'PPI::Token::Structure' => ';' + ] ); test_statement( - 'use xyz 5.1 () ;', - [ - 'PPI::Statement::Include' => 'use xyz 5.1 () ;', - 'PPI::Token::Word' => 'use', - 'PPI::Token::Word' => 'xyz', - 'PPI::Token::Number::Float' => '5.1', - 'PPI::Structure::List' => '()', - 'PPI::Token::Structure' => '(', - 'PPI::Token::Structure' => ')', - 'PPI::Token::Structure' => ';' - ] + 'use xyz 5.1 () ;', + [ + 'PPI::Statement::Include' => 'use xyz 5.1 () ;', + 'PPI::Token::Word' => 'use', + 'PPI::Token::Word' => 'xyz', + 'PPI::Token::Number::Float' => '5.1', + 'PPI::Structure::List' => '()', + 'PPI::Token::Structure' => '(', + 'PPI::Token::Structure' => ')', + 'PPI::Token::Structure' => ';' + ] ); test_statement( - 'use v5 xyz 5 ;', - [ - 'PPI::Statement::Include' => 'use v5 xyz 5 ;', - 'PPI::Token::Word' => 'use', - 'PPI::Token::Number::Version' => 'v5', - 'PPI::Token::Word' => 'xyz', - 'PPI::Token::Number' => '5', - 'PPI::Token::Structure' => ';' - ] + 'use v5 xyz 5 ;', + [ + 'PPI::Statement::Include' => 'use v5 xyz 5 ;', + 'PPI::Token::Word' => 'use', + 'PPI::Token::Number::Version' => 'v5', + 'PPI::Token::Word' => 'xyz', + 'PPI::Token::Number' => '5', + 'PPI::Token::Structure' => ';' + ] ); test_statement( - 'use 5 xyz 5 ;', - [ - 'PPI::Statement::Include' => 'use 5 xyz 5 ;', - 'PPI::Token::Word' => 'use', - 'PPI::Token::Number' => '5', - 'PPI::Token::Word' => 'xyz', - 'PPI::Token::Number' => '5', - 'PPI::Token::Structure' => ';' - ] + 'use 5 xyz 5 ;', + [ + 'PPI::Statement::Include' => 'use 5 xyz 5 ;', + 'PPI::Token::Word' => 'use', + 'PPI::Token::Number' => '5', + 'PPI::Token::Word' => 'xyz', + 'PPI::Token::Number' => '5', + 'PPI::Token::Structure' => ';' + ] ); test_statement( - 'use 5.1 xyz 5 ;', - [ - 'PPI::Statement::Include' => 'use 5.1 xyz 5 ;', - 'PPI::Token::Word' => 'use', - 'PPI::Token::Number::Float' => '5.1', - 'PPI::Token::Word' => 'xyz', - 'PPI::Token::Number' => '5', - 'PPI::Token::Structure' => ';' - ] + 'use 5.1 xyz 5 ;', + [ + 'PPI::Statement::Include' => 'use 5.1 xyz 5 ;', + 'PPI::Token::Word' => 'use', + 'PPI::Token::Number::Float' => '5.1', + 'PPI::Token::Word' => 'xyz', + 'PPI::Token::Number' => '5', + 'PPI::Token::Structure' => ';' + ] ); test_statement( - 'use xyz v5 5 ;', - [ - 'PPI::Statement::Include' => 'use xyz v5 5 ;', - 'PPI::Token::Word' => 'use', - 'PPI::Token::Word' => 'xyz', - 'PPI::Token::Number::Version' => 'v5', - 'PPI::Token::Number' => '5', - 'PPI::Token::Structure' => ';' - ] + 'use xyz v5 5 ;', + [ + 'PPI::Statement::Include' => 'use xyz v5 5 ;', + 'PPI::Token::Word' => 'use', + 'PPI::Token::Word' => 'xyz', + 'PPI::Token::Number::Version' => 'v5', + 'PPI::Token::Number' => '5', + 'PPI::Token::Structure' => ';' + ] ); test_statement( - 'use xyz 5 5 ;', - [ - 'PPI::Statement::Include' => 'use xyz 5 5 ;', - 'PPI::Token::Word' => 'use', - 'PPI::Token::Word' => 'xyz', - 'PPI::Token::Number' => '5', - 'PPI::Token::Number' => '5', - 'PPI::Token::Structure' => ';' - ] + 'use xyz 5 5 ;', + [ + 'PPI::Statement::Include' => 'use xyz 5 5 ;', + 'PPI::Token::Word' => 'use', + 'PPI::Token::Word' => 'xyz', + 'PPI::Token::Number' => '5', + 'PPI::Token::Number' => '5', + 'PPI::Token::Structure' => ';' + ] ); test_statement( - 'use xyz 5.1 5 ;', - [ - 'PPI::Statement::Include' => 'use xyz 5.1 5 ;', - 'PPI::Token::Word' => 'use', - 'PPI::Token::Word' => 'xyz', - 'PPI::Token::Number::Float' => '5.1', - 'PPI::Token::Number' => '5', - 'PPI::Token::Structure' => ';' - ] + 'use xyz 5.1 5 ;', + [ + 'PPI::Statement::Include' => 'use xyz 5.1 5 ;', + 'PPI::Token::Word' => 'use', + 'PPI::Token::Word' => 'xyz', + 'PPI::Token::Number::Float' => '5.1', + 'PPI::Token::Number' => '5', + 'PPI::Token::Structure' => ';' + ] ); test_statement( - 'use v5 xyz 5,5 ;', - [ - 'PPI::Statement::Include' => 'use v5 xyz 5,5 ;', - 'PPI::Token::Word' => 'use', - 'PPI::Token::Number::Version' => 'v5', - 'PPI::Token::Word' => 'xyz', - 'PPI::Token::Number' => '5', - 'PPI::Token::Operator' => ',', - 'PPI::Token::Number' => '5', - 'PPI::Token::Structure' => ';' - ] + 'use v5 xyz 5,5 ;', + [ + 'PPI::Statement::Include' => 'use v5 xyz 5,5 ;', + 'PPI::Token::Word' => 'use', + 'PPI::Token::Number::Version' => 'v5', + 'PPI::Token::Word' => 'xyz', + 'PPI::Token::Number' => '5', + 'PPI::Token::Operator' => ',', + 'PPI::Token::Number' => '5', + 'PPI::Token::Structure' => ';' + ] ); test_statement( - 'use 5 xyz 5,5 ;', - [ - 'PPI::Statement::Include' => 'use 5 xyz 5,5 ;', - 'PPI::Token::Word' => 'use', - 'PPI::Token::Number' => '5', - 'PPI::Token::Word' => 'xyz', - 'PPI::Token::Number' => '5', - 'PPI::Token::Operator' => ',', - 'PPI::Token::Number' => '5', - 'PPI::Token::Structure' => ';' - ] + 'use 5 xyz 5,5 ;', + [ + 'PPI::Statement::Include' => 'use 5 xyz 5,5 ;', + 'PPI::Token::Word' => 'use', + 'PPI::Token::Number' => '5', + 'PPI::Token::Word' => 'xyz', + 'PPI::Token::Number' => '5', + 'PPI::Token::Operator' => ',', + 'PPI::Token::Number' => '5', + 'PPI::Token::Structure' => ';' + ] ); test_statement( - 'use 5.1 xyz 5,5 ;', - [ - 'PPI::Statement::Include' => 'use 5.1 xyz 5,5 ;', - 'PPI::Token::Word' => 'use', - 'PPI::Token::Number::Float' => '5.1', - 'PPI::Token::Word' => 'xyz', - 'PPI::Token::Number' => '5', - 'PPI::Token::Operator' => ',', - 'PPI::Token::Number' => '5', - 'PPI::Token::Structure' => ';' - ] + 'use 5.1 xyz 5,5 ;', + [ + 'PPI::Statement::Include' => 'use 5.1 xyz 5,5 ;', + 'PPI::Token::Word' => 'use', + 'PPI::Token::Number::Float' => '5.1', + 'PPI::Token::Word' => 'xyz', + 'PPI::Token::Number' => '5', + 'PPI::Token::Operator' => ',', + 'PPI::Token::Number' => '5', + 'PPI::Token::Structure' => ';' + ] ); test_statement( - 'use xyz v5 5,5 ;', - [ - 'PPI::Statement::Include' => 'use xyz v5 5,5 ;', - 'PPI::Token::Word' => 'use', - 'PPI::Token::Word' => 'xyz', - 'PPI::Token::Number::Version' => 'v5', - 'PPI::Token::Number' => '5', - 'PPI::Token::Operator' => ',', - 'PPI::Token::Number' => '5', - 'PPI::Token::Structure' => ';' - ] + 'use xyz v5 5,5 ;', + [ + 'PPI::Statement::Include' => 'use xyz v5 5,5 ;', + 'PPI::Token::Word' => 'use', + 'PPI::Token::Word' => 'xyz', + 'PPI::Token::Number::Version' => 'v5', + 'PPI::Token::Number' => '5', + 'PPI::Token::Operator' => ',', + 'PPI::Token::Number' => '5', + 'PPI::Token::Structure' => ';' + ] ); test_statement( - 'use xyz 5 5,5 ;', - [ - 'PPI::Statement::Include' => 'use xyz 5 5,5 ;', - 'PPI::Token::Word' => 'use', - 'PPI::Token::Word' => 'xyz', - 'PPI::Token::Number' => '5', - 'PPI::Token::Number' => '5', - 'PPI::Token::Operator' => ',', - 'PPI::Token::Number' => '5', - 'PPI::Token::Structure' => ';' - ] + 'use xyz 5 5,5 ;', + [ + 'PPI::Statement::Include' => 'use xyz 5 5,5 ;', + 'PPI::Token::Word' => 'use', + 'PPI::Token::Word' => 'xyz', + 'PPI::Token::Number' => '5', + 'PPI::Token::Number' => '5', + 'PPI::Token::Operator' => ',', + 'PPI::Token::Number' => '5', + 'PPI::Token::Structure' => ';' + ] ); test_statement( - 'use xyz 5.1 5,5 ;', - [ - 'PPI::Statement::Include' => 'use xyz 5.1 5,5 ;', - 'PPI::Token::Word' => 'use', - 'PPI::Token::Word' => 'xyz', - 'PPI::Token::Number::Float' => '5.1', - 'PPI::Token::Number' => '5', - 'PPI::Token::Operator' => ',', - 'PPI::Token::Number' => '5', - 'PPI::Token::Structure' => ';' - ] + 'use xyz 5.1 5,5 ;', + [ + 'PPI::Statement::Include' => 'use xyz 5.1 5,5 ;', + 'PPI::Token::Word' => 'use', + 'PPI::Token::Word' => 'xyz', + 'PPI::Token::Number::Float' => '5.1', + 'PPI::Token::Number' => '5', + 'PPI::Token::Operator' => ',', + 'PPI::Token::Number' => '5', + 'PPI::Token::Structure' => ';' + ] ); test_statement( - 'use xyz 5.1 @a ;', - [ - 'PPI::Statement::Include' => 'use xyz 5.1 @a ;', - 'PPI::Token::Word' => 'use', - 'PPI::Token::Word' => 'xyz', - 'PPI::Token::Number::Float' => '5.1', - 'PPI::Token::Symbol' => '@a', - 'PPI::Token::Structure' => ';' - ] + 'use xyz 5.1 @a ;', + [ + 'PPI::Statement::Include' => 'use xyz 5.1 @a ;', + 'PPI::Token::Word' => 'use', + 'PPI::Token::Word' => 'xyz', + 'PPI::Token::Number::Float' => '5.1', + 'PPI::Token::Symbol' => '@a', + 'PPI::Token::Structure' => ';' + ] ); sub one_line_explain { - my ( $data ) = @_; - my @explain = explain $data; - s/\n//g for @explain; - return join "", @explain; + my ($data) = @_; + my @explain = explain $data; + s/\n//g for @explain; + return join "", @explain; } sub main_level_line { - return "" if not $TODO; - my @outer_final; - my $level = 0; - while ( my @outer = caller( $level++ ) ) { - @outer_final = @outer; - } - return "l $outer_final[2] - "; + return "" if not $TODO; + my @outer_final; + my $level = 0; + while ( my @outer = caller( $level++ ) ) { + @outer_final = @outer; + } + return "l $outer_final[2] - "; } sub test_statement { - local $Test::Builder::Level = $Test::Builder::Level + 1; - my ( $code, $expected, $msg ) = @_; - $msg = perlstring $code if !defined $msg; - - my $d = safe_new \$code; - my $tokens = $d->find( sub { $_[1]->significant } ); - $tokens = [ map { ref( $_ ), $_->content } @$tokens ]; - - if ( $expected->[0] !~ /^PPI::Statement/ ) { - $expected = [ 'PPI::Statement', $code, @$expected ]; - } - my $ok = is_deeply( $tokens, $expected, main_level_line . $msg ); - if ( !$ok ) { - diag ">>> $code -- $msg\n"; - diag "GOT: " . one_line_explain $tokens; - diag "EXP: " . one_line_explain $expected; - } - - return; + local $Test::Builder::Level = $Test::Builder::Level + 1; + my ( $code, $expected, $msg ) = @_; + $msg = perlstring $code if !defined $msg; + + my $d = safe_new \$code; + my $tokens = $d->find( sub { $_[1]->significant } ); + $tokens = [ map { ref($_), $_->content } @$tokens ]; + + if ( $expected->[0] !~ /^PPI::Statement/ ) { + $expected = [ 'PPI::Statement', $code, @$expected ]; + } + my $ok = is_deeply( $tokens, $expected, main_level_line . $msg ); + if ( !$ok ) { + diag ">>> $code -- $msg\n"; + diag "GOT: " . one_line_explain $tokens; + diag "EXP: " . one_line_explain $expected; + } + + return; } diff --git a/t/ppi_element.t b/t/ppi_element.t index eaebcd98..f1a0f4dd 100644 --- a/t/ppi_element.t +++ b/t/ppi_element.t @@ -4,40 +4,43 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 68 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 68 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); use Helper 'safe_new'; - __INSERT_AFTER: { my $Document = safe_new \"print 'Hello World';"; - my $string = $Document->find_first('Token::Quote'); + my $string = $Document->find_first('Token::Quote'); isa_ok( $string, 'PPI::Token::Quote' ); is( $string->content, "'Hello World'", 'Got expected token' ); my $foo = PPI::Token::Word->new('foo'); isa_ok( $foo, 'PPI::Token::Word' ); is( $foo->content, 'foo', 'Created Word token' ); - $string->__insert_after( $foo ); - is( $Document->serialize, "print 'Hello World'foo;", - '__insert_after actually inserts' ); + $string->__insert_after($foo); + is( + $Document->serialize, + "print 'Hello World'foo;", + '__insert_after actually inserts' + ); } - __INSERT_BEFORE: { my $Document = safe_new \"print 'Hello World';"; - my $semi = $Document->find_first('Token::Structure'); + my $semi = $Document->find_first('Token::Structure'); isa_ok( $semi, 'PPI::Token::Structure' ); is( $semi->content, ';', 'Got expected token' ); my $foo = PPI::Token::Word->new('foo'); isa_ok( $foo, 'PPI::Token::Word' ); is( $foo->content, 'foo', 'Created Word token' ); - $semi->__insert_before( $foo ); - is( $Document->serialize, "print 'Hello World'foo;", - '__insert_before actually inserts' ); + $semi->__insert_before($foo); + is( + $Document->serialize, + "print 'Hello World'foo;", + '__insert_before actually inserts' + ); } - ANCESTOR_OF: { my $Document = safe_new \'( [ thingy ] ); $blarg = 1'; ok( @@ -46,35 +49,23 @@ ANCESTOR_OF: { ); my $words = $Document->find('Token::Word'); - is(scalar @{$words}, 1, 'Document contains 1 Word.'); + is( scalar @{$words}, 1, 'Document contains 1 Word.' ); my $word = $words->[0]; + ok( $word->ancestor_of($word), 'Word is an ancestor of itself.', ); ok( - $word->ancestor_of($word), - 'Word is an ancestor of itself.', - ); - ok( - ! $word->ancestor_of($Document), + !$word->ancestor_of($Document), 'Word is not an ancestor of the Document.', ); - ok( - $Document->ancestor_of($word), - 'Document is an ancestor of the Word.', - ); + ok( $Document->ancestor_of($word), + 'Document is an ancestor of the Word.', ); my $symbols = $Document->find('Token::Symbol'); - is(scalar @{$symbols}, 1, 'Document contains 1 Symbol.'); + is( scalar @{$symbols}, 1, 'Document contains 1 Symbol.' ); my $symbol = $symbols->[0]; - ok( - ! $word->ancestor_of($symbol), - 'Word is not an ancestor the Symbol.', - ); - ok( - ! $symbol->ancestor_of($word), - 'Symbol is not an ancestor the Word.', - ); + ok( !$word->ancestor_of($symbol), 'Word is not an ancestor the Symbol.', ); + ok( !$symbol->ancestor_of($word), 'Symbol is not an ancestor the Word.', ); } - COLUMN_NUMBER: { my $document = safe_new \<<'END_PERL'; @@ -82,11 +73,10 @@ COLUMN_NUMBER: { foo END_PERL my $words = $document->find('PPI::Token::Word'); - is( scalar @{$words}, 1, 'Found expected word token.' ); + is( scalar @{$words}, 1, 'Found expected word token.' ); is( $words->[0]->column_number, 4, 'Got correct column number.' ); } - DESCENDANT_OF: { my $Document = safe_new \'( [ thingy ] ); $blarg = 1'; ok( @@ -95,63 +85,63 @@ DESCENDANT_OF: { ); my $words = $Document->find('Token::Word'); - is(scalar @{$words}, 1, 'Document contains 1 Word.'); + is( scalar @{$words}, 1, 'Document contains 1 Word.' ); my $word = $words->[0]; - ok( - $word->descendant_of($word), - 'Word is a descendant of itself.', - ); + ok( $word->descendant_of($word), 'Word is a descendant of itself.', ); ok( $word->descendant_of($Document), 'Word is a descendant of the Document.', ); ok( - ! $Document->descendant_of($word), + !$Document->descendant_of($word), 'Document is not a descendant of the Word.', ); my $symbols = $Document->find('Token::Symbol'); - is(scalar @{$symbols}, 1, 'Document contains 1 Symbol.'); + is( scalar @{$symbols}, 1, 'Document contains 1 Symbol.' ); my $symbol = $symbols->[0]; ok( - ! $word->descendant_of($symbol), + !$word->descendant_of($symbol), 'Word is not a descendant the Symbol.', ); ok( - ! $symbol->descendant_of($word), + !$symbol->descendant_of($word), 'Symbol is not a descendant the Word.', ); } - INSERT_AFTER: { my $Document = safe_new \"print 'Hello World';"; - my $string = $Document->find_first('Token::Quote'); + my $string = $Document->find_first('Token::Quote'); isa_ok( $string, 'PPI::Token::Quote' ); is( $string->content, "'Hello World'", 'Got expected token' ); my $foo = PPI::Token::Word->new('foo'); isa_ok( $foo, 'PPI::Token::Word' ); is( $foo->content, 'foo', 'Created Word token' ); - $string->insert_after( $foo ); - is( $Document->serialize, "print 'Hello World'foo;", - 'insert_after actually inserts' ); + $string->insert_after($foo); + is( + $Document->serialize, + "print 'Hello World'foo;", + 'insert_after actually inserts' + ); } - INSERT_BEFORE: { my $Document = safe_new \"print 'Hello World';"; - my $semi = $Document->find_first('Token::Structure'); + my $semi = $Document->find_first('Token::Structure'); isa_ok( $semi, 'PPI::Token::Structure' ); is( $semi->content, ';', 'Got expected token' ); my $foo = PPI::Token::Word->new('foo'); isa_ok( $foo, 'PPI::Token::Word' ); is( $foo->content, 'foo', 'Created Word token' ); - $semi->insert_before( $foo ); - is( $Document->serialize, "print 'Hello World'foo;", - 'insert_before actually inserts' ); + $semi->insert_before($foo); + is( + $Document->serialize, + "print 'Hello World'foo;", + 'insert_before actually inserts' + ); } - LINE_NUMBER: { my $document = safe_new \<<'END_PERL'; @@ -159,11 +149,10 @@ LINE_NUMBER: { foo END_PERL my $words = $document->find('PPI::Token::Word'); - is( scalar @{$words}, 1, 'Found expected word token.' ); + is( scalar @{$words}, 1, 'Found expected word token.' ); is( $words->[0]->line_number, 3, 'Got correct line number.' ); } - LOGICAL_FILENAME: { # Double quoted so that we don't really have a "#line" at the beginning and # errors in this file itself aren't affected by this. @@ -177,12 +166,10 @@ END_PERL is( scalar @{$words}, 1, 'Found expected word token.' ); is( $words->[0]->logical_filename, - 'test-file', - 'Got correct logical line number.', + 'test-file', 'Got correct logical line number.', ); } - LOGICAL_LINE_NUMBER: { # Double quoted so that we don't really have a "#line" at the beginning and # errors in this file itself aren't affected by this. @@ -194,10 +181,10 @@ LOGICAL_LINE_NUMBER: { END_PERL my $words = $document->find('PPI::Token::Word'); is( scalar @{$words}, 1, 'Found expected word token.' ); - is( $words->[0]->logical_line_number, 1, 'Got correct logical line number.' ); + is( $words->[0]->logical_line_number, + 1, 'Got correct logical line number.' ); } - VISUAL_COLUMN_NUMBER: { my $document = safe_new \<<"END_PERL"; @@ -205,7 +192,7 @@ VISUAL_COLUMN_NUMBER: { \t foo END_PERL my $tab_width = 5; - $document->tab_width($tab_width); # don't use a "usual" value. + $document->tab_width($tab_width); # don't use a "usual" value. my $words = $document->find('PPI::Token::Word'); is( scalar @{$words}, 1, 'Found expected word token.' ); is( diff --git a/t/ppi_element_replace.t b/t/ppi_element_replace.t index 652554ed..3829f19f 100755 --- a/t/ppi_element_replace.t +++ b/t/ppi_element_replace.t @@ -6,62 +6,68 @@ use lib 't/lib'; use PPI::Test::pragmas; use PPI::Document (); -use Test::More tests => ($ENV{AUTHOR_TESTING} ? 1 : 0) + 28; +use Test::More tests => ( $ENV{AUTHOR_TESTING} ? 1 : 0 ) + 28; use Helper 'safe_new'; __REPLACE_METH: { my $Document = safe_new \"print 'Hello World';"; - my $string = $Document->find_first('Token::Quote'); + my $string = $Document->find_first('Token::Quote'); isa_ok( $string, 'PPI::Token::Quote' ); is( $string->content, "'Hello World'", 'Got expected token' ); my $foo = PPI::Token::Quote::Single->new("'foo'"); isa_ok( $foo, 'PPI::Token::Quote::Single' ); is( $foo->content, "'foo'", 'Created Quote token' ); - $string->replace( $foo ); + $string->replace($foo); is( $Document->serialize, "print 'foo';", 'replace works' ); } __REPLACE_CHILD_METH: { - my $Document = safe_new \"print 'Hello World';"; + my $Document = safe_new \"print 'Hello World';"; my $statement = $Document->find_first('Statement'); isa_ok( $statement, 'PPI::Statement' ); is( $statement->content, "print 'Hello World';", 'Got expected token' ); my $doc = safe_new \'for my $var ( @vars ) { say "foo" }'; my $foo = $doc->find('PPI::Statement::Compound'); - isa_ok( $foo->[0], 'PPI::Statement::Compound'); - is( $foo->[0]->content, q~for my $var ( @vars ) { say "foo" }~, 'for loop'); - ok( $statement->parent->replace_child( $statement, $foo->[0] ), 'replace_child success' ); - is( $Document->serialize, 'for my $var ( @vars ) { say "foo" }', 'replace works' ); + isa_ok( $foo->[0], 'PPI::Statement::Compound' ); + is( $foo->[0]->content, q~for my $var ( @vars ) { say "foo" }~, + 'for loop' ); + ok( $statement->parent->replace_child( $statement, $foo->[0] ), + 'replace_child success' ); + is( + $Document->serialize, + 'for my $var ( @vars ) { say "foo" }', + 'replace works' + ); { - my $doc = safe_new \'if ($foo) { ... }'; - my $compound = $doc->find('PPI::Statement::Compound'); + my $doc = safe_new \'if ($foo) { ... }'; + my $compound = $doc->find('PPI::Statement::Compound'); my $old_child = $compound->[0]->child(2); - is( $compound->[0]->child(2), '($foo)', 'found child'); + is( $compound->[0]->child(2), '($foo)', 'found child' ); my $replacement = PPI::Token->new('($bar)'); - my $statement = $doc->find_first('Statement'); - my $success = $statement->replace_child($old_child,$replacement); + my $statement = $doc->find_first('Statement'); + my $success = $statement->replace_child( $old_child, $replacement ); ok( $success, 'replace_child returns success' ); - is( $compound->[0]->child(2), '($bar)', 'child has been replaced'); - is( $doc->content, 'if ($bar) { ... }', 'document updated'); + is( $compound->[0]->child(2), '($bar)', 'child has been replaced' ); + is( $doc->content, 'if ($bar) { ... }', 'document updated' ); } { my $text = 'if ($foo) { ... }'; - my $doc = safe_new \$text; + my $doc = safe_new \$text; my $compound = $doc->find('PPI::Statement::Compound'); - is( $compound->[0]->child(2), '($foo)', 'found child'); + is( $compound->[0]->child(2), '($foo)', 'found child' ); my $replacement = PPI::Token->new('($bar)'); - my $statement = $doc->find_first('Statement'); + my $statement = $doc->find_first('Statement'); # Try to replace a child which does not exist. - my $success = $statement->replace_child($replacement,$replacement); + my $success = $statement->replace_child( $replacement, $replacement ); ok( !$success, 'replace_child returns failure' ); - is( $doc->content, $text, 'document not updated'); + is( $doc->content, $text, 'document not updated' ); } } diff --git a/t/ppi_lexer.t b/t/ppi_lexer.t index 8f41b163..2b2f9818 100644 --- a/t/ppi_lexer.t +++ b/t/ppi_lexer.t @@ -4,19 +4,17 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 49 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 49 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); use Helper 'safe_new'; - UNMATCHED_BRACE: { - my $token = new_ok( 'PPI::Token::Structure' => [ ')' ] ); - my $brace = new_ok( 'PPI::Statement::UnmatchedBrace' => [ $token ] ); + my $token = new_ok( 'PPI::Token::Structure' => [')'] ); + my $brace = new_ok( 'PPI::Statement::UnmatchedBrace' => [$token] ); is( $brace->content, ')', '->content ok' ); } - _CURLY: { my $document = safe_new \<<'END_PERL'; use constant { One => 1 }; @@ -58,107 +56,111 @@ END_PERL $document->index_locations(); my @statements; - foreach my $elem ( @{ $document->find( 'PPI::Statement' ) || [] } ) { + foreach my $elem ( @{ $document->find('PPI::Statement') || [] } ) { $statements[ $elem->line_number() - 1 ] ||= $elem; } is( scalar(@statements), 35, 'Found 35 statements' ); - isa_ok( $statements[0]->schild(2), 'PPI::Structure::Constructor', - 'The curly in ' . $statements[0]); - isa_ok( $statements[1]->schild(3), 'PPI::Structure::Constructor', - 'The curly in ' . $statements[1]); - isa_ok( $statements[2]->schild(2), 'PPI::Structure::Subscript', - 'The curly in ' . $statements[2]); - isa_ok( $statements[3]->schild(2), 'PPI::Structure::Subscript', - 'The curly in ' . $statements[3]); - isa_ok( $statements[4]->schild(1), 'PPI::Structure::Subscript', - 'The curly in ' . $statements[4]); - isa_ok( $statements[5]->schild(1), 'PPI::Structure::Block', - 'The curly in ' . $statements[5]); - isa_ok( $statements[6]->schild(1), 'PPI::Structure::Block', - 'The curly in ' . $statements[6]); - isa_ok( $statements[7]->schild(1), 'PPI::Structure::Block', - 'The curly in ' . $statements[7]); - isa_ok( $statements[8]->schild(1), 'PPI::Structure::Block', - 'The curly in ' . $statements[8]); - isa_ok( $statements[9]->schild(1), 'PPI::Structure::Block', - 'The curly in ' . $statements[9]); - isa_ok( $statements[10]->schild(2), 'PPI::Structure::Constructor', - 'The curly in ' . $statements[10]); - isa_ok( $statements[11]->schild(2), 'PPI::Structure::Constructor', - 'The curly in ' . $statements[11]); - isa_ok( $statements[12]->schild(2), 'PPI::Structure::Constructor', - 'The curly in ' . $statements[12]); - isa_ok( $statements[13]->schild(2), 'PPI::Structure::Constructor', - 'The curly in ' . $statements[13]); - isa_ok( $statements[14]->schild(0), 'PPI::Structure::Block', - 'The curly in ' . $statements[14]); - isa_ok( $statements[15]->schild(0), 'PPI::Structure::Constructor', - 'The curly in ' . $statements[15]); - isa_ok( $statements[16]->schild(0), 'PPI::Structure::Constructor', - 'The curly in ' . $statements[16]); - isa_ok( $statements[17]->schild(1), 'PPI::Structure::Constructor', - 'The curly in ' . $statements[17]); - isa_ok( $statements[18]->schild(0), 'PPI::Structure::Block', - 'The curly in ' . $statements[18]); - isa_ok( $statements[19]->schild(1), 'PPI::Structure::Subscript', - 'The curly in ' . $statements[19]); - isa_ok( $statements[20]->schild(2), 'PPI::Structure::Subscript', - 'The curly in ' . $statements[20]); - isa_ok( $statements[21]->schild(2), 'PPI::Structure::Subscript', - 'The curly in ' . $statements[21]); - isa_ok( $statements[22]->schild(1), 'PPI::Structure::Constructor', - 'The curly in ' . $statements[22]); - isa_ok( $statements[23]->schild(1), 'PPI::Structure::Constructor', - 'The curly in ' . $statements[23]); - isa_ok( $statements[24]->schild(2), 'PPI::Structure::Constructor', - 'The curly in ' . $statements[24]); - isa_ok( $statements[25]->schild(2), 'PPI::Structure::Constructor', - 'The curly in ' . $statements[25]); - isa_ok( $statements[26]->schild(2), 'PPI::Structure::Constructor', - 'The curly in ' . $statements[26]); - - isa_ok( $statements[27]->schild(2), 'PPI::Structure::Constructor', - 'The curly in ' . $statements[27]); - isa_ok( $statements[28]->schild(2), 'PPI::Structure::Constructor', - 'The curly in ' . $statements[28]); - isa_ok( $statements[29]->schild(2), 'PPI::Structure::Constructor', - 'The curly in ' . $statements[29]); - isa_ok( $statements[30]->schild(2), 'PPI::Structure::Constructor', - 'The curly in ' . $statements[30]); - isa_ok( $statements[31]->schild(4), 'PPI::Structure::Constructor', - 'The curly in ' . $statements[31]); + isa_ok( $statements[0]->schild(2), + 'PPI::Structure::Constructor', 'The curly in ' . $statements[0] ); + isa_ok( $statements[1]->schild(3), + 'PPI::Structure::Constructor', 'The curly in ' . $statements[1] ); + isa_ok( $statements[2]->schild(2), + 'PPI::Structure::Subscript', 'The curly in ' . $statements[2] ); + isa_ok( $statements[3]->schild(2), + 'PPI::Structure::Subscript', 'The curly in ' . $statements[3] ); + isa_ok( $statements[4]->schild(1), + 'PPI::Structure::Subscript', 'The curly in ' . $statements[4] ); + isa_ok( $statements[5]->schild(1), + 'PPI::Structure::Block', 'The curly in ' . $statements[5] ); + isa_ok( $statements[6]->schild(1), + 'PPI::Structure::Block', 'The curly in ' . $statements[6] ); + isa_ok( $statements[7]->schild(1), + 'PPI::Structure::Block', 'The curly in ' . $statements[7] ); + isa_ok( $statements[8]->schild(1), + 'PPI::Structure::Block', 'The curly in ' . $statements[8] ); + isa_ok( $statements[9]->schild(1), + 'PPI::Structure::Block', 'The curly in ' . $statements[9] ); + isa_ok( $statements[10]->schild(2), + 'PPI::Structure::Constructor', 'The curly in ' . $statements[10] ); + isa_ok( $statements[11]->schild(2), + 'PPI::Structure::Constructor', 'The curly in ' . $statements[11] ); + isa_ok( $statements[12]->schild(2), + 'PPI::Structure::Constructor', 'The curly in ' . $statements[12] ); + isa_ok( $statements[13]->schild(2), + 'PPI::Structure::Constructor', 'The curly in ' . $statements[13] ); + isa_ok( $statements[14]->schild(0), + 'PPI::Structure::Block', 'The curly in ' . $statements[14] ); + isa_ok( $statements[15]->schild(0), + 'PPI::Structure::Constructor', 'The curly in ' . $statements[15] ); + isa_ok( $statements[16]->schild(0), + 'PPI::Structure::Constructor', 'The curly in ' . $statements[16] ); + isa_ok( $statements[17]->schild(1), + 'PPI::Structure::Constructor', 'The curly in ' . $statements[17] ); + isa_ok( $statements[18]->schild(0), + 'PPI::Structure::Block', 'The curly in ' . $statements[18] ); + isa_ok( $statements[19]->schild(1), + 'PPI::Structure::Subscript', 'The curly in ' . $statements[19] ); + isa_ok( $statements[20]->schild(2), + 'PPI::Structure::Subscript', 'The curly in ' . $statements[20] ); + isa_ok( $statements[21]->schild(2), + 'PPI::Structure::Subscript', 'The curly in ' . $statements[21] ); + isa_ok( $statements[22]->schild(1), + 'PPI::Structure::Constructor', 'The curly in ' . $statements[22] ); + isa_ok( $statements[23]->schild(1), + 'PPI::Structure::Constructor', 'The curly in ' . $statements[23] ); + isa_ok( $statements[24]->schild(2), + 'PPI::Structure::Constructor', 'The curly in ' . $statements[24] ); + isa_ok( $statements[25]->schild(2), + 'PPI::Structure::Constructor', 'The curly in ' . $statements[25] ); + isa_ok( $statements[26]->schild(2), + 'PPI::Structure::Constructor', 'The curly in ' . $statements[26] ); + + isa_ok( $statements[27]->schild(2), + 'PPI::Structure::Constructor', 'The curly in ' . $statements[27] ); + isa_ok( $statements[28]->schild(2), + 'PPI::Structure::Constructor', 'The curly in ' . $statements[28] ); + isa_ok( $statements[29]->schild(2), + 'PPI::Structure::Constructor', 'The curly in ' . $statements[29] ); + isa_ok( $statements[30]->schild(2), + 'PPI::Structure::Constructor', 'The curly in ' . $statements[30] ); + isa_ok( $statements[31]->schild(4), + 'PPI::Structure::Constructor', 'The curly in ' . $statements[31] ); # Check two things in the same statement - isa_ok( $statements[32]->schild(2), 'PPI::Structure::Constructor', - 'The curly in ' . $statements[32]); - isa_ok( $statements[32]->schild(4), 'PPI::Structure::Constructor', - 'The curly in ' . $statements[32]); + isa_ok( $statements[32]->schild(2), + 'PPI::Structure::Constructor', 'The curly in ' . $statements[32] ); + isa_ok( $statements[32]->schild(4), + 'PPI::Structure::Constructor', 'The curly in ' . $statements[32] ); # Scheduled block (or not) isa_ok( $statements[33], 'PPI::Statement::Scheduled', - 'Scheduled block in ' . $statements[33]); - isa_ok( $statements[34]->schild(1)->schild(0), 'PPI::Statement::Expression', - 'Expression (not scheduled block) in ' . $statements[34]); + 'Scheduled block in ' . $statements[33] ); + isa_ok( $statements[34]->schild(1)->schild(0), + 'PPI::Statement::Expression', + 'Expression (not scheduled block) in ' . $statements[34] ); } - LEX_STRUCTURE: { # Validate the creation of a null statement - SCOPE: { - my $token = new_ok( 'PPI::Token::Structure' => [ ';' ] ); - my $null = new_ok( 'PPI::Statement::Null' => [ $token ] ); + SCOPE: { + my $token = new_ok( 'PPI::Token::Structure' => [';'] ); + my $null = new_ok( 'PPI::Statement::Null' => [$token] ); is( $null->content, ';', '->content ok' ); } # Validate the creation of an empty statement - new_ok( 'PPI::Statement' => [ ] ); + new_ok( 'PPI::Statement' => [] ); } ERROR_HANDLING: { my $test_lexer = PPI::Lexer->new; is $test_lexer->errstr, "", "errstr is an empty string at the start"; - is $test_lexer->lex_file( undef ), undef, "lex_file fails without a filename"; - is( PPI::Lexer->errstr, "Did not pass a filename to PPI::Lexer::lex_file", "error can be gotten from class attribute" ); + is $test_lexer->lex_file(undef), undef, "lex_file fails without a filename"; + is( + PPI::Lexer->errstr, + "Did not pass a filename to PPI::Lexer::lex_file", + "error can be gotten from class attribute" + ); } diff --git a/t/ppi_node.t b/t/ppi_node.t index cf6686e7..2e84df96 100644 --- a/t/ppi_node.t +++ b/t/ppi_node.t @@ -4,16 +4,15 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 9 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 9 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); use Helper 'safe_new'; - PRUNE: { # Avoids a bug in old Perls relating to the detection of scripts # Known to occur in ActivePerl 5.6.1 and at least one 5.6.2 install. - my $hashbang = reverse 'lrep/nib/rsu/!#'; + my $hashbang = reverse 'lrep/nib/rsu/!#'; my $document = safe_new \<<"END_PERL"; $hashbang @@ -31,7 +30,7 @@ print "\n"; exit; END_PERL - ok( defined($document->prune ('PPI::Statement::Sub')), + ok( defined( $document->prune('PPI::Statement::Sub') ), 'Pruned multiple subs ok' ); } diff --git a/t/ppi_normal.t b/t/ppi_normal.t index 9a62c60a..d2c7fca9 100644 --- a/t/ppi_normal.t +++ b/t/ppi_normal.t @@ -4,12 +4,11 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 28 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 28 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); use Helper 'safe_new'; - NEW: { # Check we actually set the layer at creation my $layer_1 = PPI::Normal->new; @@ -23,18 +22,16 @@ NEW: { is( $layer_2->layer, 2, '->new(2) creates a layer 2' ); } - BAD: { # Test bad things - is( PPI::Normal->new(3), undef, '->new only allows up to layer 2' ); - is( PPI::Normal->new(undef), undef, '->new(evil) returns undef' ); - is( PPI::Normal->new("foo"), undef, '->new(evil) returns undef' ); - is( PPI::Normal->new(\"foo"), undef, '->new(evil) returns undef' ); - is( PPI::Normal->new([]), undef, '->new(evil) returns undef' ); - is( PPI::Normal->new({}), undef, '->new(evil) returns undef' ); + is( PPI::Normal->new(3), undef, '->new only allows up to layer 2' ); + is( PPI::Normal->new(undef), undef, '->new(evil) returns undef' ); + is( PPI::Normal->new("foo"), undef, '->new(evil) returns undef' ); + is( PPI::Normal->new( \"foo" ), undef, '->new(evil) returns undef' ); + is( PPI::Normal->new( [] ), undef, '->new(evil) returns undef' ); + is( PPI::Normal->new( {} ), undef, '->new(evil) returns undef' ); } - PROCESS: { my $doc1 = safe_new \'print "Hello World!\n";'; my $doc2 = \'print "Hello World!\n";'; @@ -44,7 +41,7 @@ PROCESS: { # Normalize them at level 1 my $layer1 = PPI::Normal->new(1); isa_ok( $layer1, 'PPI::Normal' ); - my $nor11 = $layer1->process($doc1->clone); + my $nor11 = $layer1->process( $doc1->clone ); my $nor12 = $layer1->process($doc2); my $nor13 = $layer1->process($doc3); isa_ok( $nor11, 'PPI::Document::Normalized' ); @@ -52,15 +49,15 @@ PROCESS: { isa_ok( $nor13, 'PPI::Document::Normalized' ); # The first 3 should be the same, the second not - is_deeply( { %$nor11 }, { %$nor12 }, 'Layer 1: 1 and 2 match' ); - is_deeply( { %$nor11 }, { %$nor13 }, 'Layer 1: 1 and 3 match' ); + is_deeply( {%$nor11}, {%$nor12}, 'Layer 1: 1 and 2 match' ); + is_deeply( {%$nor11}, {%$nor13}, 'Layer 1: 1 and 3 match' ); # Normalize them at level 2 my $layer2 = PPI::Normal->new(2); isa_ok( $layer2, 'PPI::Normal' ); my $nor21 = $layer2->process($doc1); my $nor22 = $layer2->process($doc2); - my $nor23 = $layer2->process($doc3); + my $nor23 = $layer2->process($doc3); my $nor24 = $layer2->process($doc4); isa_ok( $nor21, 'PPI::Document::Normalized' ); isa_ok( $nor22, 'PPI::Document::Normalized' ); @@ -68,7 +65,7 @@ PROCESS: { isa_ok( $nor24, 'PPI::Document::Normalized' ); # The first 3 should be the same, the second not - is_deeply( { %$nor21 }, { %$nor22 }, 'Layer 2: 1 and 2 match' ); - is_deeply( { %$nor21 }, { %$nor23 }, 'Layer 2: 1 and 3 match' ); - is_deeply( { %$nor21 }, { %$nor24 }, 'Layer 2: 1 and 4 match' ); + is_deeply( {%$nor21}, {%$nor22}, 'Layer 2: 1 and 2 match' ); + is_deeply( {%$nor21}, {%$nor23}, 'Layer 2: 1 and 3 match' ); + is_deeply( {%$nor21}, {%$nor24}, 'Layer 2: 1 and 4 match' ); } diff --git a/t/ppi_statement.t b/t/ppi_statement.t index 7ac25a6c..4a9e73fe 100644 --- a/t/ppi_statement.t +++ b/t/ppi_statement.t @@ -4,12 +4,11 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 23 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 23 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); use Helper 'safe_new'; - SPECIALIZED: { my $Document = safe_new \<<'END_PERL'; package Foo; @@ -25,24 +24,32 @@ END_PERL my $statements = $Document->find('Statement'); is( scalar @{$statements}, 10, 'Found the 10 test statements' ); - isa_ok( $statements->[0], 'PPI::Statement::Package', 'Statement 1: isa Package' ); - ok( $statements->[0]->specialized, 'Statement 1: is specialized' ); - isa_ok( $statements->[1], 'PPI::Statement::Include', 'Statement 2: isa Include' ); - ok( $statements->[1]->specialized, 'Statement 2: is specialized' ); - isa_ok( $statements->[2], 'PPI::Statement::Null', 'Statement 3: isa Null' ); - ok( $statements->[2]->specialized, 'Statement 3: is specialized' ); - isa_ok( $statements->[3], 'PPI::Statement::Compound', 'Statement 4: isa Compound' ); - ok( $statements->[3]->specialized, 'Statement 4: is specialized' ); - isa_ok( $statements->[4], 'PPI::Statement::Expression', 'Statement 5: isa Expression' ); - ok( $statements->[4]->specialized, 'Statement 5: is specialized' ); - isa_ok( $statements->[5], 'PPI::Statement::Break', 'Statement 6: isa Break' ); - ok( $statements->[5]->specialized, 'Statement 6: is specialized' ); - isa_ok( $statements->[6], 'PPI::Statement::Scheduled', 'Statement 7: isa Scheduled' ); - ok( $statements->[6]->specialized, 'Statement 7: is specialized' ); - isa_ok( $statements->[7], 'PPI::Statement::Sub', 'Statement 8: isa Sub' ); - ok( $statements->[7]->specialized, 'Statement 8: is specialized' ); - isa_ok( $statements->[8], 'PPI::Statement::Variable', 'Statement 9: isa Variable' ); - ok( $statements->[8]->specialized, 'Statement 9: is specialized' ); - is( ref $statements->[9], 'PPI::Statement', 'Statement 10: is a simple Statement' ); - ok( ! $statements->[9]->specialized, 'Statement 10: is not specialized' ); + isa_ok( $statements->[0], 'PPI::Statement::Package', + 'Statement 1: isa Package' ); + ok( $statements->[0]->specialized, 'Statement 1: is specialized' ); + isa_ok( $statements->[1], 'PPI::Statement::Include', + 'Statement 2: isa Include' ); + ok( $statements->[1]->specialized, 'Statement 2: is specialized' ); + isa_ok( $statements->[2], 'PPI::Statement::Null', 'Statement 3: isa Null' ); + ok( $statements->[2]->specialized, 'Statement 3: is specialized' ); + isa_ok( $statements->[3], 'PPI::Statement::Compound', + 'Statement 4: isa Compound' ); + ok( $statements->[3]->specialized, 'Statement 4: is specialized' ); + isa_ok( $statements->[4], 'PPI::Statement::Expression', + 'Statement 5: isa Expression' ); + ok( $statements->[4]->specialized, 'Statement 5: is specialized' ); + isa_ok( $statements->[5], 'PPI::Statement::Break', + 'Statement 6: isa Break' ); + ok( $statements->[5]->specialized, 'Statement 6: is specialized' ); + isa_ok( $statements->[6], 'PPI::Statement::Scheduled', + 'Statement 7: isa Scheduled' ); + ok( $statements->[6]->specialized, 'Statement 7: is specialized' ); + isa_ok( $statements->[7], 'PPI::Statement::Sub', 'Statement 8: isa Sub' ); + ok( $statements->[7]->specialized, 'Statement 8: is specialized' ); + isa_ok( $statements->[8], 'PPI::Statement::Variable', + 'Statement 9: isa Variable' ); + ok( $statements->[8]->specialized, 'Statement 9: is specialized' ); + is( ref $statements->[9], + 'PPI::Statement', 'Statement 10: is a simple Statement' ); + ok( !$statements->[9]->specialized, 'Statement 10: is not specialized' ); } diff --git a/t/ppi_statement_compound.t b/t/ppi_statement_compound.t index 2660aac4..68aaf3eb 100644 --- a/t/ppi_statement_compound.t +++ b/t/ppi_statement_compound.t @@ -4,12 +4,11 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 53 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 53 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); use Helper 'safe_new'; - TYPE: { my $Document = safe_new \<<'END_PERL'; while (1) { } @@ -73,17 +72,19 @@ END_PERL is( $statements->[0]->type, 'while', q ); is( $statements->[1]->type, 'while', q ); - is( $statements->[2]->type, 'while', q ); - is( $statements->[3]->type, 'while', q ); - is( $statements->[4]->type, 'if', q ); - is( $statements->[5]->type, 'if', q ); + is( $statements->[2]->type, + 'while', q ); + is( $statements->[3]->type, + 'while', q ); + is( $statements->[4]->type, 'if', q ); + is( $statements->[5]->type, 'if', q ); - foreach my $index (6..37) { + foreach my $index ( 6 .. 37 ) { my $statement = $statements->[$index]; is( $statement->type, 'foreach', qq ); } - foreach my $index (38..49) { + foreach my $index ( 38 .. 49 ) { my $statement = $statements->[$index]; is( $statement->type, 'for', qq ); } diff --git a/t/ppi_statement_include.t b/t/ppi_statement_include.t index c0ce60d3..cc5a8621 100644 --- a/t/ppi_statement_include.t +++ b/t/ppi_statement_include.t @@ -4,13 +4,12 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 6070 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 6070 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); -use PPI (); +use PPI (); use PPI::Singletons qw( %KEYWORDS ); use Helper 'safe_new'; - TYPE: { my $document = safe_new \<<'END_PERL'; require 5.6; @@ -29,7 +28,6 @@ END_PERL } } - MODULE_VERSION: { my $document = safe_new \<<'END_PERL'; use Integer::Version 1; @@ -44,18 +42,18 @@ use VString::Version::Decimal v1.5; END_PERL my $statements = $document->find('PPI::Statement::Include'); is( scalar @{$statements}, 9, 'Found expected include statements.' ); - is( $statements->[0]->module_version, 1, 'Integer version' ); - is( $statements->[1]->module_version, 1.5, 'Float version' ); - is( $statements->[2]->module_version, 1, 'Version and argument' ); + is( $statements->[0]->module_version, 1, 'Integer version' ); + is( $statements->[1]->module_version, 1.5, 'Float version' ); + is( $statements->[2]->module_version, 1, 'Version and argument' ); is( $statements->[3]->module_version, undef, 'No version, no arguments' ); is( $statements->[4]->module_version, undef, 'No version, with argument' ); is( $statements->[5]->module_version, undef, 'No version, with arguments' ); is( $statements->[6]->module_version, undef, 'Version include, no module' ); is( $statements->[7]->module_version, 'v10', 'Version string' ); - is( $statements->[8]->module_version, 'v1.5', 'Version string with decimal' ); + is( $statements->[8]->module_version, + 'v1.5', 'Version string with decimal' ); } - VERSION: { my $document = safe_new \<<'END_PERL'; # Examples from perlfunc in 5.10. @@ -77,21 +75,25 @@ END_PERL is( scalar @{$statements}, 11, 'Found expected include statements.' ); is( $statements->[0]->version, 'v5.6.1', 'use v-string' ); - is( $statements->[1]->version, '5.6.1', 'use v-string, no leading "v"' ); + is( $statements->[1]->version, '5.6.1', 'use v-string, no leading "v"' ); is( $statements->[2]->version, '5.006_001', 'use developer release' ); - is( $statements->[3]->version, '5.006', 'use back-compatible version, followed by...' ); - is( $statements->[4]->version, '5.6.1', '... use v-string, no leading "v"' ); + is( $statements->[3]->version, + '5.006', 'use back-compatible version, followed by...' ); + is( $statements->[4]->version, '5.6.1', + '... use v-string, no leading "v"' ); is( $statements->[5]->version, 'v5.6.1', 'require v-string' ); - is( $statements->[6]->version, '5.6.1', 'require v-string, no leading "v"' ); + is( $statements->[6]->version, '5.6.1', + 'require v-string, no leading "v"' ); is( $statements->[7]->version, '5.006_001', 'require developer release' ); - is( $statements->[8]->version, '5.006', 'require back-compatible version, followed by...' ); - is( $statements->[9]->version, '5.6.1', '... require v-string, no leading "v"' ); + is( $statements->[8]->version, + '5.006', 'require back-compatible version, followed by...' ); + is( $statements->[9]->version, + '5.6.1', '... require v-string, no leading "v"' ); is( $statements->[10]->version, '', 'use module version' ); } - VERSION_LITERAL: { my $document = safe_new \<<'END_PERL'; # Examples from perlfunc in 5.10. @@ -113,21 +115,27 @@ END_PERL is( scalar @{$statements}, 11, 'Found expected include statements.' ); is( $statements->[0]->version_literal, v5.6.1, 'use v-string' ); - is( $statements->[1]->version_literal, 5.6.1, 'use v-string, no leading "v"' ); + is( $statements->[1]->version_literal, + 5.6.1, 'use v-string, no leading "v"' ); is( $statements->[2]->version_literal, 5.006_001, 'use developer release' ); - is( $statements->[3]->version_literal, 5.006, 'use back-compatible version, followed by...' ); - is( $statements->[4]->version_literal, 5.6.1, '... use v-string, no leading "v"' ); + is( $statements->[3]->version_literal, + 5.006, 'use back-compatible version, followed by...' ); + is( $statements->[4]->version_literal, + 5.6.1, '... use v-string, no leading "v"' ); is( $statements->[5]->version_literal, v5.6.1, 'require v-string' ); - is( $statements->[6]->version_literal, 5.6.1, 'require v-string, no leading "v"' ); - is( $statements->[7]->version_literal, 5.006_001, 'require developer release' ); - is( $statements->[8]->version_literal, 5.006, 'require back-compatible version, followed by...' ); - is( $statements->[9]->version_literal, 5.6.1, '... require v-string, no leading "v"' ); + is( $statements->[6]->version_literal, + 5.6.1, 'require v-string, no leading "v"' ); + is( $statements->[7]->version_literal, + 5.006_001, 'require developer release' ); + is( $statements->[8]->version_literal, + 5.006, 'require back-compatible version, followed by...' ); + is( $statements->[9]->version_literal, + 5.6.1, '... require v-string, no leading "v"' ); is( $statements->[10]->version_literal, '', 'use module version' ); } - ARGUMENTS: { my $document = safe_new \<<'END_PERL'; use 5.006; # Don't expect anything. @@ -142,94 +150,93 @@ END_PERL is( scalar @{$statements}, 7, 'Found expected include statements.' ); is( - scalar $statements->[0]->arguments, undef, 'arguments for perl version', + scalar $statements->[0]->arguments, + undef, 'arguments for perl version', ); is( scalar $statements->[1]->arguments, - undef, - 'arguments with no arguments', + undef, 'arguments with no arguments', ); is( scalar $statements->[2]->arguments, - undef, - 'arguments with no arguments but module version', + undef, 'arguments with no arguments but module version', ); my @arguments = $statements->[3]->arguments; - is( scalar @arguments, 1, 'arguments with single argument' ); + is( scalar @arguments, 1, 'arguments with single argument' ); is( $arguments[0]->content, q<'bar'>, 'arguments with single argument' ); @arguments = $statements->[4]->arguments; is( scalar @arguments, - 1, - 'arguments with single argument and module version', + 1, 'arguments with single argument and module version', ); is( $arguments[0]->content, - q<'bar'>, - 'arguments with single argument and module version', + q<'bar'>, 'arguments with single argument and module version', ); @arguments = $statements->[5]->arguments; - is( - scalar @arguments, - 3, - 'arguments with multiple arguments', - ); + is( scalar @arguments, 3, 'arguments with multiple arguments', ); is( $arguments[0]->content, - q/qw< bar >/, - 'arguments with multiple arguments', - ); - is( - $arguments[1]->content, - q<,>, - 'arguments with multiple arguments', - ); - is( - $arguments[2]->content, - q<"baz">, - 'arguments with multiple arguments', + q/qw< bar >/, 'arguments with multiple arguments', ); + is( $arguments[1]->content, q<,>, 'arguments with multiple arguments', ); + is( $arguments[2]->content, q<"baz">, + 'arguments with multiple arguments', ); @arguments = $statements->[6]->arguments; - is( - scalar @arguments, - 5, - 'arguments with Test::More', - ); - is( - $arguments[0]->content, - 'tests', - 'arguments with Test::More', - ); - is( - $arguments[1]->content, - q[=>], - 'arguments with Test::More', - ); - is( - $arguments[2]->content, - 5, - 'arguments with Test::More', - ); - is( - $arguments[3]->content, - '*', - 'arguments with Test::More', - ); - is( - $arguments[4]->content, - 9, - 'arguments with Test::More', - ); + is( scalar @arguments, 5, 'arguments with Test::More', ); + is( $arguments[0]->content, 'tests', 'arguments with Test::More', ); + is( $arguments[1]->content, q[=>], 'arguments with Test::More', ); + is( $arguments[2]->content, 5, 'arguments with Test::More', ); + is( $arguments[3]->content, '*', 'arguments with Test::More', ); + is( $arguments[4]->content, 9, 'arguments with Test::More', ); } - KEYWORDS_AS_MODULE_NAMES: { - my %known_bad = map { $_ => 1 } 'no m 1.2.3;', 'no m ;', 'no m v1.2.3;', 'no m v10;', 'no q 1.2.3;', 'no q ;', 'no q v1.2.3;', 'no q v10;', 'no qq 1.2.3;', 'no qq ;', 'no qq v1.2.3;', 'no qq v10;', 'no qr 1.2.3;', 'no qr ;', 'no qr v1.2.3;', 'no qr v10;', 'no qw 1.2.3;', 'no qw ;', 'no qw v1.2.3;', 'no qw v10;', 'no qx 1.2.3;', 'no qx ;', 'no qx v1.2.3;', 'no qx v10;', 'no s 1.2.3;', 'no s ;', 'no s v1.2.3;', 'no s v10;', 'no tr 1.2.3;', 'no tr ;', 'no tr v1.2.3;', 'no tr v10;', 'no y 1.2.3;', 'no y ;', 'no y v1.2.3;', 'no y v10;', 'use m 1.2.3;', 'use m ;', 'use m v1.2.3;', 'use m v10;', 'use q 1.2.3;', 'use q ;', 'use q v1.2.3;', 'use q v10;', 'use qq 1.2.3;', 'use qq ;', 'use qq v1.2.3;', 'use qq v10;', 'use qr 1.2.3;', 'use qr ;', 'use qr v1.2.3;', 'use qr v10;', 'use qw 1.2.3;', 'use qw ;', 'use qw v1.2.3;', 'use qw v10;', 'use qx 1.2.3;', 'use qx ;', 'use qx v1.2.3;', 'use qx v10;', 'use s 1.2.3;', 'use s ;', 'use s v1.2.3;', 'use s v10;', 'use tr 1.2.3;', 'use tr ;', 'use tr v1.2.3;', 'use tr v10;', 'use y 1.2.3;', 'use y ;', 'use y v1.2.3;', 'use y v10;'; - my %known_badish = map { $_ => 1 } 'use not ;', 'use lt ;', 'no and 1.2.3;', 'no and ;', 'no and v1.2.3;', 'no and v10;', 'no cmp 1.2.3;', 'no cmp ;', 'no cmp v1.2.3;', 'no cmp v10;', 'no eq 1.2.3;', 'no eq ;', 'no eq v1.2.3;', 'no eq v10;', 'no ge 1.2.3;', 'no ge ;', 'no ge v1.2.3;', 'no ge v10;', 'no gt 1.2.3;', 'no gt ;', 'no gt v1.2.3;', 'no gt v10;', 'no le 1.2.3;', 'no le ;', 'no le v1.2.3;', 'no le v10;', 'no lt 1.2.3;', 'no lt ;', 'no lt v1.2.3;', 'no lt v10;', 'no ne 1.2.3;', 'no ne ;', 'no ne v1.2.3;', 'no ne v10;', 'no not 1.2.3;', 'no not ;', 'no not v1.2.3;', 'no not v10;', 'no or 1.2.3;', 'no or ;', 'no or v1.2.3;', 'no or v10;', 'no x 1.2.3;', 'no x ;', 'no x v1.2.3;', 'no x v10;', 'no xor 1.2.3;', 'no xor ;', 'no xor v1.2.3;', 'no xor v10;', 'use and 1.2.3;', 'use and ;', 'use and v1.2.3;', 'use and v10;', 'use cmp 1.2.3;', 'use cmp ;', 'use cmp v1.2.3;', 'use cmp v10;', 'use eq 1.2.3;', 'use eq ;', 'use eq v1.2.3;', 'use eq v10;', 'use ge 1.2.3;', 'use ge ;', 'use ge v1.2.3;', 'use ge v10;', 'use gt 1.2.3;', 'use gt ;', 'use gt v1.2.3;', 'use gt v10;', 'use le 1.2.3;', 'use le ;', 'use le v1.2.3;', 'use le v10;', 'use lt 1.2.3;', 'use lt v1.2.3;', 'use lt v10;', 'use ne 1.2.3;', 'use ne ;', 'use ne v1.2.3;', 'use ne v10;', 'use not 1.2.3;', 'use not v1.2.3;', 'use not v10;', 'use or 1.2.3;', 'use or ;', 'use or v1.2.3;', 'use or v10;', 'use x 1.2.3;', 'use x ;', 'use x v1.2.3;', 'use x v10;', 'use xor 1.2.3;', 'use xor ;', 'use xor v1.2.3;', 'use xor v10;'; + my %known_bad = map { $_ => 1 } 'no m 1.2.3;', 'no m ;', 'no m v1.2.3;', + 'no m v10;', 'no q 1.2.3;', 'no q ;', 'no q v1.2.3;', 'no q v10;', + 'no qq 1.2.3;', 'no qq ;', 'no qq v1.2.3;', 'no qq v10;', 'no qr 1.2.3;', + 'no qr ;', 'no qr v1.2.3;', 'no qr v10;', 'no qw 1.2.3;', 'no qw ;', + 'no qw v1.2.3;', 'no qw v10;', 'no qx 1.2.3;', 'no qx ;', + 'no qx v1.2.3;', 'no qx v10;', 'no s 1.2.3;', 'no s ;', 'no s v1.2.3;', + 'no s v10;', 'no tr 1.2.3;', 'no tr ;', 'no tr v1.2.3;', 'no tr v10;', + 'no y 1.2.3;', 'no y ;', 'no y v1.2.3;', 'no y v10;', 'use m 1.2.3;', + 'use m ;', 'use m v1.2.3;', 'use m v10;', 'use q 1.2.3;', 'use q ;', + 'use q v1.2.3;', 'use q v10;', 'use qq 1.2.3;', 'use qq ;', + 'use qq v1.2.3;', 'use qq v10;', 'use qr 1.2.3;', 'use qr ;', + 'use qr v1.2.3;', 'use qr v10;', 'use qw 1.2.3;', 'use qw ;', + 'use qw v1.2.3;', 'use qw v10;', 'use qx 1.2.3;', 'use qx ;', + 'use qx v1.2.3;', 'use qx v10;', 'use s 1.2.3;', 'use s ;', + 'use s v1.2.3;', 'use s v10;', 'use tr 1.2.3;', 'use tr ;', + 'use tr v1.2.3;', 'use tr v10;', 'use y 1.2.3;', 'use y ;', + 'use y v1.2.3;', 'use y v10;'; + my %known_badish = map { $_ => 1 } 'use not ;', 'use lt ;', + 'no and 1.2.3;', 'no and ;', 'no and v1.2.3;', 'no and v10;', + 'no cmp 1.2.3;', 'no cmp ;', 'no cmp v1.2.3;', 'no cmp v10;', + 'no eq 1.2.3;', 'no eq ;', 'no eq v1.2.3;', 'no eq v10;', 'no ge 1.2.3;', + 'no ge ;', 'no ge v1.2.3;', 'no ge v10;', 'no gt 1.2.3;', 'no gt ;', + 'no gt v1.2.3;', 'no gt v10;', 'no le 1.2.3;', 'no le ;', + 'no le v1.2.3;', 'no le v10;', 'no lt 1.2.3;', 'no lt ;', + 'no lt v1.2.3;', 'no lt v10;', 'no ne 1.2.3;', 'no ne ;', + 'no ne v1.2.3;', 'no ne v10;', 'no not 1.2.3;', 'no not ;', + 'no not v1.2.3;', 'no not v10;', 'no or 1.2.3;', 'no or ;', + 'no or v1.2.3;', 'no or v10;', 'no x 1.2.3;', 'no x ;', 'no x v1.2.3;', + 'no x v10;', 'no xor 1.2.3;', 'no xor ;', 'no xor v1.2.3;', + 'no xor v10;', 'use and 1.2.3;', 'use and ;', 'use and v1.2.3;', + 'use and v10;', 'use cmp 1.2.3;', 'use cmp ;', 'use cmp v1.2.3;', + 'use cmp v10;', 'use eq 1.2.3;', 'use eq ;', 'use eq v1.2.3;', + 'use eq v10;', 'use ge 1.2.3;', 'use ge ;', 'use ge v1.2.3;', + 'use ge v10;', 'use gt 1.2.3;', 'use gt ;', 'use gt v1.2.3;', + 'use gt v10;', 'use le 1.2.3;', 'use le ;', 'use le v1.2.3;', + 'use le v10;', 'use lt 1.2.3;', 'use lt v1.2.3;', 'use lt v10;', + 'use ne 1.2.3;', 'use ne ;', 'use ne v1.2.3;', 'use ne v10;', + 'use not 1.2.3;', 'use not v1.2.3;', 'use not v10;', 'use or 1.2.3;', + 'use or ;', 'use or v1.2.3;', 'use or v10;', 'use x 1.2.3;', 'use x ;', + 'use x v1.2.3;', 'use x v10;', 'use xor 1.2.3;', 'use xor ;', + 'use xor v1.2.3;', 'use xor v10;'; for my $name ( # normal names 'Foo', @@ -245,41 +252,56 @@ KEYWORDS_AS_MODULE_NAMES: { '__LINE__', '__SUB__', 'AUTOLOAD', - ) { - for my $include ( 'use', 'no' ) { # 'require' does not force tokes to be words + ) + { + for my $include ( 'use', 'no' ) + { # 'require' does not force tokes to be words for my $version ( '', 'v1.2.3', '1.2.3', 'v10' ) { my $code = "$include $name $version;"; my $Document = safe_new \"$code 999;"; subtest "'$code'", => sub { -{ - local $TODO = $known_bad{$code} ? "known bug" : undef; - is( $Document->schildren(), 2, "$code number of statements in document" ); -} - isa_ok( $Document->schild(0), 'PPI::Statement::Include', $code ); -{ - local $TODO = ($known_bad{$code}||$known_badish{$code}) ? "known bug" : undef; - # first child is the include statement - my $expected_tokens = [ - [ 'PPI::Token::Word', $include ], - [ 'PPI::Token::Word', $name ], - ]; - if ( $version ) { - push @$expected_tokens, [ 'PPI::Token::Number::Version', $version ]; - } - push @$expected_tokens, [ 'PPI::Token::Structure', ';' ]; - my $got_tokens = [ map { [ ref $_, "$_" ] } $Document->schild(0)->schildren() ]; - is_deeply( $got_tokens, $expected_tokens, "$code tokens as expected" ); -} - -{ - local $TODO = $known_bad{$code} ? "known bug" : undef; - # second child not swallowed up by the first - isa_ok( $Document->schild(1), 'PPI::Statement', "$code prior statement end recognized" ); - isa_ok( eval { $Document->schild(1)->schild(0) }, 'PPI::Token::Number', $code ); - is( eval { $Document->schild(1)->schild(0) }, '999', "$code number correct" ); -} + { + local $TODO = $known_bad{$code} ? "known bug" : undef; + is( $Document->schildren(), + 2, "$code number of statements in document" ); + } + isa_ok( $Document->schild(0), + 'PPI::Statement::Include', $code ); + { + local $TODO = + ( $known_bad{$code} || $known_badish{$code} ) + ? "known bug" + : undef; + # first child is the include statement + my $expected_tokens = [ + [ 'PPI::Token::Word', $include ], + [ 'PPI::Token::Word', $name ], + ]; + if ($version) { + push @$expected_tokens, + [ 'PPI::Token::Number::Version', $version ]; + } + push @$expected_tokens, + [ 'PPI::Token::Structure', ';' ]; + my $got_tokens = [ map { [ ref $_, "$_" ] } + $Document->schild(0)->schildren() ]; + is_deeply( $got_tokens, $expected_tokens, + "$code tokens as expected" ); + } + + { + local $TODO = $known_bad{$code} ? "known bug" : undef; + # second child not swallowed up by the first + isa_ok( $Document->schild(1), + 'PPI::Statement', + "$code prior statement end recognized" ); + isa_ok( eval { $Document->schild(1)->schild(0) }, + 'PPI::Token::Number', $code ); + is( eval { $Document->schild(1)->schild(0) }, + '999', "$code number correct" ); + } }; } } diff --git a/t/ppi_statement_package.t b/t/ppi_statement_package.t index 6a042c7d..b318de71 100644 --- a/t/ppi_statement_package.t +++ b/t/ppi_statement_package.t @@ -4,13 +4,12 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 2508 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 2508 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); -use PPI (); +use PPI (); use PPI::Singletons qw( %KEYWORDS ); use Helper 'safe_new'; - HASH_CONSTRUCTORS_DONT_CONTAIN_PACKAGES_RT52259: { my $Document = safe_new \<<'END_PERL'; { package => "", }; @@ -22,16 +21,17 @@ HASH_CONSTRUCTORS_DONT_CONTAIN_PACKAGES_RT52259: { END_PERL my $packages = $Document->find('PPI::Statement::Package'); - my $test_name = 'Found no package statements in hash constructors - RT #52259'; - if (not $packages) { + my $test_name = + 'Found no package statements in hash constructors - RT #52259'; + if ( not $packages ) { pass $test_name; - } elsif ( not is(scalar @{$packages}, 0, $test_name) ) { + } + elsif ( not is( scalar @{$packages}, 0, $test_name ) ) { diag 'Package statements found:'; diag $_->parent()->parent()->content() foreach @{$packages}; } } - INSIDE_SCOPE: { # Create a document with various example package statements my $Document = safe_new \<<'END_PERL'; @@ -49,18 +49,26 @@ END_PERL # Check that both of the package statements are detected my $packages = $Document->find('Statement::Package'); is( scalar(@$packages), 4, 'Found 2 package statements' ); - is( $packages->[0]->namespace, 'Foo', 'Package 1 returns correct namespace' ); - is( $packages->[1]->namespace, 'Bar::Baz', 'Package 2 returns correct namespace' ); - is( $packages->[2]->namespace, 'Other', 'Package 3 returns correct namespace' ); - is( $packages->[3]->namespace, 'Again', 'Package 4 returns correct namespace' ); - is( $packages->[0]->file_scoped, 1, '->file_scoped returns true for package 1' ); - is( $packages->[1]->file_scoped, '', '->file_scoped returns false for package 2' ); - is( $packages->[2]->file_scoped, 1, '->file_scoped returns true for package 3' ); - is( $packages->[3]->file_scoped, 1, '->file_scoped returns true for package 4' ); - is( $packages->[0]->version, '', 'Package 1 has no version' ); - is( $packages->[1]->version, '', 'Package 2 has no version' ); + is( $packages->[0]->namespace, + 'Foo', 'Package 1 returns correct namespace' ); + is( $packages->[1]->namespace, + 'Bar::Baz', 'Package 2 returns correct namespace' ); + is( $packages->[2]->namespace, + 'Other', 'Package 3 returns correct namespace' ); + is( $packages->[3]->namespace, + 'Again', 'Package 4 returns correct namespace' ); + is( $packages->[0]->file_scoped, + 1, '->file_scoped returns true for package 1' ); + is( $packages->[1]->file_scoped, + '', '->file_scoped returns false for package 2' ); + is( $packages->[2]->file_scoped, + 1, '->file_scoped returns true for package 3' ); + is( $packages->[3]->file_scoped, + 1, '->file_scoped returns true for package 4' ); + is( $packages->[0]->version, '', 'Package 1 has no version' ); + is( $packages->[1]->version, '', 'Package 2 has no version' ); is( $packages->[2]->version, 'v1.23', 'Package 3 returns correct version' ); - is( $packages->[3]->version, '0.09', 'Package 4 returns correct version' ); + is( $packages->[3]->version, '0.09', 'Package 4 returns correct version' ); } PERL_5_12_SYNTAX: { @@ -83,29 +91,30 @@ PERL_5_12_SYNTAX: { ); my @versions = ( [ 'v1.2.3 ', 'PPI::Token::Number::Version' ], - [ 'v1.2.3', 'PPI::Token::Number::Version' ], - [ '0.50 ', 'PPI::Token::Number::Float' ], - [ '0.50', 'PPI::Token::Number::Float' ], - [ '', '' ], # omit version, traditional + [ 'v1.2.3', 'PPI::Token::Number::Version' ], + [ '0.50 ', 'PPI::Token::Number::Float' ], + [ '0.50', 'PPI::Token::Number::Float' ], + [ '', '' ], # omit version, traditional ); my @blocks = ( - [ ';', 'PPI::Token::Structure' ], # traditional package syntax - [ '{ 1 }', 'PPI::Structure::Block' ], # 5.12 package syntax + [ ';', 'PPI::Token::Structure' ], # traditional package syntax + [ '{ 1 }', 'PPI::Structure::Block' ], # 5.12 package syntax ); $_->[2] = strip_ws_padding( $_->[0] ) for @versions, @blocks; - for my $name ( @names ) { - for my $version_pair ( @versions ) { - for my $block_pair ( @blocks ) { - my @test = prepare_package_test( $version_pair, $block_pair, $name ); - test_package_blocks( @test ); + for my $name (@names) { + for my $version_pair (@versions) { + for my $block_pair (@blocks) { + my @test = + prepare_package_test( $version_pair, $block_pair, $name ); + test_package_blocks(@test); } } } } sub strip_ws_padding { - my ( $string ) = @_; + my ($string) = @_; $string =~ s/(^\s+|\s+$)//g; return $string; } @@ -114,14 +123,14 @@ sub prepare_package_test { my ( $version_pair, $block_pair, $name ) = @_; my ( $version, $version_type, $version_stripped ) = @{$version_pair}; - my ( $block, $block_type, $block_stripped ) = @{$block_pair}; + my ( $block, $block_type, $block_stripped ) = @{$block_pair}; my $code = "package $name $version$block"; my $expected_package_tokens = [ [ 'PPI::Token::Word', 'package' ], [ 'PPI::Token::Word', $name ], - ($version ne '') ? [ $version_type, $version_stripped ] : (), + ( $version ne '' ) ? [ $version_type, $version_stripped ] : (), [ $block_type, $block_stripped ], ]; @@ -133,18 +142,24 @@ sub test_package_blocks { subtest "'$code'", sub { - my $Document = safe_new \"$code 999;"; - is( $Document->schildren, 2, "correct number of statements in document" ); - isa_ok( $Document->schild(0), 'PPI::Statement::Package', "entire code" ); - - # first child is the package statement - my $got_tokens = [ map { [ ref $_, "$_" ] } $Document->schild(0)->schildren ]; - is_deeply( $got_tokens, $expected_package_tokens, "tokens as expected" ); - - # second child not swallowed up by the first - isa_ok( $Document->schild(1), 'PPI::Statement', "code prior statement end recognized" ); - isa_ok( eval { $Document->schild(1)->schild(0) }, 'PPI::Token::Number', "inner code" ); - is( eval { $Document->schild(1)->schild(0) }, '999', "number correct" ); + my $Document = safe_new \"$code 999;"; + is( $Document->schildren, 2, + "correct number of statements in document" ); + isa_ok( $Document->schild(0), 'PPI::Statement::Package', + "entire code" ); + + # first child is the package statement + my $got_tokens = + [ map { [ ref $_, "$_" ] } $Document->schild(0)->schildren ]; + is_deeply( $got_tokens, $expected_package_tokens, + "tokens as expected" ); + + # second child not swallowed up by the first + isa_ok( $Document->schild(1), + 'PPI::Statement', "code prior statement end recognized" ); + isa_ok( eval { $Document->schild(1)->schild(0) }, + 'PPI::Token::Number', "inner code" ); + is( eval { $Document->schild(1)->schild(0) }, '999', "number correct" ); }; diff --git a/t/ppi_statement_scheduled.t b/t/ppi_statement_scheduled.t index c406b0ed..ed35fc9a 100644 --- a/t/ppi_statement_scheduled.t +++ b/t/ppi_statement_scheduled.t @@ -4,14 +4,13 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 280 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 280 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); use Helper 'safe_new'; - SUB_WORD_OPTIONAL: { - for my $name ( qw( BEGIN CHECK UNITCHECK INIT END ) ) { + for my $name (qw( BEGIN CHECK UNITCHECK INIT END )) { for my $sub ( '', 'sub ' ) { # '{}' -- function definition @@ -31,13 +30,15 @@ sub test_sub_as { my $Document = safe_new \$code; my ( $sub_statement, $dummy ) = $Document->schildren; - isa_ok( $sub_statement, 'PPI::Statement::Scheduled', "$code: document child is a scheduled statement" ); + isa_ok( $sub_statement, 'PPI::Statement::Scheduled', + "$code: document child is a scheduled statement" ); is( $dummy, undef, "$code: document has exactly one child" ); ok( $sub_statement->reserved, "$code: is reserved" ); is( $sub_statement->name, $name, "$code: name() correct" ); if ( $followed_by =~ /}/ ) { - isa_ok( $sub_statement->block, 'PPI::Structure::Block', "$code: has a block" ); + isa_ok( $sub_statement->block, 'PPI::Structure::Block', + "$code: has a block" ); } else { ok( !$sub_statement->block, "$code: has no block" ); diff --git a/t/ppi_statement_sub.t b/t/ppi_statement_sub.t index 7b193b39..c5ab5ab8 100644 --- a/t/ppi_statement_sub.t +++ b/t/ppi_statement_sub.t @@ -4,40 +4,42 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 1297 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 1297 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); -use PPI (); +use PPI (); use PPI::Singletons qw( %KEYWORDS ); use Helper 'safe_new'; NAME: { for my $test ( - { code => 'sub foo {}', name => 'foo' }, - { code => 'sub foo{}', name => 'foo' }, - { code => 'sub FOO {}', name => 'FOO' }, - { code => 'sub _foo {}', name => '_foo' }, - { code => 'sub _0foo {}', name => '_0foo' }, - { code => 'sub _foo0 {}', name => '_foo0' }, - { code => 'sub ___ {}', name => '___' }, - { code => 'sub bar() {}', name => 'bar' }, - { code => 'sub baz : method{}', name => 'baz' }, - { code => 'sub baz : method lvalue{}', name => 'baz' }, - { code => 'sub baz : method:lvalue{}', name => 'baz' }, + { code => 'sub foo {}', name => 'foo' }, + { code => 'sub foo{}', name => 'foo' }, + { code => 'sub FOO {}', name => 'FOO' }, + { code => 'sub _foo {}', name => '_foo' }, + { code => 'sub _0foo {}', name => '_0foo' }, + { code => 'sub _foo0 {}', name => '_foo0' }, + { code => 'sub ___ {}', name => '___' }, + { code => 'sub bar() {}', name => 'bar' }, + { code => 'sub baz : method{}', name => 'baz' }, + { code => 'sub baz : method lvalue{}', name => 'baz' }, + { code => 'sub baz : method:lvalue{}', name => 'baz' }, { code => 'sub baz (*) : method : lvalue{}', name => 'baz' }, - { code => 'sub x64 {}', name => 'x64' }, # should not be parsed as x operator - ) { + { code => 'sub x64 {}', name => 'x64' } + , # should not be parsed as x operator + ) + { my $code = $test->{code}; my $name = $test->{name}; subtest "'$code'", => sub { - my $Document = safe_new \$code; + my $Document = safe_new \$code; - my ( $sub_statement, $dummy ) = $Document->schildren; - isa_ok( $sub_statement, 'PPI::Statement::Sub', "document child" ); - is( $dummy, undef, "document has exactly one child" ); + my ( $sub_statement, $dummy ) = $Document->schildren; + isa_ok( $sub_statement, 'PPI::Statement::Sub', "document child" ); + is( $dummy, undef, "document has exactly one child" ); - is( eval { $sub_statement->name }, $name, "name() correct" ); + is( eval { $sub_statement->name }, $name, "name() correct" ); }; @@ -46,19 +48,21 @@ NAME: { LEXSUB: { for my $test ( - { code => 'sub foo {}', type => undef }, - { code => 'my sub foo {}', type => 'my' }, - { code => 'our sub foo {}', type => 'our' }, - { code => 'state sub foo {}', type => 'state' }, + { code => 'sub foo {}', type => undef }, + { code => 'my sub foo {}', type => 'my' }, + { code => 'our sub foo {}', type => 'our' }, + { code => 'state sub foo {}', type => 'state' }, { code => 'my sub foo ($) {}', type => 'my' }, - ) { + ) + { my $code = $test->{code}; my $type = $test->{type}; my $Document = safe_new \$code; my ( $sub_statement, $dummy ) = $Document->schildren(); - isa_ok( $sub_statement, 'PPI::Statement::Sub', "$code: document child is a sub" ); + isa_ok( $sub_statement, 'PPI::Statement::Sub', + "$code: document child is a sub" ); is( $dummy, undef, "$code: document has exactly one child" ); is( $sub_statement->type, $type, "$code: type matches" ); is( $sub_statement->name, 'foo', "$code: name matches" ); @@ -68,7 +72,7 @@ LEXSUB: { SUB_WORD_OPTIONAL: { # 'sub' is optional for these special subs. Make sure they're # recognized as subs and sub declarations. - for my $name ( qw( AUTOLOAD DESTROY ) ) { + for my $name (qw( AUTOLOAD DESTROY )) { for my $sub ( '', 'sub ' ) { # '{}' -- function definition @@ -85,12 +89,12 @@ SUB_WORD_OPTIONAL: { # statement terminator. Make sure statements following them are # not gobbled. my $desc = 'regression: word+block not gobbling to statement terminator'; - for my $word ( qw( AUTOLOAD DESTROY ) ) { - my $Document = safe_new \"$word {} sub foo {}"; + for my $word (qw( AUTOLOAD DESTROY )) { + my $Document = safe_new \"$word {} sub foo {}"; my $statements = $Document->find('Statement::Sub') || []; is( scalar(@$statements), 2, "$desc for $word + sub" ); - - $Document = safe_new \"$word {} package;"; + + $Document = safe_new \"$word {} package;"; $statements = $Document->find('Statement::Sub') || []; is( scalar(@$statements), 1, "$desc for $word + package" ); $statements = $Document->find('Statement::Package') || []; @@ -102,19 +106,17 @@ PROTOTYPE: { # Doesn't have to be as thorough as ppi_token_prototype.t, since # we're just making sure PPI::Token::Prototype->prototype gets # passed through correctly. - for my $test ( - [ '', undef ], - [ '()', '' ], - [ '( $*Z@ )', '$*Z@' ], - ) { + for my $test ( [ '', undef ], [ '()', '' ], [ '( $*Z@ )', '$*Z@' ], ) { my ( $proto_text, $expected ) = @$test; my $Document = safe_new \"sub foo $proto_text {}"; my ( $sub_statement, $dummy ) = $Document->schildren(); - isa_ok( $sub_statement, 'PPI::Statement::Sub', "$proto_text document child is a sub" ); + isa_ok( $sub_statement, 'PPI::Statement::Sub', + "$proto_text document child is a sub" ); is( $dummy, undef, "$proto_text document has exactly one child" ); - is( $sub_statement->prototype, $expected, "$proto_text: prototype matches" ); + is( $sub_statement->prototype, $expected, + "$proto_text: prototype matches" ); } } @@ -122,68 +124,75 @@ PROTOTYPE_LEXSUB: { # Doesn't have to be as thorough as ppi_token_prototype.t, since # we're just making sure PPI::Token::Prototype->prototype gets # passed through correctly. - for my $test ( - [ '', undef ], - [ '()', '' ], - [ '( $*Z@ )', '$*Z@' ], - ) { + for my $test ( [ '', undef ], [ '()', '' ], [ '( $*Z@ )', '$*Z@' ], ) { my ( $proto_text, $expected ) = @$test; my $Document = safe_new \"my sub foo $proto_text {}"; my ( $sub_statement, $dummy ) = $Document->schildren(); - isa_ok( $sub_statement, 'PPI::Statement::Sub', "$proto_text document child is a sub" ); + isa_ok( $sub_statement, 'PPI::Statement::Sub', + "$proto_text document child is a sub" ); is( $dummy, undef, "$proto_text document has exactly one child" ); - is( $sub_statement->prototype, $expected, "$proto_text: prototype matches" ); + is( $sub_statement->prototype, $expected, + "$proto_text: prototype matches" ); } } BLOCK_AND_FORWARD: { for my $test ( - { code => 'sub foo {1;}', block => '{1;}' }, - { code => 'sub foo{2;};', block => '{2;}' }, + { code => 'sub foo {1;}', block => '{1;}' }, + { code => 'sub foo{2;};', block => '{2;}' }, { code => "sub foo\n{3;};", block => '{3;}' }, - { code => 'sub foo;', block => '' }, - { code => 'sub foo', block => '' }, - ) { - my $code = $test->{code}; + { code => 'sub foo;', block => '' }, + { code => 'sub foo', block => '' }, + ) + { + my $code = $test->{code}; my $block = $test->{block}; my $Document = safe_new \$code; my ( $sub_statement, $dummy ) = $Document->schildren(); - isa_ok( $sub_statement, 'PPI::Statement::Sub', "$code: document child is a sub" ); + isa_ok( $sub_statement, 'PPI::Statement::Sub', + "$code: document child is a sub" ); is( $dummy, undef, "$code: document has exactly one child" ); is( $sub_statement->block, $block, "$code: block matches" ); - is( !$sub_statement->block, !!$sub_statement->forward, "$code: block and forward are opposites" ); + is( + !$sub_statement->block, + !!$sub_statement->forward, + "$code: block and forward are opposites" + ); } } RESERVED: { for my $test ( - { code => 'sub BEGIN {}', reserved => 1 }, - { code => 'sub CHECK {}', reserved => 1 }, - { code => 'sub UNITCHECK {}', reserved => 1 }, - { code => 'sub INIT {}', reserved => 1 }, - { code => 'sub END {}', reserved => 1 }, - { code => 'sub AUTOLOAD {}', reserved => 1 }, + { code => 'sub BEGIN {}', reserved => 1 }, + { code => 'sub CHECK {}', reserved => 1 }, + { code => 'sub UNITCHECK {}', reserved => 1 }, + { code => 'sub INIT {}', reserved => 1 }, + { code => 'sub END {}', reserved => 1 }, + { code => 'sub AUTOLOAD {}', reserved => 1 }, { code => 'sub CLONE_SKIP {}', reserved => 1 }, - { code => 'sub __SUB__ {}', reserved => 1 }, - { code => 'sub _FOO {}', reserved => 1 }, - { code => 'sub FOO9 {}', reserved => 1 }, - { code => 'sub FO9O {}', reserved => 1 }, - { code => 'sub FOo {}', reserved => 0 }, - ) { - my $code = $test->{code}; + { code => 'sub __SUB__ {}', reserved => 1 }, + { code => 'sub _FOO {}', reserved => 1 }, + { code => 'sub FOO9 {}', reserved => 1 }, + { code => 'sub FO9O {}', reserved => 1 }, + { code => 'sub FOo {}', reserved => 0 }, + ) + { + my $code = $test->{code}; my $reserved = $test->{reserved}; my $Document = safe_new \$code; my ( $sub_statement, $dummy ) = $Document->schildren(); - isa_ok( $sub_statement, 'PPI::Statement::Sub', "$code: document child is a sub" ); + isa_ok( $sub_statement, 'PPI::Statement::Sub', + "$code: document child is a sub" ); is( $dummy, undef, "$code: document has exactly one child" ); - is( !!$sub_statement->reserved, !!$reserved, "$code: reserved matches" ); + is( !!$sub_statement->reserved, !!$reserved, + "$code: reserved matches" ); } } @@ -194,14 +203,17 @@ sub test_sub_as { my $Document = safe_new \$code; my ( $sub_statement, $dummy ) = $Document->schildren; - isa_ok( $sub_statement, 'PPI::Statement::Sub', "$code: document child is a sub" ); - isnt( ref $sub_statement, 'PPI::Statement::Scheduled', "$code: not a PPI::Statement::Scheduled" ); + isa_ok( $sub_statement, 'PPI::Statement::Sub', + "$code: document child is a sub" ); + isnt( ref $sub_statement, + 'PPI::Statement::Scheduled', "$code: not a PPI::Statement::Scheduled" ); is( $dummy, undef, "$code: document has exactly one child" ); ok( $sub_statement->reserved, "$code: is reserved" ); is( $sub_statement->name, $name, "$code: name() correct" ); if ( $followed_by =~ /}/ ) { - isa_ok( $sub_statement->block, 'PPI::Structure::Block', "$code: has a block" ); + isa_ok( $sub_statement->block, 'PPI::Structure::Block', + "$code: has a block" ); } else { ok( !$sub_statement->block, "$code: has no block" ); @@ -227,23 +239,23 @@ KEYWORDS_AS_SUB_NAMES: { 'AUTOLOAD', ); my @blocks = ( - [ ';', 'PPI::Token::Structure' ], - [ ' ;', 'PPI::Token::Structure' ], - [ '{ 1 }', 'PPI::Structure::Block' ], + [ ';', 'PPI::Token::Structure' ], + [ ' ;', 'PPI::Token::Structure' ], + [ '{ 1 }', 'PPI::Structure::Block' ], [ ' { 1 }', 'PPI::Structure::Block' ], ); $_->[2] = strip_ws_padding( $_->[0] ) for @blocks; - for my $name ( @names ) { - for my $block_pair ( @blocks ) { + for my $name (@names) { + for my $block_pair (@blocks) { my @test = prepare_sub_test( $block_pair, $name ); - test_subs( @test ); + test_subs(@test); } } } sub strip_ws_padding { - my ( $string ) = @_; + my ($string) = @_; $string =~ s/(^\s+|\s+$)//g; return $string; } @@ -258,7 +270,7 @@ sub prepare_sub_test { my $expected_sub_tokens = [ [ 'PPI::Token::Word', 'sub' ], [ 'PPI::Token::Word', $name ], - [ $block_type, $block_stripped ], + [ $block_type, $block_stripped ], ]; return ( $code, $expected_sub_tokens ); @@ -269,17 +281,21 @@ sub test_subs { subtest "'$code'", => sub { - my $Document = safe_new \"$code 999;"; - is( $Document->schildren, 2, "number of statements in document" ); - isa_ok( $Document->schild(0), 'PPI::Statement::Sub', "entire code" ); - - my $got_tokens = [ map { [ ref $_, "$_" ] } $Document->schild(0)->schildren ]; - is_deeply( $got_tokens, $expected_sub_tokens, "$code tokens as expected" ); - - # second child not swallowed up by the first - isa_ok( $Document->schild(1), 'PPI::Statement', "prior statement end recognized" ); - isa_ok( eval { $Document->schild(1)->schild(0) }, 'PPI::Token::Number', "inner code" ); - is( eval { $Document->schild(1)->schild(0) }, '999', "number correct" ); + my $Document = safe_new \"$code 999;"; + is( $Document->schildren, 2, "number of statements in document" ); + isa_ok( $Document->schild(0), 'PPI::Statement::Sub', "entire code" ); + + my $got_tokens = + [ map { [ ref $_, "$_" ] } $Document->schild(0)->schildren ]; + is_deeply( $got_tokens, $expected_sub_tokens, + "$code tokens as expected" ); + + # second child not swallowed up by the first + isa_ok( $Document->schild(1), + 'PPI::Statement', "prior statement end recognized" ); + isa_ok( eval { $Document->schild(1)->schild(0) }, + 'PPI::Token::Number', "inner code" ); + is( eval { $Document->schild(1)->schild(0) }, '999', "number correct" ); }; diff --git a/t/ppi_statement_variable.t b/t/ppi_statement_variable.t index 220a713e..9b211915 100644 --- a/t/ppi_statement_variable.t +++ b/t/ppi_statement_variable.t @@ -4,12 +4,11 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 18 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 18 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); use Helper 'safe_new'; - VARIABLES: { # Test the things we assert to work in the synopsis my $Document = safe_new \<<'END_PERL'; @@ -27,16 +26,24 @@ END_PERL # There should be 6 statement objects my $ST = $Document->find('Statement::Variable'); - is( ref($ST), 'ARRAY', 'Found statements' ); - is( scalar(@$ST), 7, 'Found 7 ::Variable objects' ); - foreach my $Var ( @$ST ) { + is( ref($ST), 'ARRAY', 'Found statements' ); + is( scalar(@$ST), 7, 'Found 7 ::Variable objects' ); + foreach my $Var (@$ST) { isa_ok( $Var, 'PPI::Statement::Variable' ); } - is_deeply( [ $ST->[0]->variables ], [ '$foo' ], '1: Found $foo' ); - is_deeply( [ $ST->[1]->variables ], [ '$foo', '$bar' ], '2: Found $foo and $bar' ); - is_deeply( [ $ST->[2]->variables ], [ '$foo' ], '3: Found $foo' ); - is_deeply( [ $ST->[3]->variables ], [ '$foo' ], '4: Found $foo' ); - is_deeply( [ $ST->[4]->variables ], [ '$foo' ], '5: Found $foo' ); - is_deeply( [ $ST->[5]->variables ], [ '$foo' ], '6: Found $foo' ); - is_deeply( [ $ST->[6]->variables ], [ '$foo', '$bar' ], '7: Found $foo and $bar' ); + is_deeply( [ $ST->[0]->variables ], ['$foo'], '1: Found $foo' ); + is_deeply( + [ $ST->[1]->variables ], + [ '$foo', '$bar' ], + '2: Found $foo and $bar' + ); + is_deeply( [ $ST->[2]->variables ], ['$foo'], '3: Found $foo' ); + is_deeply( [ $ST->[3]->variables ], ['$foo'], '4: Found $foo' ); + is_deeply( [ $ST->[4]->variables ], ['$foo'], '5: Found $foo' ); + is_deeply( [ $ST->[5]->variables ], ['$foo'], '6: Found $foo' ); + is_deeply( + [ $ST->[6]->variables ], + [ '$foo', '$bar' ], + '7: Found $foo and $bar' + ); } diff --git a/t/ppi_token.t b/t/ppi_token.t index 850a4e67..259e2fdf 100644 --- a/t/ppi_token.t +++ b/t/ppi_token.t @@ -4,15 +4,15 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 5 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 5 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); MODIFICATION: { - my $one = PPI::Token->new( "" ); + my $one = PPI::Token->new(""); is $one->length, 0, "empty token has no length"; - ok $one->add_content( "abcde" ), "can add strings"; + ok $one->add_content("abcde"), "can add strings"; is $one->length, 5, "adding actually adds"; - ok $one->set_content( "abc" ), "can set content"; + ok $one->set_content("abc"), "can set content"; is $one->length, 3, "setting overwrites"; } diff --git a/t/ppi_token__quoteengine_full.t b/t/ppi_token__quoteengine_full.t index 4ba66b14..234f1925 100644 --- a/t/ppi_token__quoteengine_full.t +++ b/t/ppi_token__quoteengine_full.t @@ -4,93 +4,91 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 123 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 123 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); use Helper 'safe_new'; - NEW: { # Verify that Token::Quote, Token::QuoteLike and Token::Regexp # do not have ->new functions - my $RE_SYMBOL = qr/\A(?!\d)\w+\z/; - foreach my $name ( qw{Token::Quote Token::QuoteLike Token::Regexp} ) { + my $RE_SYMBOL = qr/\A(?!\d)\w+\z/; + foreach my $name (qw{Token::Quote Token::QuoteLike Token::Regexp}) { no strict 'refs'; my @functions = sort - grep { defined &{"${name}::$_"} } - grep { /$RE_SYMBOL/o } - keys %{"PPI::${name}::"}; - is( scalar(grep { $_ eq 'new' } @functions), 0, - "$name does not have a new function" ); + grep { defined &{"${name}::$_"} } + grep { /$RE_SYMBOL/o } + keys %{"PPI::${name}::"}; + is( scalar( grep { $_ eq 'new' } @functions ), + 0, "$name does not have a new function" ); } } - # This primarily to ensure that qw() with non-balanced types # are treated the same as those with balanced types. QW: { - my @seps = ( undef, undef, '/', '#', ',' ); - my @types = ( '()', '<>', '//', '##', ',,' ); - my @braced = ( qw{ 1 1 0 0 0 } ); + my @seps = ( undef, undef, '/', '#', ',' ); + my @types = ( '()', '<>', '//', '##', ',,' ); + my @braced = (qw{ 1 1 0 0 0 }); my $i = 0; - for my $q ('qw()', 'qw<>', 'qw//', 'qw##', 'qw,,') { + for my $q ( 'qw()', 'qw<>', 'qw//', 'qw##', 'qw,,' ) { my $d = safe_new \$q; my $o = $d->{children}->[0]->{children}->[0]; my $s = $o->{sections}->[0]; - is( $o->{operator}, 'qw', "$q correct operator" ); - is( $o->{_sections}, 1, "$q correct _sections" ); - is( $o->{braced}, $braced[$i], "$q correct braced" ); - is( $o->{separator}, $seps[$i], "$q correct separator" ); - is( $o->{content}, $q, "$q correct content" ); - is( $s->{position}, 3, "$q correct position" ); - is( $s->{type}, $types[$i], "$q correct type" ); - is( $s->{size}, 0, "$q correct size" ); + is( $o->{operator}, 'qw', "$q correct operator" ); + is( $o->{_sections}, 1, "$q correct _sections" ); + is( $o->{braced}, $braced[$i], "$q correct braced" ); + is( $o->{separator}, $seps[$i], "$q correct separator" ); + is( $o->{content}, $q, "$q correct content" ); + is( $s->{position}, 3, "$q correct position" ); + is( $s->{type}, $types[$i], "$q correct type" ); + is( $s->{size}, 0, "$q correct size" ); $i++; } } - QW2: { - my @stuff = ( qw-( ) < > / / -, '#', '#', ',',',' ); - my @seps = ( undef, undef, '/', '#', ',' ); - my @braced = ( qw{ 1 1 0 0 0 } ); - my @secs = ( qw{ 1 1 1 1 1 } ); + my @stuff = ( qw-( ) < > / / -, '#', '#', ',', ',' ); + my @seps = ( undef, undef, '/', '#', ',' ); + my @braced = (qw{ 1 1 0 0 0 }); + my @secs = (qw{ 1 1 1 1 1 }); my $i = 0; - while ( @stuff ) { + while (@stuff) { my $opener = shift @stuff; my $closer = shift @stuff; - my $d = safe_new \"qw${opener}a"; - my $o = $d->{children}->[0]->{children}->[0]; - my $s = $o->{sections}->[0]; - is( $o->{operator}, 'qw', "qw$opener correct operator" ); - is( $o->{_sections}, $secs[$i], "qw$opener correct _sections" ); - is( $o->{braced}, $braced[$i], "qw$opener correct braced" ); - is( $o->{separator}, $seps[$i], "qw$opener correct separator" ); - is( $o->{content}, "qw${opener}a", "qw$opener correct content" ); + my $d = safe_new \"qw${opener}a"; + my $o = $d->{children}->[0]->{children}->[0]; + my $s = $o->{sections}->[0]; + is( $o->{operator}, 'qw', "qw$opener correct operator" ); + is( $o->{_sections}, $secs[$i], "qw$opener correct _sections" ); + is( $o->{braced}, $braced[$i], "qw$opener correct braced" ); + is( $o->{separator}, $seps[$i], "qw$opener correct separator" ); + is( $o->{content}, "qw${opener}a", "qw$opener correct content" ); + if ( $secs[$i] ) { - is( $s->{type}, "$opener$closer", "qw$opener correct type" ); + is( $s->{type}, "$opener$closer", "qw$opener correct type" ); } $i++; } } - OTHER: { foreach ( - [ '/foo/i', 'foo', undef, { i => 1 }, [ '//' ] ], - [ 'mx', 'foo', undef, { x => 1 }, [ '<>' ] ], + [ '/foo/i', 'foo', undef, { i => 1 }, ['//'] ], + [ 'mx', 'foo', undef, { x => 1 }, ['<>'] ], [ 's{foo}[bar]g', 'foo', 'bar', { g => 1 }, [ '{}', '[]' ] ], [ 'tr/fo/ba/', 'fo', 'ba', {}, [ '//', '//' ] ], - [ 'qr{foo}smx', 'foo', undef, { s => 1, m => 1, x => 1 }, - [ '{}' ] ], - ) { - my ( $code, $match, $subst, $mods, $delims ) = @{ $_ }; + [ 'qr{foo}smx', 'foo', undef, { s => 1, m => 1, x => 1 }, ['{}'] ], + ) + { + my ( $code, $match, $subst, $mods, $delims ) = @{$_}; my $doc = safe_new \$code; $doc or warn "'$code' did not create a document"; - my $obj = $doc->child( 0 )->child( 0 ); - is( $obj->_section_content( 0 ), $match, "$code correct match" ); - is( $obj->_section_content( 1 ), $subst, "$code correct subst" ); + my $obj = $doc->child(0)->child(0); + is( $obj->_section_content(0), $match, "$code correct match" ); + is( $obj->_section_content(1), $subst, "$code correct subst" ); is_deeply( { $obj->_modifiers() }, $mods, "$code correct modifiers" ); - is_deeply( [ $obj->_delimiters() ], $delims, "$code correct delimiters" ); + is_deeply( [ $obj->_delimiters() ], + $delims, "$code correct delimiters" ); } } diff --git a/t/ppi_token_attribute.t b/t/ppi_token_attribute.t index a83a1d9e..b9646a6e 100644 --- a/t/ppi_token_attribute.t +++ b/t/ppi_token_attribute.t @@ -4,7 +4,7 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 2235 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 2235 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); use Helper 'safe_new'; @@ -15,10 +15,10 @@ sub permute_test; PARSING_AND_METHODS: { # no attribute execute_test 'sub foo {}', []; - execute_test 'sub foo;', []; + execute_test 'sub foo;', []; # perl allows there to be no attributes following the colon. - execute_test 'sub foo:{}', []; + execute_test 'sub foo:{}', []; execute_test 'sub foo : {}', []; # Attribute with no parameters @@ -27,7 +27,7 @@ PARSING_AND_METHODS: { permute_test 'foo', [ [ 'Attr1', undef ] ]; permute_test 'method', [ [ 'Attr1', undef ] ]; permute_test 'lvalue', [ [ 'Attr1', undef ] ]; - permute_test 'foo', [ [ '_', undef ] ]; + permute_test 'foo', [ [ '_', undef ] ]; # Attribute with parameters permute_test 'foo', [ [ 'Attr1', '' ] ]; @@ -37,15 +37,17 @@ PARSING_AND_METHODS: { permute_test 'foo', [ [ 'Attr1', ' \) ' ] ]; permute_test 'foo', [ [ 'Attr1', ' \( ' ] ]; permute_test 'foo', [ [ 'Attr1', '{' ] ]; - permute_test 'foo', [ [ '_', '' ] ]; + permute_test 'foo', [ [ '_', '' ] ]; # Multiple attributes, separated by colon+whitespace permute_test 'foo', [ [ 'Attr1', undef ], [ 'Attr2', undef ] ]; permute_test 'foo', [ [ 'Attr1', undef ], [ 'Attr2', undef ] ]; permute_test 'foo', [ [ 'Attr1', undef ], [ 'Attr2', undef ] ]; - permute_test 'foo', [ [ 'Attr1', undef ], [ 'Attr2', undef ], [ 'Attr3', undef ] ]; - permute_test 'foo', [ [ 'Attr1', '' ], [ 'Attr2', '' ], [ 'Attr3', '' ] ]; - permute_test 'foo', [ [ 'Attr1', '' ], [ 'Attr2', '___' ], [ 'Attr3', '' ] ]; + permute_test 'foo', + [ [ 'Attr1', undef ], [ 'Attr2', undef ], [ 'Attr3', undef ] ]; + permute_test 'foo', [ [ 'Attr1', '' ], [ 'Attr2', '' ], [ 'Attr3', '' ] ]; + permute_test 'foo', + [ [ 'Attr1', '' ], [ 'Attr2', '___' ], [ 'Attr3', '' ] ]; # Multiple attributes, separated by whitespace only permute_test 'foo', [ [ 'Attr1', undef ], [ 'Attr2', undef ] ]; @@ -53,12 +55,14 @@ PARSING_AND_METHODS: { # Examples from perldoc attributes permute_test 'foo', [ [ 'switch', '10,foo(7,3)' ], [ 'expensive', undef ] ]; - permute_test 'foo', [ [ 'Ugly', '\'\\("' ], [ 'Bad', undef ] ]; + permute_test 'foo', [ [ 'Ugly', '\'\\("' ], [ 'Bad', undef ] ]; permute_test 'foo', [ [ '_5x5', undef ] ]; - permute_test 'foo', [ [ 'lvalue', undef ], [ 'method', undef ] ]; + permute_test 'foo', [ [ 'lvalue', undef ], [ 'method', undef ] ]; # Mixed separators - execute_test 'sub foo : Attr1(a) Attr2(b) : Attr3(c) Attr4(d) {}', [ [ 'Attr1', 'a' ], [ 'Attr2', 'b' ], [ 'Attr3', 'c' ], [ 'Attr4', 'd' ] ]; + execute_test 'sub foo : Attr1(a) Attr2(b) : Attr3(c) Attr4(d) {}', + [ [ 'Attr1', 'a' ], [ 'Attr2', 'b' ], [ 'Attr3', 'c' ], + [ 'Attr4', 'd' ] ]; # When PPI supports anonymous subs, we'll need tests for # attributes on them, too. @@ -70,30 +74,27 @@ sub execute_test { my $Document = safe_new \$code; - my $attributes = $Document->find( 'PPI::Token::Attribute') || []; - is( scalar(@$attributes), scalar(@$expected), "'$msg' got expected number of attributes" ); - is_deeply( - [ map { [ $_->identifier, $_->parameters ] } @$attributes ], - $expected, - "'$msg' attribute properties as expected" - ); - - my $blocks = $Document->find( 'PPI::Structure::Block') || []; - my $blocks_expected = $code =~ m/{}$/ ? [ '{}' ] : []; - is_deeply( - [ map { $_->content } @$blocks ], - $blocks_expected, - "$msg blocks found as expected" - ); + my $attributes = $Document->find('PPI::Token::Attribute') || []; + is( scalar(@$attributes), scalar(@$expected), + "'$msg' got expected number of attributes" ); + is_deeply( [ map { [ $_->identifier, $_->parameters ] } @$attributes ], + $expected, "'$msg' attribute properties as expected" ); + + my $blocks = $Document->find('PPI::Structure::Block') || []; + my $blocks_expected = $code =~ m/{}$/ ? ['{}'] : []; + is_deeply( [ map { $_->content } @$blocks ], + $blocks_expected, "$msg blocks found as expected" ); return; } sub assemble_and_run { - my ( $name, $post_colon, $separator, $attributes, $post_attributes, $block ) = @_; + my ( $name, $post_colon, $separator, $attributes, $post_attributes, $block ) + = @_; $block = '{}' if !defined $block; - my $attribute_str = join $separator, map { defined $_->[1] ? "$_->[0]($_->[1])" : $_->[0] } @$attributes; + my $attribute_str = join $separator, + map { defined $_->[1] ? "$_->[0]($_->[1])" : $_->[0] } @$attributes; my $code = "sub $name :$post_colon$attribute_str$post_attributes$block"; my $msg = $code; @@ -115,13 +116,13 @@ sub permute_test { # (1.220) only supports it as whitespace when running on 5.20 # or greater. - assemble_and_run $name, '', ':', $attributes, '', '{}'; - assemble_and_run $name, '', ':', $attributes, '', ';'; - assemble_and_run $name, ' ', ' ', $attributes, ' ', '{}'; - assemble_and_run $name, ' ', "\t", $attributes, ' ', '{}'; - assemble_and_run $name, ' ', "\r", $attributes, ' ', '{}'; - assemble_and_run $name, ' ', "\n", $attributes, ' ', '{}'; - assemble_and_run $name, ' ', "\f", $attributes, ' ', '{}'; + assemble_and_run $name, '', ':', $attributes, '', '{}'; + assemble_and_run $name, '', ':', $attributes, '', ';'; + assemble_and_run $name, ' ', ' ', $attributes, ' ', '{}'; + assemble_and_run $name, ' ', "\t", $attributes, ' ', '{}'; + assemble_and_run $name, ' ', "\r", $attributes, ' ', '{}'; + assemble_and_run $name, ' ', "\n", $attributes, ' ', '{}'; + assemble_and_run $name, ' ', "\f", $attributes, ' ', '{}'; assemble_and_run $name, "\t", "\t", $attributes, "\t", '{}'; assemble_and_run $name, "\t", "\t", $attributes, "\t", ';'; diff --git a/t/ppi_token_dashedword.t b/t/ppi_token_dashedword.t index 4492fcd1..f5e28da0 100644 --- a/t/ppi_token_dashedword.t +++ b/t/ppi_token_dashedword.t @@ -4,27 +4,24 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 12 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 12 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); use Helper 'safe_new'; - LITERAL: { - my @pairs = ( - "-foo", '-foo', - "-Foo::Bar", '-Foo::Bar', - "-Foo'Bar", '-Foo::Bar', - ); - while ( @pairs ) { - my $from = shift @pairs; - my $to = shift @pairs; - my $doc = safe_new \"( $from => 1 );"; + my @pairs = + ( "-foo", '-foo', "-Foo::Bar", '-Foo::Bar', "-Foo'Bar", '-Foo::Bar', ); + while (@pairs) { + my $from = shift @pairs; + my $to = shift @pairs; + my $doc = safe_new \"( $from => 1 );"; my $word = $doc->find_first('Token::DashedWord'); - SKIP: { + SKIP: { skip( "PPI::Token::DashedWord is deactivated", 2 ); isa_ok( $word, 'PPI::Token::DashedWord' ); - is( $word && $word->literal, $to, "The source $from becomes $to ok" ); + is( $word && $word->literal, + $to, "The source $from becomes $to ok" ); } } } diff --git a/t/ppi_token_heredoc.t b/t/ppi_token_heredoc.t index 4f450850..17dcc9e1 100644 --- a/t/ppi_token_heredoc.t +++ b/t/ppi_token_heredoc.t @@ -4,7 +4,7 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 30 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 30 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); use Helper 'safe_new'; @@ -20,401 +20,410 @@ sub h; # is a special case, and is an array ref holding the expected value of # heredoc(), and defaulting to [ "Line 1\n", "Line 2\n" ]. - # Tests with a carriage return after the termination marker. -h { - name => 'Bareword terminator.', - content => "my \$heredoc = < { - _terminator_line => "HERE\n", - _damaged => undef, - _terminator => 'HERE', - _mode => 'interpolate', - _indented => undef, - _indentation => undef, - }, - }; -h { - name => 'Single-quoted bareword terminator.', - content => "my \$heredoc = <<'HERE';\nLine 1\nLine 2\nHERE\n", - expected => { - _terminator_line => "HERE\n", - _damaged => undef, - _terminator => 'HERE', - _mode => 'literal', - _indented => undef, - _indentation => undef, - }, - }; -h { - name => 'Single-quoted bareword terminator with space.', - content => "my \$heredoc = << 'HERE';\nLine 1\nLine 2\nHERE\n", - expected => { - _terminator_line => "HERE\n", - _damaged => undef, - _terminator => 'HERE', - _mode => 'literal', - _indented => undef, - _indentation => undef, - }, - }; -h { - name => 'Double-quoted bareword terminator.', - content => "my \$heredoc = <<\"HERE\";\nLine 1\nLine 2\nHERE\n", - expected => { - _terminator_line => "HERE\n", - _damaged => undef, - _terminator => 'HERE', - _mode => 'interpolate', - _indented => undef, - _indentation => undef, - }, - }; -h { - name => 'Double-quoted bareword terminator with space.', - content => "my \$heredoc = << \"HERE\";\nLine 1\nLine 2\nHERE\n", - expected => { - _terminator_line => "HERE\n", - _damaged => undef, - _terminator => 'HERE', - _mode => 'interpolate', - _indented => undef, - _indentation => undef, - }, - }; -h { - name => 'Command-quoted terminator.', - content => "my \$heredoc = <<`HERE`;\nLine 1\nLine 2\nHERE\n", - expected => { - _terminator_line => "HERE\n", - _damaged => undef, - _terminator => 'HERE', - _mode => 'command', - _indented => undef, - _indentation => undef, - }, - }; -h { - name => 'Command-quoted terminator with space.', - content => "my \$heredoc = << `HERE`;\nLine 1\nLine 2\nHERE\n", - expected => { - _terminator_line => "HERE\n", - _damaged => undef, - _terminator => 'HERE', - _mode => 'command', - _indented => undef, - _indentation => undef, - }, - }; -h { - name => 'Legacy escaped bareword terminator.', - content => "my \$heredoc = <<\\HERE;\nLine 1\nLine 2\nHERE\n", - expected => { - _terminator_line => "HERE\n", - _damaged => undef, - _terminator => 'HERE', - _mode => 'literal', - _indented => undef, - _indentation => undef, - }, - }; +# Tests with a carriage return after the termination marker. +h { + name => 'Bareword terminator.', + content => "my \$heredoc = < { + _terminator_line => "HERE\n", + _damaged => undef, + _terminator => 'HERE', + _mode => 'interpolate', + _indented => undef, + _indentation => undef, + }, +}; +h { + name => 'Single-quoted bareword terminator.', + content => "my \$heredoc = <<'HERE';\nLine 1\nLine 2\nHERE\n", + expected => { + _terminator_line => "HERE\n", + _damaged => undef, + _terminator => 'HERE', + _mode => 'literal', + _indented => undef, + _indentation => undef, + }, +}; +h { + name => 'Single-quoted bareword terminator with space.', + content => "my \$heredoc = << 'HERE';\nLine 1\nLine 2\nHERE\n", + expected => { + _terminator_line => "HERE\n", + _damaged => undef, + _terminator => 'HERE', + _mode => 'literal', + _indented => undef, + _indentation => undef, + }, +}; +h { + name => 'Double-quoted bareword terminator.', + content => "my \$heredoc = <<\"HERE\";\nLine 1\nLine 2\nHERE\n", + expected => { + _terminator_line => "HERE\n", + _damaged => undef, + _terminator => 'HERE', + _mode => 'interpolate', + _indented => undef, + _indentation => undef, + }, +}; +h { + name => 'Double-quoted bareword terminator with space.', + content => "my \$heredoc = << \"HERE\";\nLine 1\nLine 2\nHERE\n", + expected => { + _terminator_line => "HERE\n", + _damaged => undef, + _terminator => 'HERE', + _mode => 'interpolate', + _indented => undef, + _indentation => undef, + }, +}; +h { + name => 'Command-quoted terminator.', + content => "my \$heredoc = <<`HERE`;\nLine 1\nLine 2\nHERE\n", + expected => { + _terminator_line => "HERE\n", + _damaged => undef, + _terminator => 'HERE', + _mode => 'command', + _indented => undef, + _indentation => undef, + }, +}; +h { + name => 'Command-quoted terminator with space.', + content => "my \$heredoc = << `HERE`;\nLine 1\nLine 2\nHERE\n", + expected => { + _terminator_line => "HERE\n", + _damaged => undef, + _terminator => 'HERE', + _mode => 'command', + _indented => undef, + _indentation => undef, + }, +}; +h { + name => 'Legacy escaped bareword terminator.', + content => "my \$heredoc = <<\\HERE;\nLine 1\nLine 2\nHERE\n", + expected => { + _terminator_line => "HERE\n", + _damaged => undef, + _terminator => 'HERE', + _mode => 'literal', + _indented => undef, + _indentation => undef, + }, +}; - # Tests without a carriage return after the termination marker. -h { - name => 'Bareword terminator (no return).', - content => "my \$heredoc = < { - _terminator_line => 'HERE', - _damaged => 1, - _terminator => 'HERE', - _mode => 'interpolate', - _indented => undef, - _indentation => undef, - }, - }; -h { - name => 'Single-quoted bareword terminator (no return).', - content => "my \$heredoc = <<'HERE';\nLine 1\nLine 2\nHERE", - expected => { - _terminator_line => "HERE", - _damaged => 1, - _terminator => 'HERE', - _mode => 'literal', - _indented => undef, - _indentation => undef, - }, - }; -h { - name => 'Double-quoted bareword terminator (no return).', - content => "my \$heredoc = <<\"HERE\";\nLine 1\nLine 2\nHERE", - expected => { - _terminator_line => 'HERE', - _damaged => 1, - _terminator => 'HERE', - _mode => 'interpolate', - _indented => undef, - _indentation => undef, - }, - }; -h { - name => 'Command-quoted terminator (no return).', - content => "my \$heredoc = <<`HERE`;\nLine 1\nLine 2\nHERE", - expected => { - _terminator_line => 'HERE', - _damaged => 1, - _terminator => 'HERE', - _mode => 'command', - _indented => undef, - _indentation => undef, - }, - }; -h { - name => 'Legacy escaped bareword terminator (no return).', - content => "my \$heredoc = <<\\HERE;\nLine 1\nLine 2\nHERE", - expected => { - _terminator_line => 'HERE', - _damaged => 1, - _terminator => 'HERE', - _mode => 'literal', - _indented => undef, - _indentation => undef, - }, - }; +# Tests without a carriage return after the termination marker. +h { + name => 'Bareword terminator (no return).', + content => "my \$heredoc = < { + _terminator_line => 'HERE', + _damaged => 1, + _terminator => 'HERE', + _mode => 'interpolate', + _indented => undef, + _indentation => undef, + }, +}; +h { + name => 'Single-quoted bareword terminator (no return).', + content => "my \$heredoc = <<'HERE';\nLine 1\nLine 2\nHERE", + expected => { + _terminator_line => "HERE", + _damaged => 1, + _terminator => 'HERE', + _mode => 'literal', + _indented => undef, + _indentation => undef, + }, +}; +h { + name => 'Double-quoted bareword terminator (no return).', + content => "my \$heredoc = <<\"HERE\";\nLine 1\nLine 2\nHERE", + expected => { + _terminator_line => 'HERE', + _damaged => 1, + _terminator => 'HERE', + _mode => 'interpolate', + _indented => undef, + _indentation => undef, + }, +}; +h { + name => 'Command-quoted terminator (no return).', + content => "my \$heredoc = <<`HERE`;\nLine 1\nLine 2\nHERE", + expected => { + _terminator_line => 'HERE', + _damaged => 1, + _terminator => 'HERE', + _mode => 'command', + _indented => undef, + _indentation => undef, + }, +}; +h { + name => 'Legacy escaped bareword terminator (no return).', + content => "my \$heredoc = <<\\HERE;\nLine 1\nLine 2\nHERE", + expected => { + _terminator_line => 'HERE', + _damaged => 1, + _terminator => 'HERE', + _mode => 'literal', + _indented => undef, + _indentation => undef, + }, +}; - # Tests without a terminator. -h { - name => 'Unterminated heredoc block.', - content => "my \$heredoc = < { - _terminator_line => undef, - _damaged => 1, - _terminator => 'HERE', - _mode => 'interpolate', - _indented => undef, - _indentation => undef, - }, - }; +# Tests without a terminator. +h { + name => 'Unterminated heredoc block.', + content => "my \$heredoc = < { + _terminator_line => undef, + _damaged => 1, + _terminator => 'HERE', + _mode => 'interpolate', + _indented => undef, + _indentation => undef, + }, +}; - # Tests indented here-document with a carriage return after the termination marker. -h { - name => 'Bareword terminator (indented).', - content => "my \$heredoc = <<~HERE;\n\t \tLine 1\n\t \tLine 2\n\t \tHERE\n", - expected => { - _terminator_line => "HERE\n", - _damaged => undef, - _terminator => 'HERE', - _mode => 'interpolate', - _indented => 1, - _indentation => "\t \t", - }, - }; -h { - name => 'Single-quoted bareword terminator (indented).', - content => "my \$heredoc = <<~'HERE';\n\t \tLine 1\n\t \tLine 2\n\t \tHERE\n", - expected => { - _terminator_line => "HERE\n", - _damaged => undef, - _terminator => 'HERE', - _mode => 'literal', - _indented => 1, - _indentation => "\t \t", - }, - }; -h { - name => 'Single-quoted bareword terminator with space (indented).', - content => "my \$heredoc = <<~ 'HERE';\n\t \tLine 1\n\t \tLine 2\n\t \tHERE\n", - expected => { - _terminator_line => "HERE\n", - _damaged => undef, - _terminator => 'HERE', - _mode => 'literal', - _indented => 1, - _indentation => "\t \t", - }, - }; -h { - name => 'Double-quoted bareword terminator (indented).', - content => "my \$heredoc = <<~\"HERE\";\n\t \tLine 1\n\t \tLine 2\n\t \tHERE\n", - expected => { - _terminator_line => "HERE\n", - _damaged => undef, - _terminator => 'HERE', - _mode => 'interpolate', - _indented => 1, - _indentation => "\t \t", - }, - }; -h { - name => 'Double-quoted bareword terminator with space (indented).', - content => "my \$heredoc = <<~ \"HERE\";\n\t \tLine 1\n\t \tLine 2\n\t \tHERE\n", - expected => { - _terminator_line => "HERE\n", - _damaged => undef, - _terminator => 'HERE', - _mode => 'interpolate', - _indented => 1, - _indentation => "\t \t", - }, - }; -h { - name => 'Command-quoted terminator (indented).', - content => "my \$heredoc = <<~`HERE`;\n\t \tLine 1\n\t \tLine 2\n\t \tHERE\n", - expected => { - _terminator_line => "HERE\n", - _damaged => undef, - _terminator => 'HERE', - _mode => 'command', - _indented => 1, - _indentation => "\t \t", - }, - }; -h { - name => 'Command-quoted terminator with space (indented).', - content => "my \$heredoc = <<~ `HERE`;\n\t \tLine 1\n\t \tLine 2\n\t \tHERE\n", - expected => { - _terminator_line => "HERE\n", - _damaged => undef, - _terminator => 'HERE', - _mode => 'command', - _indented => 1, - _indentation => "\t \t", - }, - }; -h { - name => 'Legacy escaped bareword terminator (indented).', - content => "my \$heredoc = <<~\\HERE;\n\t \tLine 1\n\t \tLine 2\n\t \tHERE\n", - expected => { - _terminator_line => "HERE\n", - _damaged => undef, - _terminator => 'HERE', - _mode => 'literal', - _indented => 1, - _indentation => "\t \t", - }, - }; +# Tests indented here-document with a carriage return after the termination marker. +h { + name => 'Bareword terminator (indented).', + content => "my \$heredoc = <<~HERE;\n\t \tLine 1\n\t \tLine 2\n\t \tHERE\n", + expected => { + _terminator_line => "HERE\n", + _damaged => undef, + _terminator => 'HERE', + _mode => 'interpolate', + _indented => 1, + _indentation => "\t \t", + }, +}; +h { + name => 'Single-quoted bareword terminator (indented).', + content => + "my \$heredoc = <<~'HERE';\n\t \tLine 1\n\t \tLine 2\n\t \tHERE\n", + expected => { + _terminator_line => "HERE\n", + _damaged => undef, + _terminator => 'HERE', + _mode => 'literal', + _indented => 1, + _indentation => "\t \t", + }, +}; +h { + name => 'Single-quoted bareword terminator with space (indented).', + content => + "my \$heredoc = <<~ 'HERE';\n\t \tLine 1\n\t \tLine 2\n\t \tHERE\n", + expected => { + _terminator_line => "HERE\n", + _damaged => undef, + _terminator => 'HERE', + _mode => 'literal', + _indented => 1, + _indentation => "\t \t", + }, +}; +h { + name => 'Double-quoted bareword terminator (indented).', + content => + "my \$heredoc = <<~\"HERE\";\n\t \tLine 1\n\t \tLine 2\n\t \tHERE\n", + expected => { + _terminator_line => "HERE\n", + _damaged => undef, + _terminator => 'HERE', + _mode => 'interpolate', + _indented => 1, + _indentation => "\t \t", + }, +}; +h { + name => 'Double-quoted bareword terminator with space (indented).', + content => + "my \$heredoc = <<~ \"HERE\";\n\t \tLine 1\n\t \tLine 2\n\t \tHERE\n", + expected => { + _terminator_line => "HERE\n", + _damaged => undef, + _terminator => 'HERE', + _mode => 'interpolate', + _indented => 1, + _indentation => "\t \t", + }, +}; +h { + name => 'Command-quoted terminator (indented).', + content => + "my \$heredoc = <<~`HERE`;\n\t \tLine 1\n\t \tLine 2\n\t \tHERE\n", + expected => { + _terminator_line => "HERE\n", + _damaged => undef, + _terminator => 'HERE', + _mode => 'command', + _indented => 1, + _indentation => "\t \t", + }, +}; +h { + name => 'Command-quoted terminator with space (indented).', + content => + "my \$heredoc = <<~ `HERE`;\n\t \tLine 1\n\t \tLine 2\n\t \tHERE\n", + expected => { + _terminator_line => "HERE\n", + _damaged => undef, + _terminator => 'HERE', + _mode => 'command', + _indented => 1, + _indentation => "\t \t", + }, +}; +h { + name => 'Legacy escaped bareword terminator (indented).', + content => + "my \$heredoc = <<~\\HERE;\n\t \tLine 1\n\t \tLine 2\n\t \tHERE\n", + expected => { + _terminator_line => "HERE\n", + _damaged => undef, + _terminator => 'HERE', + _mode => 'literal', + _indented => 1, + _indentation => "\t \t", + }, +}; - # Tests indented here-document without a carriage return after the termination marker. -h { - name => 'Bareword terminator (indented and no return).', - content => "my \$heredoc = <<~HERE;\n\t \tLine 1\n\t \tLine 2\n\t \tHERE", - expected => { - _terminator_line => 'HERE', - _damaged => 1, - _terminator => 'HERE', - _mode => 'interpolate', - _indented => 1, - _indentation => "\t \t", - }, - }; -h { - name => 'Single-quoted bareword terminator (indented and no return).', - content => "my \$heredoc = <<~'HERE';\n\t \tLine 1\n\t \tLine 2\n\t \tHERE", - expected => { - _terminator_line => "HERE", - _damaged => 1, - _terminator => 'HERE', - _mode => 'literal', - _indented => 1, - _indentation => "\t \t", - }, - }; -h { - name => 'Double-quoted bareword terminator (indented and no return).', - content => "my \$heredoc = <<~\"HERE\";\n\t \tLine 1\n\t \tLine 2\n\t \tHERE", - expected => { - _terminator_line => 'HERE', - _damaged => 1, - _terminator => 'HERE', - _mode => 'interpolate', - _indented => 1, - _indentation => "\t \t", - }, - }; -h { - name => 'Command-quoted terminator (indented and no return).', - content => "my \$heredoc = <<~`HERE`;\n\t \tLine 1\n\t \tLine 2\n\t \tHERE", - expected => { - _terminator_line => 'HERE', - _damaged => 1, - _terminator => 'HERE', - _mode => 'command', - _indented => 1, - _indentation => "\t \t", - }, - }; -h { - name => 'Legacy escaped bareword terminator (indented and no return).', - content => "my \$heredoc = <<~\\HERE;\n\t \tLine 1\n\t \tLine 2\n\t \tHERE", - expected => { - _terminator_line => 'HERE', - _damaged => 1, - _terminator => 'HERE', - _mode => 'literal', - _indented => 1, - _indentation => "\t \t", - }, - }; +# Tests indented here-document without a carriage return after the termination marker. +h { + name => 'Bareword terminator (indented and no return).', + content => "my \$heredoc = <<~HERE;\n\t \tLine 1\n\t \tLine 2\n\t \tHERE", + expected => { + _terminator_line => 'HERE', + _damaged => 1, + _terminator => 'HERE', + _mode => 'interpolate', + _indented => 1, + _indentation => "\t \t", + }, +}; +h { + name => 'Single-quoted bareword terminator (indented and no return).', + content => "my \$heredoc = <<~'HERE';\n\t \tLine 1\n\t \tLine 2\n\t \tHERE", + expected => { + _terminator_line => "HERE", + _damaged => 1, + _terminator => 'HERE', + _mode => 'literal', + _indented => 1, + _indentation => "\t \t", + }, +}; +h { + name => 'Double-quoted bareword terminator (indented and no return).', + content => + "my \$heredoc = <<~\"HERE\";\n\t \tLine 1\n\t \tLine 2\n\t \tHERE", + expected => { + _terminator_line => 'HERE', + _damaged => 1, + _terminator => 'HERE', + _mode => 'interpolate', + _indented => 1, + _indentation => "\t \t", + }, +}; +h { + name => 'Command-quoted terminator (indented and no return).', + content => "my \$heredoc = <<~`HERE`;\n\t \tLine 1\n\t \tLine 2\n\t \tHERE", + expected => { + _terminator_line => 'HERE', + _damaged => 1, + _terminator => 'HERE', + _mode => 'command', + _indented => 1, + _indentation => "\t \t", + }, +}; +h { + name => 'Legacy escaped bareword terminator (indented and no return).', + content => "my \$heredoc = <<~\\HERE;\n\t \tLine 1\n\t \tLine 2\n\t \tHERE", + expected => { + _terminator_line => 'HERE', + _damaged => 1, + _terminator => 'HERE', + _mode => 'literal', + _indented => 1, + _indentation => "\t \t", + }, +}; - # Tests indented here-document without a terminator. -h { - name => 'Unterminated heredoc block (indented).', - content => "my \$heredoc = <<~HERE;\nLine 1\nLine 2\n", - expected => { - _terminator_line => undef, - _damaged => 1, - _terminator => 'HERE', - _mode => 'interpolate', - _indented => 1, - _indentation => undef, - }, - }; +# Tests indented here-document without a terminator. +h { + name => 'Unterminated heredoc block (indented).', + content => "my \$heredoc = <<~HERE;\nLine 1\nLine 2\n", + expected => { + _terminator_line => undef, + _damaged => 1, + _terminator => 'HERE', + _mode => 'interpolate', + _indented => 1, + _indentation => undef, + }, +}; - # Tests indented here-document where indentation doesn't match -h { - name => 'Unterminated heredoc block (indented).', - content => "my \$heredoc = <<~HERE;\nLine 1\nLine 2\n\t \tHERE\n", - expected => { - _terminator_line => "HERE\n", - _damaged => 1, - _terminator => 'HERE', - _mode => 'interpolate', - _indented => 1, - _indentation => "\t \t", - }, - }; - - # Tests indented here-document with empty line -h { - name => 'Indented heredoc with empty line.', - content => "my \$heredoc = <<~HERE;\n\tLine 1\n\n\tLine 3\n\tHERE\n", - expected => { - _terminator_line => "HERE\n", - _damaged => undef, - _terminator => 'HERE', - _mode => 'interpolate', - _indented => 1, - heredoc => [ "Line 1\n", "\n", "Line 3\n" ], - _indentation => "\t", - }, - }; +# Tests indented here-document where indentation doesn't match +h { + name => 'Unterminated heredoc block (indented).', + content => "my \$heredoc = <<~HERE;\nLine 1\nLine 2\n\t \tHERE\n", + expected => { + _terminator_line => "HERE\n", + _damaged => 1, + _terminator => 'HERE', + _mode => 'interpolate', + _indented => 1, + _indentation => "\t \t", + }, +}; +# Tests indented here-document with empty line +h { + name => 'Indented heredoc with empty line.', + content => "my \$heredoc = <<~HERE;\n\tLine 1\n\n\tLine 3\n\tHERE\n", + expected => { + _terminator_line => "HERE\n", + _damaged => undef, + _terminator => 'HERE', + _mode => 'interpolate', + _indented => 1, + heredoc => [ "Line 1\n", "\n", "Line 3\n" ], + _indentation => "\t", + }, +}; sub h { - my ( $test ) = @_; - my %exception = map { $_ => 1 } qw{ heredoc }; + my ($test) = @_; + my %exception = map { $_ => 1 } qw{ heredoc }; subtest( $test->{name}, sub { - my $exceptions = grep { $exception{$_} } keys %{ $test->{expected} }; + my $exceptions = + grep { $exception{$_} } keys %{ $test->{expected} }; plan tests => 8 - $exceptions + keys %{ $test->{expected} }; my $document = safe_new \$test->{content}; - SKIP: { + SKIP: { skip 'Damaged document', 1 if $test->{expected}{_damaged}; - is( $document->serialize(), $test->{content}, 'Document serializes correctly' ); + is( $document->serialize(), + $test->{content}, 'Document serializes correctly' ); } - my $heredocs = $document->find( 'Token::HereDoc' ); + my $heredocs = $document->find('Token::HereDoc'); is( ref $heredocs, 'ARRAY', 'Found heredocs.' ); is( scalar @$heredocs, 1, 'Found 1 heredoc block.' ); @@ -423,14 +432,16 @@ sub h { can_ok( $heredoc, 'heredoc' ); my @content = $heredoc->heredoc; - my @expected_heredoc = @{ $test->{expected}{heredoc} || [ "Line 1\n", "Line 2\n", ] }; + my @expected_heredoc = + @{ $test->{expected}{heredoc} || [ "Line 1\n", "Line 2\n", ] }; is_deeply( \@content, \@expected_heredoc, 'The returned content does not include the heredoc terminator.', ) or diag "heredoc() returned ", explain \@content; - is( $heredoc->{$_}, $test->{expected}{$_}, "property '$_'" ) for grep { ! $exception{$_} } keys %{ $test->{expected} }; + is( $heredoc->{$_}, $test->{expected}{$_}, "property '$_'" ) + for grep { !$exception{$_} } keys %{ $test->{expected} }; } ); } diff --git a/t/ppi_token_magic.t b/t/ppi_token_magic.t index 93fb4a88..28ca8519 100644 --- a/t/ppi_token_magic.t +++ b/t/ppi_token_magic.t @@ -4,12 +4,11 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 39 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 39 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); use Helper 'safe_new'; - __TOKENIZER_ON_CHAR: { my $document = safe_new \<<'END_PERL'; $[; # Magic $[ @@ -34,14 +33,14 @@ END_PERL $document->index_locations(); - my $symbols = $document->find( 'PPI::Token::Symbol' ); + my $symbols = $document->find('PPI::Token::Symbol'); is( scalar(@$symbols), 18, 'Found the correct number of symbols' ); - my $comments = $document->find( 'PPI::Token::Comment' ); + my $comments = $document->find('PPI::Token::Comment'); - foreach my $token ( @$symbols ) { - my ($hash, $class, $name, $remk) = - split /\s+/, $comments->[$token->line_number - 1], 4; + foreach my $token (@$symbols) { + my ( $hash, $class, $name, $remk ) = + split /\s+/, $comments->[ $token->line_number - 1 ], 4; isa_ok( $token, "PPI::Token::$class" ); is( $token->symbol, $name, $remk || "The symbol is $name" ); } diff --git a/t/ppi_token_number_version.t b/t/ppi_token_number_version.t index ea299587..eff3415f 100644 --- a/t/ppi_token_number_version.t +++ b/t/ppi_token_number_version.t @@ -4,18 +4,17 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 2187 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 2187 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); -use PPI (); +use PPI (); use PPI::Singletons qw( %KEYWORDS %OPERATOR %QUOTELIKE ); use Helper 'safe_new'; - LITERAL: { - my $doc1 = new_ok( 'PPI::Document' => [ \'1.2.3.4' ] ); + my $doc1 = new_ok( 'PPI::Document' => [ \'1.2.3.4' ] ); my $doc2 = new_ok( 'PPI::Document' => [ \'v1.2.3.4' ] ); - isa_ok( $doc1->child(0), 'PPI::Statement' ); - isa_ok( $doc2->child(0), 'PPI::Statement' ); + isa_ok( $doc1->child(0), 'PPI::Statement' ); + isa_ok( $doc2->child(0), 'PPI::Statement' ); isa_ok( $doc1->child(0)->child(0), 'PPI::Token::Number::Version' ); isa_ok( $doc2->child(0)->child(0), 'PPI::Token::Number::Version' ); @@ -26,19 +25,18 @@ LITERAL: { is( $literal1, $literal2, 'Literals match for 1.2.3.4 vs v1.2.3.4' ); } - VSTRING_ENDS_CORRECTLY: { my @tests = ( ( map { { - desc=>"no . in 'v49$_', so not a version string", - code=>"v49$_", - expected=>[ 'PPI::Token::Word' => "v49$_" ], + desc => "no . in 'v49$_', so not a version string", + code => "v49$_", + expected => [ 'PPI::Token::Word' => "v49$_" ], } } ( - 'x3', # not fooled by faux x operator with operand - 'e10', # not fooled by faux scientific notation + 'x3', # not fooled by faux x operator with operand + 'e10', # not fooled by faux scientific notation keys %KEYWORDS, ), ), @@ -49,12 +47,10 @@ VSTRING_ENDS_CORRECTLY: { code => "v49.49$_", expected => [ 'PPI::Token::Number::Version' => 'v49.49', - get_class($_) => $_, + get_class($_) => $_, ], }, - } ( - keys %KEYWORDS, - ), + } ( keys %KEYWORDS, ), ), ( map { @@ -63,27 +59,25 @@ VSTRING_ENDS_CORRECTLY: { code => "49.49.49$_", expected => [ 'PPI::Token::Number::Version' => '49.49.49', - get_class($_) => $_, + get_class($_) => $_, ], }, - } ( - keys %KEYWORDS, - ), + } ( keys %KEYWORDS, ), ), { - desc => 'version string, x, and operand', - code => 'v49.49.49x3', + desc => 'version string, x, and operand', + code => 'v49.49.49x3', expected => [ 'PPI::Token::Number::Version' => 'v49.49.49', - 'PPI::Token::Operator' => 'x', - 'PPI::Token::Number' => '3', + 'PPI::Token::Operator' => 'x', + 'PPI::Token::Number' => '3', ], }, ); - for my $test ( @tests ) { + for my $test (@tests) { my $code = $test->{code}; - my $d = safe_new \$test->{code}; + my $d = safe_new \$test->{code}; my $tokens = $d->find( sub { 1; } ); $tokens = [ map { ref($_), $_->content() } @$tokens ]; my $expected = $test->{expected}; @@ -98,9 +92,9 @@ VSTRING_ENDS_CORRECTLY: { } sub get_class { - my ( $t ) = @_; + my ($t) = @_; my $ql = $QUOTELIKE{$t}; - return "PPI::Token::$ql" if $ql; + return "PPI::Token::$ql" if $ql; return 'PPI::Token::Operator' if $OPERATOR{$t}; return 'PPI::Token::Word'; } diff --git a/t/ppi_token_operator.t b/t/ppi_token_operator.t index fc0ed4eb..04cbb66b 100644 --- a/t/ppi_token_operator.t +++ b/t/ppi_token_operator.t @@ -4,485 +4,491 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 3009 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 3009 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); -use PPI (); +use PPI (); use PPI::Singletons qw( %KEYWORDS %OPERATOR ); use Helper 'safe_new'; FIND_ONE_OP: { my $source = '$a = .987;'; - my $doc = safe_new \$source; - my $ops = $doc->find( 'Token::Number::Float' ); - is( ref $ops, 'ARRAY', "found number" ); - is( @$ops, 1, "number found exactly once" ); - is( $ops->[0]->content(), '.987', "text matches" ); + my $doc = safe_new \$source; + my $ops = $doc->find('Token::Number::Float'); + is( ref $ops, 'ARRAY', "found number" ); + is( @$ops, 1, "number found exactly once" ); + is( $ops->[0]->content(), '.987', "text matches" ); - $ops = $doc->find( 'Token::Operator' ); + $ops = $doc->find('Token::Operator'); is( ref $ops, 'ARRAY', "operator = found operators in number test" ); - is( @$ops, 1, "operator = found exactly once in number test" ); + is( @$ops, 1, "operator = found exactly once in number test" ); } - PARSE_ALL_OPERATORS: { foreach my $op ( sort keys %OPERATOR ) { my $source = $op eq '<>' || $op eq '<<>>' ? $op . ';' : "\$foo $op 2;"; - my $doc = safe_new \$source; - my $ops = $doc->find( $op eq '<<>>' || $op eq '<>' - ? 'Token::QuoteLike::Readline' : 'Token::Operator' ); - is( ref $ops, 'ARRAY', "operator $op found operators" ); - is( @$ops, 1, "operator $op found exactly once" ); + my $doc = safe_new \$source; + my $ops = $doc->find( + $op eq '<<>>' || $op eq '<>' + ? 'Token::QuoteLike::Readline' + : 'Token::Operator' + ); + is( ref $ops, 'ARRAY', "operator $op found operators" ); + is( @$ops, 1, "operator $op found exactly once" ); is( $ops->[0]->content(), $op, "operator $op operator text matches" ); } } - OPERATOR_X: { my @tests = ( { - desc => 'generic bareword with integer', # github #133 - code => 'bareword x 3', + desc => 'generic bareword with integer', # github #133 + code => 'bareword x 3', expected => [ - 'PPI::Token::Word' => 'bareword', + 'PPI::Token::Word' => 'bareword', 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Operator' => 'x', + 'PPI::Token::Operator' => 'x', 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Number' => '3', + 'PPI::Token::Number' => '3', ], }, { desc => 'generic bareword with integer run together', # github #133 code => 'bareword x3', expected => [ - 'PPI::Token::Word' => 'bareword', + 'PPI::Token::Word' => 'bareword', 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Operator' => 'x', - 'PPI::Token::Number' => '3', + 'PPI::Token::Operator' => 'x', + 'PPI::Token::Number' => '3', ], }, { - desc => 'preceding word looks like a force but is not', # github #133 - code => '$a->package x3', + desc => + 'preceding word looks like a force but is not', # github #133 + code => '$a->package x3', expected => [ - 'PPI::Token::Symbol' => '$a', - 'PPI::Token::Operator' => '->', - 'PPI::Token::Word' => 'package', + 'PPI::Token::Symbol' => '$a', + 'PPI::Token::Operator' => '->', + 'PPI::Token::Word' => 'package', 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Operator' => 'x', - 'PPI::Token::Number' => '3', + 'PPI::Token::Operator' => 'x', + 'PPI::Token::Number' => '3', ], }, { - desc => 'method with method', - code => 'sort { $a->package cmp $b->package } ();', + desc => 'method with method', + code => 'sort { $a->package cmp $b->package } ();', expected => [ - 'PPI::Token::Word' => 'sort', + 'PPI::Token::Word' => 'sort', 'PPI::Token::Whitespace' => ' ', - 'PPI::Structure::Block'=> '{ $a->package cmp $b->package }', - 'PPI::Token::Structure'=> '{', - 'PPI::Token::Whitespace'=> ' ', - 'PPI::Statement'=> '$a->package cmp $b->package', - 'PPI::Token::Symbol'=> '$a', - 'PPI::Token::Operator'=> '->', - 'PPI::Token::Word'=> 'package', - 'PPI::Token::Whitespace'=> ' ', - 'PPI::Token::Operator'=> 'cmp', - 'PPI::Token::Whitespace'=> ' ', - 'PPI::Token::Symbol'=> '$b', - 'PPI::Token::Operator'=> '->', - 'PPI::Token::Word'=> 'package', - 'PPI::Token::Whitespace'=> ' ', - 'PPI::Token::Structure'=> '}', - 'PPI::Token::Whitespace'=> ' ', - 'PPI::Structure::List'=> '()', - 'PPI::Token::Structure'=> '(', - 'PPI::Token::Structure'=> ')', - 'PPI::Token::Structure'=> ';' + 'PPI::Structure::Block' => '{ $a->package cmp $b->package }', + 'PPI::Token::Structure' => '{', + 'PPI::Token::Whitespace' => ' ', + 'PPI::Statement' => '$a->package cmp $b->package', + 'PPI::Token::Symbol' => '$a', + 'PPI::Token::Operator' => '->', + 'PPI::Token::Word' => 'package', + 'PPI::Token::Whitespace' => ' ', + 'PPI::Token::Operator' => 'cmp', + 'PPI::Token::Whitespace' => ' ', + 'PPI::Token::Symbol' => '$b', + 'PPI::Token::Operator' => '->', + 'PPI::Token::Word' => 'package', + 'PPI::Token::Whitespace' => ' ', + 'PPI::Token::Structure' => '}', + 'PPI::Token::Whitespace' => ' ', + 'PPI::Structure::List' => '()', + 'PPI::Token::Structure' => '(', + 'PPI::Token::Structure' => ')', + 'PPI::Token::Structure' => ';' ], }, { - desc => 'method with integer', - code => 'c->d x 3', + desc => 'method with integer', + code => 'c->d x 3', expected => [ - 'PPI::Token::Word' => 'c', - 'PPI::Token::Operator' => '->', - 'PPI::Token::Word' => 'd', + 'PPI::Token::Word' => 'c', + 'PPI::Token::Operator' => '->', + 'PPI::Token::Word' => 'd', 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Operator' => 'x', + 'PPI::Token::Operator' => 'x', 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Number' => '3', + 'PPI::Token::Number' => '3', ], }, { - desc => 'integer with integer', - code => '1 x 3', + desc => 'integer with integer', + code => '1 x 3', expected => [ - 'PPI::Token::Number' => '1', + 'PPI::Token::Number' => '1', 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Operator' => 'x', + 'PPI::Token::Operator' => 'x', 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Number' => '3', + 'PPI::Token::Number' => '3', ], }, { - desc => 'string with integer', - code => '"y" x 3', + desc => 'string with integer', + code => '"y" x 3', expected => [ 'PPI::Token::Quote::Double' => '"y"', - 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Operator' => 'x', - 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Number' => '3', + 'PPI::Token::Whitespace' => ' ', + 'PPI::Token::Operator' => 'x', + 'PPI::Token::Whitespace' => ' ', + 'PPI::Token::Number' => '3', ], }, { - desc => 'string with integer', - code => 'qq{y} x 3', + desc => 'string with integer', + code => 'qq{y} x 3', expected => [ 'PPI::Token::Quote::Interpolate' => 'qq{y}', - 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Operator' => 'x', - 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Number' => '3', + 'PPI::Token::Whitespace' => ' ', + 'PPI::Token::Operator' => 'x', + 'PPI::Token::Whitespace' => ' ', + 'PPI::Token::Number' => '3', ], }, { - desc => 'string no whitespace with integer', - code => '"y"x 3', + desc => 'string no whitespace with integer', + code => '"y"x 3', expected => [ 'PPI::Token::Quote::Double' => '"y"', - 'PPI::Token::Operator' => 'x', - 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Number' => '3', + 'PPI::Token::Operator' => 'x', + 'PPI::Token::Whitespace' => ' ', + 'PPI::Token::Number' => '3', ], }, { - desc => 'variable with integer', - code => '$a x 3', + desc => 'variable with integer', + code => '$a x 3', expected => [ - 'PPI::Token::Symbol' => '$a', + 'PPI::Token::Symbol' => '$a', 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Operator' => 'x', + 'PPI::Token::Operator' => 'x', 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Number' => '3', + 'PPI::Token::Number' => '3', ], }, { - desc => 'variable with no whitespace integer', - code => '$a x3', + desc => 'variable with no whitespace integer', + code => '$a x3', expected => [ - 'PPI::Token::Symbol' => '$a', + 'PPI::Token::Symbol' => '$a', 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Operator' => 'x', - 'PPI::Token::Number' => '3', + 'PPI::Token::Operator' => 'x', + 'PPI::Token::Number' => '3', ], }, { - desc => 'variable, post ++, x, no whitespace anywhere', - code => '$a++x3', + desc => 'variable, post ++, x, no whitespace anywhere', + code => '$a++x3', expected => [ - 'PPI::Token::Symbol' => '$a', + 'PPI::Token::Symbol' => '$a', 'PPI::Token::Operator' => '++', 'PPI::Token::Operator' => 'x', - 'PPI::Token::Number' => '3', + 'PPI::Token::Number' => '3', ], }, { - desc => 'double quote, no whitespace', - code => '"y"x 3', + desc => 'double quote, no whitespace', + code => '"y"x 3', expected => [ 'PPI::Token::Quote::Double' => '"y"', - 'PPI::Token::Operator' => 'x', - 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Number' => '3', + 'PPI::Token::Operator' => 'x', + 'PPI::Token::Whitespace' => ' ', + 'PPI::Token::Number' => '3', ], }, { - desc => 'single quote, no whitespace', - code => "'y'x 3", + desc => 'single quote, no whitespace', + code => "'y'x 3", expected => [ 'PPI::Token::Quote::Single' => "'y'", - 'PPI::Token::Operator' => 'x', - 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Number' => '3', + 'PPI::Token::Operator' => 'x', + 'PPI::Token::Whitespace' => ' ', + 'PPI::Token::Number' => '3', ], }, { - desc => 'parens, no whitespace, number', - code => "(5)x 3", + desc => 'parens, no whitespace, number', + code => "(5)x 3", expected => [ - 'PPI::Structure::List' => '(5)', - 'PPI::Token::Structure' => '(', + 'PPI::Structure::List' => '(5)', + 'PPI::Token::Structure' => '(', 'PPI::Statement::Expression' => '5', - 'PPI::Token::Number' => '5', - 'PPI::Token::Structure' => ')', - 'PPI::Token::Operator' => 'x', - 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Number' => '3', + 'PPI::Token::Number' => '5', + 'PPI::Token::Structure' => ')', + 'PPI::Token::Operator' => 'x', + 'PPI::Token::Whitespace' => ' ', + 'PPI::Token::Number' => '3', ], }, { - desc => 'number following x is hex', - code => "1x0x1", + desc => 'number following x is hex', + code => "1x0x1", expected => [ - 'PPI::Token::Number' => '1', - 'PPI::Token::Operator' => 'x', + 'PPI::Token::Number' => '1', + 'PPI::Token::Operator' => 'x', 'PPI::Token::Number::Hex' => '0x1', ], }, { - desc => 'x followed by symbol', - code => '1 x$y', + desc => 'x followed by symbol', + code => '1 x$y', expected => [ - 'PPI::Token::Number' => '1', + 'PPI::Token::Number' => '1', 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Operator' => 'x', - 'PPI::Token::Symbol' => '$y', + 'PPI::Token::Operator' => 'x', + 'PPI::Token::Symbol' => '$y', ], }, { - desc => 'x= with no trailing whitespace, symbol', - code => '$z x=3', + desc => 'x= with no trailing whitespace, symbol', + code => '$z x=3', expected => [ - 'PPI::Token::Symbol' => '$z', + 'PPI::Token::Symbol' => '$z', 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Operator' => 'x=', - 'PPI::Token::Number' => '3', + 'PPI::Token::Operator' => 'x=', + 'PPI::Token::Number' => '3', ], }, { - desc => 'x= with no trailing whitespace, symbol', - code => '$z x=$y', + desc => 'x= with no trailing whitespace, symbol', + code => '$z x=$y', expected => [ - 'PPI::Token::Symbol' => '$z', + 'PPI::Token::Symbol' => '$z', 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Operator' => 'x=', - 'PPI::Token::Symbol' => '$y', + 'PPI::Token::Operator' => 'x=', + 'PPI::Token::Symbol' => '$y', ], }, { - desc => 'x plus whitespace on the left of => that is not the first token in the doc', - code => '1;x =>1;', + desc => +'x plus whitespace on the left of => that is not the first token in the doc', + code => '1;x =>1;', expected => [ - 'PPI::Statement' => '1;', - 'PPI::Token::Number' => '1', - 'PPI::Token::Structure' => ';', - 'PPI::Statement' => 'x =>1;', - 'PPI::Token::Word' => 'x', + 'PPI::Statement' => '1;', + 'PPI::Token::Number' => '1', + 'PPI::Token::Structure' => ';', + 'PPI::Statement' => 'x =>1;', + 'PPI::Token::Word' => 'x', 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Operator' => '=>', - 'PPI::Token::Number' => '1', - 'PPI::Token::Structure' => ';', + 'PPI::Token::Operator' => '=>', + 'PPI::Token::Number' => '1', + 'PPI::Token::Structure' => ';', ], }, { - desc => 'x on the left of => that is not the first token in the doc', - code => '1;x=>1;', + desc => + 'x on the left of => that is not the first token in the doc', + code => '1;x=>1;', expected => [ - 'PPI::Statement' => '1;', - 'PPI::Token::Number' => '1', + 'PPI::Statement' => '1;', + 'PPI::Token::Number' => '1', 'PPI::Token::Structure' => ';', - 'PPI::Statement' => 'x=>1;', - 'PPI::Token::Word' => 'x', - 'PPI::Token::Operator' => '=>', - 'PPI::Token::Number' => '1', + 'PPI::Statement' => 'x=>1;', + 'PPI::Token::Word' => 'x', + 'PPI::Token::Operator' => '=>', + 'PPI::Token::Number' => '1', 'PPI::Token::Structure' => ';', ], }, { - desc => 'x on the left of => that is not the first token in the doc', - code => '$hash{x}=1;', + desc => + 'x on the left of => that is not the first token in the doc', + code => '$hash{x}=1;', expected => [ - 'PPI::Token::Symbol' => '$hash', - 'PPI::Structure::Subscript' => '{x}', - 'PPI::Token::Structure' => '{', + 'PPI::Token::Symbol' => '$hash', + 'PPI::Structure::Subscript' => '{x}', + 'PPI::Token::Structure' => '{', 'PPI::Statement::Expression' => 'x', - 'PPI::Token::Word' => 'x', - 'PPI::Token::Structure' => '}', - 'PPI::Token::Operator' => '=', - 'PPI::Token::Number' => '1', - 'PPI::Token::Structure' => ';', + 'PPI::Token::Word' => 'x', + 'PPI::Token::Structure' => '}', + 'PPI::Token::Operator' => '=', + 'PPI::Token::Number' => '1', + 'PPI::Token::Structure' => ';', ], }, { desc => 'x plus whitespace on the left of => is not an operator', code => 'x =>1', expected => [ - 'PPI::Token::Word' => 'x', + 'PPI::Token::Word' => 'x', 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Operator' => '=>', - 'PPI::Token::Number' => '1', + 'PPI::Token::Operator' => '=>', + 'PPI::Token::Number' => '1', ], }, { - desc => 'x immediately followed by => should not be mistaken for x=', - code => 'x=>1', + desc => + 'x immediately followed by => should not be mistaken for x=', + code => 'x=>1', expected => [ - 'PPI::Token::Word' => 'x', + 'PPI::Token::Word' => 'x', 'PPI::Token::Operator' => '=>', - 'PPI::Token::Number' => '1', + 'PPI::Token::Number' => '1', ], }, { - desc => 'xx on left of => not mistaken for an x operator', - code => 'xx=>1', + desc => 'xx on left of => not mistaken for an x operator', + code => 'xx=>1', expected => [ - 'PPI::Token::Word' => 'xx', + 'PPI::Token::Word' => 'xx', 'PPI::Token::Operator' => '=>', - 'PPI::Token::Number' => '1', + 'PPI::Token::Number' => '1', ], }, { - desc => 'x right of => is not an operator', - code => '1=>x', + desc => 'x right of => is not an operator', + code => '1=>x', expected => [ - 'PPI::Token::Number' => '1', + 'PPI::Token::Number' => '1', 'PPI::Token::Operator' => '=>', - 'PPI::Token::Word' => 'x', + 'PPI::Token::Word' => 'x', ], }, { - desc => 'xor right of => is an operator', - code => '1=>xor', + desc => 'xor right of => is an operator', + code => '1=>xor', expected => [ - 'PPI::Token::Number' => '1', + 'PPI::Token::Number' => '1', 'PPI::Token::Operator' => '=>', 'PPI::Token::Operator' => 'xor', ], }, { - desc => 'RT 37892: list as arg to x operator 1', - code => '(1) x 6', + desc => 'RT 37892: list as arg to x operator 1', + code => '(1) x 6', expected => [ - 'PPI::Structure::List' => '(1)', - 'PPI::Token::Structure' => '(', + 'PPI::Structure::List' => '(1)', + 'PPI::Token::Structure' => '(', 'PPI::Statement::Expression' => '1', - 'PPI::Token::Number' => '1', - 'PPI::Token::Structure' => ')', - 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Operator' => 'x', - 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Number' => '6', + 'PPI::Token::Number' => '1', + 'PPI::Token::Structure' => ')', + 'PPI::Token::Whitespace' => ' ', + 'PPI::Token::Operator' => 'x', + 'PPI::Token::Whitespace' => ' ', + 'PPI::Token::Number' => '6', ], }, { - desc => 'RT 37892: list as arg to x operator 2', - code => '(1) x6', + desc => 'RT 37892: list as arg to x operator 2', + code => '(1) x6', expected => [ - 'PPI::Structure::List' => '(1)', - 'PPI::Token::Structure' => '(', + 'PPI::Structure::List' => '(1)', + 'PPI::Token::Structure' => '(', 'PPI::Statement::Expression' => '1', - 'PPI::Token::Number' => '1', - 'PPI::Token::Structure' => ')', - 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Operator' => 'x', - 'PPI::Token::Number' => '6', + 'PPI::Token::Number' => '1', + 'PPI::Token::Structure' => ')', + 'PPI::Token::Whitespace' => ' ', + 'PPI::Token::Operator' => 'x', + 'PPI::Token::Number' => '6', ], }, { - desc => 'RT 37892: list as arg to x operator 3', - code => '(1)x6', + desc => 'RT 37892: list as arg to x operator 3', + code => '(1)x6', expected => [ - 'PPI::Structure::List' => '(1)', - 'PPI::Token::Structure' => '(', + 'PPI::Structure::List' => '(1)', + 'PPI::Token::Structure' => '(', 'PPI::Statement::Expression' => '1', - 'PPI::Token::Number' => '1', - 'PPI::Token::Structure' => ')', - 'PPI::Token::Operator' => 'x', - 'PPI::Token::Number' => '6', + 'PPI::Token::Number' => '1', + 'PPI::Token::Structure' => ')', + 'PPI::Token::Operator' => 'x', + 'PPI::Token::Number' => '6', ], }, { - desc => 'RT 37892: x following function is operator', - code => 'foo()x6', + desc => 'RT 37892: x following function is operator', + code => 'foo()x6', expected => [ - 'PPI::Token::Word' => 'foo', - 'PPI::Structure::List' => '()', + 'PPI::Token::Word' => 'foo', + 'PPI::Structure::List' => '()', 'PPI::Token::Structure' => '(', 'PPI::Token::Structure' => ')', - 'PPI::Token::Operator' => 'x', - 'PPI::Token::Number' => '6', + 'PPI::Token::Operator' => 'x', + 'PPI::Token::Number' => '6', ], }, { - desc => 'RT 37892: list as arg to x operator 4', - code => 'qw(1)x6', + desc => 'RT 37892: list as arg to x operator 4', + code => 'qw(1)x6', expected => [ 'PPI::Token::QuoteLike::Words' => 'qw(1)', - 'PPI::Token::Operator' => 'x', - 'PPI::Token::Number' => '6', + 'PPI::Token::Operator' => 'x', + 'PPI::Token::Number' => '6', ], }, { - desc => 'RT 37892: list as arg to x operator 5', - code => 'qw<1>x6', + desc => 'RT 37892: list as arg to x operator 5', + code => 'qw<1>x6', expected => [ 'PPI::Token::QuoteLike::Words' => 'qw<1>', - 'PPI::Token::Operator' => 'x', - 'PPI::Token::Number' => '6', + 'PPI::Token::Operator' => 'x', + 'PPI::Token::Number' => '6', ], }, { - desc => 'RT 37892: listref as arg to x operator 6', - code => '[1]x6', + desc => 'RT 37892: listref as arg to x operator 6', + code => '[1]x6', expected => [ 'PPI::Structure::Constructor' => '[1]', - 'PPI::Token::Structure' => '[', - 'PPI::Statement' => '1', - 'PPI::Token::Number' => '1', - 'PPI::Token::Structure' => ']', - 'PPI::Token::Operator' => 'x', - 'PPI::Token::Number' => '6', + 'PPI::Token::Structure' => '[', + 'PPI::Statement' => '1', + 'PPI::Token::Number' => '1', + 'PPI::Token::Structure' => ']', + 'PPI::Token::Operator' => 'x', + 'PPI::Token::Number' => '6', ], }, { - desc => 'x followed by sigil $ that is not also an operator', - code => '1x$bar', + desc => 'x followed by sigil $ that is not also an operator', + code => '1x$bar', expected => [ - 'PPI::Token::Number' => '1', + 'PPI::Token::Number' => '1', 'PPI::Token::Operator' => 'x', - 'PPI::Token::Symbol' => '$bar', + 'PPI::Token::Symbol' => '$bar', ], }, { - desc => 'x followed by sigil @ that is not also an operator', - code => '1x@bar', + desc => 'x followed by sigil @ that is not also an operator', + code => '1x@bar', expected => [ - 'PPI::Token::Number' => '1', + 'PPI::Token::Number' => '1', 'PPI::Token::Operator' => 'x', - 'PPI::Token::Symbol' => '@bar', + 'PPI::Token::Symbol' => '@bar', ], }, { - desc => 'sub name /^x/', - code => 'sub xyzzy : _5x5 {1;}', + desc => 'sub name /^x/', + code => 'sub xyzzy : _5x5 {1;}', expected => [ - 'PPI::Statement::Sub' => 'sub xyzzy : _5x5 {1;}', - 'PPI::Token::Word' => 'sub', + 'PPI::Statement::Sub' => 'sub xyzzy : _5x5 {1;}', + 'PPI::Token::Word' => 'sub', 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Word' => 'xyzzy', + 'PPI::Token::Word' => 'xyzzy', 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Operator' => ':', + 'PPI::Token::Operator' => ':', 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Attribute' => '_5x5', + 'PPI::Token::Attribute' => '_5x5', 'PPI::Token::Whitespace' => ' ', - 'PPI::Structure::Block' => '{1;}', - 'PPI::Token::Structure' => '{', - 'PPI::Statement' => '1;', - 'PPI::Token::Number' => '1', - 'PPI::Token::Structure' => ';', - 'PPI::Token::Structure' => '}', + 'PPI::Structure::Block' => '{1;}', + 'PPI::Token::Structure' => '{', + 'PPI::Statement' => '1;', + 'PPI::Token::Number' => '1', + 'PPI::Token::Structure' => ';', + 'PPI::Token::Structure' => '}', ] }, { - desc => 'label plus x', - code => 'LABEL: x64', + desc => 'label plus x', + code => 'LABEL: x64', expected => [ 'PPI::Statement::Compound' => 'LABEL:', - 'PPI::Token::Label' => 'LABEL:', - 'PPI::Token::Whitespace' => ' ', - 'PPI::Statement' => 'x64', - 'PPI::Token::Word' => 'x64', + 'PPI::Token::Label' => 'LABEL:', + 'PPI::Token::Whitespace' => ' ', + 'PPI::Statement' => 'x64', + 'PPI::Token::Word' => 'x64', ] }, ); @@ -493,7 +499,8 @@ OPERATOR_X: { # operator. my %operators = ( %OPERATOR, - map { $_ => 1 } qw( -r -w -x -o -R -W -X -O -e -z -s -f -d -l -p -S -b -c -t -u -g -k -T -B -M -A -C ) + map { $_ => 1 } + qw( -r -w -x -o -R -W -X -O -e -z -s -f -d -l -p -S -b -c -t -u -g -k -T -B -M -A -C ) ); # Don't try to test operators for which PPI currently (1.215) # doesn't recognize when they're followed immediately by a word. @@ -510,27 +517,38 @@ OPERATOR_X: { if ( $operator =~ /^\w/ ) { $code .= '$a '; - push @expected, ( 'PPI::Token::Symbol' => '$a' ); + push @expected, ( 'PPI::Token::Symbol' => '$a' ); push @expected, ( 'PPI::Token::Whitespace' => ' ' ); } - elsif ( $operator !~ /^-\w/ ) { # filetest operators + elsif ( $operator !~ /^-\w/ ) { # filetest operators $code .= '$a'; push @expected, ( 'PPI::Token::Symbol' => '$a' ); } $code .= $operator; - push @expected, ( ($operator eq '<<>>' || $operator eq '<>' ? - 'PPI::Token::QuoteLike::Readline' : 'PPI::Token::Operator') => $operator ); + push @expected, + ( + ( + $operator eq '<<>>' || $operator eq '<>' + ? 'PPI::Token::QuoteLike::Readline' + : 'PPI::Token::Operator' + ) => $operator + ); - if ( $operator =~ /\w$/ || $operator eq '<<' ) { # want << operator, not heredoc + if ( $operator =~ /\w$/ || $operator eq '<<' ) + { # want << operator, not heredoc $code .= ' '; push @expected, ( 'PPI::Token::Whitespace' => ' ' ); } $code .= 'x3'; my $desc; - if ( $operator eq '--' || $operator eq '++' || $operator eq '<>' || $operator eq '<<>>' ) { + if ( $operator eq '--' + || $operator eq '++' + || $operator eq '<>' + || $operator eq '<<>>' ) + { push @expected, ( 'PPI::Token::Operator' => 'x' ); - push @expected, ( 'PPI::Token::Number' => '3' ); + push @expected, ( 'PPI::Token::Number' => '3' ); $desc = "operator $operator does not imply following 'x' is a word"; } else { @@ -541,47 +559,52 @@ OPERATOR_X: { push @tests, { desc => $desc, code => $code, expected => \@expected }; } - # Test that Perl builtins known to have a null prototype do not # force a following 'x' to be a word. my %noprotos = map { $_ => 1 } qw( - endgrent - endhostent - endnetent - endprotoent - endpwent - endservent - fork - getgrent - gethostent - getlogin - getnetent - getppid - getprotoent - getpwent - getservent - setgrent - setpwent - time - times - wait - wantarray - __SUB__ + endgrent + endhostent + endnetent + endprotoent + endpwent + endservent + fork + getgrent + gethostent + getlogin + getnetent + getppid + getprotoent + getpwent + getservent + setgrent + setpwent + time + times + wait + wantarray + __SUB__ ); + foreach my $noproto ( keys %noprotos ) { - my $code = "$noproto x3"; + my $code = "$noproto x3"; my @expected = ( - 'PPI::Token::Word' => $noproto, + 'PPI::Token::Word' => $noproto, 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Operator' => 'x', - 'PPI::Token::Number' => '3', + 'PPI::Token::Operator' => 'x', + 'PPI::Token::Number' => '3', ); my $desc = "builtin $noproto does not force following x to be a word"; - push @tests, { desc => "builtin $noproto does not force following x to be a word", code => $code, expected => \@expected }; + push @tests, + { + desc => "builtin $noproto does not force following x to be a word", + code => $code, + expected => \@expected + }; } - foreach my $test ( @tests ) { - my $d = safe_new \$test->{code}; + foreach my $test (@tests) { + my $d = safe_new \$test->{code}; my $tokens = $d->find( sub { 1; } ); $tokens = [ map { ref($_), $_->content() } @$tokens ]; my $expected = $test->{expected}; @@ -597,83 +620,94 @@ OPERATOR_X: { } } - OPERATOR_FAT_COMMA: { my @tests = ( { - desc => 'integer with integer', - code => '1 => 2', - expected => [ - 'PPI::Token::Number' => '1', - 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Operator' => '=>', - 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Number' => '2', - ], - }, - { - desc => 'word with integer', - code => 'foo => 2', - expected => [ - 'PPI::Token::Word' => 'foo', - 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Operator' => '=>', - 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Number' => '2', - ], - }, - { - desc => 'dashed word with integer', - code => '-foo => 2', - expected => [ - 'PPI::Token::Word' => '-foo', - 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Operator' => '=>', - 'PPI::Token::Whitespace' => ' ', - 'PPI::Token::Number' => '2', - ], - }, - ( map { { - desc=>$_, - code=>"$_=>2", - expected=>[ - 'PPI::Token::Word' => $_, - 'PPI::Token::Operator' => '=>', - 'PPI::Token::Number' => '2', - ] - } } keys %KEYWORDS ), - ( map { { - desc=>$_, - code=>"($_=>2)", - expected=>[ - 'PPI::Structure::List' => "($_=>2)", - 'PPI::Token::Structure' => '(', - 'PPI::Statement::Expression' => "$_=>2", - 'PPI::Token::Word' => $_, - 'PPI::Token::Operator' => '=>', - 'PPI::Token::Number' => '2', - 'PPI::Token::Structure' => ')', - ] - } } keys %KEYWORDS ), - ( map { { - desc=>$_, - code=>"{$_=>2}", - expected=>[ - 'PPI::Structure::Constructor' => "{$_=>2}", - 'PPI::Token::Structure' => '{', - 'PPI::Statement::Expression' => "$_=>2", - 'PPI::Token::Word' => $_, - 'PPI::Token::Operator' => '=>', - 'PPI::Token::Number' => '2', - 'PPI::Token::Structure' => '}', - ] - } } keys %KEYWORDS ), + desc => 'integer with integer', + code => '1 => 2', + expected => [ + 'PPI::Token::Number' => '1', + 'PPI::Token::Whitespace' => ' ', + 'PPI::Token::Operator' => '=>', + 'PPI::Token::Whitespace' => ' ', + 'PPI::Token::Number' => '2', + ], + }, + { + desc => 'word with integer', + code => 'foo => 2', + expected => [ + 'PPI::Token::Word' => 'foo', + 'PPI::Token::Whitespace' => ' ', + 'PPI::Token::Operator' => '=>', + 'PPI::Token::Whitespace' => ' ', + 'PPI::Token::Number' => '2', + ], + }, + { + desc => 'dashed word with integer', + code => '-foo => 2', + expected => [ + 'PPI::Token::Word' => '-foo', + 'PPI::Token::Whitespace' => ' ', + 'PPI::Token::Operator' => '=>', + 'PPI::Token::Whitespace' => ' ', + 'PPI::Token::Number' => '2', + ], + }, + ( + map { + { + desc => $_, + code => "$_=>2", + expected => [ + 'PPI::Token::Word' => $_, + 'PPI::Token::Operator' => '=>', + 'PPI::Token::Number' => '2', + ] + } + } keys %KEYWORDS + ), + ( + map { + { + desc => $_, + code => "($_=>2)", + expected => [ + 'PPI::Structure::List' => "($_=>2)", + 'PPI::Token::Structure' => '(', + 'PPI::Statement::Expression' => "$_=>2", + 'PPI::Token::Word' => $_, + 'PPI::Token::Operator' => '=>', + 'PPI::Token::Number' => '2', + 'PPI::Token::Structure' => ')', + ] + } + } keys %KEYWORDS + ), + ( + map { + { + desc => $_, + code => "{$_=>2}", + expected => [ + 'PPI::Structure::Constructor' => "{$_=>2}", + 'PPI::Token::Structure' => '{', + 'PPI::Statement::Expression' => "$_=>2", + 'PPI::Token::Word' => $_, + 'PPI::Token::Operator' => '=>', + 'PPI::Token::Number' => '2', + 'PPI::Token::Structure' => '}', + ] + } + } keys %KEYWORDS + ), ); - for my $test ( @tests ) { + for my $test (@tests) { my $code = $test->{code}; - my $d = safe_new \$test->{code}; + my $d = safe_new \$test->{code}; my $tokens = $d->find( sub { 1; } ); $tokens = [ map { ref($_), $_->content() } @$tokens ]; my $expected = $test->{expected}; @@ -690,41 +724,37 @@ OPERATOR_FAT_COMMA: { } OPERATORS_PLUS_MINUS: { - my @operands = ( - '1', '2', - '1', '(2)', - '(1)', '(2)' - ); + my @operands = ( '1', '2', '1', '(2)', '(1)', '(2)' ); - for my $op (qw/- +/) { - for ( my $i = 0; $i < @operands; $i += 2 ) { - my ( $a, $b ) = @operands[ $i, $i + 1 ]; - my $code = "${a}${op}${b}"; - my $doc = safe_new \$code; - my $ops = $doc->find('Token::Operator'); - is( ref $ops, 'ARRAY', "found operator $op" ); - is( @$ops, 1, "operator $op found exactly once" ); - is( $ops->[0]->content(), $op, "operator $op text matches" ); - } - } + for my $op (qw/- +/) { + for ( my $i = 0 ; $i < @operands ; $i += 2 ) { + my ( $a, $b ) = @operands[ $i, $i + 1 ]; + my $code = "${a}${op}${b}"; + my $doc = safe_new \$code; + my $ops = $doc->find('Token::Operator'); + is( ref $ops, 'ARRAY', "found operator $op" ); + is( @$ops, 1, "operator $op found exactly once" ); + is( $ops->[0]->content(), $op, "operator $op text matches" ); + } + } - # Add "'(1)', '2'" into operands once TODO is resolved. - { - my ( $a, $b ) = ( '(1)', '2' ); - my $op = '+'; - my $code = "${a}${op}${b}"; - my $doc = safe_new \$code; - my $ops = $doc->find('Token::Operator'); - is( ref $ops, 'ARRAY', "found operator $op" ); - } + # Add "'(1)', '2'" into operands once TODO is resolved. + { + my ( $a, $b ) = ( '(1)', '2' ); + my $op = '+'; + my $code = "${a}${op}${b}"; + my $doc = safe_new \$code; + my $ops = $doc->find('Token::Operator'); + is( ref $ops, 'ARRAY', "found operator $op" ); + } - TODO: { - my ( $a, $b ) = ( '(1)', '2' ); - my $op = '-'; - my $code = "${a}${op}${b}"; - my $doc = safe_new \$code; - my $ops = $doc->find('Token::Operator'); - local $TODO = "(1)-2 not parsed correctly"; - is( ref $ops, 'ARRAY', "found operator $op" ); - } + TODO: { + my ( $a, $b ) = ( '(1)', '2' ); + my $op = '-'; + my $code = "${a}${op}${b}"; + my $doc = safe_new \$code; + my $ops = $doc->find('Token::Operator'); + local $TODO = "(1)-2 not parsed correctly"; + is( ref $ops, 'ARRAY', "found operator $op" ); + } } diff --git a/t/ppi_token_pod.t b/t/ppi_token_pod.t index 60027bb8..1480cc36 100644 --- a/t/ppi_token_pod.t +++ b/t/ppi_token_pod.t @@ -4,11 +4,10 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 8 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 8 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); - MERGE: { # Create the test fragments my $one = PPI::Token::Pod->new("=pod\n\nOne\n\n=cut\n"); @@ -17,20 +16,25 @@ MERGE: { isa_ok( $two, 'PPI::Token::Pod' ); # Create the combined Pod - my $merged = PPI::Token::Pod->merge($one, $two); + my $merged = PPI::Token::Pod->merge( $one, $two ); isa_ok( $merged, 'PPI::Token::Pod' ); - is( $merged->content, "=pod\n\nOne\n\nTwo\n\n=cut\n", 'Merged POD looks ok' ); + is( + $merged->content, + "=pod\n\nOne\n\nTwo\n\n=cut\n", + 'Merged POD looks ok' + ); } - TOKENIZE: { foreach my $test ( - [ "=pod\n=cut", [ 'PPI::Token::Pod' ] ], - [ "=pod\n=cut\n", [ 'PPI::Token::Pod' ] ], + [ "=pod\n=cut", ['PPI::Token::Pod'] ], + [ "=pod\n=cut\n", ['PPI::Token::Pod'] ], [ "=pod\n=cut\n\n", [ 'PPI::Token::Pod', 'PPI::Token::Whitespace' ] ], - [ "=pod\n=Cut\n\n", [ 'PPI::Token::Pod' ] ], # pod doesn't end, so no whitespace token - ) { - my $T = PPI::Tokenizer->new( \$test->[0] ); + [ "=pod\n=Cut\n\n", ['PPI::Token::Pod'] ] + , # pod doesn't end, so no whitespace token + ) + { + my $T = PPI::Tokenizer->new( \$test->[0] ); my @tokens = map { ref $_ } @{ $T->all_tokens }; is_deeply( \@tokens, $test->[1], 'all tokens as expected' ); } diff --git a/t/ppi_token_prototype.t b/t/ppi_token_prototype.t index 2a8a02ad..fa807061 100644 --- a/t/ppi_token_prototype.t +++ b/t/ppi_token_prototype.t @@ -21,7 +21,7 @@ PARSING: { check_w_subs \@sub_patterns, '', '', ''; check_w_subs \@sub_patterns, '()', '()', ''; check_w_subs \@sub_patterns, '( )', '( )', ''; - check_w_subs \@sub_patterns, ' () ',, '()', ''; + check_w_subs \@sub_patterns, ' () ', '()', ''; check_w_subs \@sub_patterns, '(+@)', '(+@)', '+@'; check_w_subs \@sub_patterns, ' (+@) ', '(+@)', '+@'; check_w_subs \@sub_patterns, '(\[$;$_@])', '(\[$;$_@])', '\[$;$_@]'; diff --git a/t/ppi_token_quote.t b/t/ppi_token_quote.t index 5cea1ced..c152ec79 100644 --- a/t/ppi_token_quote.t +++ b/t/ppi_token_quote.t @@ -4,12 +4,11 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 16 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 16 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); use Helper 'safe_new'; - STRING: { # Prove what we say in the ->string docs my $Document = safe_new \<<'END_PERL'; @@ -20,12 +19,12 @@ STRING: { END_PERL my $quotes = $Document->find('Token::Quote'); - is( ref($quotes), 'ARRAY', 'Found quotes' ); - is( scalar(@$quotes), 4, 'Found 4 quotes' ); - foreach my $Quote ( @$quotes ) { - isa_ok( $Quote, 'PPI::Token::Quote'); - can_ok( $Quote, 'string' ); - is( $Quote->string, 'foo', '->string returns "foo" for ' - . $Quote->content ); + is( ref($quotes), 'ARRAY', 'Found quotes' ); + is( scalar(@$quotes), 4, 'Found 4 quotes' ); + foreach my $Quote (@$quotes) { + isa_ok( $Quote, 'PPI::Token::Quote' ); + can_ok( $Quote, 'string' ); + is( $Quote->string, 'foo', + '->string returns "foo" for ' . $Quote->content ); } } diff --git a/t/ppi_token_quote_double.t b/t/ppi_token_quote_double.t index a94181c2..5380dd9b 100644 --- a/t/ppi_token_quote_double.t +++ b/t/ppi_token_quote_double.t @@ -4,12 +4,11 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 22 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 22 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); use Helper 'safe_new'; - INTERPOLATIONS: { # Get a set of objects my $Document = safe_new \<<'END_PERL'; @@ -21,16 +20,15 @@ INTERPOLATIONS: { "" # False content to test double-negation scoping END_PERL my $strings = $Document->find('Token::Quote::Double'); - is( scalar @{$strings}, 6, 'Found the 6 test strings' ); - is( $strings->[0]->interpolations, '', 'String 1: No interpolations' ); - is( $strings->[1]->interpolations, '', 'String 2: No interpolations' ); + is( scalar @{$strings}, 6, 'Found the 6 test strings' ); + is( $strings->[0]->interpolations, '', 'String 1: No interpolations' ); + is( $strings->[1]->interpolations, '', 'String 2: No interpolations' ); is( $strings->[2]->interpolations, 1, 'String 3: Has interpolations' ); is( $strings->[3]->interpolations, 1, 'String 4: Has interpolations' ); is( $strings->[4]->interpolations, 1, 'String 5: Has interpolations' ); - is( $strings->[5]->interpolations, '', 'String 6: No interpolations' ); + is( $strings->[5]->interpolations, '', 'String 6: No interpolations' ); } - SIMPLIFY: { my $Document = safe_new \<<'END_PERL'; "no special characters" @@ -42,18 +40,37 @@ SIMPLIFY: { END_PERL my $strings = $Document->find('Token::Quote::Double'); is( scalar @{$strings}, 6, 'Found the 6 test strings' ); - is( $strings->[0]->simplify, q<'no special characters'>, 'String 1: No special characters' ); - is( $strings->[1]->simplify, q<"has \"double\" quotes">, 'String 2: Double quotes' ); - is( $strings->[2]->simplify, q<"has 'single' quotes">, 'String 3: Single quotes' ); - is( $strings->[3]->simplify, q<"has $interpolation">, 'String 3: Has interpolation' ); - is( $strings->[4]->simplify, q<"has @interpolation">, 'String 4: Has interpolation' ); - is( $strings->[5]->simplify, q<''>, 'String 6: Empty string' ); + is( + $strings->[0]->simplify, + q<'no special characters'>, + 'String 1: No special characters' + ); + is( + $strings->[1]->simplify, + q<"has \"double\" quotes">, + 'String 2: Double quotes' + ); + is( + $strings->[2]->simplify, + q<"has 'single' quotes">, + 'String 3: Single quotes' + ); + is( + $strings->[3]->simplify, + q<"has $interpolation">, + 'String 3: Has interpolation' + ); + is( + $strings->[4]->simplify, + q<"has @interpolation">, + 'String 4: Has interpolation' + ); + is( $strings->[5]->simplify, q<''>, 'String 6: Empty string' ); } - STRING: { my $Document = safe_new \'print "foo";'; - my $Double = $Document->find_first('Token::Quote::Double'); + my $Double = $Document->find_first('Token::Quote::Double'); isa_ok( $Double, 'PPI::Token::Quote::Double' ); is( $Double->string, 'foo', '->string returns as expected' ); } diff --git a/t/ppi_token_quote_interpolate.t b/t/ppi_token_quote_interpolate.t index b239f987..4b46327f 100644 --- a/t/ppi_token_quote_interpolate.t +++ b/t/ppi_token_quote_interpolate.t @@ -4,14 +4,13 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 9 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 9 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); use Helper 'safe_new'; - STRING: { - my $Document = safe_new \"print qq{foo}, qq!bar!, qq ;"; + my $Document = safe_new \"print qq{foo}, qq!bar!, qq ;"; my $Interpolate = $Document->find('Token::Quote::Interpolate'); is( scalar(@$Interpolate), 3, '->find returns three objects' ); isa_ok( $Interpolate->[0], 'PPI::Token::Quote::Interpolate' ); diff --git a/t/ppi_token_quote_literal.t b/t/ppi_token_quote_literal.t index 8dfc9245..276d778f 100644 --- a/t/ppi_token_quote_literal.t +++ b/t/ppi_token_quote_literal.t @@ -12,7 +12,7 @@ use Helper 'safe_new'; STRING: { my $Document = safe_new \"print q{foo}, q!bar!, q , q((foo));"; - my $literal = $Document->find('Token::Quote::Literal'); + my $literal = $Document->find('Token::Quote::Literal'); is( scalar(@$literal), 4, '->find returns three objects' ); isa_ok( $literal->[0], 'PPI::Token::Quote::Literal' ); isa_ok( $literal->[1], 'PPI::Token::Quote::Literal' ); @@ -26,7 +26,7 @@ STRING: { LITERAL: { my $Document = safe_new \"print q{foo}, q!bar!, q , q((foo));"; - my $literal = $Document->find('Token::Quote::Literal'); + my $literal = $Document->find('Token::Quote::Literal'); is( $literal->[0]->literal, 'foo', '->literal returns as expected' ); is( $literal->[1]->literal, 'bar', '->literal returns as expected' ); is( $literal->[2]->literal, 'foo', '->literal returns as expected' ); @@ -62,7 +62,7 @@ test_statement( ); sub one_line_explain { - my ( $data ) = @_; + my ($data) = @_; my @explain = explain $data; s/\n//g for @explain; return join "", @explain; @@ -83,9 +83,9 @@ sub test_statement { my ( $code, $expected, $msg ) = @_; $msg = perlstring $code if !defined $msg; - my $d = safe_new \$code; + my $d = safe_new \$code; my $tokens = $d->find( sub { $_[1]->significant } ); - $tokens = [ map { ref( $_ ), $_->content } @$tokens ]; + $tokens = [ map { ref($_), $_->content } @$tokens ]; if ( $expected->[0] !~ /^PPI::Statement/ ) { $expected = [ 'PPI::Statement', $code, @$expected ]; diff --git a/t/ppi_token_quote_single.t b/t/ppi_token_quote_single.t index 4996266f..caa263fb 100644 --- a/t/ppi_token_quote_single.t +++ b/t/ppi_token_quote_single.t @@ -4,31 +4,26 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 32 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 32 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); use Helper 'safe_new'; - STRING: { my $Document = safe_new \"print 'foo';"; - my $Single = $Document->find_first('Token::Quote::Single'); + my $Single = $Document->find_first('Token::Quote::Single'); isa_ok( $Single, 'PPI::Token::Quote::Single' ); is( $Single->string, 'foo', '->string returns as expected' ); } - LITERAL: { my @pairs = ( - "''", '', - "'f'", 'f', - "'f\\'b'", "f\'b", - "'f\\nb'", "f\\nb", - "'f\\\\b'", "f\\b", - "'f\\\\\\b'", "f\\\\b", + "''", '', "'f'", 'f', + "'f\\'b'", "f\'b", "'f\\nb'", "f\\nb", + "'f\\\\b'", "f\\b", "'f\\\\\\b'", "f\\\\b", "'f\\\\\\\''", "f\\'", ); - while ( @pairs ) { + while (@pairs) { my $from = shift @pairs; my $to = shift @pairs; my $doc = safe_new \"print $from;"; diff --git a/t/ppi_token_quotelike_regexp.t b/t/ppi_token_quotelike_regexp.t index 0d508494..22b75eeb 100644 --- a/t/ppi_token_quotelike_regexp.t +++ b/t/ppi_token_quotelike_regexp.t @@ -6,7 +6,7 @@ use lib 't/lib'; use PPI::Test::pragmas; # Execute the tests -use Test::More tests => 7 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 7 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use lib 't/lib'; use Helper qw( check_with ); @@ -15,10 +15,11 @@ run(); sub run { check_with "qr{a}i", sub { - my $qr = $_->find_first( 'Token::QuoteLike::Regexp' ); + my $qr = $_->find_first('Token::QuoteLike::Regexp'); ok $qr, 'found qr token'; - is $qr->get_match_string, "a", "sucessfully retrieved match string"; - is $qr->get_substitute_string, undef, "substitute string method exists but returns undef"; + is $qr->get_match_string, "a", "sucessfully retrieved match string"; + is $qr->get_substitute_string, undef, + "substitute string method exists but returns undef"; ok $qr->get_modifiers->{i}, "regex modifiers can be queried"; is( ( $qr->get_delimiters )[0], "{}", "delimiters can be retrieved" ); }; diff --git a/t/ppi_token_quotelike_words.t b/t/ppi_token_quotelike_words.t index f2494aa0..e0164288 100644 --- a/t/ppi_token_quotelike_words.t +++ b/t/ppi_token_quotelike_words.t @@ -4,7 +4,7 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 2425 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 2425 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); use Helper 'safe_new'; @@ -12,7 +12,30 @@ use Helper 'safe_new'; sub permute_test; sub assemble_and_run; -my %known_bad = map { $_ => 1 } "qw ' \\' '", "qw ( \\( )", "qw ( \\) )", "qw / \\/ /", "qw 1 a \\1 1", "qw < \\< >", "qw < \\> >", "qw [ \\[ ]", "qw [ \\] ]", "qw \" \\\" \"", "qw a \\a a", "qw { \\{ }", "qw { \\} }", "qw# \\# #", "qw#\\##", "qw#\n\\#\n#", "qw' \\' '", "qw'\\''", "qw'\f\\'\f'", "qw'\n\\'\n'", "qw'\t\\'\t'", "qw( \\( )", "qw( \\) )", "qw( \\\\ )", "qw(\\()", "qw(\\))", "qw(\f\\(\f)", "qw(\f\\)\f)", "qw(\n\\(\n)", "qw(\n\\)\n)", "qw(\n\\\\\n)", "qw(\t\\(\t)", "qw(\t\\)\t)", "qw/ \\/ /", "qw/\\//", "qw/\f\\/\f/", "qw/\n\\/\n/", "qw/\t\\/\t/", "qw< \\< >", "qw< \\> >", "qw<\\<>", "qw<\\>>", "qw<\f\\<\f>", "qw<\f\\>\f>", "qw<\n\\<\n>", "qw<\n\\>\n>", "qw<\t\\<\t>", "qw<\t\\>\t>", "qw[ \\[ ]", "qw[ \\] ]", "qw[\\[]", "qw[\\]]", "qw[\f\\[\f]", "qw[\f\\]\f]", "qw[\n\\[\n]", "qw[\n\\]\n]", "qw[\t\\[\t]", "qw[\t\\]\t]", "qw\" \\\" \"", "qw\"\\\"\"", "qw\"\f\\\"\f\"", "qw\"\n\\\"\n\"", "qw\"\t\\\"\t\"", "qw\f'\f\\'\f'", "qw\f(\f\\(\f)", "qw\f(\f\\)\f)", "qw\f/\f\\/\f/", "qw\f<\f\\<\f>", "qw\f<\f\\>\f>", "qw\f[\f\\[\f]", "qw\f[\f\\]\f]", "qw\f\"\f\\\"\f\"", "qw\f{\f\\{\f}", "qw\f{\f\\}\f}", "qw\n'\n\\'\n'", "qw\n(\n\\(\n)", "qw\n(\n\\)\n)", "qw\n/\n\\/\n/", "qw\n<\n\\<\n>", "qw\n<\n\\>\n>", "qw\n[\n\\[\n]", "qw\n[\n\\]\n]", "qw\n\"\n\\\"\n\"", "qw\na\n\\a\na", "qw\n{\n\\{\n}", "qw\n{\n\\}\n}", "qw\t'\t\\'\t'", "qw\t(\t\\(\t)", "qw\t(\t\\)\t)", "qw\t/\t\\/\t/", "qw\t<\t\\<\t>", "qw\t<\t\\>\t>", "qw\t[\t\\[\t]", "qw\t[\t\\]\t]", "qw\t\"\t\\\"\t\"", "qw\t{\t\\{\t}", "qw\t{\t\\}\t}", "qw{ \\{ }", "qw{ \\} }", "qw{\\{}", "qw{\\}}", "qw{\f\\{\f}", "qw{\f\\}\f}", "qw{\n\\{\n}", "qw{\n\\}\n}", "qw{\t\\{\t}", "qw{\t\\}\t}"; +my %known_bad = map { $_ => 1 } "qw ' \\' '", "qw ( \\( )", "qw ( \\) )", + "qw / \\/ /", "qw 1 a \\1 1", "qw < \\< >", "qw < \\> >", "qw [ \\[ ]", + "qw [ \\] ]", "qw \" \\\" \"", "qw a \\a a", "qw { \\{ }", "qw { \\} }", + "qw# \\# #", "qw#\\##", "qw#\n\\#\n#", "qw' \\' '", "qw'\\''", "qw'\f\\'\f'", + "qw'\n\\'\n'", "qw'\t\\'\t'", "qw( \\( )", "qw( \\) )", "qw( \\\\ )", + "qw(\\()", "qw(\\))", "qw(\f\\(\f)", "qw(\f\\)\f)", "qw(\n\\(\n)", + "qw(\n\\)\n)", "qw(\n\\\\\n)", "qw(\t\\(\t)", "qw(\t\\)\t)", "qw/ \\/ /", + "qw/\\//", "qw/\f\\/\f/", "qw/\n\\/\n/", "qw/\t\\/\t/", "qw< \\< >", + "qw< \\> >", "qw<\\<>", "qw<\\>>", "qw<\f\\<\f>", "qw<\f\\>\f>", + "qw<\n\\<\n>", "qw<\n\\>\n>", "qw<\t\\<\t>", "qw<\t\\>\t>", "qw[ \\[ ]", + "qw[ \\] ]", "qw[\\[]", "qw[\\]]", "qw[\f\\[\f]", "qw[\f\\]\f]", + "qw[\n\\[\n]", "qw[\n\\]\n]", "qw[\t\\[\t]", "qw[\t\\]\t]", "qw\" \\\" \"", + "qw\"\\\"\"", "qw\"\f\\\"\f\"", "qw\"\n\\\"\n\"", "qw\"\t\\\"\t\"", + "qw\f'\f\\'\f'", "qw\f(\f\\(\f)", "qw\f(\f\\)\f)", "qw\f/\f\\/\f/", + "qw\f<\f\\<\f>", "qw\f<\f\\>\f>", "qw\f[\f\\[\f]", "qw\f[\f\\]\f]", + "qw\f\"\f\\\"\f\"", "qw\f{\f\\{\f}", "qw\f{\f\\}\f}", "qw\n'\n\\'\n'", + "qw\n(\n\\(\n)", "qw\n(\n\\)\n)", "qw\n/\n\\/\n/", "qw\n<\n\\<\n>", + "qw\n<\n\\>\n>", "qw\n[\n\\[\n]", "qw\n[\n\\]\n]", "qw\n\"\n\\\"\n\"", + "qw\na\n\\a\na", "qw\n{\n\\{\n}", "qw\n{\n\\}\n}", "qw\t'\t\\'\t'", + "qw\t(\t\\(\t)", "qw\t(\t\\)\t)", "qw\t/\t\\/\t/", "qw\t<\t\\<\t>", + "qw\t<\t\\>\t>", "qw\t[\t\\[\t]", "qw\t[\t\\]\t]", "qw\t\"\t\\\"\t\"", + "qw\t{\t\\{\t}", "qw\t{\t\\}\t}", "qw{ \\{ }", "qw{ \\} }", "qw{\\{}", + "qw{\\}}", "qw{\f\\{\f}", "qw{\f\\}\f}", "qw{\n\\{\n}", "qw{\n\\}\n}", + "qw{\t\\{\t}", "qw{\t\\}\t}"; LITERAL: { # empty @@ -25,10 +48,10 @@ LITERAL: { permute_test [], '<', '>', []; # words - permute_test ['a', 'b', 'c'], '/', '/', ['a', 'b', 'c']; - permute_test ['a,', 'b', 'c,'], '/', '/', ['a,', 'b', 'c,']; - permute_test ['a', ',', '#', 'c'], '/', '/', ['a', ',', '#', 'c']; - permute_test ['f_oo', 'b_ar'], '/', '/', ['f_oo', 'b_ar']; + permute_test [ 'a', 'b', 'c' ], '/', '/', [ 'a', 'b', 'c' ]; + permute_test [ 'a,', 'b', 'c,' ], '/', '/', [ 'a,', 'b', 'c,' ]; + permute_test [ 'a', ',', '#', 'c' ], '/', '/', [ 'a', ',', '#', 'c' ]; + permute_test [ 'f_oo', 'b_ar' ], '/', '/', [ 'f_oo', 'b_ar' ]; # it's allowed for both delims to be closers permute_test ['a'], ')', ')', ['a']; @@ -37,56 +60,56 @@ LITERAL: { permute_test ['a'], '>', '>', ['a']; # containing things that sometimes are delimiters - permute_test ['/'], '(', ')', ['/']; - permute_test ['//'], '(', ')', ['//']; - permute_test ['qw()'], '(', ')', ['qw()']; - permute_test ['qw', '()'], '(', ')', ['qw', '()']; - permute_test ['qw//'], '(', ')', ['qw//']; + permute_test ['/'], '(', ')', ['/']; + permute_test ['//'], '(', ')', ['//']; + permute_test ['qw()'], '(', ')', ['qw()']; + permute_test [ 'qw', '()' ], '(', ')', [ 'qw', '()' ]; + permute_test ['qw//'], '(', ')', ['qw//']; # nested delimiters - permute_test ['()'], '(', ')', ['()']; - permute_test ['{}'], '{', '}', ['{}']; - permute_test ['[]'], '[', ']', ['[]']; - permute_test ['<>'], '<', '>', ['<>']; - permute_test ['((', ')', ')'], '(', ')', ['((', ')', ')']; - permute_test ['{{', '}', '}'], '{', '}', ['{{', '}', '}']; - permute_test ['[[', ']', ']'], '[', ']', ['[[', ']', ']']; - permute_test ['<<', '>', '>'], '<', '>', ['<<', '>', '>']; + permute_test ['()'], '(', ')', ['()']; + permute_test ['{}'], '{', '}', ['{}']; + permute_test ['[]'], '[', ']', ['[]']; + permute_test ['<>'], '<', '>', ['<>']; + permute_test [ '((', ')', ')' ], '(', ')', [ '((', ')', ')' ]; + permute_test [ '{{', '}', '}' ], '{', '}', [ '{{', '}', '}' ]; + permute_test [ '[[', ']', ']' ], '[', ']', [ '[[', ']', ']' ]; + permute_test [ '<<', '>', '>' ], '<', '>', [ '<<', '>', '>' ]; - my $bs = '\\'; # a single backslash character + my $bs = '\\'; # a single backslash character # escaped opening and closing - permute_test ["$bs)"], '(', ')', [')']; - permute_test ["$bs("], '(', ')', ['(']; - permute_test ["$bs}"], '{', '}', ['}']; - permute_test [$bs.'{'], '{', '}', ['{']; - permute_test ["$bs]"], '[', ']', [']']; - permute_test [$bs.'['], '[', ']', ['[']; - permute_test ["$bs<"], '<', '>', ['<']; - permute_test ["$bs>"], '<', '>', ['>']; - permute_test ["$bs/"], '/', '/', ['/']; - permute_test ["$bs'"], "'", "'", ["'"]; - permute_test [$bs.'"'], '"', '"', ['"']; + permute_test ["$bs)"], '(', ')', [')']; + permute_test ["$bs("], '(', ')', ['(']; + permute_test ["$bs}"], '{', '}', ['}']; + permute_test [ $bs . '{' ], '{', '}', ['{']; + permute_test ["$bs]"], '[', ']', [']']; + permute_test [ $bs . '[' ], '[', ']', ['[']; + permute_test ["$bs<"], '<', '>', ['<']; + permute_test ["$bs>"], '<', '>', ['>']; + permute_test ["$bs/"], '/', '/', ['/']; + permute_test ["$bs'"], "'", "'", ["'"]; + permute_test [ $bs . '"' ], '"', '"', ['"']; # alphanum delims have to be separated from qw - assemble_and_run " ", ['a', "${bs}1"], '1', " ", " ", '1', ['a', '1']; - assemble_and_run " ", ["${bs}a"], 'a', " ", " ", 'a', ['a']; - assemble_and_run "\n", ["${bs}a"], 'a', "\n", "\n", 'a', ['a']; + assemble_and_run " ", [ 'a', "${bs}1" ], '1', " ", " ", '1', [ 'a', '1' ]; + assemble_and_run " ", ["${bs}a"], 'a', " ", " ", 'a', ['a']; + assemble_and_run "\n", ["${bs}a"], 'a', "\n", "\n", 'a', ['a']; # '#' delims cannot be separated from qw - assemble_and_run '', ['a'], '#', '', ' ', '#', ['a']; - assemble_and_run '', ['a'], '#', ' ', ' ', '#', ['a']; - assemble_and_run '', ["$bs#"], '#', '', ' ', '#', ['#']; - assemble_and_run '', ["$bs#"], '#', ' ', ' ', '#', ['#']; - assemble_and_run '', ["$bs#"], '#', "\n", "\n", '#', ['#']; + assemble_and_run '', ['a'], '#', '', ' ', '#', ['a']; + assemble_and_run '', ['a'], '#', ' ', ' ', '#', ['a']; + assemble_and_run '', ["$bs#"], '#', '', ' ', '#', ['#']; + assemble_and_run '', ["$bs#"], '#', ' ', ' ', '#', ['#']; + assemble_and_run '', ["$bs#"], '#', "\n", "\n", '#', ['#']; # a single backslash represents itself - assemble_and_run '', [$bs], '(', ' ', ' ', ')', [$bs]; - assemble_and_run '', [$bs], '(', "\n", ' ', ')', [$bs]; + assemble_and_run '', [$bs], '(', ' ', ' ', ')', [$bs]; + assemble_and_run '', [$bs], '(', "\n", ' ', ')', [$bs]; # a double backslash represents itself - assemble_and_run '', ["$bs$bs"], '(', ' ', ' ', ')', [$bs]; - assemble_and_run '', ["$bs$bs"], '(', "\n", ' ', ')', [$bs]; + assemble_and_run '', ["$bs$bs"], '(', ' ', ' ', ')', [$bs]; + assemble_and_run '', ["$bs$bs"], '(', "\n", ' ', ')', [$bs]; # even backslash can be a delimiter, in when it is, backslashes # can't be embedded or escaped. @@ -100,19 +123,26 @@ LITERAL: { sub execute_test { my ( $code, $expected, $msg ) = @_; - my $d = safe_new \$code; - my $found = $d->find( 'PPI::Token::QuoteLike::Words' ) || []; - is( @$found, 1, "$msg - exactly one qw" ); + my $d = safe_new \$code; + my $found = $d->find('PPI::Token::QuoteLike::Words') || []; + is( @$found, 1, "$msg - exactly one qw" ); is( $found->[0]->content, $code, "$msg content()" ); - is_deeply( [ $found->[0]->literal ], $expected, "literal()" ); # can't dump $msg, as it breaks TODO parsing + is_deeply( [ $found->[0]->literal ], $expected, "literal()" ) + ; # can't dump $msg, as it breaks TODO parsing return; } sub assemble_and_run { - my ( $pre_left_delim, $words_in, $left_delim, $delim_padding, $word_separator, $right_delim, $expected ) = @_; - - my $code = "qw$pre_left_delim$left_delim$delim_padding" . join(' ', @$words_in) . "$delim_padding$right_delim"; + my ( + $pre_left_delim, $words_in, $left_delim, $delim_padding, + $word_separator, $right_delim, $expected + ) = @_; + + my $code = + "qw$pre_left_delim$left_delim$delim_padding" + . join( ' ', @$words_in ) + . "$delim_padding$right_delim"; execute_test $code, $expected, $code; return; @@ -121,20 +151,32 @@ sub assemble_and_run { sub permute_test { my ( $words_in, $left_delim, $right_delim, $expected ) = @_; - assemble_and_run "", $words_in, $left_delim, "", " ", $right_delim, $expected; - assemble_and_run "", $words_in, $left_delim, "", "\t", $right_delim, $expected; - assemble_and_run "", $words_in, $left_delim, "", "\n", $right_delim, $expected; - assemble_and_run "", $words_in, $left_delim, "", "\f", $right_delim, $expected; - - assemble_and_run "", $words_in, $left_delim, " ", " ", $right_delim, $expected; - assemble_and_run "", $words_in, $left_delim, "\t", "\t", $right_delim, $expected; - assemble_and_run "", $words_in, $left_delim, "\n", "\n", $right_delim, $expected; - assemble_and_run "", $words_in, $left_delim, "\f", "\f", $right_delim, $expected; - - assemble_and_run " ", $words_in, $left_delim, " ", " ", $right_delim, $expected; - assemble_and_run "\t", $words_in, $left_delim, "\t", "\t", $right_delim, $expected; - assemble_and_run "\n", $words_in, $left_delim, "\n", "\n", $right_delim, $expected; - assemble_and_run "\f", $words_in, $left_delim, "\f", "\f", $right_delim, $expected; + assemble_and_run "", $words_in, $left_delim, "", " ", $right_delim, + $expected; + assemble_and_run "", $words_in, $left_delim, "", "\t", $right_delim, + $expected; + assemble_and_run "", $words_in, $left_delim, "", "\n", $right_delim, + $expected; + assemble_and_run "", $words_in, $left_delim, "", "\f", $right_delim, + $expected; + + assemble_and_run "", $words_in, $left_delim, " ", " ", $right_delim, + $expected; + assemble_and_run "", $words_in, $left_delim, "\t", "\t", $right_delim, + $expected; + assemble_and_run "", $words_in, $left_delim, "\n", "\n", $right_delim, + $expected; + assemble_and_run "", $words_in, $left_delim, "\f", "\f", $right_delim, + $expected; + + assemble_and_run " ", $words_in, $left_delim, " ", " ", $right_delim, + $expected; + assemble_and_run "\t", $words_in, $left_delim, "\t", "\t", $right_delim, + $expected; + assemble_and_run "\n", $words_in, $left_delim, "\n", "\n", $right_delim, + $expected; + assemble_and_run "\f", $words_in, $left_delim, "\f", "\f", $right_delim, + $expected; return; } diff --git a/t/ppi_token_regexp.t b/t/ppi_token_regexp.t index 440a2a2b..808026d9 100644 --- a/t/ppi_token_regexp.t +++ b/t/ppi_token_regexp.t @@ -6,7 +6,7 @@ use lib 't/lib'; use PPI::Test::pragmas; # Execute the tests -use Test::More tests => 11 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 11 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use lib 't/lib'; use Helper qw( check_with ); @@ -15,17 +15,19 @@ run(); sub run { check_with "m{a}i", sub { - my $qr = $_->find_first( 'Token::Regexp' ); + my $qr = $_->find_first('Token::Regexp'); ok $qr, 'found qr token'; - is $qr->get_match_string, "a", "sucessfully retrieved match string"; - is $qr->get_substitute_string, undef, "substitute string method exists but returns undef"; + is $qr->get_match_string, "a", "sucessfully retrieved match string"; + is $qr->get_substitute_string, undef, + "substitute string method exists but returns undef"; ok $qr->get_modifiers->{i}, "regex modifiers can be queried"; is( ( $qr->get_delimiters )[0], "{}", "delimiters can be retrieved" ); }; check_with "s{a}{b}i", sub { - my $qr = $_->find_first( 'Token::Regexp' ); + my $qr = $_->find_first('Token::Regexp'); ok $qr, 'found qr token'; - is $qr->get_substitute_string, "b", "substitute string can be extracted"; + is $qr->get_substitute_string, "b", + "substitute string can be extracted"; }; } diff --git a/t/ppi_token_structure.t b/t/ppi_token_structure.t index 2fa3c810..689ea3e4 100644 --- a/t/ppi_token_structure.t +++ b/t/ppi_token_structure.t @@ -13,16 +13,18 @@ run(); sub run { check_with "(1)", sub { - my $qr = $_->find_first( 'Token::Structure' ); + my $qr = $_->find_first('Token::Structure'); ok $qr, 'found qr token'; - is $qr->snext_sibling, "", "non-semicolon tokens shortcut to empty strong for significant siblings"; - is $qr->sprevious_sibling, "", "non-semicolon tokens shortcut to empty strong for significant siblings"; + is $qr->snext_sibling, "", +"non-semicolon tokens shortcut to empty strong for significant siblings"; + is $qr->sprevious_sibling, "", +"non-semicolon tokens shortcut to empty strong for significant siblings"; }; check_with "(", sub { - my $tokens = $_->find( 'Token::Structure' ); + my $tokens = $_->find('Token::Structure'); ok $tokens->[0], 'found qr token'; is $tokens->[0]->next_token, '', - "empty string is returned as next token for an unclosed structure without children"; +"empty string is returned as next token for an unclosed structure without children"; }; } diff --git a/t/ppi_token_symbol.t b/t/ppi_token_symbol.t index 28820fbd..b8ac3557 100644 --- a/t/ppi_token_symbol.t +++ b/t/ppi_token_symbol.t @@ -4,89 +4,347 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 216 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 216 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); use Helper 'safe_new'; - -my $Token = PPI::Token::Symbol->new( '$foo' ); +my $Token = PPI::Token::Symbol->new('$foo'); isa_ok( $Token, 'PPI::Token::Symbol' ); - TOKEN_FROM_PARSE: { - parse_and_test( '$x', { content => '$x', canonical => '$x', raw_type => '$', symbol_type => '$', symbol => '$x' } ); - parse_and_test( '$x[0]', { content => '$x', canonical => '$x', raw_type => '$', symbol_type => '@', symbol => '@x' } ); - parse_and_test( '$x{0}', { content => '$x', canonical => '$x', raw_type => '$', symbol_type => '%', symbol => '%x' } ); - parse_and_test( '$::x', { content => '$::x', canonical => '$main::x', raw_type => '$', symbol_type => '$', symbol => '$main::x' } ); - parse_and_test( q{$'x}, { content => q{$'x}, canonical => '$main::x', raw_type => '$', symbol_type => '$', symbol => '$main::x' } ); - - parse_and_test( '@x', { content => '@x', canonical => '@x', raw_type => '@', symbol_type => '@', symbol => '@x' } ); - parse_and_test( '@x[0]', { content => '@x', canonical => '@x', raw_type => '@', symbol_type => '@', symbol => '@x' } ); - parse_and_test( '@x[0,1]', { content => '@x', canonical => '@x', raw_type => '@', symbol_type => '@', symbol => '@x' } ); - parse_and_test( '@x{0}', { content => '@x', canonical => '@x', raw_type => '@', symbol_type => '%', symbol => '%x' } ); - parse_and_test( '@::x', { content => '@::x', canonical => '@main::x', raw_type => '@', symbol_type => '@', symbol => '@main::x' } ); - - parse_and_test( '%x', { content => '%x', canonical => '%x', raw_type => '%', symbol_type => '%', symbol => '%x' } ); - parse_and_test( '%x[0]', { content => '%x', canonical => '%x', raw_type => '%', symbol_type => '@', symbol => '@x' } ); - parse_and_test( '%x[0,1]', { content => '%x', canonical => '%x', raw_type => '%', symbol_type => '@', symbol => '@x' } ); - parse_and_test( '%x{0}', { content => '%x', canonical => '%x', raw_type => '%', symbol_type => '%', symbol => '%x' } ); - parse_and_test( '%::x', { content => '%::x', canonical => '%main::x', raw_type => '%', symbol_type => '%', symbol => '%main::x' } ); - - parse_and_test( '&x', { content => '&x', canonical => '&x', raw_type => '&', symbol_type => '&', symbol => '&x' } ); - parse_and_test( '&::x', { content => '&::x', canonical => '&main::x', raw_type => '&', symbol_type => '&', symbol => '&main::x' } ); - - parse_and_test( '*x', { content => '*x', canonical => '*x', raw_type => '*', symbol_type => '*', symbol => '*x' } ); - parse_and_test( '*::x', { content => '*::x', canonical => '*main::x', raw_type => '*', symbol_type => '*', symbol => '*main::x' } ); - - parse_and_test( '$$x[0]', { content => '$x', canonical => '$x', raw_type => '$', symbol_type => '$', symbol => '$x' } ); - parse_and_test( '@$x[0]', { content => '$x', canonical => '$x', raw_type => '$', symbol_type => '$', symbol => '$x' } ); - parse_and_test( '%$x[0]', { content => '$x', canonical => '$x', raw_type => '$', symbol_type => '$', symbol => '$x' } ); - parse_and_test( '$$x{0}', { content => '$x', canonical => '$x', raw_type => '$', symbol_type => '$', symbol => '$x' } ); - parse_and_test( '@$x{0}', { content => '$x', canonical => '$x', raw_type => '$', symbol_type => '$', symbol => '$x' } ); - parse_and_test( '%$x{0}', { content => '$x', canonical => '$x', raw_type => '$', symbol_type => '$', symbol => '$x' } ); -} + parse_and_test( + '$x', + { + content => '$x', + canonical => '$x', + raw_type => '$', + symbol_type => '$', + symbol => '$x' + } + ); + parse_and_test( + '$x[0]', + { + content => '$x', + canonical => '$x', + raw_type => '$', + symbol_type => '@', + symbol => '@x' + } + ); + parse_and_test( + '$x{0}', + { + content => '$x', + canonical => '$x', + raw_type => '$', + symbol_type => '%', + symbol => '%x' + } + ); + parse_and_test( + '$::x', + { + content => '$::x', + canonical => '$main::x', + raw_type => '$', + symbol_type => '$', + symbol => '$main::x' + } + ); + parse_and_test( + q{$'x}, + { + content => q{$'x}, + canonical => '$main::x', + raw_type => '$', + symbol_type => '$', + symbol => '$main::x' + } + ); + + parse_and_test( + '@x', + { + content => '@x', + canonical => '@x', + raw_type => '@', + symbol_type => '@', + symbol => '@x' + } + ); + parse_and_test( + '@x[0]', + { + content => '@x', + canonical => '@x', + raw_type => '@', + symbol_type => '@', + symbol => '@x' + } + ); + parse_and_test( + '@x[0,1]', + { + content => '@x', + canonical => '@x', + raw_type => '@', + symbol_type => '@', + symbol => '@x' + } + ); + parse_and_test( + '@x{0}', + { + content => '@x', + canonical => '@x', + raw_type => '@', + symbol_type => '%', + symbol => '%x' + } + ); + parse_and_test( + '@::x', + { + content => '@::x', + canonical => '@main::x', + raw_type => '@', + symbol_type => '@', + symbol => '@main::x' + } + ); + + parse_and_test( + '%x', + { + content => '%x', + canonical => '%x', + raw_type => '%', + symbol_type => '%', + symbol => '%x' + } + ); + parse_and_test( + '%x[0]', + { + content => '%x', + canonical => '%x', + raw_type => '%', + symbol_type => '@', + symbol => '@x' + } + ); + parse_and_test( + '%x[0,1]', + { + content => '%x', + canonical => '%x', + raw_type => '%', + symbol_type => '@', + symbol => '@x' + } + ); + parse_and_test( + '%x{0}', + { + content => '%x', + canonical => '%x', + raw_type => '%', + symbol_type => '%', + symbol => '%x' + } + ); + parse_and_test( + '%::x', + { + content => '%::x', + canonical => '%main::x', + raw_type => '%', + symbol_type => '%', + symbol => '%main::x' + } + ); + + parse_and_test( + '&x', + { + content => '&x', + canonical => '&x', + raw_type => '&', + symbol_type => '&', + symbol => '&x' + } + ); + parse_and_test( + '&::x', + { + content => '&::x', + canonical => '&main::x', + raw_type => '&', + symbol_type => '&', + symbol => '&main::x' + } + ); + parse_and_test( + '*x', + { + content => '*x', + canonical => '*x', + raw_type => '*', + symbol_type => '*', + symbol => '*x' + } + ); + parse_and_test( + '*::x', + { + content => '*::x', + canonical => '*main::x', + raw_type => '*', + symbol_type => '*', + symbol => '*main::x' + } + ); + + parse_and_test( + '$$x[0]', + { + content => '$x', + canonical => '$x', + raw_type => '$', + symbol_type => '$', + symbol => '$x' + } + ); + parse_and_test( + '@$x[0]', + { + content => '$x', + canonical => '$x', + raw_type => '$', + symbol_type => '$', + symbol => '$x' + } + ); + parse_and_test( + '%$x[0]', + { + content => '$x', + canonical => '$x', + raw_type => '$', + symbol_type => '$', + symbol => '$x' + } + ); + parse_and_test( + '$$x{0}', + { + content => '$x', + canonical => '$x', + raw_type => '$', + symbol_type => '$', + symbol => '$x' + } + ); + parse_and_test( + '@$x{0}', + { + content => '$x', + canonical => '$x', + raw_type => '$', + symbol_type => '$', + symbol => '$x' + } + ); + parse_and_test( + '%$x{0}', + { + content => '$x', + canonical => '$x', + raw_type => '$', + symbol_type => '$', + symbol => '$x' + } + ); +} CONSTRUCT_OWN_TOKEN: { # Test behavior that parsing does not support as of PPI 1.220. - test_symbol( PPI::Token::Symbol->new('$ foo'), { content => '$ foo', canonical => '$foo', raw_type => '$', symbol_type => '$', symbol => '$foo' }, '$ foo' ); - test_symbol( PPI::Token::Symbol->new('$ foo\'bar'), { content => '$ foo\'bar', canonical => '$foo::bar', raw_type => '$', symbol_type => '$', symbol => '$foo::bar' }, '$ foo\'bar' ); + test_symbol( + PPI::Token::Symbol->new('$ foo'), + { + content => '$ foo', + canonical => '$foo', + raw_type => '$', + symbol_type => '$', + symbol => '$foo' + }, + '$ foo' + ); + test_symbol( + PPI::Token::Symbol->new('$ foo\'bar'), + { + content => '$ foo\'bar', + canonical => '$foo::bar', + raw_type => '$', + symbol_type => '$', + symbol => '$foo::bar' + }, + '$ foo\'bar' + ); # example from PPI::Token::Symbol->canonical documentation - test_symbol( PPI::Token::Symbol->new('$ ::foo\'bar::baz'), { content => '$ ::foo\'bar::baz', canonical => '$main::foo::bar::baz', raw_type => '$', symbol_type => '$', symbol => '$main::foo::bar::baz' }, '$ ::foo\'bar::baz' ); + test_symbol( + PPI::Token::Symbol->new('$ ::foo\'bar::baz'), + { + content => '$ ::foo\'bar::baz', + canonical => '$main::foo::bar::baz', + raw_type => '$', + symbol_type => '$', + symbol => '$main::foo::bar::baz' + }, + '$ ::foo\'bar::baz' + ); } - sub parse_and_test { - local $Test::Builder::Level = $Test::Builder::Level+1; + local $Test::Builder::Level = $Test::Builder::Level + 1; my ( $code, $symbol_expected, $msg ) = @_; $msg = $code if !defined $msg; my $Document = safe_new \$code; - my $symbols = $Document->find( 'PPI::Token::Symbol') || []; + my $symbols = $Document->find('PPI::Token::Symbol') || []; is( scalar(@$symbols), 1, "$msg got exactly one symbol" ); test_symbol( $symbols->[0], $symbol_expected, $msg ); return; } - sub test_symbol { - local $Test::Builder::Level = $Test::Builder::Level+1; + local $Test::Builder::Level = $Test::Builder::Level + 1; my ( $symbol, $symbol_expected, $msg ) = @_; - is( $symbol->content, $symbol_expected->{content}, "$msg: content" ); + is( $symbol->content, $symbol_expected->{content}, "$msg: content" ); { - local $TODO = $ENV{TODO} if $ENV{TODO}; - is( $symbol->canonical, $symbol_expected->{canonical}, "$msg: canonical" ); + local $TODO = $ENV{TODO} if $ENV{TODO}; + is( + $symbol->canonical, + $symbol_expected->{canonical}, + "$msg: canonical" + ); } - is( $symbol->raw_type, $symbol_expected->{raw_type}, "$msg: raw_type" ); - is( $symbol->symbol_type, $symbol_expected->{symbol_type}, "$msg: symbol_type" ); + is( $symbol->raw_type, $symbol_expected->{raw_type}, "$msg: raw_type" ); + is( + $symbol->symbol_type, + $symbol_expected->{symbol_type}, + "$msg: symbol_type" + ); local $TODO = $ENV{TODO} if $ENV{TODO}; - is( $symbol->symbol, $symbol_expected->{symbol}, "$msg: symbol" ); + is( $symbol->symbol, $symbol_expected->{symbol}, "$msg: symbol" ); return; } diff --git a/t/ppi_token_unknown.t b/t/ppi_token_unknown.t index 35c52b10..5b231ebf 100644 --- a/t/ppi_token_unknown.t +++ b/t/ppi_token_unknown.t @@ -4,71 +4,71 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 2328 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 2328 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use PPI (); -use B qw( perlstring ); +use B qw( perlstring ); use Helper 'safe_new'; our %known_bad_seps; OPERATOR_CAST: { my @nothing = ( '', [] ); - my @number = ( '1', [ 'PPI::Token::Number' => '1' ] ); + my @number = ( '1', [ 'PPI::Token::Number' => '1' ] ); - my @asterisk_op = ( '*', [ 'PPI::Token::Operator' => '*' ] ); - my @asteriskeq_op = ( '*=', [ 'PPI::Token::Operator' => '*=' ] ); - my @percent_op = ( '%', [ 'PPI::Token::Operator' => '%' ] ); - my @percenteq_op = ( '%=', [ 'PPI::Token::Operator' => '%=' ] ); - my @ampersand_op = ( '&', [ 'PPI::Token::Operator' => '&' ] ); + my @asterisk_op = ( '*', [ 'PPI::Token::Operator' => '*' ] ); + my @asteriskeq_op = ( '*=', [ 'PPI::Token::Operator' => '*=' ] ); + my @percent_op = ( '%', [ 'PPI::Token::Operator' => '%' ] ); + my @percenteq_op = ( '%=', [ 'PPI::Token::Operator' => '%=' ] ); + my @ampersand_op = ( '&', [ 'PPI::Token::Operator' => '&' ] ); my @ampersandeq_op = ( '&=', [ 'PPI::Token::Operator' => '&=' ] ); - my @exp_op = ( '**', [ 'PPI::Token::Operator' => '**' ] ); + my @exp_op = ( '**', [ 'PPI::Token::Operator' => '**' ] ); - my @asterisk_cast = ( '*', [ 'PPI::Token::Cast' => '*' ] ); - my @percent_cast = ( '%', [ 'PPI::Token::Cast' => '%' ] ); + my @asterisk_cast = ( '*', [ 'PPI::Token::Cast' => '*' ] ); + my @percent_cast = ( '%', [ 'PPI::Token::Cast' => '%' ] ); my @ampersand_cast = ( '&', [ 'PPI::Token::Cast' => '&' ] ); - my @at_cast = ( '@', [ 'PPI::Token::Cast' => '@' ] ); + my @at_cast = ( '@', [ 'PPI::Token::Cast' => '@' ] ); - my @scalar = ( '$a', [ 'PPI::Token::Symbol' => '$a' ] ); - my @list = ( '@a', [ 'PPI::Token::Symbol' => '@a' ] ); - my @hash = ( '%a', [ 'PPI::Token::Symbol' => '%a' ] ); - my @glob = ( '*a', [ 'PPI::Token::Symbol' => '*a' ] ); - my @bareword = ( 'word', [ 'PPI::Token::Word' => 'word' ] ); + my @scalar = ( '$a', [ 'PPI::Token::Symbol' => '$a' ] ); + my @list = ( '@a', [ 'PPI::Token::Symbol' => '@a' ] ); + my @hash = ( '%a', [ 'PPI::Token::Symbol' => '%a' ] ); + my @glob = ( '*a', [ 'PPI::Token::Symbol' => '*a' ] ); + my @bareword = ( 'word', [ 'PPI::Token::Word' => 'word' ] ); my @hashctor1 = ( '{2}', [ -# 'PPI::Structure::Constructor' => '{2}', - 'PPI::Structure::Block' => '{2}', # should be constructor + # 'PPI::Structure::Constructor' => '{2}', + 'PPI::Structure::Block' => '{2}', # should be constructor 'PPI::Token::Structure' => '{', -# 'PPI::Statement::Expression' => '2', - 'PPI::Statement' => '2', # should be expression - 'PPI::Token::Number' => '2', + # 'PPI::Statement::Expression' => '2', + 'PPI::Statement' => '2', # should be expression + 'PPI::Token::Number' => '2', 'PPI::Token::Structure' => '}', ] ); my @hashctor2 = ( '{x=>2}', [ -# 'PPI::Structure::Constructor' => '{x=>2}', - 'PPI::Structure::Block' => '{x=>2}', # should be constructor + # 'PPI::Structure::Constructor' => '{x=>2}', + 'PPI::Structure::Block' => '{x=>2}', # should be constructor 'PPI::Token::Structure' => '{', -# 'PPI::Statement::Expression' => 'x=>2', - 'PPI::Statement' => 'x=>2', # should be expression - 'PPI::Token::Word' => 'x', - 'PPI::Token::Operator' => '=>', - 'PPI::Token::Number' => '2', + # 'PPI::Statement::Expression' => 'x=>2', + 'PPI::Statement' => 'x=>2', # should be expression + 'PPI::Token::Word' => 'x', + 'PPI::Token::Operator' => '=>', + 'PPI::Token::Number' => '2', 'PPI::Token::Structure' => '}', ] ); my @hashctor3 = ( '{$args}', [ -# 'PPI::Structure::Constructor' => '{$args}', - 'PPI::Structure::Block' => '{$args}', # should be constructor + # 'PPI::Structure::Constructor' => '{$args}', + 'PPI::Structure::Block' => '{$args}', # should be constructor 'PPI::Token::Structure' => '{', -# 'PPI::Statement::Expression' => '$args', - 'PPI::Statement' => '$args', # should be expression - 'PPI::Token::Symbol' => '$args', + # 'PPI::Statement::Expression' => '$args', + 'PPI::Statement' => '$args', # should be expression + 'PPI::Token::Symbol' => '$args', 'PPI::Token::Structure' => '}', ] ); @@ -76,10 +76,10 @@ OPERATOR_CAST: { '[$args]', [ 'PPI::Structure::Constructor' => '[$args]', - 'PPI::Token::Structure' => '[', -# 'PPI::Statement::Expression' => '$args', - 'PPI::Statement' => '$args', # should be expression - 'PPI::Token::Symbol' => '$args', + 'PPI::Token::Structure' => '[', + # 'PPI::Statement::Expression' => '$args', + 'PPI::Statement' => '$args', # should be expression + 'PPI::Token::Symbol' => '$args', 'PPI::Token::Structure' => ']', ] ); @@ -90,323 +90,331 @@ OPERATOR_CAST: { test_varying_whitespace( @number, @asterisk_op, @hashctor1 ); test_varying_whitespace( @number, @asterisk_op, @hashctor2 ); test_varying_whitespace( @number, @asterisk_op, @hashctor3 ); - test_varying_whitespace( @number, @exp_op, @bareword ); - test_varying_whitespace( @number, @exp_op, @hashctor3 ); # doesn't compile, but make sure ** is operator + test_varying_whitespace( @number, @exp_op, @bareword ); + test_varying_whitespace( @number, @exp_op, @hashctor3 ) + ; # doesn't compile, but make sure ** is operator test_varying_whitespace( @number, @asteriskeq_op, @bareword ); - test_varying_whitespace( @number, @asteriskeq_op, @hashctor3 ); # doesn't compile, but make sure it's an operator -{ - local %known_bad_seps = map { $_ => 1 } qw( space ); - test_varying_whitespace( @nothing, @asterisk_cast, @scalar ); -} + test_varying_whitespace( @number, @asteriskeq_op, @hashctor3 ) + ; # doesn't compile, but make sure it's an operator + { + local %known_bad_seps = map { $_ => 1 } qw( space ); + test_varying_whitespace( @nothing, @asterisk_cast, @scalar ); + } - test_varying_whitespace( @number, @percent_op, @scalar ); - test_varying_whitespace( @number, @percent_op, @list ); - test_varying_whitespace( @number, @percent_op, @hash ); - test_varying_whitespace( @number, @percent_op, @glob ); - test_varying_whitespace( @number, @percent_op, @hashctor1 ); - test_varying_whitespace( @number, @percent_op, @hashctor2 ); - test_varying_whitespace( @number, @percent_op, @hashctor3 ); + test_varying_whitespace( @number, @percent_op, @scalar ); + test_varying_whitespace( @number, @percent_op, @list ); + test_varying_whitespace( @number, @percent_op, @hash ); + test_varying_whitespace( @number, @percent_op, @glob ); + test_varying_whitespace( @number, @percent_op, @hashctor1 ); + test_varying_whitespace( @number, @percent_op, @hashctor2 ); + test_varying_whitespace( @number, @percent_op, @hashctor3 ); test_varying_whitespace( @number, @percenteq_op, @bareword ); - test_varying_whitespace( @number, @percenteq_op, @hashctor3 ); # doesn't compile, but make sure it's an operator -{ - local %known_bad_seps = map { $_ => 1 } qw( space ); - test_varying_whitespace( @nothing, @percent_cast, @scalar ); -} + test_varying_whitespace( @number, @percenteq_op, @hashctor3 ) + ; # doesn't compile, but make sure it's an operator + { + local %known_bad_seps = map { $_ => 1 } qw( space ); + test_varying_whitespace( @nothing, @percent_cast, @scalar ); + } test_varying_whitespace( @number, @ampersand_op, @scalar ); test_varying_whitespace( @number, @ampersand_op, @list ); test_varying_whitespace( @number, @ampersand_op, @hash ); - test_varying_whitespace( @number, @ampersand_op, @glob ); - test_varying_whitespace( @number, @ampersand_op, @hashctor1 ); - test_varying_whitespace( @number, @ampersand_op, @hashctor2 ); - test_varying_whitespace( @number, @ampersand_op, @hashctor3 ); + test_varying_whitespace( @number, @ampersand_op, @glob ); + test_varying_whitespace( @number, @ampersand_op, @hashctor1 ); + test_varying_whitespace( @number, @ampersand_op, @hashctor2 ); + test_varying_whitespace( @number, @ampersand_op, @hashctor3 ); test_varying_whitespace( @number, @ampersandeq_op, @bareword ); - test_varying_whitespace( @number, @ampersandeq_op, @hashctor3 ); # doesn't compile, but make sure it's an operator -{ - local %known_bad_seps = map { $_ => 1 } qw( space ); - test_varying_whitespace( @nothing, @ampersand_cast, @scalar ); -} + test_varying_whitespace( @number, @ampersandeq_op, @hashctor3 ) + ; # doesn't compile, but make sure it's an operator + { + local %known_bad_seps = map { $_ => 1 } qw( space ); + test_varying_whitespace( @nothing, @ampersand_cast, @scalar ); + } my @plus = ( '+', [ 'PPI::Token::Operator' => '+', ] ); - my @ex = ( 'x', [ 'PPI::Token::Word' => 'x', ] ); -{ - local %known_bad_seps = map { $_ => 1 } qw( space ); - test_varying_whitespace( @plus, @asterisk_cast, @scalar ); - test_varying_whitespace( @plus, @asterisk_cast, @hashctor3 ); - test_varying_whitespace( @plus, @percent_cast, @scalar ); - test_varying_whitespace( @plus, @percent_cast, @hashctor3 ); - test_varying_whitespace( @plus, @ampersand_cast, @scalar ); - test_varying_whitespace( @plus, @ampersand_cast, @hashctor3 ); - test_varying_whitespace( @ex, @asterisk_cast, @scalar ); - test_varying_whitespace( @ex, @asterisk_cast, @hashctor3 ); - test_varying_whitespace( @ex, @percent_cast, @scalar ); - test_varying_whitespace( @ex, @percent_cast, @hashctor3 ); - test_varying_whitespace( @ex, @ampersand_cast, @scalar ); - test_varying_whitespace( @ex, @ampersand_cast, @hashctor3 ); -} + my @ex = ( 'x', [ 'PPI::Token::Word' => 'x', ] ); + { + local %known_bad_seps = map { $_ => 1 } qw( space ); + test_varying_whitespace( @plus, @asterisk_cast, @scalar ); + test_varying_whitespace( @plus, @asterisk_cast, @hashctor3 ); + test_varying_whitespace( @plus, @percent_cast, @scalar ); + test_varying_whitespace( @plus, @percent_cast, @hashctor3 ); + test_varying_whitespace( @plus, @ampersand_cast, @scalar ); + test_varying_whitespace( @plus, @ampersand_cast, @hashctor3 ); + test_varying_whitespace( @ex, @asterisk_cast, @scalar ); + test_varying_whitespace( @ex, @asterisk_cast, @hashctor3 ); + test_varying_whitespace( @ex, @percent_cast, @scalar ); + test_varying_whitespace( @ex, @percent_cast, @hashctor3 ); + test_varying_whitespace( @ex, @ampersand_cast, @scalar ); + test_varying_whitespace( @ex, @ampersand_cast, @hashctor3 ); + } my @single = ( "'3'", [ 'PPI::Token::Quote::Single' => "'3'", ] ); - test_varying_whitespace( @single, @asterisk_op, @scalar ); - test_varying_whitespace( @single, @asterisk_op, @hashctor3 ); - test_varying_whitespace( @single, @percent_op, @scalar ); - test_varying_whitespace( @single, @percent_op, @hashctor3 ); + test_varying_whitespace( @single, @asterisk_op, @scalar ); + test_varying_whitespace( @single, @asterisk_op, @hashctor3 ); + test_varying_whitespace( @single, @percent_op, @scalar ); + test_varying_whitespace( @single, @percent_op, @hashctor3 ); test_varying_whitespace( @single, @ampersand_op, @scalar ); test_varying_whitespace( @single, @ampersand_op, @hashctor3 ); my @double = ( '"3"', [ 'PPI::Token::Quote::Double' => '"3"', ] ); - test_varying_whitespace( @double, @asterisk_op, @scalar ); - test_varying_whitespace( @double, @asterisk_op, @hashctor3 ); - test_varying_whitespace( @double, @percent_op, @scalar ); - test_varying_whitespace( @double, @percent_op, @hashctor3 ); + test_varying_whitespace( @double, @asterisk_op, @scalar ); + test_varying_whitespace( @double, @asterisk_op, @hashctor3 ); + test_varying_whitespace( @double, @percent_op, @scalar ); + test_varying_whitespace( @double, @percent_op, @hashctor3 ); test_varying_whitespace( @double, @ampersand_op, @scalar ); test_varying_whitespace( @double, @ampersand_op, @hashctor3 ); - test_varying_whitespace( @scalar, @asterisk_op, @scalar ); - test_varying_whitespace( @scalar, @percent_op, @scalar ); + test_varying_whitespace( @scalar, @asterisk_op, @scalar ); + test_varying_whitespace( @scalar, @percent_op, @scalar ); test_varying_whitespace( @scalar, @ampersand_op, @scalar ); my @package = ( 'package foo {}', [ 'PPI::Statement::Package' => 'package foo {}', - 'PPI::Token::Word' => 'package', - 'PPI::Token::Word' => 'foo', - 'PPI::Structure::Block' => '{}', - 'PPI::Token::Structure' => '{', - 'PPI::Token::Structure' => '}', - ] - ); -{ - local %known_bad_seps = map { $_ => 1 } qw( space ); - test_varying_whitespace( @package, @asterisk_cast, @scalar, 1 ); - test_varying_whitespace( @package, @asterisk_cast, @hashctor3, 1 ); - test_varying_whitespace( @package, @percent_cast, @scalar, 1 ); - test_varying_whitespace( @package, @percent_cast, @hashctor3, 1 ); - test_varying_whitespace( @package, @ampersand_cast, @scalar, 1 ); - test_varying_whitespace( @package, @ampersand_cast, @hashctor3, 1 ); -} - test_varying_whitespace( @package, @at_cast, @scalar, 1 ); + 'PPI::Token::Word' => 'package', + 'PPI::Token::Word' => 'foo', + 'PPI::Structure::Block' => '{}', + 'PPI::Token::Structure' => '{', + 'PPI::Token::Structure' => '}', + ] + ); + { + local %known_bad_seps = map { $_ => 1 } qw( space ); + test_varying_whitespace( @package, @asterisk_cast, @scalar, 1 ); + test_varying_whitespace( @package, @asterisk_cast, @hashctor3, 1 ); + test_varying_whitespace( @package, @percent_cast, @scalar, 1 ); + test_varying_whitespace( @package, @percent_cast, @hashctor3, 1 ); + test_varying_whitespace( @package, @ampersand_cast, @scalar, 1 ); + test_varying_whitespace( @package, @ampersand_cast, @hashctor3, 1 ); + } + test_varying_whitespace( @package, @at_cast, @scalar, 1 ); test_varying_whitespace( @package, @at_cast, @listctor, 1 ); my @sub = ( 'sub foo {}', [ - 'PPI::Statement::Sub' => 'sub foo {}', - 'PPI::Token::Word' => 'sub', - 'PPI::Token::Word' => 'foo', + 'PPI::Statement::Sub' => 'sub foo {}', + 'PPI::Token::Word' => 'sub', + 'PPI::Token::Word' => 'foo', 'PPI::Structure::Block' => '{}', 'PPI::Token::Structure' => '{', 'PPI::Token::Structure' => '}', ] ); -{ - local %known_bad_seps = map { $_ => 1 } qw( space ); - test_varying_whitespace( @sub, @asterisk_cast, @scalar, 1 ); - test_varying_whitespace( @sub, @asterisk_cast, @hashctor3, 1 ); - test_varying_whitespace( @sub, @percent_cast, @scalar, 1 ); - test_varying_whitespace( @sub, @percent_cast, @hashctor3, 1 ); - test_varying_whitespace( @sub, @ampersand_cast, @scalar, 1 ); - test_varying_whitespace( @sub, @ampersand_cast, @hashctor3, 1 ); -} - test_varying_whitespace( @sub, @at_cast, @scalar, 1 ); + { + local %known_bad_seps = map { $_ => 1 } qw( space ); + test_varying_whitespace( @sub, @asterisk_cast, @scalar, 1 ); + test_varying_whitespace( @sub, @asterisk_cast, @hashctor3, 1 ); + test_varying_whitespace( @sub, @percent_cast, @scalar, 1 ); + test_varying_whitespace( @sub, @percent_cast, @hashctor3, 1 ); + test_varying_whitespace( @sub, @ampersand_cast, @scalar, 1 ); + test_varying_whitespace( @sub, @ampersand_cast, @hashctor3, 1 ); + } + test_varying_whitespace( @sub, @at_cast, @scalar, 1 ); test_varying_whitespace( @sub, @at_cast, @listctor, 1 ); my @statement = ( '1;', [ - 'PPI::Statement' => '1;', - 'PPI::Token::Number' => '1', + 'PPI::Statement' => '1;', + 'PPI::Token::Number' => '1', 'PPI::Token::Structure' => ';', ] ); -{ - local %known_bad_seps = map { $_ => 1 } qw( space ); - test_varying_whitespace( @statement, @asterisk_cast, @scalar, 1 ); - test_varying_whitespace( @statement, @asterisk_cast, @hashctor3, 1 ); - test_varying_whitespace( @statement, @percent_cast, @scalar, 1 ); - test_varying_whitespace( @statement, @percent_cast, @hashctor3, 1 ); - test_varying_whitespace( @statement, @ampersand_cast, @scalar, 1 ); - test_varying_whitespace( @statement, @ampersand_cast, @hashctor3, 1 ); -} - test_varying_whitespace( @statement, @at_cast, @scalar, 1 ); + { + local %known_bad_seps = map { $_ => 1 } qw( space ); + test_varying_whitespace( @statement, @asterisk_cast, @scalar, 1 ); + test_varying_whitespace( @statement, @asterisk_cast, @hashctor3, 1 ); + test_varying_whitespace( @statement, @percent_cast, @scalar, 1 ); + test_varying_whitespace( @statement, @percent_cast, @hashctor3, 1 ); + test_varying_whitespace( @statement, @ampersand_cast, @scalar, 1 ); + test_varying_whitespace( @statement, @ampersand_cast, @hashctor3, 1 ); + } + test_varying_whitespace( @statement, @at_cast, @scalar, 1 ); test_varying_whitespace( @statement, @at_cast, @listctor, 1 ); my @label = ( 'LABEL:', [ 'PPI::Statement::Compound' => 'LABEL:', - 'PPI::Token::Label' => 'LABEL:', + 'PPI::Token::Label' => 'LABEL:', ] ); -{ - local %known_bad_seps = map { $_ => 1 } qw( space ); - test_varying_whitespace( @label, @asterisk_cast, @scalar, 1 ); - test_varying_whitespace( @label, @asterisk_cast, @hashctor3, 1 ); - test_varying_whitespace( @label, @percent_cast, @scalar, 1 ); - test_varying_whitespace( @label, @percent_cast, @hashctor3, 1 ); - test_varying_whitespace( @label, @ampersand_cast, @scalar, 1 ); - test_varying_whitespace( @label, @ampersand_cast, @hashctor3, 1 ); -} - test_varying_whitespace( @label, @at_cast, @scalar, 1 ); + { + local %known_bad_seps = map { $_ => 1 } qw( space ); + test_varying_whitespace( @label, @asterisk_cast, @scalar, 1 ); + test_varying_whitespace( @label, @asterisk_cast, @hashctor3, 1 ); + test_varying_whitespace( @label, @percent_cast, @scalar, 1 ); + test_varying_whitespace( @label, @percent_cast, @hashctor3, 1 ); + test_varying_whitespace( @label, @ampersand_cast, @scalar, 1 ); + test_varying_whitespace( @label, @ampersand_cast, @hashctor3, 1 ); + } + test_varying_whitespace( @label, @at_cast, @scalar, 1 ); test_varying_whitespace( @label, @at_cast, @listctor, 1 ); my @map = ( 'map {1}', [ - 'PPI::Token::Word' => 'map', + 'PPI::Token::Word' => 'map', 'PPI::Structure::Block' => '{1}', 'PPI::Token::Structure' => '{', - 'PPI::Statement' => '1', - 'PPI::Token::Number' => '1', + 'PPI::Statement' => '1', + 'PPI::Token::Number' => '1', 'PPI::Token::Structure' => '}', ] ); -{ - local %known_bad_seps = map { $_ => 1 } qw( space ); - test_varying_whitespace( @map, @asterisk_cast, @scalar ); - test_varying_whitespace( @map, @asterisk_cast, @hashctor3 ); - test_varying_whitespace( @map, @percent_cast, @scalar ); - test_varying_whitespace( @map, @percent_cast, @hashctor3 ); - test_varying_whitespace( @map, @ampersand_cast, @scalar ); - test_varying_whitespace( @map, @ampersand_cast, @hashctor3 ); -} + { + local %known_bad_seps = map { $_ => 1 } qw( space ); + test_varying_whitespace( @map, @asterisk_cast, @scalar ); + test_varying_whitespace( @map, @asterisk_cast, @hashctor3 ); + test_varying_whitespace( @map, @percent_cast, @scalar ); + test_varying_whitespace( @map, @percent_cast, @hashctor3 ); + test_varying_whitespace( @map, @ampersand_cast, @scalar ); + test_varying_whitespace( @map, @ampersand_cast, @hashctor3 ); + } test_varying_whitespace( @map, @at_cast, @scalar ); test_varying_whitespace( @map, @at_cast, @listctor ); my @evalblock = ( 'eval {2}', [ - 'PPI::Token::Word' => 'eval', + 'PPI::Token::Word' => 'eval', 'PPI::Structure::Block' => '{2}', 'PPI::Token::Structure' => '{', - 'PPI::Statement' => '2', - 'PPI::Token::Number' => '2', + 'PPI::Statement' => '2', + 'PPI::Token::Number' => '2', 'PPI::Token::Structure' => '}', ] ); - test_varying_whitespace( @evalblock, @asterisk_op, @scalar ); - test_varying_whitespace( @double, @asterisk_op, @hashctor3 ); - test_varying_whitespace( @evalblock, @percent_op, @scalar ); - test_varying_whitespace( @evalblock, @percent_op, @hashctor3 ); + test_varying_whitespace( @evalblock, @asterisk_op, @scalar ); + test_varying_whitespace( @double, @asterisk_op, @hashctor3 ); + test_varying_whitespace( @evalblock, @percent_op, @scalar ); + test_varying_whitespace( @evalblock, @percent_op, @hashctor3 ); test_varying_whitespace( @evalblock, @ampersand_op, @scalar ); test_varying_whitespace( @evalblock, @ampersand_op, @hashctor3 ); my @evalstring = ( 'eval "2"', [ - 'PPI::Token::Word' => 'eval', + 'PPI::Token::Word' => 'eval', 'PPI::Token::Quote::Double' => '"2"', ] ); - test_varying_whitespace( @evalstring, @asterisk_op, @scalar ); - test_varying_whitespace( @evalstring, @asterisk_op, @hashctor3 ); - test_varying_whitespace( @evalstring, @percent_op, @scalar ); - test_varying_whitespace( @evalstring, @percent_op, @hashctor3 ); + test_varying_whitespace( @evalstring, @asterisk_op, @scalar ); + test_varying_whitespace( @evalstring, @asterisk_op, @hashctor3 ); + test_varying_whitespace( @evalstring, @percent_op, @scalar ); + test_varying_whitespace( @evalstring, @percent_op, @hashctor3 ); test_varying_whitespace( @evalstring, @ampersand_op, @scalar ); test_varying_whitespace( @evalstring, @ampersand_op, @hashctor3 ); my @curly_subscript1 = ( '$y->{x}', [ - 'PPI::Token::Symbol' => '$y', - 'PPI::Token::Operator' => '->', - 'PPI::Structure::Subscript' => '{x}', - 'PPI::Token::Structure' => '{', + 'PPI::Token::Symbol' => '$y', + 'PPI::Token::Operator' => '->', + 'PPI::Structure::Subscript' => '{x}', + 'PPI::Token::Structure' => '{', 'PPI::Statement::Expression' => 'x', - 'PPI::Token::Word' => 'x', - 'PPI::Token::Structure' => '}', + 'PPI::Token::Word' => 'x', + 'PPI::Token::Structure' => '}', ] ); my @curly_subscript2 = ( '$y->{z}{x}', [ - 'PPI::Token::Symbol' => '$y', - 'PPI::Token::Operator' => '->', - 'PPI::Structure::Subscript' => '{z}', - 'PPI::Token::Structure' => '{', + 'PPI::Token::Symbol' => '$y', + 'PPI::Token::Operator' => '->', + 'PPI::Structure::Subscript' => '{z}', + 'PPI::Token::Structure' => '{', 'PPI::Statement::Expression' => 'z', - 'PPI::Token::Word' => 'z', - 'PPI::Token::Structure' => '}', - 'PPI::Structure::Subscript' => '{x}', - 'PPI::Token::Structure' => '{', + 'PPI::Token::Word' => 'z', + 'PPI::Token::Structure' => '}', + 'PPI::Structure::Subscript' => '{x}', + 'PPI::Token::Structure' => '{', 'PPI::Statement::Expression' => 'x', - 'PPI::Token::Word' => 'x', - 'PPI::Token::Structure' => '}', + 'PPI::Token::Word' => 'x', + 'PPI::Token::Structure' => '}', ] ); my @curly_subscript3 = ( '$y->[z]{x}', [ - 'PPI::Token::Symbol' => '$y', - 'PPI::Token::Operator' => '->', - 'PPI::Structure::Subscript' => '[z]', - 'PPI::Token::Structure' => '[', + 'PPI::Token::Symbol' => '$y', + 'PPI::Token::Operator' => '->', + 'PPI::Structure::Subscript' => '[z]', + 'PPI::Token::Structure' => '[', 'PPI::Statement::Expression' => 'z', - 'PPI::Token::Word' => 'z', - 'PPI::Token::Structure' => ']', - 'PPI::Structure::Subscript' => '{x}', - 'PPI::Token::Structure' => '{', + 'PPI::Token::Word' => 'z', + 'PPI::Token::Structure' => ']', + 'PPI::Structure::Subscript' => '{x}', + 'PPI::Token::Structure' => '{', 'PPI::Statement::Expression' => 'x', - 'PPI::Token::Word' => 'x', - 'PPI::Token::Structure' => '}', + 'PPI::Token::Word' => 'x', + 'PPI::Token::Structure' => '}', ] ); my @square_subscript1 = ( '$y->[x]', [ - 'PPI::Token::Symbol' => '$y', - 'PPI::Token::Operator' => '->', - 'PPI::Structure::Subscript' => '[x]', - 'PPI::Token::Structure' => '[', + 'PPI::Token::Symbol' => '$y', + 'PPI::Token::Operator' => '->', + 'PPI::Structure::Subscript' => '[x]', + 'PPI::Token::Structure' => '[', 'PPI::Statement::Expression' => 'x', - 'PPI::Token::Word' => 'x', - 'PPI::Token::Structure' => ']', - ] - ); - - test_varying_whitespace( @curly_subscript1, @asterisk_op, @scalar ); - test_varying_whitespace( @curly_subscript1, @percent_op, @scalar ); - test_varying_whitespace( @curly_subscript1, @ampersand_op, @scalar ); - test_varying_whitespace( @curly_subscript2, @asterisk_op, @scalar ); - test_varying_whitespace( @curly_subscript2, @percent_op, @scalar ); - test_varying_whitespace( @curly_subscript2, @ampersand_op, @scalar ); - test_varying_whitespace( @curly_subscript3, @asterisk_op, @scalar ); - test_varying_whitespace( @curly_subscript3, @percent_op, @scalar ); - test_varying_whitespace( @curly_subscript3, @ampersand_op, @scalar ); - test_varying_whitespace( @square_subscript1, @asterisk_op, @scalar ); - test_varying_whitespace( @square_subscript1, @percent_op, @scalar ); + 'PPI::Token::Word' => 'x', + 'PPI::Token::Structure' => ']', + ] + ); + + test_varying_whitespace( @curly_subscript1, @asterisk_op, @scalar ); + test_varying_whitespace( @curly_subscript1, @percent_op, @scalar ); + test_varying_whitespace( @curly_subscript1, @ampersand_op, @scalar ); + test_varying_whitespace( @curly_subscript2, @asterisk_op, @scalar ); + test_varying_whitespace( @curly_subscript2, @percent_op, @scalar ); + test_varying_whitespace( @curly_subscript2, @ampersand_op, @scalar ); + test_varying_whitespace( @curly_subscript3, @asterisk_op, @scalar ); + test_varying_whitespace( @curly_subscript3, @percent_op, @scalar ); + test_varying_whitespace( @curly_subscript3, @ampersand_op, @scalar ); + test_varying_whitespace( @square_subscript1, @asterisk_op, @scalar ); + test_varying_whitespace( @square_subscript1, @percent_op, @scalar ); test_varying_whitespace( @square_subscript1, @ampersand_op, @scalar ); -{ - local %known_bad_seps = map { $_ => 1 } qw( space ); - test_varying_whitespace( 'keys', [ 'PPI::Token::Word' => 'keys' ], @percent_cast, @scalar ); - test_varying_whitespace( 'values', [ 'PPI::Token::Word' => 'values' ], @percent_cast, @scalar ); - - test_varying_whitespace( 'keys', [ 'PPI::Token::Word' => 'keys' ], @percent_cast, @hashctor3 ); - test_varying_whitespace( 'values', [ 'PPI::Token::Word' => 'values' ], @percent_cast, @hashctor3 ); -} + { + local %known_bad_seps = map { $_ => 1 } qw( space ); + test_varying_whitespace( 'keys', [ 'PPI::Token::Word' => 'keys' ], + @percent_cast, @scalar ); + test_varying_whitespace( 'values', [ 'PPI::Token::Word' => 'values' ], + @percent_cast, @scalar ); + + test_varying_whitespace( 'keys', [ 'PPI::Token::Word' => 'keys' ], + @percent_cast, @hashctor3 ); + test_varying_whitespace( 'values', [ 'PPI::Token::Word' => 'values' ], + @percent_cast, @hashctor3 ); + } test_statement( - '} *$a', # unbalanced '}' before '*', arbitrary decision + '} *$a', # unbalanced '}' before '*', arbitrary decision [ 'PPI::Statement::UnmatchedBrace' => '}', - 'PPI::Token::Structure' => '}', - 'PPI::Statement' => '*$a', - 'PPI::Token::Operator' => '*', - 'PPI::Token::Symbol' => '$a', + 'PPI::Token::Structure' => '}', + 'PPI::Statement' => '*$a', + 'PPI::Token::Operator' => '*', + 'PPI::Token::Symbol' => '$a', ] ); test_statement( - '$bar = \%*$foo', # multiple consecutive casts + '$bar = \%*$foo', # multiple consecutive casts [ - 'PPI::Token::Symbol' => '$bar', + 'PPI::Token::Symbol' => '$bar', 'PPI::Token::Operator' => '=', - 'PPI::Token::Cast' => '\\', - 'PPI::Token::Cast' => '%', - 'PPI::Token::Cast' => '*', - 'PPI::Token::Symbol' => '$foo', + 'PPI::Token::Cast' => '\\', + 'PPI::Token::Cast' => '%', + 'PPI::Token::Cast' => '*', + 'PPI::Token::Symbol' => '$foo', ] ); @@ -414,54 +422,54 @@ OPERATOR_CAST: { '$#tmp*$#tmp2', [ 'PPI::Token::ArrayIndex' => '$#tmp', - 'PPI::Token::Operator' => '*', + 'PPI::Token::Operator' => '*', 'PPI::Token::ArrayIndex' => '$#tmp2', ] ); test_statement( - '[ %{$req->parameters} ]', # preceded by '[' + '[ %{$req->parameters} ]', # preceded by '[' [ 'PPI::Structure::Constructor' => '[ %{$req->parameters} ]', - 'PPI::Token::Structure' => '[', - 'PPI::Statement' => '%{$req->parameters}', - 'PPI::Token::Cast' => '%', - 'PPI::Structure::Block' => '{$req->parameters}', - 'PPI::Token::Structure' => '{', - 'PPI::Statement' => '$req->parameters', - 'PPI::Token::Symbol' => '$req', - 'PPI::Token::Operator' => '->', - 'PPI::Token::Word' => 'parameters', - 'PPI::Token::Structure' => '}', - 'PPI::Token::Structure' => ']', + 'PPI::Token::Structure' => '[', + 'PPI::Statement' => '%{$req->parameters}', + 'PPI::Token::Cast' => '%', + 'PPI::Structure::Block' => '{$req->parameters}', + 'PPI::Token::Structure' => '{', + 'PPI::Statement' => '$req->parameters', + 'PPI::Token::Symbol' => '$req', + 'PPI::Token::Operator' => '->', + 'PPI::Token::Word' => 'parameters', + 'PPI::Token::Structure' => '}', + 'PPI::Token::Structure' => ']', ] ); test_statement( - '( %{$req->parameters} )', # preceded by '(' + '( %{$req->parameters} )', # preceded by '(' [ - 'PPI::Structure::List' => '( %{$req->parameters} )', - 'PPI::Token::Structure' => '(', + 'PPI::Structure::List' => '( %{$req->parameters} )', + 'PPI::Token::Structure' => '(', 'PPI::Statement::Expression' => '%{$req->parameters}', - 'PPI::Token::Cast' => '%', - 'PPI::Structure::Block' => '{$req->parameters}', - 'PPI::Token::Structure' => '{', - 'PPI::Statement' => '$req->parameters', - 'PPI::Token::Symbol' => '$req', - 'PPI::Token::Operator' => '->', - 'PPI::Token::Word' => 'parameters', - 'PPI::Token::Structure' => '}', - 'PPI::Token::Structure' => ')', + 'PPI::Token::Cast' => '%', + 'PPI::Structure::Block' => '{$req->parameters}', + 'PPI::Token::Structure' => '{', + 'PPI::Statement' => '$req->parameters', + 'PPI::Token::Symbol' => '$req', + 'PPI::Token::Operator' => '->', + 'PPI::Token::Word' => 'parameters', + 'PPI::Token::Structure' => '}', + 'PPI::Token::Structure' => ')', ] ); test_statement( - '++$i%$f', # '%' wrongly a cast through 1.220. + '++$i%$f', # '%' wrongly a cast through 1.220. [ - 'PPI::Statement' => '++$i%$f', + 'PPI::Statement' => '++$i%$f', 'PPI::Token::Operator' => '++', - 'PPI::Token::Symbol' => '$i', + 'PPI::Token::Symbol' => '$i', 'PPI::Token::Operator' => '%', - 'PPI::Token::Symbol' => '$f', + 'PPI::Token::Symbol' => '$f', ] ); @@ -470,174 +478,174 @@ OPERATOR_CAST: { test_statement( '$foo->$*', [ - 'PPI::Statement' => '$foo->$*', - 'PPI::Token::Symbol' => '$foo', + 'PPI::Statement' => '$foo->$*', + 'PPI::Token::Symbol' => '$foo', 'PPI::Token::Operator' => '->', - 'PPI::Token::Cast' => '$*', + 'PPI::Token::Cast' => '$*', ] ); test_statement( '$foo->@*', [ - 'PPI::Statement' => '$foo->@*', - 'PPI::Token::Symbol' => '$foo', + 'PPI::Statement' => '$foo->@*', + 'PPI::Token::Symbol' => '$foo', 'PPI::Token::Operator' => '->', - 'PPI::Token::Cast' => '@*', + 'PPI::Token::Cast' => '@*', ] ); test_statement( '$foo->$#*', [ - 'PPI::Statement' => '$foo->$#*', - 'PPI::Token::Symbol' => '$foo', + 'PPI::Statement' => '$foo->$#*', + 'PPI::Token::Symbol' => '$foo', 'PPI::Token::Operator' => '->', - 'PPI::Token::Cast' => '$#*', + 'PPI::Token::Cast' => '$#*', ] ); test_statement( '$foo->%*', [ - 'PPI::Statement' => '$foo->%*', - 'PPI::Token::Symbol' => '$foo', + 'PPI::Statement' => '$foo->%*', + 'PPI::Token::Symbol' => '$foo', 'PPI::Token::Operator' => '->', - 'PPI::Token::Cast' => '%*', + 'PPI::Token::Cast' => '%*', ] ); test_statement( '$foo->&*', [ - 'PPI::Statement' => '$foo->&*', - 'PPI::Token::Symbol' => '$foo', + 'PPI::Statement' => '$foo->&*', + 'PPI::Token::Symbol' => '$foo', 'PPI::Token::Operator' => '->', - 'PPI::Token::Cast' => '&*', + 'PPI::Token::Cast' => '&*', ] ); test_statement( '$foo->**', [ - 'PPI::Statement' => '$foo->**', - 'PPI::Token::Symbol' => '$foo', + 'PPI::Statement' => '$foo->**', + 'PPI::Token::Symbol' => '$foo', 'PPI::Token::Operator' => '->', - 'PPI::Token::Cast' => '**', + 'PPI::Token::Cast' => '**', ] ); test_statement( '$foo->@[0]', [ - 'PPI::Statement' => '$foo->@[0]', - 'PPI::Token::Symbol' => '$foo', - 'PPI::Token::Operator' => '->', - 'PPI::Token::Cast' => '@', - 'PPI::Structure::Subscript' => '[0]', - 'PPI::Token::Structure' => '[', + 'PPI::Statement' => '$foo->@[0]', + 'PPI::Token::Symbol' => '$foo', + 'PPI::Token::Operator' => '->', + 'PPI::Token::Cast' => '@', + 'PPI::Structure::Subscript' => '[0]', + 'PPI::Token::Structure' => '[', 'PPI::Statement::Expression' => '0', - 'PPI::Token::Number' => '0', - 'PPI::Token::Structure' => ']', + 'PPI::Token::Number' => '0', + 'PPI::Token::Structure' => ']', ] ); test_statement( '$foo->@{0}', [ - 'PPI::Statement' => '$foo->@{0}', - 'PPI::Token::Symbol' => '$foo', - 'PPI::Token::Operator' => '->', - 'PPI::Token::Cast' => '@', - 'PPI::Structure::Subscript' => '{0}', - 'PPI::Token::Structure' => '{', + 'PPI::Statement' => '$foo->@{0}', + 'PPI::Token::Symbol' => '$foo', + 'PPI::Token::Operator' => '->', + 'PPI::Token::Cast' => '@', + 'PPI::Structure::Subscript' => '{0}', + 'PPI::Token::Structure' => '{', 'PPI::Statement::Expression' => '0', - 'PPI::Token::Number' => '0', - 'PPI::Token::Structure' => '}', + 'PPI::Token::Number' => '0', + 'PPI::Token::Structure' => '}', ] ); test_statement( '$foo->%["bar"]', [ - 'PPI::Statement' => '$foo->%["bar"]', - 'PPI::Token::Symbol' => '$foo', - 'PPI::Token::Operator' => '->', - 'PPI::Token::Cast' => '%', - 'PPI::Structure::Subscript' => '["bar"]', - 'PPI::Token::Structure' => '[', + 'PPI::Statement' => '$foo->%["bar"]', + 'PPI::Token::Symbol' => '$foo', + 'PPI::Token::Operator' => '->', + 'PPI::Token::Cast' => '%', + 'PPI::Structure::Subscript' => '["bar"]', + 'PPI::Token::Structure' => '[', 'PPI::Statement::Expression' => '"bar"', - 'PPI::Token::Quote::Double' => '"bar"', - 'PPI::Token::Structure' => ']', + 'PPI::Token::Quote::Double' => '"bar"', + 'PPI::Token::Structure' => ']', ] ); test_statement( '$foo->%{bar}', [ - 'PPI::Statement' => '$foo->%{bar}', - 'PPI::Token::Symbol' => '$foo', - 'PPI::Token::Operator' => '->', - 'PPI::Token::Cast' => '%', - 'PPI::Structure::Subscript' => '{bar}', - 'PPI::Token::Structure' => '{', + 'PPI::Statement' => '$foo->%{bar}', + 'PPI::Token::Symbol' => '$foo', + 'PPI::Token::Operator' => '->', + 'PPI::Token::Cast' => '%', + 'PPI::Structure::Subscript' => '{bar}', + 'PPI::Token::Structure' => '{', 'PPI::Statement::Expression' => 'bar', - 'PPI::Token::Word' => 'bar', - 'PPI::Token::Structure' => '}', + 'PPI::Token::Word' => 'bar', + 'PPI::Token::Structure' => '}', ] ); test_statement( '$foo->*{CODE}', [ - 'PPI::Statement' => '$foo->*{CODE}', - 'PPI::Token::Symbol' => '$foo', - 'PPI::Token::Operator' => '->', - 'PPI::Token::Cast' => '*', - 'PPI::Structure::Subscript' => '{CODE}', - 'PPI::Token::Structure' => '{', + 'PPI::Statement' => '$foo->*{CODE}', + 'PPI::Token::Symbol' => '$foo', + 'PPI::Token::Operator' => '->', + 'PPI::Token::Cast' => '*', + 'PPI::Structure::Subscript' => '{CODE}', + 'PPI::Token::Structure' => '{', 'PPI::Statement::Expression' => 'CODE', - 'PPI::Token::Word' => 'CODE', - 'PPI::Token::Structure' => '}', - ] - ); - -{ # these need to be fixed in PPI::Lexer->_statement, fixing these will break other tests that need to be changed - local $TODO = "clarify type of statement in constructor"; - test_statement( - '[$args]', - [ - 'PPI::Structure::Constructor' => '[$args]', - 'PPI::Token::Structure' => '[', - 'PPI::Statement::Expression' => '$args', - 'PPI::Token::Symbol' => '$args', - 'PPI::Token::Structure' => ']', - ] - ); - test_statement( - '{$args}', - [ - 'PPI::Structure::Constructor' => '{$args}', - 'PPI::Token::Structure' => '{', - 'PPI::Statement::Expression' => '$args', - 'PPI::Token::Symbol' => '$args', - 'PPI::Token::Structure' => '}', - ] - ); - local $TODO = "hash constructors are currently mistaken for blocks"; - test_statement( - '1 * {2}', - [ - 'PPI::Token::Number' => '1' , - 'PPI::Token::Operator' => '*', - 'PPI::Structure::Constructor' => '{2}', - 'PPI::Token::Structure' => '{', - 'PPI::Statement' => '2', - 'PPI::Token::Number' => '2', - 'PPI::Token::Structure' => '}', - ] - ) -} + 'PPI::Token::Word' => 'CODE', + 'PPI::Token::Structure' => '}', + ] + ); + + { # these need to be fixed in PPI::Lexer->_statement, fixing these will break other tests that need to be changed + local $TODO = "clarify type of statement in constructor"; + test_statement( + '[$args]', + [ + 'PPI::Structure::Constructor' => '[$args]', + 'PPI::Token::Structure' => '[', + 'PPI::Statement::Expression' => '$args', + 'PPI::Token::Symbol' => '$args', + 'PPI::Token::Structure' => ']', + ] + ); + test_statement( + '{$args}', + [ + 'PPI::Structure::Constructor' => '{$args}', + 'PPI::Token::Structure' => '{', + 'PPI::Statement::Expression' => '$args', + 'PPI::Token::Symbol' => '$args', + 'PPI::Token::Structure' => '}', + ] + ); + local $TODO = "hash constructors are currently mistaken for blocks"; + test_statement( + '1 * {2}', + [ + 'PPI::Token::Number' => '1', + 'PPI::Token::Operator' => '*', + 'PPI::Structure::Constructor' => '{2}', + 'PPI::Token::Structure' => '{', + 'PPI::Statement' => '2', + 'PPI::Token::Number' => '2', + 'PPI::Token::Structure' => '}', + ] + ) + } } sub one_line_explain { @@ -651,25 +659,25 @@ sub main_level_line { return "" if not $TODO; my @outer_final; my $level = 0; - while ( my @outer = caller($level++) ) { + while ( my @outer = caller( $level++ ) ) { @outer_final = @outer; } return "l $outer_final[2] - "; } sub test_statement { - local $Test::Builder::Level = $Test::Builder::Level+1; + local $Test::Builder::Level = $Test::Builder::Level + 1; my ( $code, $expected, $msg ) = @_; $msg = perlstring $code if !defined $msg; - my $d = safe_new \$code; + my $d = safe_new \$code; my $tokens = $d->find( sub { $_[1]->significant } ); $tokens = [ map { ref($_), $_->content } @$tokens ]; if ( $expected->[0] !~ /^PPI::Statement/ ) { $expected = [ 'PPI::Statement', $code, @$expected ]; } - my $ok = is_deeply( $tokens, $expected, main_level_line.$msg ); + my $ok = is_deeply( $tokens, $expected, main_level_line . $msg ); if ( !$ok ) { diag ">>> $code -- $msg\n"; diag one_line_explain $tokens; @@ -680,42 +688,60 @@ sub test_statement { } sub test_varying_whitespace { - local $Test::Builder::Level = $Test::Builder::Level+1; - my( $left, $left_expected, $cast_or_op, $cast_or_op_expected, $right, $right_expected, $right_is_statement ) = @_; - -{ - local $TODO = "known bug" if $known_bad_seps{null}; - assemble_and_test( "", $left, $left_expected, $cast_or_op, $cast_or_op_expected, $right, $right_expected, $right_is_statement ); -} -{ - local $TODO = "known bug" if $known_bad_seps{space}; - assemble_and_test( " ", $left, $left_expected, $cast_or_op, $cast_or_op_expected, $right, $right_expected, $right_is_statement ); - assemble_and_test( "\t", $left, $left_expected, $cast_or_op, $cast_or_op_expected, $right, $right_expected, $right_is_statement ); - assemble_and_test( "\n", $left, $left_expected, $cast_or_op, $cast_or_op_expected, $right, $right_expected, $right_is_statement ); - assemble_and_test( "\f", $left, $left_expected, $cast_or_op, $cast_or_op_expected, $right, $right_expected, $right_is_statement ); -} + local $Test::Builder::Level = $Test::Builder::Level + 1; + my ( $left, $left_expected, $cast_or_op, $cast_or_op_expected, $right, + $right_expected, $right_is_statement ) + = @_; + + { + local $TODO = "known bug" if $known_bad_seps{null}; + assemble_and_test( "", $left, $left_expected, $cast_or_op, + $cast_or_op_expected, $right, $right_expected, + $right_is_statement ); + } + { + local $TODO = "known bug" if $known_bad_seps{space}; + assemble_and_test( " ", $left, $left_expected, $cast_or_op, + $cast_or_op_expected, $right, $right_expected, + $right_is_statement ); + assemble_and_test( "\t", $left, $left_expected, $cast_or_op, + $cast_or_op_expected, $right, $right_expected, + $right_is_statement ); + assemble_and_test( "\n", $left, $left_expected, $cast_or_op, + $cast_or_op_expected, $right, $right_expected, + $right_is_statement ); + assemble_and_test( "\f", $left, $left_expected, $cast_or_op, + $cast_or_op_expected, $right, $right_expected, + $right_is_statement ); + } local $TODO = "\\r is being nuked to \\n, need to fix that first"; - assemble_and_test( "\r", $left, $left_expected, $cast_or_op, $cast_or_op_expected, $right, $right_expected, $right_is_statement ); # fix this -- different breakage from \n, \t, etc. + assemble_and_test( "\r", $left, $left_expected, $cast_or_op, + $cast_or_op_expected, $right, $right_expected, $right_is_statement ) + ; # fix this -- different breakage from \n, \t, etc. return; } - sub assemble_and_test { - local $Test::Builder::Level = $Test::Builder::Level+1; - my( $whitespace, $left, $left_expected, $cast_or_op, $cast_or_op_expected, $right, $right_expected, $right_is_statement ) = @_; - - my $code = $left eq '' ? "$cast_or_op$whitespace$right" : "$left$whitespace$cast_or_op$whitespace$right"; - - if ( $right_is_statement ) { - $cast_or_op_expected = [ 'PPI::Statement' => "$cast_or_op$whitespace$right", @$cast_or_op_expected ]; + local $Test::Builder::Level = $Test::Builder::Level + 1; + my ( $whitespace, $left, $left_expected, $cast_or_op, $cast_or_op_expected, + $right, $right_expected, $right_is_statement ) + = @_; + + my $code = + $left eq '' + ? "$cast_or_op$whitespace$right" + : "$left$whitespace$cast_or_op$whitespace$right"; + + if ($right_is_statement) { + $cast_or_op_expected = [ + 'PPI::Statement' => "$cast_or_op$whitespace$right", + @$cast_or_op_expected + ]; } - my $expected = [ - @$left_expected, - @$cast_or_op_expected, - @$right_expected, - ]; + my $expected = + [ @$left_expected, @$cast_or_op_expected, @$right_expected, ]; test_statement( $code, $expected ); return; diff --git a/t/ppi_token_whitespace.t b/t/ppi_token_whitespace.t index c56b198b..c3c498eb 100644 --- a/t/ppi_token_whitespace.t +++ b/t/ppi_token_whitespace.t @@ -8,11 +8,11 @@ use PPI::Token::Whitespace (); use Test::More tests => 6 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); TIDY: { - my $ws1 = PPI::Token::Whitespace::->new( " " ); + my $ws1 = PPI::Token::Whitespace::->new(" "); is $ws1->length, "3"; ok $ws1->tidy; is $ws1->length, "3"; - my $ws2 = PPI::Token::Whitespace::->new( " \n" ); + my $ws2 = PPI::Token::Whitespace::->new(" \n"); is $ws2->length, "4"; ok $ws2->tidy; is $ws2->length, "0"; diff --git a/t/ppi_token_word.t b/t/ppi_token_word.t index c396ffb9..dacac74a 100644 --- a/t/ppi_token_word.t +++ b/t/ppi_token_word.t @@ -7,26 +7,21 @@ use PPI::Test::pragmas; use Helper qw( check_with ); use PPI (); -use Test::More tests => 2017 + ($ENV{AUTHOR_TESTING} ? 1 : 0); +use Test::More tests => 2017 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use Helper 'safe_new'; LITERAL: { - my @pairs = ( - "F", 'F', - "Foo::Bar", 'Foo::Bar', - "Foo'Bar", 'Foo::Bar', - ); - while ( @pairs ) { - my $from = shift @pairs; - my $to = shift @pairs; - my $doc = safe_new \"$from;"; + my @pairs = ( "F", 'F', "Foo::Bar", 'Foo::Bar', "Foo'Bar", 'Foo::Bar', ); + while (@pairs) { + my $from = shift @pairs; + my $to = shift @pairs; + my $doc = safe_new \"$from;"; my $word = $doc->find_first('Token::Word'); isa_ok( $word, 'PPI::Token::Word' ); is( $word->literal, $to, "The source $from becomes $to ok" ); } } - METHOD_CALL: { my $Document = safe_new \<<'END_PERL'; indirect $foo; @@ -48,83 +43,65 @@ END_PERL my %words = map { $_ => $_ } @{$words}; is( scalar $words{indirect}->method_call, - undef, - 'Indirect notation is unknown.', + undef, 'Indirect notation is unknown.', ); is( scalar $words{indirect_class_with_colon}->method_call, - 1, - 'Indirect notation with following word ending with colons is true.', + 1, 'Indirect notation with following word ending with colons is true.', ); is( scalar $words{method_with_parentheses}->method_call, - 1, - 'Method with parentheses is true.', + 1, 'Method with parentheses is true.', ); is( scalar $words{method_without_parentheses}->method_call, - 1, - 'Method without parentheses is true.', - ); - is( - scalar $words{print}->method_call, - undef, - 'Plain print is unknown.', + 1, 'Method without parentheses is true.', ); + is( scalar $words{print}->method_call, undef, 'Plain print is unknown.', ); is( scalar $words{SomeClass}->method_call, - undef, - 'Class in class method call is unknown.', - ); - is( - scalar $words{sub_call}->method_call, - 0, - 'Subroutine call is false.', + undef, 'Class in class method call is unknown.', ); + is( scalar $words{sub_call}->method_call, 0, 'Subroutine call is false.', ); is( scalar $words{chained_from}->method_call, - 1, - 'Method that is chained from is true.', + 1, 'Method that is chained from is true.', ); is( scalar $words{chained_to}->method_call, - 1, - 'Method that is chained to is true.', + 1, 'Method that is chained to is true.', ); is( scalar $words{a_first_thing}->method_call, - undef, - 'First bareword is unknown.', + undef, 'First bareword is unknown.', ); is( scalar $words{a_middle_thing}->method_call, - undef, - 'Bareword in the middle is unknown.', + undef, 'Bareword in the middle is unknown.', ); is( scalar $words{a_last_thing}->method_call, - 0, - 'Bareword at the end is false.', + 0, 'Bareword at the end is false.', ); + foreach my $false_word ( qw< - first_list_element second_list_element third_list_element - first_comma_separated_word second_comma_separated_word third_comma_separated_word - single_bareword_statement - bareword_no_semicolon_end_of_block - hash_key - fat_comma_left_side + first_list_element second_list_element third_list_element + first_comma_separated_word second_comma_separated_word third_comma_separated_word + single_bareword_statement + bareword_no_semicolon_end_of_block + hash_key + fat_comma_left_side > - ) { + ) + { is( scalar $words{$false_word}->method_call, - 0, - "$false_word is false.", + 0, "$false_word is false.", ); } } - __TOKENIZER__ON_CHAR: { # PPI::Statement::Operator for my $test ( @@ -140,16 +117,19 @@ __TOKENIZER__ON_CHAR: { [ q{$foo or'bar';}, 'or' ], [ q{$foo x'bar';}, 'x' ], [ q{$foo xor'bar';}, 'xor' ], - ) { + ) + { my ( $code, $expected ) = @$test; - my ( $Document, $statement ) = _parse_to_statement( $code, 'PPI::Statement' ); + my ( $Document, $statement ) = + _parse_to_statement( $code, 'PPI::Statement' ); is( $statement, $code, "$code: statement text matches" ); - _compare_child( $statement, 2, 'PPI::Token::Operator', $expected, $code ); - _compare_child( $statement, 3, 'PPI::Token::Quote::Single', "'bar'", $code ); + _compare_child( $statement, 2, 'PPI::Token::Operator', $expected, + $code ); + _compare_child( $statement, 3, 'PPI::Token::Quote::Single', "'bar'", + $code ); _compare_child( $statement, 4, 'PPI::Token::Structure', ';', $code ); } - # PPI::Token::Quote::* for my $test ( [ q{q'foo';}, q{q'foo'}, 'PPI::Token::Quote::Literal' ], @@ -157,345 +137,360 @@ __TOKENIZER__ON_CHAR: { [ q{qr'foo';}, q{qr'foo'}, 'PPI::Token::QuoteLike::Regexp' ], [ q{qw'foo';}, q{qw'foo'}, 'PPI::Token::QuoteLike::Words' ], [ q{qx'foo';}, q{qx'foo'}, 'PPI::Token::QuoteLike::Command' ], - ) { + ) + { my ( $code, $expected, $type ) = @$test; - my ( $Document, $statement ) = _parse_to_statement( $code, 'PPI::Statement' ); + my ( $Document, $statement ) = + _parse_to_statement( $code, 'PPI::Statement' ); is( $statement, $code, "$code: statement text matches" ); - _compare_child( $statement, 0, $type, $expected, $code ); + _compare_child( $statement, 0, $type, $expected, $code ); _compare_child( $statement, 1, 'PPI::Token::Structure', ';', $code ); } - # PPI::Token::Regexp::* for my $test ( [ q{m'foo';}, q{m'foo'}, 'PPI::Token::Regexp::Match' ], [ q{s'foo'bar';}, q{s'foo'bar'}, 'PPI::Token::Regexp::Substitute' ], [ q{tr'fo'ba';}, q{tr'fo'ba'}, 'PPI::Token::Regexp::Transliterate' ], [ q{y'fo'ba';}, q{y'fo'ba'}, 'PPI::Token::Regexp::Transliterate' ], - ) { + ) + { my ( $code, $expected, $type ) = @$test; - my ( $Document, $statement ) = _parse_to_statement( $code, 'PPI::Statement' ); + my ( $Document, $statement ) = + _parse_to_statement( $code, 'PPI::Statement' ); is( $statement, $code, "$code: statement text matches" ); - _compare_child( $statement, 0, $type, $expected, $code ); + _compare_child( $statement, 0, $type, $expected, $code ); _compare_child( $statement, 1, 'PPI::Token::Structure', ';', $code ); } - # PPI::Token::Word for my $test ( - [ q{abs'3';}, 'abs' ], - [ q{accept'1234',2345;}, 'accept' ], - [ q{alarm'5';}, 'alarm' ], - [ q{atan2'5';}, 'atan2' ], - [ q{bind'5',"";}, 'bind' ], - [ q{binmode'5';}, 'binmode' ], - [ q{bless'foo', 'bar';}, 'bless' ], - [ q{break'foo' when 1;}, 'break' ], - [ q{caller'3';}, 'caller' ], - [ q{chdir'foo';}, 'chdir' ], - [ q{chmod'0777', 'foo';}, 'chmod' ], - [ q{chomp'a';}, 'chomp' ], - [ q{chop'a';}, 'chop' ], - [ q{chown'a';}, 'chown' ], - [ q{chr'32';}, 'chr' ], - [ q{chroot'a';}, 'chroot' ], - [ q{close'1';}, 'close' ], - [ q{closedir'1';}, 'closedir' ], - [ q{connect'1234',$foo;}, 'connect' ], - [ q{continue'a';}, 'continue' ], - [ q{cos'3';}, 'cos' ], - [ q{crypt'foo', 'bar';}, 'crypt' ], - [ q{dbmclose'foo';}, 'dbmclose' ], - [ q{dbmopen'foo','bar';}, 'dbmopen' ], - [ q{default'a' {}}, 'default' ], - [ q{defined'foo';}, 'defined' ], - [ q{delete'foo';}, 'delete' ], - [ q{die'foo';}, 'die' ], - [ q{do'foo';}, 'do' ], - [ q{dump'foo';}, 'dump' ], - [ q{each'foo';}, 'each' ], - [ q{else'foo' {};}, 'else' ], - [ q{elsif'foo' {};}, 'elsif' ], - [ q{endgrent'foo';}, 'endgrent' ], - [ q{endhostent'foo';}, 'endhostent' ], - [ q{endnetent'foo';}, 'endnetent' ], - [ q{endprotoent'foo';}, 'endprotoent' ], - [ q{endpwent'foo';}, 'endpwent' ], - [ q{endservent'foo';}, 'endservent' ], - [ q{eof'foo';}, 'eof' ], - [ q{eval'foo';}, 'eval' ], - [ q{evalbytes'foo';}, 'evalbytes' ], - [ q{exec'foo';}, 'exec' ], - [ q{exists'foo';}, 'exists' ], - [ q{exit'foo';}, 'exit' ], - [ q{exp'foo';}, 'exp' ], - [ q{fc'foo';}, 'fc' ], - [ q{fcntl'1';}, 'fcntl' ], - [ q{fileno'1';}, 'fileno' ], - [ q{flock'1', LOCK_EX;}, 'flock' ], - [ q{fork'';}, 'fork' ], - [ qq{format''=\n.}, 'format' ], - [ q{formline'@',1;}, 'formline' ], - [ q{getc'1';}, 'getc' ], - [ q{getgrent'foo';}, 'getgrent' ], - [ q{getgrgid'1';}, 'getgrgid' ], - [ q{getgrnam'foo';}, 'getgrnam' ], - [ q{gethostbyaddr'1', AF_INET;}, 'gethostbyaddr' ], - [ q{gethostbyname'foo';}, 'gethostbyname' ], - [ q{gethostent'foo';}, 'gethostent' ], - [ q{getlogin'foo';}, 'getlogin' ], - [ q{getnetbyaddr'1', AF_INET;}, 'getnetbyaddr' ], - [ q{getnetbyname'foo';}, 'getnetbyname' ], - [ q{getnetent'foo';}, 'getnetent' ], - [ q{getpeername'foo';}, 'getpeername' ], - [ q{getpgrp'1';}, 'getpgrp' ], - [ q{getppid'1';}, 'getppid' ], - [ q{getpriority'1',2;}, 'getpriority' ], - [ q{getprotobyname'tcp';}, 'getprotobyname' ], - [ q{getprotobynumber'6';}, 'getprotobynumber' ], - [ q{getprotoent'foo';}, 'getprotoent' ], - [ q{getpwent'foo';}, 'getpwent' ], - [ q{getpwnam'foo';}, 'getpwnam' ], - [ q{getpwuid'1';}, 'getpwuid' ], - [ q{getservbyname'foo', 'bar';}, 'getservbyname' ], - [ q{getservbyport'23', 'tcp';}, 'getservbyport' ], - [ q{getservent'foo';}, 'getservent' ], - [ q{getsockname'foo';}, 'getsockname' ], + [ q{abs'3';}, 'abs' ], + [ q{accept'1234',2345;}, 'accept' ], + [ q{alarm'5';}, 'alarm' ], + [ q{atan2'5';}, 'atan2' ], + [ q{bind'5',"";}, 'bind' ], + [ q{binmode'5';}, 'binmode' ], + [ q{bless'foo', 'bar';}, 'bless' ], + [ q{break'foo' when 1;}, 'break' ], + [ q{caller'3';}, 'caller' ], + [ q{chdir'foo';}, 'chdir' ], + [ q{chmod'0777', 'foo';}, 'chmod' ], + [ q{chomp'a';}, 'chomp' ], + [ q{chop'a';}, 'chop' ], + [ q{chown'a';}, 'chown' ], + [ q{chr'32';}, 'chr' ], + [ q{chroot'a';}, 'chroot' ], + [ q{close'1';}, 'close' ], + [ q{closedir'1';}, 'closedir' ], + [ q{connect'1234',$foo;}, 'connect' ], + [ q{continue'a';}, 'continue' ], + [ q{cos'3';}, 'cos' ], + [ q{crypt'foo', 'bar';}, 'crypt' ], + [ q{dbmclose'foo';}, 'dbmclose' ], + [ q{dbmopen'foo','bar';}, 'dbmopen' ], + [ q{default'a' {}}, 'default' ], + [ q{defined'foo';}, 'defined' ], + [ q{delete'foo';}, 'delete' ], + [ q{die'foo';}, 'die' ], + [ q{do'foo';}, 'do' ], + [ q{dump'foo';}, 'dump' ], + [ q{each'foo';}, 'each' ], + [ q{else'foo' {};}, 'else' ], + [ q{elsif'foo' {};}, 'elsif' ], + [ q{endgrent'foo';}, 'endgrent' ], + [ q{endhostent'foo';}, 'endhostent' ], + [ q{endnetent'foo';}, 'endnetent' ], + [ q{endprotoent'foo';}, 'endprotoent' ], + [ q{endpwent'foo';}, 'endpwent' ], + [ q{endservent'foo';}, 'endservent' ], + [ q{eof'foo';}, 'eof' ], + [ q{eval'foo';}, 'eval' ], + [ q{evalbytes'foo';}, 'evalbytes' ], + [ q{exec'foo';}, 'exec' ], + [ q{exists'foo';}, 'exists' ], + [ q{exit'foo';}, 'exit' ], + [ q{exp'foo';}, 'exp' ], + [ q{fc'foo';}, 'fc' ], + [ q{fcntl'1';}, 'fcntl' ], + [ q{fileno'1';}, 'fileno' ], + [ q{flock'1', LOCK_EX;}, 'flock' ], + [ q{fork'';}, 'fork' ], + [ qq{format''=\n.}, 'format' ], + [ q{formline'@',1;}, 'formline' ], + [ q{getc'1';}, 'getc' ], + [ q{getgrent'foo';}, 'getgrent' ], + [ q{getgrgid'1';}, 'getgrgid' ], + [ q{getgrnam'foo';}, 'getgrnam' ], + [ q{gethostbyaddr'1', AF_INET;}, 'gethostbyaddr' ], + [ q{gethostbyname'foo';}, 'gethostbyname' ], + [ q{gethostent'foo';}, 'gethostent' ], + [ q{getlogin'foo';}, 'getlogin' ], + [ q{getnetbyaddr'1', AF_INET;}, 'getnetbyaddr' ], + [ q{getnetbyname'foo';}, 'getnetbyname' ], + [ q{getnetent'foo';}, 'getnetent' ], + [ q{getpeername'foo';}, 'getpeername' ], + [ q{getpgrp'1';}, 'getpgrp' ], + [ q{getppid'1';}, 'getppid' ], + [ q{getpriority'1',2;}, 'getpriority' ], + [ q{getprotobyname'tcp';}, 'getprotobyname' ], + [ q{getprotobynumber'6';}, 'getprotobynumber' ], + [ q{getprotoent'foo';}, 'getprotoent' ], + [ q{getpwent'foo';}, 'getpwent' ], + [ q{getpwnam'foo';}, 'getpwnam' ], + [ q{getpwuid'1';}, 'getpwuid' ], + [ q{getservbyname'foo', 'bar';}, 'getservbyname' ], + [ q{getservbyport'23', 'tcp';}, 'getservbyport' ], + [ q{getservent'foo';}, 'getservent' ], + [ q{getsockname'foo';}, 'getsockname' ], [ q{getsockopt'foo', 'bar', TCP_NODELAY;}, 'getsockopt' ], - [ q{glob'foo';}, 'glob' ], - [ q{gmtime'1';}, 'gmtime' ], - [ q{goto'label';}, 'goto' ], - [ q{hex'1';}, 'hex' ], - [ q{index'1','foo';}, 'index' ], - [ q{int'1';}, 'int' ], - [ q{ioctl'1',1;}, 'ioctl' ], - [ q{join'a',@foo;}, 'join' ], - [ q{keys'foo';}, 'keys' ], - [ q{kill'KILL';}, 'kill' ], - [ q{last'label';}, 'last' ], - [ q{lc'foo';}, 'lc' ], - [ q{lcfirst'foo';}, 'lcfirst' ], - [ q{length'foo';}, 'length' ], - [ q{link'foo','bar';}, 'link' ], - [ q{listen'1234',10;}, 'listen' ], - [ q{local'foo';}, 'local' ], - [ q{localtime'1';}, 'localtime' ], - [ q{lock'foo';}, 'lock' ], - [ q{log'foo';}, 'log' ], - [ q{lstat'foo';}, 'lstat' ], - [ q{mkdir'foo';}, 'mkdir' ], - [ q{msgctl'1','foo',1;}, 'msgctl' ], - [ q{msgget'1',1}, 'msgget' ], - [ q{msgrcv'1',$foo,1,1,1;}, 'msgrcv' ], - [ q{msgsnd'1',$foo,1;}, 'msgsnd' ], - [ q{my'foo';}, 'my' ], - [ q{next'label';}, 'next' ], - [ q{oct'foo';}, 'oct' ], - [ q{open'foo';}, 'open' ], - [ q{opendir'foo';}, 'opendir' ], - [ q{ord'foo';}, 'ord' ], - [ q{our'foo';}, 'our' ], - [ q{pack'H*',$data;}, 'pack' ], - [ q{pipe'in','out';}, 'pipe' ], - [ q{pop'foo';}, 'pop' ], - [ q{pos'foo';}, 'pos' ], - [ q{print'foo';}, 'print' ], - [ q{printf'foo','bar';}, 'printf' ], - [ q{prototype'foo';}, 'prototype' ], - [ q{push'foo','bar';}, 'push' ], - [ q{quotemeta'foo';}, 'quotemeta' ], - [ q{rand'1';}, 'rand' ], - [ q{read'1',$foo,100;}, 'read' ], - [ q{readdir'1';}, 'readdir' ], - [ q{readline'1';}, 'readline' ], - [ q{readlink'1';}, 'readlink' ], - [ q{readpipe'1';}, 'readpipe' ], - [ q{recv'1',$foo,100,1;}, 'recv' ], - [ q{redo'label';}, 'redo' ], - [ q{ref'foo';}, 'ref' ], - [ q{rename'foo','bar';}, 'rename' ], - [ q{require'foo';}, 'require' ], - [ q{reset'f';}, 'reset' ], - [ q{return'foo';}, 'return' ], - [ q{reverse'foo','bar';}, 'reverse' ], - [ q{rewinddir'1';}, 'rewinddir' ], - [ q{rindex'1','foo';}, 'rindex' ], - [ q{rmdir'foo';}, 'rmdir' ], - [ q{say'foo';}, 'say' ], - [ q{scalar'foo','bar';}, 'scalar' ], - [ q{seek'1',100,0;}, 'seek' ], - [ q{seekdir'1',100;}, 'seekdir' ], - [ q{select'1';}, 'select' ], - [ q{semctl'1',1,1;}, 'semctl' ], - [ q{semget'foo',1,1;}, 'semget' ], - [ q{semop'foo','bar';}, 'semop' ], - [ q{send'1',$foo'100,1;}, 'send' ], - [ q{setgrent'foo';}, 'setgrent' ], - [ q{sethostent'1';}, 'sethostent' ], - [ q{setnetent'1';}, 'setnetent' ], - [ q{setpgrp'1',2;}, 'setpgrp' ], - [ q{setpriority'1',2, 3;}, 'setpriority' ], - [ q{setprotoent'1';}, 'setprotoent' ], - [ q{setpwent'foo';}, 'setpwent' ], - [ q{setservent'1';}, 'setservent' ], - [ q{setsockopt'1',2,'foo',3;}, 'setsockopt' ], - [ q{shift'1','2';}, 'shift' ], - [ q{shmctl'1',2,$foo;}, 'shmctl' ], - [ q{shmget'1',2,1;}, 'shmget' ], - [ q{shmread'1',$foo,0,10;}, 'shmread' ], - [ q{shmwrite'1',$foo,0,10;}, 'shmwrite' ], - [ q{shutdown'1',0;}, 'shutdown' ], - [ q{sin'1';}, 'sin' ], - [ q{sleep'1';}, 'sleep' ], - [ q{socket'1',2,3,6;}, 'socket' ], - [ q{socketpair'1',2,3,4,6;}, 'socketpair' ], - [ q{splice'1',2;}, 'splice' ], - [ q{split'1','foo';}, 'split' ], - [ q{sprintf'foo','bar';}, 'sprintf' ], - [ q{sqrt'1';}, 'sqrt' ], - [ q{srand'1';}, 'srand' ], - [ q{stat'foo';}, 'stat' ], - [ q{state'foo';}, 'state' ], - [ q{study'foo';}, 'study' ], - [ q{substr'foo',1;}, 'substr' ], - [ q{symlink'foo','bar';}, 'symlink' ], - [ q{syscall'foo';}, 'syscall' ], - [ q{sysopen'foo','bar',1;}, 'sysopen' ], - [ q{sysread'1',$bar,1;}, 'sysread' ], - [ q{sysseek'1',0,0;}, 'sysseek' ], - [ q{system'foo';}, 'system' ], - [ q{syswrite'1',$bar,1;}, 'syswrite' ], - [ q{tell'1';}, 'tell' ], - [ q{telldir'1';}, 'telldir' ], - [ q{tie'foo',$bar;}, 'tie' ], - [ q{tied'foo';}, 'tied' ], - [ q{time'foo';}, 'time' ], - [ q{times'foo';}, 'times' ], - [ q{truncate'foo',1;}, 'truncate' ], - [ q{uc'foo';}, 'uc' ], - [ q{ucfirst'foo';}, 'ucfirst' ], - [ q{umask'foo';}, 'umask' ], - [ q{undef'foo';}, 'undef' ], - [ q{unlink'foo';}, 'unlink' ], - [ q{unpack'H*',$data;}, 'unpack' ], - [ q{unshift'1';}, 'unshift' ], - [ q{untie'foo';}, 'untie' ], - [ q{utime'1','2';}, 'utime' ], - [ q{values'foo';}, 'values' ], - [ q{vec'1',0.0;}, 'vec' ], - [ q{wait'1';}, 'wait' ], - [ q{waitpid'1',0;}, 'waitpid' ], - [ q{wantarray'foo';}, 'wantarray' ], - [ q{warn'foo';}, 'warn' ], - [ q{when'foo' {}}, 'when' ], - [ q{write'foo';}, 'write' ], - ) { + [ q{glob'foo';}, 'glob' ], + [ q{gmtime'1';}, 'gmtime' ], + [ q{goto'label';}, 'goto' ], + [ q{hex'1';}, 'hex' ], + [ q{index'1','foo';}, 'index' ], + [ q{int'1';}, 'int' ], + [ q{ioctl'1',1;}, 'ioctl' ], + [ q{join'a',@foo;}, 'join' ], + [ q{keys'foo';}, 'keys' ], + [ q{kill'KILL';}, 'kill' ], + [ q{last'label';}, 'last' ], + [ q{lc'foo';}, 'lc' ], + [ q{lcfirst'foo';}, 'lcfirst' ], + [ q{length'foo';}, 'length' ], + [ q{link'foo','bar';}, 'link' ], + [ q{listen'1234',10;}, 'listen' ], + [ q{local'foo';}, 'local' ], + [ q{localtime'1';}, 'localtime' ], + [ q{lock'foo';}, 'lock' ], + [ q{log'foo';}, 'log' ], + [ q{lstat'foo';}, 'lstat' ], + [ q{mkdir'foo';}, 'mkdir' ], + [ q{msgctl'1','foo',1;}, 'msgctl' ], + [ q{msgget'1',1}, 'msgget' ], + [ q{msgrcv'1',$foo,1,1,1;}, 'msgrcv' ], + [ q{msgsnd'1',$foo,1;}, 'msgsnd' ], + [ q{my'foo';}, 'my' ], + [ q{next'label';}, 'next' ], + [ q{oct'foo';}, 'oct' ], + [ q{open'foo';}, 'open' ], + [ q{opendir'foo';}, 'opendir' ], + [ q{ord'foo';}, 'ord' ], + [ q{our'foo';}, 'our' ], + [ q{pack'H*',$data;}, 'pack' ], + [ q{pipe'in','out';}, 'pipe' ], + [ q{pop'foo';}, 'pop' ], + [ q{pos'foo';}, 'pos' ], + [ q{print'foo';}, 'print' ], + [ q{printf'foo','bar';}, 'printf' ], + [ q{prototype'foo';}, 'prototype' ], + [ q{push'foo','bar';}, 'push' ], + [ q{quotemeta'foo';}, 'quotemeta' ], + [ q{rand'1';}, 'rand' ], + [ q{read'1',$foo,100;}, 'read' ], + [ q{readdir'1';}, 'readdir' ], + [ q{readline'1';}, 'readline' ], + [ q{readlink'1';}, 'readlink' ], + [ q{readpipe'1';}, 'readpipe' ], + [ q{recv'1',$foo,100,1;}, 'recv' ], + [ q{redo'label';}, 'redo' ], + [ q{ref'foo';}, 'ref' ], + [ q{rename'foo','bar';}, 'rename' ], + [ q{require'foo';}, 'require' ], + [ q{reset'f';}, 'reset' ], + [ q{return'foo';}, 'return' ], + [ q{reverse'foo','bar';}, 'reverse' ], + [ q{rewinddir'1';}, 'rewinddir' ], + [ q{rindex'1','foo';}, 'rindex' ], + [ q{rmdir'foo';}, 'rmdir' ], + [ q{say'foo';}, 'say' ], + [ q{scalar'foo','bar';}, 'scalar' ], + [ q{seek'1',100,0;}, 'seek' ], + [ q{seekdir'1',100;}, 'seekdir' ], + [ q{select'1';}, 'select' ], + [ q{semctl'1',1,1;}, 'semctl' ], + [ q{semget'foo',1,1;}, 'semget' ], + [ q{semop'foo','bar';}, 'semop' ], + [ q{send'1',$foo'100,1;}, 'send' ], + [ q{setgrent'foo';}, 'setgrent' ], + [ q{sethostent'1';}, 'sethostent' ], + [ q{setnetent'1';}, 'setnetent' ], + [ q{setpgrp'1',2;}, 'setpgrp' ], + [ q{setpriority'1',2, 3;}, 'setpriority' ], + [ q{setprotoent'1';}, 'setprotoent' ], + [ q{setpwent'foo';}, 'setpwent' ], + [ q{setservent'1';}, 'setservent' ], + [ q{setsockopt'1',2,'foo',3;}, 'setsockopt' ], + [ q{shift'1','2';}, 'shift' ], + [ q{shmctl'1',2,$foo;}, 'shmctl' ], + [ q{shmget'1',2,1;}, 'shmget' ], + [ q{shmread'1',$foo,0,10;}, 'shmread' ], + [ q{shmwrite'1',$foo,0,10;}, 'shmwrite' ], + [ q{shutdown'1',0;}, 'shutdown' ], + [ q{sin'1';}, 'sin' ], + [ q{sleep'1';}, 'sleep' ], + [ q{socket'1',2,3,6;}, 'socket' ], + [ q{socketpair'1',2,3,4,6;}, 'socketpair' ], + [ q{splice'1',2;}, 'splice' ], + [ q{split'1','foo';}, 'split' ], + [ q{sprintf'foo','bar';}, 'sprintf' ], + [ q{sqrt'1';}, 'sqrt' ], + [ q{srand'1';}, 'srand' ], + [ q{stat'foo';}, 'stat' ], + [ q{state'foo';}, 'state' ], + [ q{study'foo';}, 'study' ], + [ q{substr'foo',1;}, 'substr' ], + [ q{symlink'foo','bar';}, 'symlink' ], + [ q{syscall'foo';}, 'syscall' ], + [ q{sysopen'foo','bar',1;}, 'sysopen' ], + [ q{sysread'1',$bar,1;}, 'sysread' ], + [ q{sysseek'1',0,0;}, 'sysseek' ], + [ q{system'foo';}, 'system' ], + [ q{syswrite'1',$bar,1;}, 'syswrite' ], + [ q{tell'1';}, 'tell' ], + [ q{telldir'1';}, 'telldir' ], + [ q{tie'foo',$bar;}, 'tie' ], + [ q{tied'foo';}, 'tied' ], + [ q{time'foo';}, 'time' ], + [ q{times'foo';}, 'times' ], + [ q{truncate'foo',1;}, 'truncate' ], + [ q{uc'foo';}, 'uc' ], + [ q{ucfirst'foo';}, 'ucfirst' ], + [ q{umask'foo';}, 'umask' ], + [ q{undef'foo';}, 'undef' ], + [ q{unlink'foo';}, 'unlink' ], + [ q{unpack'H*',$data;}, 'unpack' ], + [ q{unshift'1';}, 'unshift' ], + [ q{untie'foo';}, 'untie' ], + [ q{utime'1','2';}, 'utime' ], + [ q{values'foo';}, 'values' ], + [ q{vec'1',0.0;}, 'vec' ], + [ q{wait'1';}, 'wait' ], + [ q{waitpid'1',0;}, 'waitpid' ], + [ q{wantarray'foo';}, 'wantarray' ], + [ q{warn'foo';}, 'warn' ], + [ q{when'foo' {}}, 'when' ], + [ q{write'foo';}, 'write' ], + ) + { my ( $code, $expected ) = @$test; - my ( $Document, $statement ) = _parse_to_statement( $code, 'PPI::Statement' ); + my ( $Document, $statement ) = + _parse_to_statement( $code, 'PPI::Statement' ); is( $statement, $code, "$code: statement text matches" ); _compare_child( $statement, 0, 'PPI::Token::Word', $expected, $code ); - isa_ok( $statement->child(1), 'PPI::Token::Quote::Single', "$code: second child is a 'PPI::Token::Quote::Single'" ); + isa_ok( $statement->child(1), + 'PPI::Token::Quote::Single', + "$code: second child is a 'PPI::Token::Quote::Single'" ); } for my $test ( - [ q{1 for'foo';}, 'for' ], - [ q{1 foreach'foo';}, 'foreach' ], - [ q{1 if'foo';}, 'if' ], - [ q{1 unless'foo';}, 'unless' ], - [ q{1 until'foo';}, 'until' ], - [ q{1 while'foo';}, 'while' ], - ) { + [ q{1 for'foo';}, 'for' ], + [ q{1 foreach'foo';}, 'foreach' ], + [ q{1 if'foo';}, 'if' ], + [ q{1 unless'foo';}, 'unless' ], + [ q{1 until'foo';}, 'until' ], + [ q{1 while'foo';}, 'while' ], + ) + { my ( $code, $expected ) = @$test; - my ( $Document, $statement ) = _parse_to_statement( $code, 'PPI::Statement' ); + my ( $Document, $statement ) = + _parse_to_statement( $code, 'PPI::Statement' ); is( $statement, $code, "$code: statement text matches" ); _compare_child( $statement, 2, 'PPI::Token::Word', $expected, $code ); - _compare_child( $statement, 3, 'PPI::Token::Quote::Single', "'foo'", $code ); + _compare_child( $statement, 3, 'PPI::Token::Quote::Single', "'foo'", + $code ); } # Untested: given, grep map, sort, sub - # PPI::Statement::Include for my $test ( [ "no'foo';", 'no' ], [ "require'foo';", 'require' ], [ "use'foo';", 'use' ], - ) { + ) + { my ( $code, $expected ) = @$test; - my ( $Document, $statement ) = _parse_to_statement( $code, 'PPI::Statement::Include' ); + my ( $Document, $statement ) = + _parse_to_statement( $code, 'PPI::Statement::Include' ); is( $statement, $code, "$code: statement text matches" ); _compare_child( $statement, 0, 'PPI::Token::Word', $expected, $code ); - _compare_child( $statement, 1, 'PPI::Token::Quote::Single', "'foo'", $code ); + _compare_child( $statement, 1, 'PPI::Token::Quote::Single', "'foo'", + $code ); _compare_child( $statement, 2, 'PPI::Token::Structure', ';', $code ); } - # PPI::Statement::Package - my ( $PackageDocument, $statement ) = _parse_to_statement( "package'foo';", 'PPI::Statement::Package' ); + my ( $PackageDocument, $statement ) = + _parse_to_statement( "package'foo';", 'PPI::Statement::Package' ); is( $statement, q{package'foo';}, q{package'foo'} ); - _compare_child( $statement, 0, 'PPI::Token::Word', 'package', 'package statement' ); - _compare_child( $statement, 1, 'PPI::Token::Quote::Single', "'foo'", 'package statement' ); - _compare_child( $statement, 2, 'PPI::Token::Structure', ';', 'package statement' ); + _compare_child( $statement, 0, 'PPI::Token::Word', 'package', + 'package statement' ); + _compare_child( $statement, 1, 'PPI::Token::Quote::Single', "'foo'", + 'package statement' ); + _compare_child( $statement, 2, 'PPI::Token::Structure', ';', + 'package statement' ); } - sub _parse_to_statement { - local $Test::Builder::Level = $Test::Builder::Level+1; + local $Test::Builder::Level = $Test::Builder::Level + 1; my $code = shift; my $type = shift; - my $Document = safe_new \$code; - my $statements = $Document->find( $type ); + my $Document = safe_new \$code; + my $statements = $Document->find($type); is( scalar(@$statements), 1, "$code: got one $type" ); isa_ok( $statements->[0], $type, "$code: got the statement" ); return ( $Document, $statements->[0] ); } - sub _compare_child { - local $Test::Builder::Level = $Test::Builder::Level+1; + local $Test::Builder::Level = $Test::Builder::Level + 1; my $statement = shift; - my $childno = shift; - my $type = shift; - my $content = shift; - my $desc = shift; + my $childno = shift; + my $type = shift; + my $content = shift; + my $desc = shift; - isa_ok( $statement->child($childno), $type, "$desc child $childno is a $type"); + isa_ok( $statement->child($childno), + $type, "$desc child $childno is a $type" ); is( $statement->child($childno), $content, "$desc child $childno is 1" ); return; } check_with "1.eqm'bar';", sub { - is $_->child( 0 )->child( 1 )->content, "eqm'bar", + is $_->child(0)->child(1)->content, "eqm'bar", "eqm' bareword after number and concat op is not mistaken for eq"; }; check_with "__DATA__", sub { - is $_->child( 1 ), undef, 'DATA segment without following newline does not get one added'; + is $_->child(1), undef, + 'DATA segment without following newline does not get one added'; }; check_with "__DATA__ a", sub { - is $_->child( 1 )->content, ' a', - 'DATA segment without following newline, but text, has text added as comment in following token'; + is $_->child(1)->content, ' a', +'DATA segment without following newline, but text, has text added as comment in following token'; }; check_with "__END__", sub { - is $_->child( 1 ), undef, 'END segment without following newline does not get one added'; + is $_->child(1), undef, + 'END segment without following newline does not get one added'; }; check_with "__END__ a", sub { - is $_->child( 0 )->child( 1 )->content, ' a', - 'END segment without following newline, but text, has text added as comment in children list'; + is $_->child(0)->child(1)->content, ' a', +'END segment without following newline, but text, has text added as comment in children list'; }; check_with "__END__ a\n", sub { - is $_->child( 0 )->child( 1 )->content, ' a', - 'END segment, followed by text and newline, has text added as comment in children list'; + is $_->child(0)->child(1)->content, ' a', +'END segment, followed by text and newline, has text added as comment in children list'; }; check_with "__DATA__ a\n", sub { - is $_->child( 1 )->content, ' a', - 'DATA segment, followed by text and newline, has text added as comment in following token'; + is $_->child(1)->content, ' a', +'DATA segment, followed by text and newline, has text added as comment in following token'; }; 1; diff --git a/xt/DepReqs.pm b/xt/DepReqs.pm index 61304ccb..5bb1592d 100644 --- a/xt/DepReqs.pm +++ b/xt/DepReqs.pm @@ -16,7 +16,7 @@ use Safe::Isa '$_call_if_object'; __PACKAGE__->run unless caller; sub exclusions { - qr@^( + qr@^( # don't remember why i excluded these Apache2-SSI|Devel-IPerl|Padre # fails tests regarding directory write permissions, probably not PPI @@ -75,7 +75,7 @@ sub exclusions { } sub cpm_install_fails { - qr@^( + qr@^( Apache2::Const | AptPkg::Cache | AptPkg::Config | BSON::XS | Code::Splice | Config::ApacheFile | Data::Dump::Steamer | Devel::MyDebugger | Dist::Zilla::Plugin::Test::NewVersion | Git::Github::Creator | Hook::Lex::Wrap @@ -91,92 +91,92 @@ sub cpm_install_fails { } sub force_big_metacpan_fetch { - ## force metacpan to actually return the whole dependents list - # https://github.com/metacpan/metacpan-client/issues/122 - my $old_fetch = \&MetaCPAN::Client::fetch; - my $new_fetch = sub { $old_fetch->( shift, shift . "?size=5000", @_ ) }; - { no warnings 'redefine'; *MetaCPAN::Client::fetch = $new_fetch; } + ## force metacpan to actually return the whole dependents list + # https://github.com/metacpan/metacpan-client/issues/122 + my $old_fetch = \&MetaCPAN::Client::fetch; + my $new_fetch = sub { $old_fetch->( shift, shift . "?size=5000", @_ ) }; + { no warnings 'redefine'; *MetaCPAN::Client::fetch = $new_fetch; } - return $old_fetch; + return $old_fetch; } sub run { - my $old_fetch = force_big_metacpan_fetch; - - { no warnings 'redefine'; *MetaCPAN::Client::fetch = $old_fetch; } - - my $c = MetaCPAN::Client->new; - - my @deps = _resolve_reverse_dependencies( PPI => 10, exclusions(), $c ); - - say "writing dependents file"; - io( -e "xt" ? "xt/dependents" : "dependents" )->print( join "\n", @deps ); - - say "getting modules to pre-install"; - my $cpm_fails = cpm_install_fails; - my @reqs; - my @skip; - for my $dependent (@deps) { - say $dependent; - my @dep_reqs = map @{ $c->release($_)->dependency }, $dependent; - my @fails = # - map $_->{module}, grep $_->{module} =~ $cpm_fails, @dep_reqs; - if (@fails) { - push @skip, $dependent; - say "skipping dependent $dependent because " - . "it requires modules that fail to install: @fails"; - next; - } - push @reqs, @dep_reqs; - } - say "skipping dependents because " - . "they requires modules that fail to install: @skip" - if @skip; - - say "writing dependency pre-install file"; - io("xt/cpanfile") - ->print( join "\n", - uniqstr map qq[requires "$_->{module}" => "$_->{version}";], @reqs ); - - say "debug printing file"; - say io("xt/cpanfile")->all; - - # test early that all modules don't have an author that crashes tests later - # !!! careful, this changes CWD !!! - say "testing dists for author names"; - Test::DependentModules::_load_cpan; - for my $name (@deps) { - say $name; - my $mod = $name; - $mod =~ s/-/::/g; - next unless # - my $dist = Test::DependentModules::_get_distro($mod); - $dist->author->id; - } - - say "done"; + my $old_fetch = force_big_metacpan_fetch; + + { no warnings 'redefine'; *MetaCPAN::Client::fetch = $old_fetch; } + + my $c = MetaCPAN::Client->new; + + my @deps = _resolve_reverse_dependencies( PPI => 10, exclusions(), $c ); + + say "writing dependents file"; + io( -e "xt" ? "xt/dependents" : "dependents" )->print( join "\n", @deps ); + + say "getting modules to pre-install"; + my $cpm_fails = cpm_install_fails; + my @reqs; + my @skip; + for my $dependent (@deps) { + say $dependent; + my @dep_reqs = map @{ $c->release($_)->dependency }, $dependent; + my @fails = # + map $_->{module}, grep $_->{module} =~ $cpm_fails, @dep_reqs; + if (@fails) { + push @skip, $dependent; + say "skipping dependent $dependent because " + . "it requires modules that fail to install: @fails"; + next; + } + push @reqs, @dep_reqs; + } + say "skipping dependents because " + . "they requires modules that fail to install: @skip" + if @skip; + + say "writing dependency pre-install file"; + io("xt/cpanfile") + ->print( join "\n", + uniqstr map qq[requires "$_->{module}" => "$_->{version}";], @reqs ); + + say "debug printing file"; + say io("xt/cpanfile")->all; + + # test early that all modules don't have an author that crashes tests later + # !!! careful, this changes CWD !!! + say "testing dists for author names"; + Test::DependentModules::_load_cpan; + for my $name (@deps) { + say $name; + my $mod = $name; + $mod =~ s/-/::/g; + next unless # + my $dist = Test::DependentModules::_get_distro($mod); + $dist->author->id; + } + + say "done"; } sub _resolve_reverse_dependencies { - my ( $base_dist, $depth, $exclude, $c ) = @_; + my ( $base_dist, $depth, $exclude, $c ) = @_; - my ( @work, %deps, %seen ) = ($base_dist); + my ( @work, %deps, %seen ) = ($base_dist); - for my $level ( 1 .. $depth ) { - say "resolving level: $level"; - for my $dist (@work) { - my $deps = $c->rev_deps($dist); + for my $level ( 1 .. $depth ) { + say "resolving level: $level"; + for my $dist (@work) { + my $deps = $c->rev_deps($dist); - while ( my $dist = $deps->next->$_call_if_object("distribution") ) { - next if $seen{$dist}++; - next if $exclude and $dist =~ $exclude; - $deps{$level}{$dist} = 1; - } - } + while ( my $dist = $deps->next->$_call_if_object("distribution") ) { + next if $seen{$dist}++; + next if $exclude and $dist =~ $exclude; + $deps{$level}{$dist} = 1; + } + } - @work = sort keys %{ $deps{$level} }; - } + @work = sort keys %{ $deps{$level} }; + } - my @deps = uniqstr map keys %{$_}, values %deps; - return sort @deps; + my @deps = uniqstr map keys %{$_}, values %deps; + return sort @deps; } diff --git a/xt/author/api.t b/xt/author/api.t index 3c0bc468..04ed16c6 100644 --- a/xt/author/api.t +++ b/xt/author/api.t @@ -21,7 +21,7 @@ $Test::ClassAPI::IGNORE{'reftype'}++; $Test::ClassAPI::IGNORE{'blessed'}++; # Execute the tests -Test::ClassAPI->execute('complete', 'collisions'); +Test::ClassAPI->execute( 'complete', 'collisions' ); exit(0); # Now, define the API for the classes diff --git a/xt/author/author.t b/xt/author/author.t index 502c56e3..20026c90 100644 --- a/xt/author/author.t +++ b/xt/author/author.t @@ -1,6 +1,7 @@ #!/usr/bin/perl use strict; + BEGIN { $| = 1; $^W = 1; diff --git a/xt/author/meta.t b/xt/author/meta.t index 713d6616..e07804aa 100644 --- a/xt/author/meta.t +++ b/xt/author/meta.t @@ -3,6 +3,7 @@ # Test that our META.yml file matches the current specification. use strict; + BEGIN { $| = 1; $^W = 1; diff --git a/xt/author/perltidy.t b/xt/author/perltidy.t new file mode 100644 index 00000000..dec3b400 --- /dev/null +++ b/xt/author/perltidy.t @@ -0,0 +1,49 @@ +use 5.010; +use strictures 2; + +=head1 DESCRIPTION + +This test checks all perl files for tidyness, applying the local .perltidyrc rules. It is also set +up to use a cache file generated by our in-dev version of Dist::Zilla::App::Command::perltidy. Said +cache file normally lives in ~/.perltidychk. + +=cut + +use Test::More; +use Test::PerlTidy; +use Dist::Zilla::Path; +use YAML; +use Digest::SHA 'sha1_base64'; +use File::HomeDir; + +run(); + +sub run { + my %args = ( + exclude => [ + ".build/", "_build/", "blib", ".git", ".vscode", # + qr@^[A-Za-z].*-[0-9._]+(-TRIAL)?/@ + ], + perltidyrc => ".perltidyrc", + ); + my @files = Test::PerlTidy::list_files(%args); + + my %seen; + my $seen_file = path( File::HomeDir->my_home, ".perltidychk" ); + %seen = %{ Load $seen_file->slurp_raw } if $seen_file->exists; + my @to_skip = grep $seen{"./$_"}{ sha1_base64 path($_)->slurp_raw }, @files; + return pass "all files tidy", done_testing if @files == @to_skip; + + push @{ $args{exclude} }, @to_skip; + @files = Test::PerlTidy::list_files(%args); + + my @ok_files = + grep ok( Test::PerlTidy::is_file_tidy( $_, $args{perltidyrc} ), "'$_'" ), + @files; + $seen{"./$_"}{ sha1_base64 path($_)->slurp_raw } = 1 for @ok_files; + $seen_file->spew( Dump \%seen ); + + done_testing; + + return; +} diff --git a/xt/author/pmv.t b/xt/author/pmv.t index 79d1f9fa..29e4384c 100644 --- a/xt/author/pmv.t +++ b/xt/author/pmv.t @@ -3,9 +3,10 @@ # Test that our declared minimum Perl version matches our syntax use strict; + BEGIN { - $| = 1; - $^W = 1; + $| = 1; + $^W = 1; } my @MODULES = ( @@ -19,15 +20,20 @@ my @MODULES = ( use Test::More; # Load the testing modules -foreach my $MODULE ( @MODULES ) { - die "Failed to load required release-testing module $MODULE" - if not eval "use $MODULE; 1"; +foreach my $MODULE (@MODULES) { + die "Failed to load required release-testing module $MODULE" + if not eval "use $MODULE; 1"; } -all_minimum_version_from_metayml_ok( { - paths => [ - grep - { !/14_charsets\.t/ and !/24_v6\// and !/xt\// and !/Token\/Data\.pm/ } - File::Find::Rule->perl_file->in('.') - ], -} ); +all_minimum_version_from_metayml_ok( + { + paths => [ + grep { + !/14_charsets\.t/ + and !/24_v6\// + and !/xt\// + and !/Token\/Data\.pm/ + } File::Find::Rule->perl_file->in('.') + ], + } +);