File Coverage

blib/lib/TestML/Compiler.pm
Criterion Covered Total %
statement 55 59 93.2
branch 24 38 63.1
condition 3 6 50.0
subroutine 4 4 100.0
pod 0 2 0.0
total 86 109 78.9


line stmt bran cond sub pod time code
1 27     27   1187 use TestML::Runtime;
  27         31  
  27         784  
2              
3             package TestML::Compiler;
4              
5 27     27   104 use TestML::Base;
  27         33  
  27         130  
6              
7             has code => ();
8             has data => ();
9             has text => ();
10             has directives => ();
11             has function => ();
12              
13             sub compile {
14 42     42 0 62 my ($self, $input) = @_;
15              
16 42         139 $self->preprocess($input, 'top');
17 42         139 $self->compile_code;
18 42         1233 $self->compile_data;
19              
20 42 50       108 if ($self->directives->{DumpAST}) {
21 0         0 XXX($self->function);
22             }
23              
24 42         106 $self->function->namespace->{TestML} = $self->directives->{TestML};
25              
26 42         97 $self->function->outer(TestML::Function->new);
27 42         96 return $self->function;
28             }
29              
30             sub preprocess {
31 46     46 0 79 my ($self, $input, $top) = @_;
32              
33 46         665 my @parts = split /^((?:\%\w+.*|\#.*|\ *)\n)/m, $input;
34 46         64 $input = '';
35              
36             $self->{directives} = {
37 46         255 TestML => '',
38             DataMarker => '',
39             BlockMarker => '===',
40             PointMarker => '---',
41             };
42              
43 46         56 my $order_error = 0;
44 46         70 for my $part (@parts) {
45 778 100       959 next unless length($part);
46 600 100       1198 if ($part =~ /^(\#.*|\ *)\n/) {
47 321         238 $input .= "\n";
48 321         313 next;
49             }
50 279 100       516 if ($part =~ /^%(\w+)\s*(.*?)\s*\n/) {
51 50         134 my ($directive, $value) = ($1, $2);
52 50         68 $input .= "\n";
53 50 100       154 if ($directive eq 'TestML') {
54 42 50       168 die "Invalid TestML directive"
55             unless $value =~ /^\d+\.\d+\.\d+$/;
56             die "More than one TestML directive found"
57 42 50       158 if $self->directives->{TestML};
58             $self->directives->{TestML} =
59 42         198 TestML::Str->new(value => $value);
60 42         77 next;
61             }
62 8 50       18 $order_error = 1 unless $self->directives->{TestML};
63 8 100       26 if ($directive eq 'Include') {
    50          
    0          
64 4 50       7 my $runtime = $TestML::Runtime::Singleton
65             or die "Can't process Include. No runtime available";
66 4         14 my $include = ref($self)->new;
67 4         9 $include->preprocess($runtime->read_testml_file($value));
68 4         16 $input .= $include->text;
69             $self->directives->{DataMarker} =
70 4         9 $include->directives->{DataMarker};
71             $self->directives->{BlockMarker} =
72 4         7 $include->directives->{BlockMarker};
73             $self->directives->{PointMarker} =
74 4         7 $include->directives->{PointMarker};
75             die "Can't define %TestML in an Included file"
76 4 50       8 if $include->directives->{TestML};
77             }
78             elsif ($directive =~ /^(DataMarker|BlockMarker|PointMarker)$/) {
79 4         7 $self->directives->{$directive} = $value;
80             }
81             elsif ($directive =~ /^(DebugPegex|DumpAST)$/) {
82 0 0       0 $value = 1 unless length($value);
83 0         0 $self->directives->{$directive} = $value;
84             }
85             else {
86 0         0 die "Unknown TestML directive '$directive'";
87             }
88             }
89             else {
90 229 50 66     583 $order_error = 1 if $input and not $self->directives->{TestML};
91 229         326 $input .= $part;
92             }
93             }
94              
95 46 100       120 if ($top) {
96             die "No TestML directive found"
97 42 50       102 unless $self->directives->{TestML};
98 42 50       88 die "%TestML directive must be the first (non-comment) statement"
99             if $order_error;
100              
101             my $DataMarker =
102             $self->directives->{DataMarker} ||=
103 42   33     92 $self->directives->{BlockMarker};
104 42 100       170 if ((my $split = index($input, "\n$DataMarker")) >= 0) {
105 29         115 $self->{code} = substr($input, 0, $split + 1);
106 29         127 $self->{data} = substr($input, $split + 1);
107             }
108             else {
109 13         30 $self->{code} = $input;
110 13         39 $self->{data} = '';
111             }
112             }
113             else {
114 4         9 $self->{text} = $input;
115             }
116             }
117              
118             1;