root/trunk/Padre/t/71-perl.t @ 4007

Revision 4007, 6.4 KB (checked in by szabgab, 17 months ago)

update expected line numbers in test

Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5#use Test::NeedsDisplay;
6use Test::More;
7BEGIN {
8        if (not $ENV{DISPLAY} and not $^O eq 'MSWin32') {
9                plan skip_all => 'Needs DISPLAY';
10                exit 0;
11        }
12}
13
14use Test::NoWarnings;
15use File::Spec::Functions ':ALL';
16
17# Padre can move the cwd around, so lock in the location of the
18# test files early before that happens
19my $files = rel2abs( catdir( 't', 'files' ) );
20
21my $tests;
22plan tests => $tests + 1;
23
24use t::lib::Padre;
25use t::lib::Padre::Editor;
26
27use Padre::Document;
28use Padre::PPI;
29use PPI::Document;
30
31# Create the object so that Padre->ide works
32my $app = Padre->new;
33isa_ok($app, 'Padre');
34BEGIN { $tests += 1; }
35
36SCOPE: {
37        my $editor = t::lib::Padre::Editor->new;
38        my $file   = catfile( $files, 'missing_brace_1.pl' );
39        my $doc    = Padre::Document->new(
40                filename  => $file,
41        );
42        $doc->set_editor($editor);
43        $editor->configure_editor($doc);
44
45        my $msgs = $doc->check_syntax;
46        is_deeply ($msgs, [
47           {
48             'msg' => 'Missing right curly or square bracket, at end of line',
49             'severity' => 'E',
50             'line' => '17'
51           },
52           {
53             'msg' => 'syntax error, at EOF',
54             'severity' => 'E',
55             'line' => '17'
56           }
57        ]);
58
59        isa_ok($doc, 'Padre::Document');
60        isa_ok($doc, 'Padre::Document::Perl');
61        is($doc->filename, $file, 'filename');
62
63        #Padre::PPI::find_unmatched_brace();
64        BEGIN { $tests += 4; }
65}
66
67
68
69
70
71# first block of tests for Padre::PPI::find_variable_declaration
72# and ...find_token_at_location
73SCOPE: {
74        my $infile = catfile( $files, 'find_variable_declaration_1.pm' );
75        my $text = do { local $/=undef; open my $fh, '<', $infile or die $!; <$fh> };
76 
77        my $doc = PPI::Document->new( \$text );
78        isa_ok($doc, "PPI::Document");
79        $doc->index_locations;
80 
81        my $elem = find_var_simple($doc, '$n_threads_to_kill', 137);
82        isa_ok( $elem, 'PPI::Token::Symbol' );
83 
84        $doc->flush_locations(); # TODO: This shouldn't have to be here. But remove it and things break -- Adam?
85        #my $doc2 = PPI::Document->new( \$text );
86        my $cmp_elem = Padre::PPI::find_token_at_location($doc, [137, 26, 26]);
87        ok( $elem == $cmp_elem, 'find_token_at_location returns the same token as a manual search' );
88        my $declaration;
89        $doc->find_first(
90                sub {
91                        return 0 if not $_[1]->isa('PPI::Statement::Variable')
92                                 or not $_[1]->location->[0] == 131;
93                        $declaration = $_[1];
94                        return 1;
95                }
96        );
97        isa_ok( $declaration, 'PPI::Statement::Variable' );
98 
99        $doc->flush_locations(); # TODO: This shouldn't have to be here. But remove it and things break -- Adam?
100        my $cmp_declaration = Padre::PPI::find_token_at_location($doc, [131, 2, 9]);
101        # They're not really the same. The manual search finds the entire Statement node. Hence the first_element.
102        ok( $declaration->first_element() == $cmp_declaration, 'find_token_at_location returns the same token as a manual search' );
103
104        my $result_declaration = Padre::PPI::find_variable_declaration($elem);
105
106        ok( $declaration == $result_declaration, 'Correct declaration found');
107
108        BEGIN { $tests += 6; }
109}
110
111# second block of tests for Padre::PPI::find_variable_declaration
112# and ...find_token_at_location
113SCOPE: {
114        my $infile = catfile( $files, 'find_variable_declaration_2.pm' );
115        my $text = do { local $/=undef; open my $fh, '<', $infile or die $!; <$fh> };
116 
117        my $doc = PPI::Document->new( \$text );
118        isa_ok($doc, "PPI::Document");
119        $doc->index_locations;
120 
121        # Test foreach my $i
122        my $elem = find_var_simple($doc, '$i', 8); # search $i in line 8
123        isa_ok( $elem, 'PPI::Token::Symbol' );
124 
125        $doc->flush_locations(); # TODO: This shouldn't have to be here. But remove it and things break -- Adam?
126        my $cmp_elem = Padre::PPI::find_token_at_location($doc, [8, 5, 5]);
127        ok( $elem == $cmp_elem, 'find_token_at_location returns the same token as a manual search' );
128
129        $doc->flush_locations(); # TODO: This shouldn't have to be here. But remove it and things break -- Adam?
130        my $declaration = Padre::PPI::find_token_at_location($doc, [7, 14, 14]);
131        isa_ok( $declaration, 'PPI::Token::Symbol' );
132        my $prev_sibling = $declaration->sprevious_sibling();
133        ok(
134                (defined($prev_sibling) and $prev_sibling->isa('PPI::Token::Word')
135                and $prev_sibling->content() =~ /^(?:my|our)$/),
136                "Find variable declaration in foreach"
137        );
138 
139        $doc->flush_locations(); # TODO: This shouldn't have to be here. But remove it and things break -- Adam?
140        my $result_declaration = Padre::PPI::find_variable_declaration($elem);
141        ok( $declaration == $result_declaration, 'Correct declaration found');
142
143        # Now the same for "for our $k"
144        $elem = find_var_simple($doc, '$k', 11); # search $k in line 11
145        isa_ok( $elem, 'PPI::Token::Symbol' );
146 
147        $doc->flush_locations(); # TODO: This shouldn't have to be here. But remove it and things break -- Adam?
148        $cmp_elem = Padre::PPI::find_token_at_location($doc, [11, 5, 5]);
149        ok( $elem == $cmp_elem, 'find_token_at_location returns the same token as a manual search' );
150
151        $doc->flush_locations(); # TODO: This shouldn't have to be here. But remove it and things break -- Adam?
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(
156                (defined($prev_sibling) and $prev_sibling->isa('PPI::Token::Word')
157                and $prev_sibling->content() =~ /^(?:my|our)$/),
158                "Find variable declaration in foreach"
159        );
160 
161        $doc->flush_locations(); # TODO: This shouldn't have to be here. But remove it and things break -- Adam?
162        TODO: {
163                local $TODO = "PPI parses 'for our \$foo (...){}' wrongly";
164                $result_declaration = Padre::PPI::find_variable_declaration($elem);
165                ok( $declaration == $result_declaration, 'Correct declaration found');
166        }
167
168        BEGIN { $tests += 11; }
169}
170
171
172
173# Test for check_syntax
174SCOPE: {
175        my $editor = t::lib::Padre::Editor->new;
176        my $file   = catfile( $files, 'one_char.pl' );
177        my $doc    = Padre::Document->new(
178                filename  => $file,
179        );
180        $doc->set_editor($editor);
181        $editor->configure_editor($doc);
182
183        my $msgs = $doc->check_syntax;
184        my $end  = $msgs->[-1];
185        is_deeply(
186                $end,
187                {
188                        'msg'      => 'Useless use of a constant in void context',
189                        'severity' => 'W',
190                        'line'     => '1',
191                }
192        );
193        BEGIN { $tests += 1; }
194}
195
196
197
198sub find_var_simple {
199        my $doc = shift;
200        my $varname = shift;
201        my $line = shift;
202
203        my $elem;
204        $doc->find_first(
205                sub {
206                        return 0 if not $_[1]->isa('PPI::Token::Symbol')
207                                 or not $_[1]->content eq $varname
208                                 or not $_[1]->location->[0] == $line;
209                        $elem = $_[1];
210                        return 1;
211                }
212        );
213        return $elem;
214}
215
216
Note: See TracBrowser for help on using the browser.