use Test::More;
use Filter::Keyword;
-sub ::dd {
- use Data::Dumper ();
- local $Data::Dumper::Useqq = 1;
- local $Data::Dumper::Terse = 1;
- my $out = Data::Dumper::Dumper($_[0]);
- chomp $out;
- return $out;
+my $shadowed_called = 0;
+sub shadowed ($&) {
+ my ($name, $sub) = @_;
+ $shadowed_called++;
+ is($name, 'fun', 'shadowed sub called with correct name');
+ is($sub->(), 'OH WHAT FUN', 'shadowed sub called with correct sub');
}
BEGIN {
parser => sub {
my $kw = shift;
if (my ($stripped, $matches) = $kw->match_source('', '{')) {
- my $name = $kw->current_match->[0];
- $stripped =~ s/{/; sub ${name} { my \$self = shift;/;
+ my $name = $kw->current_match;
+ $stripped =~ s/{/sub ${name} { my \$self = shift;/;
return ($stripped, 1);
}
else {
))->install;
(our $Kw2 = Filter::Keyword->new(
target_package => __PACKAGE__,
- keyword_name => 'function',
+ keyword_name => 'shadowed',
parser => sub {
my $kw = shift;
if (my ($stripped, $matches) = $kw->match_source('', '{')) {
- my $name = $kw->current_match->[0];
- $stripped =~ s/{/; sub ${name} {/;
+ my $name = $kw->current_match;
+ $stripped =~ s/{/shadowed "${name}", sub { BEGIN { Filter::Keyword::inject_after_scope(';') } /;
return ($stripped, 1);
}
else {
))->install;
}
-method yay { is(__LINE__, 38, 'line number correct inside method' ); "YAY $self" } is(__LINE__, 38, 'line number correct on same line after method');
+#line 1
+method yay { is(__LINE__, 1, 'line number correct inside keyword' ); "YAY $self" } is(__LINE__, 1, 'line number correct on same line after keyword');
+is(__LINE__, 2, 'line number correct after first keyword');
-is(__LINE__, 40, 'line number correct after first method');
+#line 1
+my $x = __LINE__ . " @{[ __LINE__ ]} method foo @{[ __LINE__ ]} bar baz " . __LINE__;
+is(__LINE__, 2, 'line number correct after string with keyword');
+is($x, '1 1 method foo 1 bar baz 1', 'line numbers in constructed string are correct');
-my $x = "method foo bar baz";
-
-is(__PACKAGE__->yay, 'YAY ' . __PACKAGE__, 'result of method correct');
+is(__PACKAGE__->yay, 'YAY ' . __PACKAGE__, 'result of keyword correct');
+#line 1
method spoon {
- is(__LINE__, 47, 'line number correct in multiline method');
+ is(__LINE__, 2, 'line number correct in multiline keyword');
'I HAZ A SPOON'
}
is(__PACKAGE__->spoon, 'I HAZ A SPOON', 'result of second method correct');
-function fun { is(__LINE__, 53, 'line number in function correct'); 'OH WHAT FUN' }
+#line 1
+shadowed fun { is(__LINE__, 1, 'line number correct inside second keyword'); 'OH WHAT FUN' }
-is(__PACKAGE__->fun, 'OH WHAT FUN', 'result of function correct');
+is($shadowed_called, 1, 'shadowed sub called only by filter output');
-is(__LINE__, 57, 'line number after function correct');
+is(__LINE__, 5, 'line number after shadowed correct');
done_testing;