File Coverage

blib/lib/App/RecordStream/Operation/assert.pm
Criterion Covered Total %
statement 37 41 90.2
branch 4 4 100.0
condition n/a
subroutine 9 10 90.0
pod 0 4 0.0
total 50 59 84.7


line stmt bran cond sub pod time code
1             package App::RecordStream::Operation::assert;
2              
3             our $VERSION = "4.0.24";
4              
5 2     2   1237 use strict;
  2         6  
  2         65  
6 2     2   13 use warnings;
  2         6  
  2         70  
7              
8 2     2   14 use base qw(App::RecordStream::Operation);
  2         6  
  2         166  
9              
10 2     2   393 use App::RecordStream::Executor::Getopt;
  2         7  
  2         61  
11 2     2   16 use App::RecordStream::Executor;
  2         5  
  2         46  
12 2     2   13 use Data::Dumper ();
  2         5  
  2         781  
13              
14             sub init {
15 3     3 0 7 my $this = shift;
16 3         6 my $args = shift;
17              
18 3         8 $this->{DIAGNOSTIC} = '';
19 3         8 $this->{VERBOSE} = 0;
20              
21 3         26 my $executor_options = App::RecordStream::Executor::Getopt->new();
22             my $spec = {
23             'diagnostic|d=s' => \$this->{DIAGNOSTIC},
24             'verbose|v' => \$this->{VERBOSE},
25 3         17 $executor_options->arguments(),
26             };
27              
28 3         29 $this->parse_options($args, $spec, ['bundling']);
29              
30 3         25 my $expression = $executor_options->get_string($args);
31 3         29 my $executor = App::RecordStream::Executor->new($expression);
32              
33 3         10 $this->{'ASSERTION'} = $expression;
34 3         525 $this->{'EXECUTOR'} = $executor;
35             }
36              
37             sub accept_record {
38 7     7 0 16 my $this = shift;
39 7         13 my $record = shift;
40              
41 7 100       24 unless ($this->{'EXECUTOR'}->execute_code($record)) {
42             die "Assertion failed! $this->{DIAGNOSTIC}\n",
43             "Expression: « $this->{ASSERTION} »\n",
44             "Filename: ", $this->get_current_filename, "\n",
45             "Line: $.\n",
46             ($this->{VERBOSE}
47 2 100       19 ? ("Record: ", Data::Dumper->Dump([$record->as_hashref], ['r']), "\n")
48             : ());
49             }
50              
51 5         38 $this->push_record($record);
52 5         24 return 1;
53             }
54              
55             sub add_help_types {
56 3     3 0 10 my $this = shift;
57 3         16 $this->use_help_type('snippet');
58             }
59              
60             sub usage {
61 0     0 0   my $this = shift;
62              
63 0           my $options = [
64             ['diagnostic|-d ' => 'Include the diagnostic string in any failed assertion errors'],
65             ['verbose|-v' => 'Verbose output for failed assertions; dumps the current record'],
66             App::RecordStream::Executor::options_help(),
67             ];
68              
69 0           my $args_string = $this->options_string($options);
70              
71 0           my $usage = <
72             Usage: recs-assert []
73             __FORMAT_TEXT__
74             Asserts that every record in the stream must pass the given .
75              
76             is evaluated as Perl on each record of input (or records from
77             ) with \$r set to a App::RecordStream::Record object and \$line set
78             to the current line number (starting at 1). If does not evaluate to
79             true, processing is immediately aborted and an error message printed. See
80             --help-snippets for more information on code snippets.
81             __FORMAT_TEXT__
82              
83             $args_string
84              
85             Examples:
86             Require each record to have a "date" field.
87             recs-assert '\$r->{date}'
88             USAGE
89             }
90              
91             1;