| 1 | #!/usr/bin/eval perl |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use English '-no_match_vars'; |
|---|
| 5 | |
|---|
| 6 | BEGIN { |
|---|
| 7 | $WARNING = 1; |
|---|
| 8 | } |
|---|
| 9 | use Test::More tests => 47; |
|---|
| 10 | use Test::Exception; |
|---|
| 11 | ###### |
|---|
| 12 | # let's check our requirments. |
|---|
| 13 | ###### |
|---|
| 14 | use_ok('Carp'); |
|---|
| 15 | |
|---|
| 16 | ### you could do this but why? |
|---|
| 17 | # |
|---|
| 18 | #use Carp qw(croak); |
|---|
| 19 | #eval( use_ok( 'PPI', '1.215' ) ); |
|---|
| 20 | #if ($EVAL_ERROR) { croak; } |
|---|
| 21 | # |
|---|
| 22 | ### when you can just do this |
|---|
| 23 | use_ok( 'PPI', '1.215' ); |
|---|
| 24 | use_ok('PPI::Find'); |
|---|
| 25 | use_ok( 'Class::XSAccessor', '1.02' ); |
|---|
| 26 | use_ok('File::Spec'); |
|---|
| 27 | use_ok('File::Basename'); |
|---|
| 28 | use_ok('File::Find'); |
|---|
| 29 | use_ok('File::Temp'); |
|---|
| 30 | use_ok( 'Test::More', '0.88' ); |
|---|
| 31 | |
|---|
| 32 | #use_ok( 'Test::Most'); # craps out !! |
|---|
| 33 | use_ok('Test::Differences'); |
|---|
| 34 | |
|---|
| 35 | #use_ok( 'Test::NoWarnings', '0.084'); # craps out !! |
|---|
| 36 | |
|---|
| 37 | ###### |
|---|
| 38 | # Testing PPIx::EditorTools |
|---|
| 39 | ###### |
|---|
| 40 | my $pet_obj; |
|---|
| 41 | require PPIx::EditorTools; |
|---|
| 42 | $pet_obj = new_ok('PPIx::EditorTools'); |
|---|
| 43 | |
|---|
| 44 | my @subs = |
|---|
| 45 | qw( new code ppi process_doc find_unmatched_brace get_all_variable_declarations element_depth find_token_at_location find_variable_declaration ); |
|---|
| 46 | use_ok( 'PPIx::EditorTools', @subs ); |
|---|
| 47 | |
|---|
| 48 | foreach my $subs (@subs) { |
|---|
| 49 | can_ok( 'PPIx::EditorTools', $subs ); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | #TODO need more pkg tests |
|---|
| 53 | use PPIx::EditorTools; |
|---|
| 54 | my $space = q{ }; # space |
|---|
| 55 | my $empty = q{}; # empty string |
|---|
| 56 | my $one = q{1}; |
|---|
| 57 | |
|---|
| 58 | ####### |
|---|
| 59 | # Testing PPIx::EditorTools->process_doc() |
|---|
| 60 | ####### |
|---|
| 61 | # Check that something died - we do not care why |
|---|
| 62 | dies_ok { PPIx::EditorTools->process_doc() } 'expecting PPIx::EditorTools->process_doc() to die'; |
|---|
| 63 | |
|---|
| 64 | # check code to ppi |
|---|
| 65 | my @test_files = ( |
|---|
| 66 | { filename => 't/outline/Foo.pm', }, |
|---|
| 67 | { filename => 't/outline/file1.pl', }, |
|---|
| 68 | { filename => 't/outline/file2.pl', }, |
|---|
| 69 | { filename => 't/outline/Mooclass.pm', }, |
|---|
| 70 | { filename => 't/outline/Moorole.pm', }, |
|---|
| 71 | { filename => 't/outline/Moofirst.pm', }, |
|---|
| 72 | ); |
|---|
| 73 | my $obj = PPIx::EditorTools->new(); |
|---|
| 74 | $obj->ppi(undef); |
|---|
| 75 | $obj->code(undef); |
|---|
| 76 | my $code; |
|---|
| 77 | foreach my $file (@test_files) { |
|---|
| 78 | ##my $code; |
|---|
| 79 | if ( $file->{filename} ) { |
|---|
| 80 | open my $file_handle, '<', $file->{filename} or die; |
|---|
| 81 | local $INPUT_RECORD_SEPARATOR = undef; |
|---|
| 82 | $code = <$file_handle>; |
|---|
| 83 | } |
|---|
| 84 | ok( $obj->process_doc( code => $code ), |
|---|
| 85 | "process_doc(code) from $file->{filename}" |
|---|
| 86 | ); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | ## check ppi source |
|---|
| 90 | my %ppi = ( |
|---|
| 91 | 'attributes' => [ |
|---|
| 92 | { 'line' => 7, |
|---|
| 93 | 'name' => 'balance', |
|---|
| 94 | }, |
|---|
| 95 | { 'line' => 13, |
|---|
| 96 | 'name' => 'overdraft', |
|---|
| 97 | }, |
|---|
| 98 | { 'line' => 23, |
|---|
| 99 | 'name' => 'name', |
|---|
| 100 | }, |
|---|
| 101 | { 'line' => 25, |
|---|
| 102 | 'name' => 'account', |
|---|
| 103 | }, |
|---|
| 104 | ], |
|---|
| 105 | 'line' => 3, |
|---|
| 106 | 'methods' => [ |
|---|
| 107 | { 'line' => 27, |
|---|
| 108 | 'name' => '_build_overdraft', |
|---|
| 109 | }, |
|---|
| 110 | ], |
|---|
| 111 | 'modules' => [ |
|---|
| 112 | { 'line' => 1, |
|---|
| 113 | 'name' => 'MooseX::Declare', |
|---|
| 114 | }, |
|---|
| 115 | ], |
|---|
| 116 | 'name' => 'Moofirst', |
|---|
| 117 | 'pragmata' => [ |
|---|
| 118 | { 'line' => 5, |
|---|
| 119 | 'name' => 'version', |
|---|
| 120 | }, |
|---|
| 121 | ], |
|---|
| 122 | ); |
|---|
| 123 | |
|---|
| 124 | $obj->ppi('PPI::Document'); |
|---|
| 125 | $obj->code(undef); |
|---|
| 126 | ok( $obj->process_doc(%ppi), 'process_doc(ppi)' ); |
|---|
| 127 | ## check neither ppi or code fails |
|---|
| 128 | $obj->ppi(undef); |
|---|
| 129 | $obj->code(undef); |
|---|
| 130 | my %case = ( one => 'ppi', two => 'code', three => 'PPI::Document', ); |
|---|
| 131 | throws_ok { $obj->process_doc(%case) } '/arguments ppi or code required/', 'arguments ppi or code required'; |
|---|
| 132 | |
|---|
| 133 | ######## |
|---|
| 134 | # Testing PPIx::EditorTools->find_unmatched_brace() |
|---|
| 135 | ######## |
|---|
| 136 | dies_ok { PPIx::EditorTools->find_unmatched_brace() } 'expecting PPIx::EditorTools->find_unmatched_brace() to die'; |
|---|
| 137 | |
|---|
| 138 | TODO: |
|---|
| 139 | { |
|---|
| 140 | local $TODO = "PPIx::EditorTools->find_unmatched_brace() is untesable at it allwas returns \'\'"; |
|---|
| 141 | |
|---|
| 142 | $code = 'sub foo {\n1;\n}\n} # <--- This is an unmatched brace'; |
|---|
| 143 | $obj->ppi('PPI::Statement::UnmatchedBrace'); |
|---|
| 144 | is( $obj->find_unmatched_brace( code => $code ), |
|---|
| 145 | $one, "should have return $one" |
|---|
| 146 | ); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | $code = 'sub foo {\n1;\n}\n#} # <--- This is not an unmatched brace'; |
|---|
| 150 | $obj->ppi('PPI::Structure'); |
|---|
| 151 | is( $obj->find_unmatched_brace( code => $code ), $empty, 'spurious result' ); |
|---|
| 152 | |
|---|
| 153 | $code = $space; |
|---|
| 154 | $obj->ppi(undef); |
|---|
| 155 | is( $obj->find_unmatched_brace( code => $code ), $empty, 'spurious result' ); |
|---|
| 156 | |
|---|
| 157 | ######## |
|---|
| 158 | # Testing PPIx::EditorTools->get_all_variable_declarations() |
|---|
| 159 | ######## |
|---|
| 160 | dies_ok { PPIx::EditorTools->get_all_variable_declarations() } |
|---|
| 161 | 'expecting PPIx::EditorTools->get_all_variable_declarations() to die'; |
|---|
| 162 | |
|---|
| 163 | # additional tests located in t/08-getallvariabledeclarations.t |
|---|
| 164 | |
|---|
| 165 | ######## |
|---|
| 166 | # Testing PPIx::EditorTools->element_depth() |
|---|
| 167 | ######## |
|---|
| 168 | #TODO add more tests |
|---|
| 169 | dies_ok { PPIx::EditorTools->element_depth() } 'expecting PPIx::EditorTools->element_depth() to die'; |
|---|
| 170 | |
|---|
| 171 | ######## |
|---|
| 172 | # Testing PPIx::EditorTools->find_token_at_location() |
|---|
| 173 | ######## |
|---|
| 174 | #TODO add more tests |
|---|
| 175 | dies_ok { PPIx::EditorTools->find_token_at_location() } 'expecting PPIx::EditorTools->find_token_at_location() to die'; |
|---|
| 176 | |
|---|
| 177 | ######## |
|---|
| 178 | # Testing PPIx::EditorTools->find_variable_declaration() |
|---|
| 179 | ######## |
|---|
| 180 | #TODO add more tests |
|---|
| 181 | #dies_ok { PPIx::EditorTools->find_variable_declaration() } 'expecting PPIx::EditorTools->find_variable_declaration() to die'; |
|---|
| 182 | |
|---|
| 183 | ###### |
|---|
| 184 | # let's check our lib's are here. |
|---|
| 185 | ###### |
|---|
| 186 | my $test_object; |
|---|
| 187 | require PPIx::EditorTools::FindUnmatchedBrace; |
|---|
| 188 | $test_object = new_ok('PPIx::EditorTools::FindUnmatchedBrace'); |
|---|
| 189 | |
|---|
| 190 | require PPIx::EditorTools::FindVariableDeclaration; |
|---|
| 191 | $test_object = new_ok('PPIx::EditorTools::FindVariableDeclaration'); |
|---|
| 192 | |
|---|
| 193 | require PPIx::EditorTools::IntroduceTemporaryVariable; |
|---|
| 194 | $test_object = new_ok('PPIx::EditorTools::IntroduceTemporaryVariable'); |
|---|
| 195 | |
|---|
| 196 | require PPIx::EditorTools::RenamePackage; |
|---|
| 197 | $test_object = new_ok('PPIx::EditorTools::RenamePackage'); |
|---|
| 198 | |
|---|
| 199 | require PPIx::EditorTools::RenamePackageFromPath; |
|---|
| 200 | $test_object = new_ok('PPIx::EditorTools::RenamePackageFromPath'); |
|---|
| 201 | |
|---|
| 202 | require PPIx::EditorTools::RenameVariable; |
|---|
| 203 | $test_object = new_ok('PPIx::EditorTools::RenameVariable'); |
|---|
| 204 | |
|---|
| 205 | require PPIx::EditorTools::FindUnmatchedBrace; |
|---|
| 206 | $test_object = new_ok('PPIx::EditorTools::FindUnmatchedBrace'); |
|---|
| 207 | |
|---|
| 208 | require PPIx::EditorTools::Outline; |
|---|
| 209 | $test_object = new_ok('PPIx::EditorTools::Outline'); |
|---|
| 210 | |
|---|
| 211 | require PPIx::EditorTools::Lexer; |
|---|
| 212 | $test_object = new_ok('PPIx::EditorTools::Lexer'); |
|---|
| 213 | |
|---|
| 214 | require PPIx::EditorTools::ReturnObject; |
|---|
| 215 | $test_object = new_ok('PPIx::EditorTools::ReturnObject'); |
|---|
| 216 | |
|---|
| 217 | done_testing() |
|---|