8
unless ( $ENV{DISPLAY} or $^O eq 'MSWin32' ) {
9
plan skip_all => 'Needs DISPLAY';
15
use File::Spec::Functions ':ALL';
17
# Padre can move the cwd around, so save in the location of the
18
# test files early before that happens
19
my $files = rel2abs( catdir( 't', 'files' ) );
22
use t::lib::Padre::Editor;
29
# Create the object so that ide works
31
isa_ok( $app, 'Padre' );
34
my $editor = t::lib::Padre::Editor->new;
35
my $file = catfile( $files, 'missing_brace_1.pl' );
36
my $doc = Padre::Document->new(
39
$doc->set_editor($editor);
40
$editor->set_document($doc);
45
like( $row->{message}, $arg{message}, "message regex match in '$arg{test_name}'" );
46
is( $row->{line}, $arg{line}, "line match in '$arg{test_name}'" );
47
is( $row->{type}, $arg{type}, "type match in '$arg{test_name}'" );
50
isa_ok( $doc, 'Padre::Document' );
51
isa_ok( $doc, 'Padre::Document::Perl' );
52
is( $doc->filename, $file, 'filename' );
55
# first block of tests for Padre::PPI::find_variable_declaration
56
# and ...find_token_at_location
58
my $infile = catfile( $files, 'find_variable_declaration_1.pm' );
61
open my $fh, '<', $infile or die $!;
66
my $doc = PPI::Document->new( \$text );
67
isa_ok( $doc, "PPI::Document" );
68
$doc->index_locations;
70
my $elem = find_var_simple( $doc, '$n_threads_to_kill', 137 );
71
isa_ok( $elem, 'PPI::Token::Symbol' );
73
$doc->flush_locations(); # TODO: This shouldn't have to be here. But remove it and things break -- Adam?
74
#my $doc2 = PPI::Document->new( \$text );
75
my $cmp_elem = Padre::PPI::find_token_at_location( $doc, [ 137, 26, 26 ] );
76
ok( $elem == $cmp_elem, 'find_token_at_location returns the same token as a manual search' );
81
if not $_[1]->isa('PPI::Statement::Variable')
82
or not $_[1]->location->[0] == 131;
87
isa_ok( $declaration, 'PPI::Statement::Variable' );
89
$doc->flush_locations(); # TODO: This shouldn't have to be here. But remove it and things break -- Adam?
90
my $cmp_declaration = Padre::PPI::find_token_at_location( $doc, [ 131, 2, 9 ] );
92
# They're not really the same. The manual search finds the entire Statement node. Hence the first_element.
93
ok( $declaration->first_element() == $cmp_declaration,
94
'find_token_at_location returns the same token as a manual search'
97
my $result_declaration = Padre::PPI::find_variable_declaration($elem);
99
ok( $declaration == $result_declaration, 'Correct declaration found' );
102
# second block of tests for Padre::PPI::find_variable_declaration
103
# and ...find_token_at_location
105
my $infile = catfile( $files, 'find_variable_declaration_2.pm' );
108
open my $fh, '<', $infile or die $!;
114
my $doc = PPI::Document->new( \$text );
115
isa_ok( $doc, "PPI::Document" );
116
$doc->index_locations;
119
my $elem = find_var_simple( $doc, '$i', 8 ); # search $i in line 8
120
isa_ok( $elem, 'PPI::Token::Symbol' );
122
$doc->flush_locations(); # TODO: This shouldn't have to be here. But remove it and things break -- Adam?
123
my $cmp_elem = Padre::PPI::find_token_at_location( $doc, [ 8, 5, 5 ] );
124
ok( $elem == $cmp_elem, 'find_token_at_location returns the same token as a manual search' );
126
$doc->flush_locations(); # TODO: This shouldn't have to be here. But remove it and things break -- Adam?
127
my $declaration = Padre::PPI::find_token_at_location( $doc, [ 7, 14, 14 ] );
128
isa_ok( $declaration, 'PPI::Token::Symbol' );
129
my $prev_sibling = $declaration->sprevious_sibling();
130
ok( ( defined($prev_sibling)
131
and $prev_sibling->isa('PPI::Token::Word')
132
and $prev_sibling->content() =~ /^(?:my|our)$/
134
"Find variable declaration in foreach"
137
$doc->flush_locations(); # TODO: This shouldn't have to be here. But remove it and things break -- Adam?
138
my $result_declaration = Padre::PPI::find_variable_declaration($elem);
139
ok( $declaration == $result_declaration, 'Correct declaration found' );
141
# Now the same for "for our $k"
142
$elem = find_var_simple( $doc, '$k', 11 ); # search $k in line 11
143
isa_ok( $elem, 'PPI::Token::Symbol' );
145
# TODO: This shouldn't have to be here. But remove it and things break -- Adam?
146
$doc->flush_locations();
147
$cmp_elem = Padre::PPI::find_token_at_location( $doc, [ 11, 5, 5 ] );
148
ok( $elem == $cmp_elem, 'find_token_at_location returns the same token as a manual search' );
150
# TODO: This shouldn't have to be here. But remove it and things break -- Adam?
151
$doc->flush_locations();
152
$declaration = Padre::PPI::find_token_at_location( $doc, [ 10, 11, 11 ] );
153
isa_ok( $declaration, 'PPI::Token::Symbol' );
154
$prev_sibling = $declaration->sprevious_sibling();
155
ok( ( defined($prev_sibling)
156
and $prev_sibling->isa('PPI::Token::Word')
157
and $prev_sibling->content() =~ /^(?:my|our)$/
159
"Find variable declaration in foreach"
162
# TODO: This shouldn't have to be here. But remove it and things break -- Adam?
163
$doc->flush_locations();
165
skip( "PPI parses 'for our \$foo (...){}' badly", 1 );
166
$result_declaration = Padre::PPI::find_variable_declaration($elem);
167
ok( $declaration == $result_declaration, 'Correct declaration found' );
171
# Regression test for functions
173
my $editor = t::lib::Padre::Editor->new;
174
my $file = catfile( $files, 'perl_functions.pl' );
175
my $doc = Padre::Document->new(
178
$doc->set_editor($editor);
179
$editor->set_document($doc);
181
my @functions = $doc->functions;
185
guess_indentation_style
193
'Found expected Perl functions',
197
# Regression test for functions on Method::Signatures-style method declarators
200
{ 'filename' => 'method_declarator_1.pm',
203
_build__ca_state_holidays
204
is_holiday_or_weekend
208
{ 'filename' => 'method_declarator_2.pm',
216
{ 'filename' => 'method_declarator_3.pm',
225
foreach my $test_file (@test_files) {
226
my $editor = t::lib::Padre::Editor->new;
227
my $file = catfile( $files, $test_file->{'filename'} );
228
my $doc = Padre::Document->new(
231
$doc->set_editor($editor);
232
$editor->set_document($doc);
234
my @functions = $doc->functions;
237
$test_file->{'methods'},
238
'Found expected declarator-declared Perl functions',
243
# Tests for content intuition
245
my $editor = t::lib::Padre::Editor->new;
246
my $doc = Padre::Document::Perl->new;
247
$doc->set_editor($editor);
248
$editor->set_document($doc);
249
$doc->text_set(<<'END_PERL');
250
package Foo::Bar::Baz;
256
my $filename = $doc->guess_filename;
257
is( $filename, 'Baz.pm', '->guess_filename ok' );
260
my @subpath = $doc->guess_subpath;
261
is_deeply( \@subpath, [qw{ lib Foo Bar }], '->guess_subpath' );
264
# Test POD endification
266
use_ok('Padre::PPI::EndifyPod');
267
my $merge = Padre::PPI::EndifyPod->new;
268
isa_ok( $merge, 'Padre::PPI::EndifyPod' );
269
my $document = PPI::Document->new( \<<'END_PERL' );
288
isa_ok( $document, 'PPI::Document' );
289
ok( $merge->apply($document), 'Transform applied ok' );
290
is( $document->serialize, <<'END_PERL', 'Transformed ok' );
311
# Test copyright updating
313
use_ok('Padre::PPI::UpdateCopyright');
314
my $copyright = Padre::PPI::UpdateCopyright->new(
315
name => 'Adam Kennedy',
317
isa_ok( $copyright, 'Padre::PPI::UpdateCopyright' );
318
my $document = PPI::Document->new( \<<'END_PERL' );
323
Copyright 2008 - 2009 Adam Kennedy.
329
isa_ok( $document, 'PPI::Document' );
330
ok( $copyright->apply($document), 'Transform applied ok' );
331
my $serialized = $document->serialize;
332
ok( $serialized =~ /2008 - (\d\d\d\d)/, 'Found copyright statement' );
333
ok( $1 ne '2009', 'Copyright year has changed' );
334
ok( $1 > 2009, 'Copyright year is newer' );
341
######################################################################
344
sub find_var_simple {
353
if not $_[1]->isa('PPI::Token::Symbol')
354
or not $_[1]->content eq $varname
355
or not $_[1]->location->[0] == $line;