blib/lib/App/RecordStream/Operation/assert.pm

filter: Statementstmt 100% 43 / 43 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 4 totaltotal 93.4% 57 / 61 CC 3 SCAR 4.1
CC
3
Cov
100%
accept_record
3.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.5
« blib/lib/App/RecordStream/Accumulator.pm ↑ Summary blib/lib/App/RecordStream/DomainLanguage/Executor.pm »
1 package App::RecordStream::Operation::assert;
2  
3 2 our $VERSION = "4.0.23";
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use base qw(App::RecordStream::Operation);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use App::RecordStream::Executor::Getopt;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use App::RecordStream::Executor;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Data::Dumper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub init {
15 4 ▶ my $this = shift;
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
16 4 my $args = shift;
17  
18 4 $this->{DIAGNOSTIC} = '';
19 4 $this->{VERBOSE} = 0;
20  
21 4 my $executor_options = App::RecordStream::Executor::Getopt->new();
22 my $spec = {
23 'diagnostic|d=s' => \$this->{DIAGNOSTIC},
24 'verbose|v' => \$this->{VERBOSE},
25 4 $executor_options->arguments(),
26 };
27  
28 4 $this->parse_options($args, $spec, ['bundling']);
29  
30 3 my $expression = $executor_options->get_string($args);
31 3 my $executor = App::RecordStream::Executor->new($expression);
32  
33 3 $this->{'ASSERTION'} = $expression;
34 3 $this->{'EXECUTOR'} = $executor;
35 }
36  
37 sub accept_record {
38 7 ▶ my $this = shift;
Subroutine accept_record: called CC=3 SCAR=11.0
Pod undocumented
39 7 my $record = shift;
40  
41 7 ▶ unless ($this->{'EXECUTOR'}->execute_code($record)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->{"EXECUTOR"}->execute_code($record)) 2 5 7
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 ▶ ? ("Record: ", Data::Dumper->Dump([$record->as_hashref], ['r']), "\n")
Branch: 1 branch
BranchTrueFalseTotal
$this->{'VERBOSE'} ? : 1 1 2
48 : ());
49 }
50  
51 5 $this->push_record($record);
52 5 return 1;
53 }
54  
55 sub add_help_types {
56 4 ▶ my $this = shift;
Subroutine add_help_types: called CC=1 SCAR=0.0
Pod undocumented
57 4 $this->use_help_type('snippet');
58 }
59  
60 sub usage {
61 1 ▶ my $this = shift;
Subroutine usage: called CC=1 SCAR=0.0
Pod undocumented
62  
63 1 my $options = [
64 ['diagnostic|-d <text>' => 'Include the diagnostic string <text> 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 1 my $args_string = $this->options_string($options);
70  
71 1 my $usage = <<USAGE;
72 Usage: recs-assert <args> <expr> [<files>]
73 __FORMAT_TEXT__
74 Asserts that every record in the stream must pass the given <expr>.
75  
76 <expr> is evaluated as Perl on each record of input (or records from
77 <files>) with \$r set to a App::RecordStream::Record object and \$line set
78 to the current line number (starting at 1). If <expr> 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 2 1;
« blib/lib/App/RecordStream/Accumulator.pm ↑ Summary blib/lib/App/RecordStream/DomainLanguage/Executor.pm »