File: 06_round_trip.t

package info (click to toggle)
libppi-perl 1.215-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,820 kB
  • sloc: perl: 12,129; makefile: 8
file content (120 lines) | stat: -rw-r--r-- 3,027 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/perl

# Load ALL of the PPI files, lex them in, dump them
# out, and verify that the code goes in and out cleanly.

use strict;
BEGIN {
	no warnings 'once';
	$| = 1;
	$PPI::XS_DISABLE = 1;
	$PPI::Lexer::X_TOKENIZER ||= $ENV{X_TOKENIZER};
}
use Test::More; # Plan comes later
use Test::NoWarnings;
use File::Spec::Functions ':ALL';
use PPI;





#####################################################################
# Prepare

# Find all of the files to be checked
my %tests = map { $_ => $INC{$_} } grep { ! /\bXS\.pm/ } grep { /^PPI\b/ } keys %INC;
my @files = sort values %tests;
unless ( @files ) {
	Test::More::plan( tests => 2 );
	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'
) {
	my @perl = find_files( catdir( 't', 'data', $dir ) );
	push @files, @perl;
}

# Add the test scripts themselves
push @files, find_files( 't' );

# Declare our plan
Test::More::plan( tests => 1 + scalar(@files) * 9 );





#####################################################################
# Run the Tests

foreach my $file ( @files ) {
	roundtrip_ok( $file );
}





#####################################################################
# Test Functions

sub roundtrip_ok {
	my $file = shift;
	local *FILE;
	my $rv = open( FILE, '<', $file );
	ok( $rv, "$file: Found file " );
	SKIP: {
		skip "No file to test", 7 unless $rv;
		my $source = do { local $/ = undef; <FILE> };
		close FILE;
		ok( length $source, "$file: Loaded cleanly" );
		$source =~ s/(?:\015{1,2}\012|\015|\012)/\n/g;

		# Load the file as a Document
		SKIP: {
			skip( 'Ignoring 14_charset.t', 7 ) if $file =~ /14_charset/;

			my $Document = PPI::Document->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;
			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" );
		}
	}	
}

# Find file names in named t/data dirs
sub find_files {
	my $testdir  = shift;
	
	# Does the test directory exist?
	-e $testdir and -d $testdir and -r $testdir or die "Failed to find test directory $testdir";
	
	# Find the .code test files
	opendir( TESTDIR, $testdir ) or die "opendir: $!";
	my @perl = map { catfile( $testdir, $_ ) } sort grep { /\.(?:code|pm|t)$/ } readdir(TESTDIR);
	closedir( TESTDIR ) or die "closedir: $!";
	return @perl;
}