Login
Forgot to add PPIx::Utilities::Node to required modules in
[gknop/Perl-Critic.git] / t / 08_document.t
CommitLineData
faa35de4
JRT
1#!perl
2
6036a254 3##############################################################################
b22c8627
JRT
4# $URL$
5# $Date$
6# $Author$
7# $Revision$
8##############################################################################
9
df6dee2b 10use 5.006001;
b22c8627
JRT
11use strict;
12use warnings;
267b39b4 13
f9a8aec7
ES
14use Carp qw< carp >;
15
267b39b4
ES
16use version;
17
f3b652c3 18
013aa3aa 19use Perl::Critic::Document qw< >;
267b39b4
ES
20use Perl::Critic::Utils::DataConversion qw< dor >;
21
f3b652c3 22
013aa3aa
ES
23use Test::Deep;
24use Test::More tests => 37;
b22c8627
JRT
25
26#-----------------------------------------------------------------------------
27
c263f914 28our $VERSION = '1.108';
b185fa17
ES
29
30#-----------------------------------------------------------------------------
31
b22c8627 32can_ok('Perl::Critic::Document', 'new');
267b39b4 33can_ok('Perl::Critic::Document', 'filename');
b22c8627 34can_ok('Perl::Critic::Document', 'find');
3d6744c8
JRT
35can_ok('Perl::Critic::Document', 'find_first');
36can_ok('Perl::Critic::Document', 'find_any');
013aa3aa
ES
37can_ok('Perl::Critic::Document', 'namespaces');
38can_ok('Perl::Critic::Document', 'subdocuments_for_namespace');
267b39b4
ES
39can_ok('Perl::Critic::Document', 'highest_explicit_perl_version');
40can_ok('Perl::Critic::Document', 'ppi_document');
1b936936 41can_ok('Perl::Critic::Document', 'is_program');
d533eee5 42can_ok('Perl::Critic::Document', 'is_module');
b22c8627
JRT
43
44{
45 my $code = q{'print 'Hello World';}; #Has 6 PPI::Element
46 my $ppi_doc = PPI::Document->new( \$code );
d533eee5 47 my $pc_doc = Perl::Critic::Document->new( '-source' => $ppi_doc );
b22c8627 48 isa_ok($pc_doc, 'Perl::Critic::Document');
a08d9933
ES
49 isa_ok($pc_doc, 'PPI::Document');
50 isa_ok($pc_doc, 'PPI::Node');
51 isa_ok($pc_doc, 'PPI::Element');
b22c8627
JRT
52
53
54 my $nodes_ref = $pc_doc->find('PPI::Element');
3d6744c8 55 is( scalar @{ $nodes_ref }, 6, 'find by class name');
b22c8627
JRT
56
57 $nodes_ref = $pc_doc->find( sub{ return 1 } );
3d6744c8 58 is( scalar @{ $nodes_ref }, 6, 'find by wanted() handler');
b22c8627 59
7d1077fa 60 $nodes_ref = $pc_doc->find( q{Element} );
3d6744c8
JRT
61 is( scalar @{ $nodes_ref }, 6, 'find by shortened class name');
62
63 #---------------------------
64
65 my $node = $pc_doc->find_first('PPI::Element');
66 is( ref $node, 'PPI::Statement', 'find_first by class name');
67
68 $node = $pc_doc->find_first( sub{ return 1 } );
69 is( ref $node, 'PPI::Statement', 'find_first by wanted() handler');
70
71 $node = $pc_doc->find_first( q{Element} );
72 is( ref $node, 'PPI::Statement', 'find_first by shortened class name');
73
74 #---------------------------
75
76 my $found = $pc_doc->find_any('PPI::Element');
77 is( $found, 1, 'find_any by class name');
78
79 $found = $pc_doc->find_any( sub{ return 1 } );
80 is( $found, 1, 'find_any by wanted() handler');
81
82 $found = $pc_doc->find_any( q{Element} );
83 is( $found, 1, 'find_any by shortened class name');
84
85 #-------------------------------------------------------------------------
7d1077fa
CD
86
87 {
88 # Ignore "Cannot create search condition for 'PPI::': Not a PPI::Element"
d91a8cdf 89 local $SIG{__WARN__} = sub {
369d6f3a 90 $_[0] =~ m/\QCannot create search condition for\E/xms || carp @_
d91a8cdf 91 };
7d1077fa 92 $nodes_ref = $pc_doc->find( q{} );
3d6744c8
JRT
93 is( $nodes_ref, undef, 'find by empty class name');
94
95 $node = $pc_doc->find_first( q{} );
96 is( $node, undef, 'find_first by empty class name');
97
98 $found = $pc_doc->find_any( q{} );
99 is( $found, undef, 'find_any by empty class name');
100
7d1077fa 101 }
d533eee5
ES
102
103 #-------------------------------------------------------------------------
104
013aa3aa
ES
105 cmp_deeply(
106 [ $pc_doc->namespaces() ],
107 ['main'],
108 q<everything is in the "main" namespace>,
109 );
110
d533eee5 111 ok( $pc_doc->is_module(), q{document type 'module' is a module});
1b936936 112 ok( ! $pc_doc->is_program(), q{document type 'module' is not a program});
d533eee5 113
b22c8627 114}
737d3b65 115
34cc6052
ES
116#-----------------------------------------------------------------------------
117
267b39b4 118{
013aa3aa
ES
119 my $ppi_document = PPI::Document->new(\'foo(); package Foo; package Bar');
120 my $critic_document =
121 Perl::Critic::Document->new(-source => $ppi_document);
122
123 cmp_deeply(
124 [ $critic_document->namespaces() ],
125 bag( qw< main Foo Bar > ),
126 'Got expected namespaces',
127 );
128}
129
130#-----------------------------------------------------------------------------
131
132
133{
267b39b4
ES
134 test_version( 'sub { 1 }', undef );
135 test_version( 'use 5.006', version->new('5.006') );
136 test_version( 'use 5.8.3', version->new('5.8.3') );
137 test_version(
138 'use 5.006; use 5.8.3; use 5.005005',
139 version->new('5.8.3'),
140 );
81e74a91
ES
141 test_version( 'use 5.005_05; use 5.005_03', version->new('5.005_05') );
142 test_version( 'use 5.005_03; use 5.005_05', version->new('5.005_05') );
267b39b4
ES
143}
144
145sub test_version {
146 my ($code, $expected_version) = @_;
147
148 my $description_version = dor( $expected_version, '<undef>' );
149
150 my $document =
151 Perl::Critic::Document->new(
d533eee5 152 '-source' => PPI::Document->new( \$code )
267b39b4
ES
153 );
154
155 is(
156 $document->highest_explicit_perl_version(),
157 $expected_version,
158 qq<Get "$description_version" for "$code".>,
159 );
2a4245c2
ES
160
161 return;
267b39b4
ES
162}
163
164#-----------------------------------------------------------------------------
165
9e7d390d 166# ensure we return true if this test is loaded by
34cc6052
ES
167# t/08_document.t_without_optional_dependencies.t
1681;
169
737d3b65
CD
170# Local Variables:
171# mode: cperl
172# cperl-indent-level: 4
173# fill-column: 78
174# indent-tabs-mode: nil
175# c-indentation-style: bsd
176# End:
96fed375 177# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :