refactor test
[p5sagit/Filter-Keyword.git] / t / simple.t
CommitLineData
3b08744b 1use strictures 1;
a5049e16 2use Test::More;
3b08744b 3use Filter::Keyword;
4
d5503d9d 5my $shadowed_called = 0;
6sub shadowed ($&) {
7 my ($name, $sub) = @_;
8 $shadowed_called++;
9 is($name, 'fun', 'shadowed sub called with correct name');
10 is($sub->(), 'OH WHAT FUN', 'shadowed sub called with correct sub');
17022343 11}
12
3b08744b 13BEGIN {
14 (our $Kw = Filter::Keyword->new(
a5049e16 15 target_package => __PACKAGE__,
16 keyword_name => 'method',
17 parser => sub {
18 my $kw = shift;
19 if (my ($stripped, $matches) = $kw->match_source('', '{')) {
d5503d9d 20 my $name = $kw->current_match;
21 $stripped =~ s/{/sub ${name} { my \$self = shift;/;
a5049e16 22 return ($stripped, 1);
23 }
24 else {
25 return ('', 1);
26 }
27 },
28 ))->install;
29 (our $Kw2 = Filter::Keyword->new(
30 target_package => __PACKAGE__,
d5503d9d 31 keyword_name => 'shadowed',
a5049e16 32 parser => sub {
33 my $kw = shift;
34 if (my ($stripped, $matches) = $kw->match_source('', '{')) {
d5503d9d 35 my $name = $kw->current_match;
36 $stripped =~ s/{/shadowed "${name}", sub { BEGIN { Filter::Keyword::inject_after_scope(';') } /;
a5049e16 37 return ($stripped, 1);
38 }
39 else {
40 return ('', 1);
c46d1069 41 }
42 },
43 ))->install;
3b08744b 44}
45
66b17c0b 46my ($line1, $line2);
d5503d9d 47#line 1
66b17c0b 48method yay { $line1 = __LINE__; "YAY $self" } $line2 = __LINE__;
d5503d9d 49is(__LINE__, 2, 'line number correct after first keyword');
a5049e16 50
66b17c0b 51is(__PACKAGE__->yay, 'YAY ' . __PACKAGE__, 'result of keyword correct');
52is($line1, 1, 'line number correct in keyword');
53is($line2, 1, 'line number correct on same line as keyword');
54
d5503d9d 55#line 1
66b17c0b 56my $x = __LINE__ . " @{[ __LINE__ ]} method foo @{[ __LINE__ ]} " . __LINE__;
d5503d9d 57is(__LINE__, 2, 'line number correct after string with keyword');
58is($x, '1 1 method foo 1 bar baz 1', 'line numbers in constructed string are correct');
3b08744b 59
66b17c0b 60undef $line1;
61undef $line2;
d5503d9d 62#line 1
a5049e16 63method spoon {
66b17c0b 64 $line1 = __LINE__;
a5049e16 65 'I HAZ A SPOON'
66}
67
66b17c0b 68is(__PACKAGE__->spoon, 'I HAZ A SPOON', 'result of multiline keyword');
69is($line1, 2, 'line number correct in multiline keyword');
a5049e16 70
d5503d9d 71#line 1
66b17c0b 72method spoon2 {
73 $line2 = __LINE__;
74 'I HAZ A SPOON'
75}
76
77is(__PACKAGE__->spoon2, 'I HAZ A SPOON', 'result of second multiline keyword correct');
78is($line2, 2, 'line number correct in second multiline keyword');
79
80undef $line1;
81undef $line2;
82#line 1
83shadowed fun { $line1 = __LINE__; 'OH WHAT FUN' }
84is($line1, 1, 'line number correct inside second keyword');
85
86shadowed fun { $line2 = __LINE__; 'OH WHAT FUN' }
87is($line1, 4, 'line number correct inside second keyword repeat');
88
89undef $line1;
90undef $line2;
91#line 1
92shadowed fun {
93 $line1 = __LINE__;
94 'OH WHAT FUN';
95}
96is($line1, 2, 'line number correct inside second keyword multiline');
a5049e16 97
66b17c0b 98shadowed fun {
99 $line2 = __LINE__;
100 'OH WHAT FUN';
101};
102is($line2, 8, 'line number correct inside second keyword multiline');
9854aa8f 103
66b17c0b 104is($shadowed_called, 4, 'shadowed sub called only by filter output');
a5049e16 105
66b17c0b 106is(__LINE__, 15, 'line number after shadowed correct');
3b08744b 107
a5049e16 108done_testing;