1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#!/usr/bin/perl -w
use strict;
use warnings;
use Test::More 'no_plan';
use Method::Signatures::Signature;
my %tests = (
'$foo' => ['$foo'],
'$foo, $bar' => ['$foo', '$bar'],
':$foo, $bar?' => [':$foo', '$bar?'],
'' => [],
'$sum = 2+2, $div = 2/2' => ['$sum = 2+2', '$div = 2/2'],
'$foo = "Hello, world!"' => ['$foo = "Hello, world!"'],
'@args = (1,2,3)' => ['@args = (1,2,3)'],
'$foo = [1,2,3], $bar = { this => 23, that => 42 }' => [
'$foo = [1,2,3]', '$bar = { this => 23, that => 42 }'
],
'$code = sub { my $bar = 2+2; }, :$this' => ['$code = sub { my $bar = 2+2; }', ':$this'],
'$foo, $, $bar, $ = $,, $' => [
'$foo', '$', '$bar', '$ = $,', '$'
],
'$foo, @' => ['$foo', '@'],
'$foo, %' => ['$foo', '%'],
q[
$num = 42,
$string = q[Hello, world!],
$hash = { this => 42, that => 23 },
$code = sub { $num + 4 },
@nums = (1,2,3)
] =>
[
'$num = 42',
'$string = q[Hello, world!]',
'$hash = { this => 42, that => 23 }',
'$code = sub { $num + 4 }',
'@nums = (1,2,3)'
],
);
while(my($args, $expect) = each %tests) {
test_tokenize_sig( $args, $expect );
}
SKIP: {
skip q(Perl 5.10 or later needed to test 'where' constraints), 1 if $] < 5.010;
test_tokenize_sig(
'Int $ where { $_ < 10 } = 4',
[ 'Int $ where { $_ < 10 } = 4' ]
);
};
sub test_tokenize_sig {
my($args, $expect) = @_;
my $sig = Method::Signatures::Signature->new(
signature_string => $args,
# we just want to test the tokenizing
no_checks => 1,
);
is_deeply [map { $_->original_code } @{$sig->parameters}], $expect, "parameters - $args";
}
note "line numbers"; {
my $code = q[
$num = 42,
$hash = { this => 42,
that => 23 },
$code = sub { $num + 4 },
];
my $sig = Method::Signatures::Signature->new(
signature_string => $code,
no_checks => 1,
);
my $parameters = $sig->parameters;
is $parameters->[0]->first_line_number, 2;
is $parameters->[1]->first_line_number, 3;
is $parameters->[2]->first_line_number, 5;
}
|