Login
Make Test::Deep required so that I can use it for 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
ES
18
19use Perl::Critic::Document;
267b39b4
ES
20use Perl::Critic::Utils::DataConversion qw< dor >;
21
f3b652c3
ES
22
23use Test::More tests => 33;
b22c8627
JRT
24
25#-----------------------------------------------------------------------------
26
c263f914 27our $VERSION = '1.108';
b185fa17
ES
28
29#-----------------------------------------------------------------------------
30
b22c8627 31can_ok('Perl::Critic::Document', 'new');
267b39b4 32can_ok('Perl::Critic::Document', 'filename');
b22c8627 33can_ok('Perl::Critic::Document', 'find');
3d6744c8
JRT
34can_ok('Perl::Critic::Document', 'find_first');
35can_ok('Perl::Critic::Document', 'find_any');
267b39b4
ES
36can_ok('Perl::Critic::Document', 'highest_explicit_perl_version');
37can_ok('Perl::Critic::Document', 'ppi_document');
1b936936 38can_ok('Perl::Critic::Document', 'is_program');
d533eee5 39can_ok('Perl::Critic::Document', 'is_module');
b22c8627
JRT
40
41{
42 my $code = q{'print 'Hello World';}; #Has 6 PPI::Element
43 my $ppi_doc = PPI::Document->new( \$code );
d533eee5 44 my $pc_doc = Perl::Critic::Document->new( '-source' => $ppi_doc );
b22c8627 45 isa_ok($pc_doc, 'Perl::Critic::Document');
a08d9933
ES
46 isa_ok($pc_doc, 'PPI::Document');
47 isa_ok($pc_doc, 'PPI::Node');
48 isa_ok($pc_doc, 'PPI::Element');
b22c8627
JRT
49
50
51 my $nodes_ref = $pc_doc->find('PPI::Element');
3d6744c8 52 is( scalar @{ $nodes_ref }, 6, 'find by class name');
b22c8627
JRT
53
54 $nodes_ref = $pc_doc->find( sub{ return 1 } );
3d6744c8 55 is( scalar @{ $nodes_ref }, 6, 'find by wanted() handler');
b22c8627 56
7d1077fa 57 $nodes_ref = $pc_doc->find( q{Element} );
3d6744c8
JRT
58 is( scalar @{ $nodes_ref }, 6, 'find by shortened class name');
59
60 #---------------------------
61
62 my $node = $pc_doc->find_first('PPI::Element');
63 is( ref $node, 'PPI::Statement', 'find_first by class name');
64
65 $node = $pc_doc->find_first( sub{ return 1 } );
66 is( ref $node, 'PPI::Statement', 'find_first by wanted() handler');
67
68 $node = $pc_doc->find_first( q{Element} );
69 is( ref $node, 'PPI::Statement', 'find_first by shortened class name');
70
71 #---------------------------
72
73 my $found = $pc_doc->find_any('PPI::Element');
74 is( $found, 1, 'find_any by class name');
75
76 $found = $pc_doc->find_any( sub{ return 1 } );
77 is( $found, 1, 'find_any by wanted() handler');
78
79 $found = $pc_doc->find_any( q{Element} );
80 is( $found, 1, 'find_any by shortened class name');
81
82 #-------------------------------------------------------------------------
7d1077fa
CD
83
84 {
85 # Ignore "Cannot create search condition for 'PPI::': Not a PPI::Element"
d91a8cdf 86 local $SIG{__WARN__} = sub {
369d6f3a 87 $_[0] =~ m/\QCannot create search condition for\E/xms || carp @_
d91a8cdf 88 };
7d1077fa 89 $nodes_ref = $pc_doc->find( q{} );
3d6744c8
JRT
90 is( $nodes_ref, undef, 'find by empty class name');
91
92 $node = $pc_doc->find_first( q{} );
93 is( $node, undef, 'find_first by empty class name');
94
95 $found = $pc_doc->find_any( q{} );
96 is( $found, undef, 'find_any by empty class name');
97
7d1077fa 98 }
d533eee5
ES
99
100 #-------------------------------------------------------------------------
101
d533eee5 102 ok( $pc_doc->is_module(), q{document type 'module' is a module});
1b936936 103 ok( ! $pc_doc->is_program(), q{document type 'module' is not a program});
d533eee5 104
b22c8627 105}
737d3b65 106
34cc6052
ES
107#-----------------------------------------------------------------------------
108
267b39b4
ES
109{
110 test_version( 'sub { 1 }', undef );
111 test_version( 'use 5.006', version->new('5.006') );
112 test_version( 'use 5.8.3', version->new('5.8.3') );
113 test_version(
114 'use 5.006; use 5.8.3; use 5.005005',
115 version->new('5.8.3'),
116 );
81e74a91
ES
117 test_version( 'use 5.005_05; use 5.005_03', version->new('5.005_05') );
118 test_version( 'use 5.005_03; use 5.005_05', version->new('5.005_05') );
267b39b4
ES
119}
120
121sub test_version {
122 my ($code, $expected_version) = @_;
123
124 my $description_version = dor( $expected_version, '<undef>' );
125
126 my $document =
127 Perl::Critic::Document->new(
d533eee5 128 '-source' => PPI::Document->new( \$code )
267b39b4
ES
129 );
130
131 is(
132 $document->highest_explicit_perl_version(),
133 $expected_version,
134 qq<Get "$description_version" for "$code".>,
135 );
2a4245c2
ES
136
137 return;
267b39b4
ES
138}
139
140#-----------------------------------------------------------------------------
141
9e7d390d 142# ensure we return true if this test is loaded by
34cc6052
ES
143# t/08_document.t_without_optional_dependencies.t
1441;
145
737d3b65
CD
146# Local Variables:
147# mode: cperl
148# cperl-indent-level: 4
149# fill-column: 78
150# indent-tabs-mode: nil
151# c-indentation-style: bsd
152# End:
96fed375 153# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :