File: ppi_normal.t

package info (click to toggle)
libppi-perl 1.283-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,216 kB
  • sloc: perl: 15,295; makefile: 8
file content (74 lines) | stat: -rwxr-xr-x 2,501 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl

# Unit testing for PPI::Normal

use lib 't/lib';
use PPI::Test::pragmas;
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;
	isa_ok( $layer_1, 'PPI::Normal' );
	is( $layer_1->layer, 1, '->new creates a layer 1' );
	my $layer_1a = PPI::Normal->new(1);
	isa_ok( $layer_1a, 'PPI::Normal' );
	is( $layer_1a->layer, 1, '->new(1) creates a layer 1' );
	my $layer_2 = PPI::Normal->new(2);
	isa_ok( $layer_2, 'PPI::Normal' );
	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' );
}


PROCESS: {
	my $doc1 = safe_new \'print "Hello World!\n";';
	my $doc2 = \'print "Hello World!\n";';
	my $doc3 = \' print  "Hello World!\n"; # comment';
	my $doc4 = \'print "Hello World!\n"';

	# Normalize them at level 1
	my $layer1 = PPI::Normal->new(1);
	isa_ok( $layer1, 'PPI::Normal' );
	my $nor11 = $layer1->process($doc1->clone);
	my $nor12 = $layer1->process($doc2);
	my $nor13 = $layer1->process($doc3);
	isa_ok( $nor11, 'PPI::Document::Normalized' );
	isa_ok( $nor12, 'PPI::Document::Normalized' );
	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' );

	# 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 $nor24 = $layer2->process($doc4);
	isa_ok( $nor21, 'PPI::Document::Normalized' );
	isa_ok( $nor22, 'PPI::Document::Normalized' );
	isa_ok( $nor23, 'PPI::Document::Normalized' );
	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' );
}