Login
Remove debugging code from P::C::Document.
[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 23use Test::Deep;
76b854e4 24use Test::More tests => 40;
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 39can_ok('Perl::Critic::Document', 'highest_explicit_perl_version');
76b854e4 40can_ok('Perl::Critic::Document', 'uses_module');
267b39b4 41can_ok('Perl::Critic::Document', 'ppi_document');
1b936936 42can_ok('Perl::Critic::Document', 'is_program');
d533eee5 43can_ok('Perl::Critic::Document', 'is_module');
b22c8627
JRT
44
45{
46 my $code = q{'print 'Hello World';}; #Has 6 PPI::Element
47 my $ppi_doc = PPI::Document->new( \$code );
d533eee5 48 my $pc_doc = Perl::Critic::Document->new( '-source' => $ppi_doc );
b22c8627 49 isa_ok($pc_doc, 'Perl::Critic::Document');
a08d9933
ES
50 isa_ok($pc_doc, 'PPI::Document');
51 isa_ok($pc_doc, 'PPI::Node');
52 isa_ok($pc_doc, 'PPI::Element');
b22c8627
JRT
53
54
55 my $nodes_ref = $pc_doc->find('PPI::Element');
3d6744c8 56 is( scalar @{ $nodes_ref }, 6, 'find by class name');
b22c8627
JRT
57
58 $nodes_ref = $pc_doc->find( sub{ return 1 } );
3d6744c8 59 is( scalar @{ $nodes_ref }, 6, 'find by wanted() handler');
b22c8627 60
7d1077fa 61 $nodes_ref = $pc_doc->find( q{Element} );
3d6744c8
JRT
62 is( scalar @{ $nodes_ref }, 6, 'find by shortened class name');
63
64 #---------------------------
65
66 my $node = $pc_doc->find_first('PPI::Element');
67 is( ref $node, 'PPI::Statement', 'find_first by class name');
68
69 $node = $pc_doc->find_first( sub{ return 1 } );
70 is( ref $node, 'PPI::Statement', 'find_first by wanted() handler');
71
72 $node = $pc_doc->find_first( q{Element} );
73 is( ref $node, 'PPI::Statement', 'find_first by shortened class name');
74
75 #---------------------------
76
77 my $found = $pc_doc->find_any('PPI::Element');
78 is( $found, 1, 'find_any by class name');
79
80 $found = $pc_doc->find_any( sub{ return 1 } );
81 is( $found, 1, 'find_any by wanted() handler');
82
83 $found = $pc_doc->find_any( q{Element} );
84 is( $found, 1, 'find_any by shortened class name');
85
86 #-------------------------------------------------------------------------
7d1077fa
CD
87
88 {
89 # Ignore "Cannot create search condition for 'PPI::': Not a PPI::Element"
d91a8cdf 90 local $SIG{__WARN__} = sub {
369d6f3a 91 $_[0] =~ m/\QCannot create search condition for\E/xms || carp @_
d91a8cdf 92 };
7d1077fa 93 $nodes_ref = $pc_doc->find( q{} );
3d6744c8
JRT
94 is( $nodes_ref, undef, 'find by empty class name');
95
96 $node = $pc_doc->find_first( q{} );
97 is( $node, undef, 'find_first by empty class name');
98
99 $found = $pc_doc->find_any( q{} );
100 is( $found, undef, 'find_any by empty class name');
101
7d1077fa 102 }
d533eee5
ES
103
104 #-------------------------------------------------------------------------
105
013aa3aa
ES
106 cmp_deeply(
107 [ $pc_doc->namespaces() ],
108 ['main'],
109 q<everything is in the "main" namespace>,
110 );
111
d533eee5 112 ok( $pc_doc->is_module(), q{document type 'module' is a module});
1b936936 113 ok( ! $pc_doc->is_program(), q{document type 'module' is not a program});
d533eee5 114
b22c8627 115}
737d3b65 116
34cc6052
ES
117#-----------------------------------------------------------------------------
118
267b39b4 119{
013aa3aa
ES
120 my $ppi_document = PPI::Document->new(\'foo(); package Foo; package Bar');
121 my $critic_document =
122 Perl::Critic::Document->new(-source => $ppi_document);
123
124 cmp_deeply(
125 [ $critic_document->namespaces() ],
126 bag( qw< main Foo Bar > ),
127 'Got expected namespaces',
128 );
129}
130
131#-----------------------------------------------------------------------------
132
76b854e4
ES
133{
134 my $ppi_document = PPI::Document->new(\'use Moose');
135 my $critic_document =
136 Perl::Critic::Document->new(-source => $ppi_document);
137
138 ok(!! $critic_document->uses_module('Moose'), 'Moose is used.');
139 ok( ! $critic_document->uses_module('Moose::Role'), 'Moose::Role is not used.');
140}
141
142#-----------------------------------------------------------------------------
143
013aa3aa
ES
144
145{
267b39b4
ES
146 test_version( 'sub { 1 }', undef );
147 test_version( 'use 5.006', version->new('5.006') );
148 test_version( 'use 5.8.3', version->new('5.8.3') );
149 test_version(
150 'use 5.006; use 5.8.3; use 5.005005',
151 version->new('5.8.3'),
152 );
81e74a91
ES
153 test_version( 'use 5.005_05; use 5.005_03', version->new('5.005_05') );
154 test_version( 'use 5.005_03; use 5.005_05', version->new('5.005_05') );
267b39b4
ES
155}
156
157sub test_version {
158 my ($code, $expected_version) = @_;
159
160 my $description_version = dor( $expected_version, '<undef>' );
161
162 my $document =
163 Perl::Critic::Document->new(
d533eee5 164 '-source' => PPI::Document->new( \$code )
267b39b4
ES
165 );
166
167 is(
168 $document->highest_explicit_perl_version(),
169 $expected_version,
170 qq<Get "$description_version" for "$code".>,
171 );
2a4245c2
ES
172
173 return;
267b39b4
ES
174}
175
176#-----------------------------------------------------------------------------
177
9e7d390d 178# ensure we return true if this test is loaded by
34cc6052
ES
179# t/08_document.t_without_optional_dependencies.t
1801;
181
737d3b65
CD
182# Local Variables:
183# mode: cperl
184# cperl-indent-level: 4
185# fill-column: 78
186# indent-tabs-mode: nil
187# c-indentation-style: bsd
188# End:
96fed375 189# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :