blib/lib/MarpaX/Tester.pm

filter: Statementstmt 96.0% 73 / 76 Branchbran 75.0% 15 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod 100% 4 / 4 totaltotal 93.0% 107 / 115 CC 13 SCAR 8.3
CC
13
Cov
92%
result
3.1
_one_test
3.0
new
3.0
CRAP 2.3
↑ Summary
1 package MarpaX::Tester;
2
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Marpa::R2;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 =head1 NAME
10
11 MarpaX::Tester - Given a Marpa grammar and one or more test cases, generates output
12
13 =head1 VERSION
14
15 Version 0.02
16
17 =cut
18
19 3 our $VERSION = '0.02';
20
21
22 =head1 SYNOPSIS
23
24 When working with parsers, I find test-driven development to be the quickest and most
25 effective way of getting where I want to go. (Also when working with anything else, but
26 we're talking about parsers right now.) This module does that.
27
28 use MarpaX::Tester;
29
30 my $r = MarpaX::Tester->new($grammar);
31 my $results = $r->test($text);
32 $results = $r->test([$text1, $text2]);
33
34 There's also a command-line utility to produce conveniently formatted versions for
35 posting and gisting.
36
37 The results are a hashref; "grammar" is the text of the grammar in question, "status" is
38 binary indicating OK or not OK at the grammar parsing level (i.e. has Marpa accepted the grammar
39 itself), "error" is the error raised by Marpa, if the status is false. If the error has a line and
40 column specified (not all errors do), then the text of the grammar will have an extra line inserted
41 pointing at the error with a '........^' format.
42
43 Either way, "ver" is the Marpa version used to generate this test.
44
45 (These are already available immediately after the grammar is defined, of course, and can be
46 retrieved at that stage with C<<$r->status>> and C<<$r->result>>, but to keep things dead simple,
47 you can still throw your test cases at an invalid grammar - they just won't be parsed, obviously.)
48
49 So if there is a parse error, you get a structure like this:
50
51 {
52 grammar => '...',
53 status => 0,
54 error => '...',
55 }
56
57 If the grammar succeeds in compiling, then results are in "results", either a hashref or an array of
58 hashrefs, like this:
59
60 {
61 grammar => '...',
62 status => 1,
63 results => {
64 test => '...',
65 status => 0,
66 error => '...',
67 },
68 }
69
70 or conversely like this:
71
72 {
73 grammar => '...',
74 status => 1,
75 results => {
76 test => '...',
77 status => 1,
78 parse => '...',
79 parse_val => ...
80 },
81 }
82
83 And if you pass in a list of tests, you'll get this:
84
85 {
86 grammar => '...',
87 status => 1,
88 results => [
89 {
90 test => '...',
91 status => 0,
92 error => '...',
93 },
94 {
95 test => '...',
96 status => 1,
97 parse => '...',
98 parse_val => ...,
99 }
100 ],
101 }
102
103 In successful tests, you get both the parse_val (the actual value returned from the parse) and the Data::Dumper
104 text version in "parse". This makes it easy for the command-line utility to use the Template Toolkit to format
105 the results.
106
107 =head1 METHODS
108
109 =head2 new
110
111 Given a string, makes a grammar and recognizer and stashes them for later use, or saves the error
112 after convenient formatting.
113
114 =cut
115
116 2 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
117 2 my $class = shift;
118 2 my $self = bless {}, $class;
119 2 $self->{grammar} = shift;
120 2 my $g = $self->{grammar};
121 2 $self->{g} = eval { Marpa::R2::Scanless::G->new({source => \$g}); };
122 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 1 1 2
123 1 $self->{status} = 0;
124 1 $self->{error} = _format_error($@);
125 1 my ($line, $column) = _error_location($self->{error});
126 1 ▶ if (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 1 0 1
127 1 $self->{grammar} = _decorate_text ($self->{grammar}, $line, $column);
128 }
129 } else {
130 1 $self->{status} = 1;
131 }
132 2 return $self;
133 }
134
135 1 ▶sub _format_error {
Subroutine _format_error: called CC=1 SCAR=0.0
136 1 my $error = shift;
137 1 $error =~ s/Marpa::R2 exception at.*\n//gm;
138 1 $error =~ s/^\*.*\n//gm;
139 1 return $error;
140 }
141 1 ▶sub _error_location {
Subroutine _error_location: called CC=2 SCAR=7.6
142 1 my $error = shift;
143 1 ▶ if ($error =~ /line (\d+), column (\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error =~ /line (\d+), column (\d+)/) { } 1 0 1
144 1 return ($1, $2);
145 } else {
146 0 return ();
147 }
148 }
149 1 ▶sub _decorate_text {
Subroutine _decorate_text: called CC=3 SCAR=11.0
150 1 my ($text, $line, $column) = @_;
151 1 my $output = '';
152 1 my $current = 1;
153 1 foreach my $tline (split /\n/, $text) {
154 6 $output .= "$tline\n";
155 6 ▶ $output .= '.' x ($column - 1) . "^ ERROR!\n" if $current == $line;
Branch: 1 branch
BranchTrueFalseTotal
if $current == $line 1 5 6
156 6 $current++;
157 }
158 1 $output;
159 }
160
161 =head2 test
162
163 Tests one or more texts and returns the results in the structure described above.
164
165 =cut
166
167 2 ▶sub test {
Subroutine test: called CC=2 SCAR=7.1
Pod documented
168 2 my $self = shift;
169 2 ▶ return $self->result unless $self->{status}; # Don't try if there's no recognizer.
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"status"} 0 2 2
170
171 $self->{result} =
172 {
173 grammar => $self->{grammar},
174 2 ver => $Marpa::R2::VERSION,
175 status => 1,
176 result => $self->_run_test(@_),
177 }
178 }
179
180 2 ▶sub _run_test {
Subroutine _run_test: called CC=3 SCAR=11.0
181 2 my $self = shift;
182 2 my $test = shift;
183
184 2 ▶ return $self->_one_test ($test) unless ref($test) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $test eq "ARRAY" 1 1 2
185 1 my @results = ();
186 1 foreach my $individual (@$test) {
187 2 push @results, $self->_one_test($individual);
188 }
189 1 \@results;
190 }
191
192 3 ▶sub _one_test {
Subroutine _one_test: called CC=3 SCAR=11.1
193 3 my $self = shift;
194 3 my $string = shift;
195
196 3 my $r = Marpa::R2::Scanless::R->new({ grammar => $self->{g} });
197
198 3 eval { $r->read(\$string); };
199 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 3 3
200 return {
201 0 test => $string,
202 status => 0,
203 error => $@,
204 };
205 } else {
206 3 my $value = $r->value;
207 return {
208 3 ▶ test => $string,
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 2 1 3
209 status => defined $value ? 1 : 0,
210 parse => _format_dump($value),
211 parse_val => $value,
212 };
213 }
214 }
215
216 3 ▶sub _format_dump { # The Dumper format doesn't appeal to me as diagnostic output.
Subroutine _format_dump: called CC=1 SCAR=0.0
217 3 my $dump = shift;
218 3 $dump = Dumper($dump);
219 3 $dump =~ s/\$VAR1 = \\//;
220 3 $dump =~ s/^ //gm;
221 3 $dump;
222 }
223
224
225 =head2 status
226
227 Returns the BNF parse status of the original grammar (I know, two levels of parsing make it
228 hard to distinguish - this is the status of the grammar specification itself, not the status of
229 your test cases).
230
231 =cut
232
233 2 ▶sub status { $_[0]->{status} }
Subroutine status: called CC=1 SCAR=0.0
Pod documented
234
235 =head2 result
236
237 Returns the last result from the test object - either the parse error or the last test set.
238
239 =cut
240
241 2 ▶sub result {
Subroutine result: called CC=3 SCAR=11.3
Pod documented
242 2 my $self = shift;
243 2 ▶ return $self->{result} if $self->{result};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"result"} 1 1 2
244 1 ▶ if ($self->{status}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"status"}) 0 1 1
245 return {
246 grammar => $self->{grammar},
247 0 ver => $Marpa::R2::VERSION,
248 status => 1,
249 };
250 }
251 return {
252 grammar => $self->{grammar},
253 ver => $Marpa::R2::VERSION,
254 status => 0,
255 error => $self->{error},
256 1 };
257 }
258
259 =head1 AUTHOR
260
261 Michael Roberts, C<< <michael at vivtek.com> >>
262
263 =head1 BUGS
264
265 Please report any bugs or feature requests to C<bug-marpax-tester at rt.cpan.org>, or through
266 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MarpaX-Tester>. I will be notified, and then you'll
267 automatically be notified of progress on your bug as I make changes.
268
269
270
271
272 =head1 SUPPORT
273
274 You can find documentation for this module with the perldoc command.
275
276 perldoc MarpaX::Tester
277
278
279 You can also look for information at:
280
281 =over 4
282
283 =item * RT: CPAN's request tracker (report bugs here)
284
285 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MarpaX-Tester>
286
287 =item * AnnoCPAN: Annotated CPAN documentation
288
289 L<http://annocpan.org/dist/MarpaX-Tester>
290
291 =item * CPAN Ratings
292
293 L<http://cpanratings.perl.org/d/MarpaX-Tester>
294
295 =item * Search CPAN
296
297 L<http://search.cpan.org/dist/MarpaX-Tester/>
298
299 =back
300
301
302 =head1 ACKNOWLEDGEMENTS
303
304
305 =head1 LICENSE AND COPYRIGHT
306
307 Copyright 2014 Michael Roberts.
308
309 This program is free software; you can redistribute it and/or modify it
310 under the terms of the the Artistic License (2.0). You may obtain a
311 copy of the full license at:
312
313 L<http://www.perlfoundation.org/artistic_license_2_0>
314
315 Any use, modification, and distribution of the Standard or Modified
316 Versions is governed by this Artistic License. By using, modifying or
317 distributing the Package, you accept this license. Do not use, modify,
318 or distribute the Package, if you do not accept this license.
319
320 If your Modified Version has been derived from a Modified Version made
321 by someone other than you, you are nevertheless required to ensure that
322 your Modified Version complies with the requirements of this license.
323
324 This license does not grant you the right to use any trademark, service
325 mark, tradename, or logo of the Copyright Holder.
326
327 This license includes the non-exclusive, worldwide, free-of-charge
328 patent license to make, have made, use, offer to sell, sell, import and
329 otherwise transfer the Package with respect to any patent claims
330 licensable by the Copyright Holder that are necessarily infringed by the
331 Package. If you institute patent litigation (including a cross-claim or
332 counterclaim) against any party alleging that the Package constitutes
333 direct or contributory patent infringement, then this Artistic License
334 to you shall terminate on the date that such litigation is filed.
335
336 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
337 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
338 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
339 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
340 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
341 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
342 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
343 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
344
345
346 =cut
347
348 3 1; # End of MarpaX::Tester
↑ Summary