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