| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
Test.pm-to-Test-More.pl - semi-automatically and partially convert Test.pm |
| 6 |
scripts to Test::More. |
| 7 |
|
| 8 |
=head1 USAGE |
| 9 |
|
| 10 |
perl Test.pm-to-Test-More.pl -o new.t t/old.t |
| 11 |
|
| 12 |
=head1 VERSION |
| 13 |
|
| 14 |
0.2.0 |
| 15 |
|
| 16 |
=cut |
| 17 |
|
| 18 |
use strict; |
| 19 |
use warnings; |
| 20 |
|
| 21 |
use Getopt::Long; |
| 22 |
use PPI; |
| 23 |
|
| 24 |
my $out_filename; |
| 25 |
my $inplace = ''; |
| 26 |
if (!GetOptions( |
| 27 |
'o|output=s' => \$out_filename, |
| 28 |
'inplace!' => \$inplace, |
| 29 |
)) |
| 30 |
{ |
| 31 |
die "Cannot process arguments."; |
| 32 |
} |
| 33 |
|
| 34 |
if ($inplace && defined($out_filename)) |
| 35 |
{ |
| 36 |
die 'Inplace is mutually exclusive with specifying an output file!'; |
| 37 |
} |
| 38 |
|
| 39 |
my $filename = shift(@ARGV); |
| 40 |
|
| 41 |
if ($inplace) |
| 42 |
{ |
| 43 |
$out_filename = $filename; |
| 44 |
} |
| 45 |
|
| 46 |
my $doc = PPI::Document->new($filename); |
| 47 |
|
| 48 |
my $statements = $doc->find('PPI::Statement'); |
| 49 |
|
| 50 |
if (! $statements) |
| 51 |
{ |
| 52 |
die "Could not find any statements."; |
| 53 |
} |
| 54 |
|
| 55 |
sub is_comma |
| 56 |
{ |
| 57 |
my $node = shift; |
| 58 |
return $node->isa('PPI::Token::Operator') && ($node->content() eq ","); |
| 59 |
} |
| 60 |
|
| 61 |
foreach my $stmt (@{$statements}) |
| 62 |
{ |
| 63 |
my $call = $stmt->child(0); |
| 64 |
if ($call->isa('PPI::Token::Word') |
| 65 |
&& ($call->literal() eq "ok") |
| 66 |
) |
| 67 |
{ |
| 68 |
# print "$stmt\n"; |
| 69 |
my $comment = PPI::Token::Comment->new; |
| 70 |
$comment->line(1); |
| 71 |
$comment->set_content ("# TEST\n"); |
| 72 |
|
| 73 |
my $which_to_prepend = $stmt; |
| 74 |
my $prev = $stmt->previous_sibling; |
| 75 |
if ($prev->isa('PPI::Token::Whitespace')) |
| 76 |
{ |
| 77 |
my $space = PPI::Token::Whitespace->new; |
| 78 |
$space->set_content($prev->content()); |
| 79 |
$prev->insert_before($space); |
| 80 |
$prev->insert_before($comment); |
| 81 |
} |
| 82 |
else |
| 83 |
{ |
| 84 |
$stmt->insert_before( $comment ); |
| 85 |
} |
| 86 |
|
| 87 |
my $args = $stmt->find_first('PPI::Structure::List')->find_first('PPI::Statement::Expression'); |
| 88 |
|
| 89 |
my $num_childs = scalar (() = $args->children()); |
| 90 |
|
| 91 |
my $num_args = 1 + scalar (() = grep { is_comma($_) } $args->children()); |
| 92 |
|
| 93 |
my $last_child = $args->child($num_childs - 1); |
| 94 |
if (is_comma($last_child) |
| 95 |
|| |
| 96 |
( |
| 97 |
$last_child->isa('PPI::Token::Whitespace') |
| 98 |
&& |
| 99 |
is_comma($args->child($num_childs - 2)) |
| 100 |
) |
| 101 |
) |
| 102 |
{ |
| 103 |
$num_args--; |
| 104 |
} |
| 105 |
|
| 106 |
if ( $num_args == 2) |
| 107 |
{ |
| 108 |
$call->set_content('is'); |
| 109 |
} |
| 110 |
|
| 111 |
my $test_op = PPI::Token::Operator->new(q{,}); |
| 112 |
my $test_ws = PPI::Token::Whitespace->new; |
| 113 |
$test_ws->set_content(' '); |
| 114 |
my $test_name = PPI::Token::Quote::Single->new(q{' TODO : Add test name'}); |
| 115 |
# $test_name->string(' TODO : Add test name'); |
| 116 |
$args->add_element($test_op); |
| 117 |
$args->add_element($test_ws); |
| 118 |
$args->add_element($test_name); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
$doc->save($out_filename); |
| 123 |
|
| 124 |
=begin removed |
| 125 |
|
| 126 |
{ |
| 127 |
my $out_fh; |
| 128 |
if (defined($out_filename)) |
| 129 |
{ |
| 130 |
open $out_fh, ">", $out_filename |
| 131 |
or die qq{Cannot open "$out_filename" for writing!}; |
| 132 |
} |
| 133 |
else |
| 134 |
{ |
| 135 |
open $out_fh, ">&STDOUT"; |
| 136 |
} |
| 137 |
|
| 138 |
print {$out_fh} "$doc"; |
| 139 |
|
| 140 |
close ($out_fh) |
| 141 |
} |
| 142 |
|
| 143 |
=end removed |
| 144 |
|
| 145 |
=cut |
| 146 |
|
| 147 |
=head1 COPYRIGHT & LICENSE |
| 148 |
|
| 149 |
Copyright 2011 by Shlomi Fish |
| 150 |
|
| 151 |
This program is distributed under the MIT (X11) License: |
| 152 |
L<http://www.opensource.org/licenses/mit-license.php> |
| 153 |
|
| 154 |
Permission is hereby granted, free of charge, to any person |
| 155 |
obtaining a copy of this software and associated documentation |
| 156 |
files (the "Software"), to deal in the Software without |
| 157 |
restriction, including without limitation the rights to use, |
| 158 |
copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 159 |
copies of the Software, and to permit persons to whom the |
| 160 |
Software is furnished to do so, subject to the following |
| 161 |
conditions: |
| 162 |
|
| 163 |
The above copyright notice and this permission notice shall be |
| 164 |
included in all copies or substantial portions of the Software. |
| 165 |
|
| 166 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 167 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 168 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 169 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 170 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 171 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 172 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 173 |
OTHER DEALINGS IN THE SOFTWARE. |
| 174 |
|
| 175 |
=cut |