| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::RecordStream::Operation::assert; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | our $VERSION = "4.0.25"; | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 2 |  |  | 2 |  | 876 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 39 |  | 
| 6 | 2 |  |  | 2 |  | 6 | use warnings; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 38 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 2 |  |  | 2 |  | 7 | use base qw(App::RecordStream::Operation); | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 128 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 2 |  |  | 2 |  | 298 | use App::RecordStream::Executor::Getopt; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 35 |  | 
| 11 | 2 |  |  | 2 |  | 8 | use App::RecordStream::Executor; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 23 |  | 
| 12 | 2 |  |  | 2 |  | 5 | use Data::Dumper (); | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 501 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | sub init { | 
| 15 | 3 |  |  | 3 | 0 | 5 | my $this = shift; | 
| 16 | 3 |  |  |  |  | 4 | my $args = shift; | 
| 17 |  |  |  |  |  |  |  | 
| 18 | 3 |  |  |  |  | 7 | $this->{DIAGNOSTIC} = ''; | 
| 19 | 3 |  |  |  |  | 4 | $this->{VERBOSE}    = 0; | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 3 |  |  |  |  | 24 | my $executor_options = App::RecordStream::Executor::Getopt->new(); | 
| 22 |  |  |  |  |  |  | my $spec = { | 
| 23 |  |  |  |  |  |  | 'diagnostic|d=s' => \$this->{DIAGNOSTIC}, | 
| 24 |  |  |  |  |  |  | 'verbose|v'      => \$this->{VERBOSE}, | 
| 25 | 3 |  |  |  |  | 9 | $executor_options->arguments(), | 
| 26 |  |  |  |  |  |  | }; | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 3 |  |  |  |  | 15 | $this->parse_options($args, $spec, ['bundling']); | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 3 |  |  |  |  | 8 | my $expression = $executor_options->get_string($args); | 
| 31 | 3 |  |  |  |  | 17 | my $executor = App::RecordStream::Executor->new($expression); | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 3 |  |  |  |  | 6 | $this->{'ASSERTION'} = $expression; | 
| 34 | 3 |  |  |  |  | 33 | $this->{'EXECUTOR'}  = $executor; | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | sub accept_record { | 
| 38 | 7 |  |  | 7 | 0 | 8 | my $this   = shift; | 
| 39 | 7 |  |  |  |  | 7 | my $record = shift; | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 7 | 100 |  |  |  | 17 | 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 |  |  |  | 12 | ? ("Record: ", Data::Dumper->Dump([$record->as_hashref], ['r']), "\n") | 
| 48 |  |  |  |  |  |  | : ()); | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 5 |  |  |  |  | 18 | $this->push_record($record); | 
| 52 | 5 |  |  |  |  | 24 | return 1; | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | sub add_help_types { | 
| 56 | 3 |  |  | 3 | 0 | 6 | my $this = shift; | 
| 57 | 3 |  |  |  |  | 18 | $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; |