Skip to content

Commit

Permalink
split out checked document creation in test helper module
Browse files Browse the repository at this point in the history
  • Loading branch information
wchristian committed Dec 29, 2022
1 parent 2652f8c commit 5baa38d
Show file tree
Hide file tree
Showing 56 changed files with 304 additions and 355 deletions.
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Revision history for Perl extension PPI

{{$NEXT}}
Details:
- Wrapped most Document->new calls in tests with automatic checks

1.276 2022-07-19 21:43:50Z
Summary:
Expand Down
25 changes: 9 additions & 16 deletions t/03_document.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

use lib 't/lib';
use PPI::Test::pragmas;
use Test::More tests => 13 + ($ENV{AUTHOR_TESTING} ? 1 : 0);
use Test::More tests => 19 + ($ENV{AUTHOR_TESTING} ? 1 : 0);

use File::Spec::Functions qw( catfile );
use PPI ();
use Helper 'safe_new';


#####################################################################
Expand All @@ -18,8 +19,7 @@ NEW: {
my $file = catfile(qw{ t data 03_document test.dat });
ok( -f $file, 'Found test.dat' );

my $doc1 = PPI::Document->new( $file );
isa_ok( $doc1, 'PPI::Document' );
my $doc1 = safe_new $file;

# Test script
my $script = <<'END_PERL';
Expand All @@ -29,17 +29,15 @@ NEW: {
print "Hello World!\n";
END_PERL
my $doc2 = PPI::Document->new( \$script );
isa_ok( $doc2, 'PPI::Document' );
my $doc2 = safe_new \$script;

my $doc3 = PPI::Document->new( [
my $doc3 = safe_new [
"#!/usr/bin/perl",
"",
"# A simple test script",
"",
"print \"Hello World!\\n\";",
] );
isa_ok( $doc3, 'PPI::Document' );
];

# Compare the three forms
is_deeply( $doc1, $doc2, 'Stringref form matches file form' );
Expand All @@ -51,14 +49,9 @@ NEW_EMPTY: {
my $empty = catfile(qw{ t data 03_document empty.dat });
ok( -f $empty, 'Found empty.dat' );

my $doc1 = PPI::Document->new( $empty );
isa_ok( $doc1, 'PPI::Document' );

my $doc2 = PPI::Document->new( \'' );
isa_ok( $doc2, 'PPI::Document' );

my $doc3 = PPI::Document->new( [ ] );
isa_ok( $doc3, 'PPI::Document' );
my $doc1 = safe_new $empty;
my $doc2 = safe_new \'';
my $doc3 = safe_new [ ];

# Compare the three forms
is_deeply( $doc1, $doc2, 'Stringref form matches file form' );
Expand Down
20 changes: 8 additions & 12 deletions t/04_element.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

use lib 't/lib';
use PPI::Test::pragmas;
use Test::More tests => 220 + ($ENV{AUTHOR_TESTING} ? 1 : 0);
use Test::More tests => 227 + ($ENV{AUTHOR_TESTING} ? 1 : 0);

use PPI ();
use PPI::Singletons qw( %_PARENT );
use PPI::Test qw( pause );
use Scalar::Util qw( refaddr );
use Helper 'safe_new';

my $RE_IDENTIFIER = qr/[^\W\d]\w*/;

Expand Down Expand Up @@ -231,7 +232,7 @@ is( $Token7->next_sibling, '', 'Last token returns false for next_sibling' );

# More extensive test for next_sibling
SCOPE: {
my $doc = PPI::Document->new( \"sub foo { bar(); }" );
my $doc = safe_new \"sub foo { bar(); }";
my $end = $doc->last_token;
isa_ok( $end, 'PPI::Token::Structure' );
is( $end->content, '}', 'Got end token' );
Expand All @@ -254,7 +255,7 @@ is_object( $Token7->previous_sibling, $Braces, "Last token sees braces as previo

# More extensive test for next_sibling
SCOPE: {
my $doc = PPI::Document->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' );
Expand Down Expand Up @@ -286,8 +287,7 @@ is_object( $Token7->sprevious_sibling, $Braces, "Last token sees braces as sprev

# Test snext_sibling and sprevious_sibling cases when inside a parent block
SCOPE: {
my $cpan13454 = PPI::Document->new( \'{ 1 }' );
isa_ok( $cpan13454, 'PPI::Document' );
my $cpan13454 = safe_new \'{ 1 }';
my $num = $cpan13454->find_first('Token::Number');
isa_ok( $num, 'PPI::Token::Number' );
my $prev = $num->sprevious_sibling;
Expand Down Expand Up @@ -376,7 +376,6 @@ SCOPE: {
# Cloning an Element/Node
SCOPE: {
my $Doc2 = $Document->clone;
isa_ok( $Doc2, 'PPI::Document' );
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,
Expand Down Expand Up @@ -439,8 +438,7 @@ SCOPE: {
my $k2;
my $k3;
SCOPE: {
my $NodeDocument = PPI::Document->new( $INC{"PPI/Node.pm"} );
isa_ok( $NodeDocument, 'PPI::Document' );
my $NodeDocument = safe_new $INC{"PPI/Node.pm"};
$k2 = scalar keys %_PARENT;
ok( $k2 > ($k1 + 3000), 'PARENT keys increases after loading document' );
$NodeDocument->DESTROY;
Expand All @@ -456,8 +454,7 @@ SCOPE: {
my $k2;
my $k3;
SCOPE: {
my $NodeDocument = PPI::Document->new( $INC{"PPI/Node.pm"} );
isa_ok( $NodeDocument, 'PPI::Document' );
my $NodeDocument = safe_new $INC{"PPI/Node.pm"};
$k2 = scalar keys %_PARENT;
ok( $k2 > ($k1 + 3000), 'PARENT keys increases after loading document' );
}
Expand Down Expand Up @@ -487,8 +484,7 @@ END_PERL
$code =~ s/\s+$//s;

# Create the document
my $doc = PPI::Document->new( \$code );
isa_ok( $doc, 'PPI::Document' );
my $doc = safe_new \$code;

# Basic first_token and last_token using a single non-trival sample
### FIXME - Make this more thorough
Expand Down
6 changes: 3 additions & 3 deletions t/06_round_trip.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use Test::More; # Plan comes later
use File::Spec::Functions qw( catdir );
use PPI ();
use PPI::Test qw( find_files );
use Helper 'safe_new';



Expand Down Expand Up @@ -42,7 +43,7 @@ foreach my $dir (
push @files, find_files( 't' );

# Declare our plan
Test::More::plan( tests => ($ENV{AUTHOR_TESTING} ? 1 : 0) + scalar(@files) * 9 );
Test::More::plan( tests => ($ENV{AUTHOR_TESTING} ? 1 : 0) + scalar(@files) * 10 - 1 );



Expand Down Expand Up @@ -78,9 +79,8 @@ sub roundtrip_ok {
SKIP: {
skip( 'Ignoring 14_charset.t', 7 ) if $file =~ /14_charset/;

my $Document = PPI::Document->new( $file );
my $Document = safe_new $file;
ok( $Document, "$file: ->new returned true" );
isa_ok( $Document, 'PPI::Document' );

# Serialize it back out, and compare with the raw version
skip( "Ignoring failed parse of $file", 5 ) unless defined $Document;
Expand Down
Loading

0 comments on commit 5baa38d

Please sign in to comment.