4e179fb1c92a5e876151ab4ea3b8502f65ca29e1
[p5sagit/Filter-Keyword.git] / t / simple.t
1 use strictures 1;
2 use Test::More;
3 use Filter::Keyword;
4
5 sub ::dd {
6   use Data::Dumper ();
7   local $Data::Dumper::Useqq = 1;
8   local $Data::Dumper::Terse = 1;
9   my $out = Data::Dumper::Dumper($_[0]);
10   chomp $out;
11   return $out;
12 }
13
14 BEGIN {
15   (our $Kw = Filter::Keyword->new(
16     target_package => __PACKAGE__,
17     keyword_name => 'method',
18     parser => sub {
19       my $kw = shift;
20       if (my ($stripped, $matches) = $kw->match_source('', '{')) {
21         my $name = $kw->current_match->[0];
22         $stripped =~ s/{/; sub ${name} { my \$self = shift;/;
23         return ($stripped, 1);
24       }
25       else {
26         return ('', 1);
27       }
28     },
29   ))->install;
30   (our $Kw2 = Filter::Keyword->new(
31     target_package => __PACKAGE__,
32     keyword_name => 'function',
33     parser => sub {
34       my $kw = shift;
35       if (my ($stripped, $matches) = $kw->match_source('', '{')) {
36         my $name = $kw->current_match->[0];
37         $stripped =~ s/{/; sub ${name} {/;
38         return ($stripped, 1);
39       }
40       else {
41         return ('', 1);
42       }
43     },
44   ))->install;
45 }
46
47 method yay { is(__LINE__, 38, 'line number correct inside method' ); "YAY $self" } is(__LINE__, 38, 'line number correct on same line after method');
48
49 is(__LINE__, 40, 'line number correct after first method');
50
51 my $x = "method foo bar baz";
52
53 is(__PACKAGE__->yay, 'YAY ' . __PACKAGE__, 'result of method correct');
54
55 method spoon {
56   is(__LINE__, 47, 'line number correct in multiline method');
57   'I HAZ A SPOON'
58 }
59
60 is(__PACKAGE__->spoon, 'I HAZ A SPOON', 'result of second method correct');
61
62 function fun { is(__LINE__, 53, 'line number in function correct'); 'OH WHAT FUN' }
63
64 is(__PACKAGE__->fun, 'OH WHAT FUN', 'result of function correct');
65
66 is(__LINE__, 57, 'line number after function correct');
67
68 done_testing;