lib/Serge/Command/test_parser.pm

filter: Statementstmt 29.6% 16 / 54 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 5 / 10 Podpod 0.0% 0 / 4 totaltotal 22.5% 21 / 93 SCAR 44.3
CC
12
Cov
21%
validate_data
72.0
run
12.0
init
6.0
CRAP 83.6
« lib/Serge/Engine/Plugin/parse_json_keyvalue.pm ↑ Summary lib/Serge/Engine/Plugin/test_language.pm »
1 package Serge::Command::test_parser;
2 1 ▶use parent Serge::Command, Serge::Interface::PluginHost;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Serge::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub get_commands {
12 return {
13 0 ▶ 'test-parser' => {handler => \&run, info => 'Test selected parser against a provided file'},
Subroutine get_commands: not called CC=1 SCAR=6.9
Pod undocumented
14 }
15 }
16  
17 sub init {
18 0 ▶ my ($self, $command) = @_;
Subroutine init: not called CC=2 SCAR=17.9
Pod undocumented
19  
20 0 $self->SUPER::init($command);
21  
22 GetOptions(
23 "data-file:s" => \$self->{data_file},
24 "import-mode" => \$self->{import_mode},
25 "output-mode:s" => \$self->{output_mode},
26 "as-objects" => \$self->{as_objects},
27 0 ▶ ) or die "Failed to parse some command-line parameters.";
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("data-file:s", \$self->{"data_file"}, "import-mode", \$self->{"import_mode"}, "output-mode:s", \$self->{"output_mode"}, "as-objects", \$self->{"as_objects"}) or die "Failed to parse some command-line parameters." 0 0 0
28  
29 $self->{parser} = {
30 0 plugin => shift @ARGV
31 };
32  
33 0 $self->{path} = shift @ARGV;
34 }
35  
36 sub validate_data {
37 0 ▶ my ($self, $command) = @_;
Subroutine validate_data: not called CC=8 SCAR=42.8
Pod undocumented
38  
39 0 $self->SUPER::validate_data($command);
40  
41 0 ▶ die "Unknown --output-mode value\n" unless $self->{output_mode} =~ m/^(|dumper)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"output_mode"} =~ /^(|dumper)$/ 0 0 0
42 0 ▶ die "Please provide the name of the parser as a first argument to the command" unless defined $self->{parser}->{plugin};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"parser"}{"plugin"} 0 0 0
43 0 ▶ die "Please provide the path to the file as a second argument to the command" unless defined $self->{path};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"path"} 0 0 0
44 0 ▶ die "The provided path doesn't point to a valid file" unless -f $self->{path};
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self->{"path"} 0 0 0
45 0 ▶ die "The provided '--data-file' parameter doesn't point to a valid file" if ($self->{data_file} ne '') && (!-f $self->{data_file});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"data_file"} ne "" and not -f $self->{"data_file"} 0 0 0
Condition0%0/3
$self->{"data_file"} ne "" and not -f $self->{"data_file"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"data_file"} ne "" and not -f $self->{"data_file"}
$self->{"data_file"} ne "" not -f $self->{"data_file"}
Truth table0%0/3
A and not(B)
A= $self->{"data_file"} ne ""B= not -f $self->{"data_file"}
ABresult
0X0
100
111
46  
47 0 ▶ if ($self->{data_file} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"data_file"} ne "") 0 0 0
48 0 eval 'use Config::Neat::Inheritable';
49 0 my $c = Config::Neat::Inheritable->new();
50  
51 0 $self->{parser}->{data} = $c->parse_file($self->{data_file});
52 }
53 }
54  
55 sub run {
56 0 ▶ my ($self, $command) = @_;
Subroutine run: not called CC=3 SCAR=24.8
Pod undocumented
57  
58 # Loading parser plugin
59  
60 0 my $parser = $self->load_plugin_from_node('Serge::Engine::Plugin', $self->{parser});
61  
62 0 print "Using plugin: ".$parser->name."\n";
63  
64 0 my $src = read_and_normalize_file($self->{path});
65 0 my @data;
66  
67 0 ▶ $parser->{import_mode} = 1 if $self->{import_mode};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"import_mode"} 0 0 0
68 $parser->parse(\$src, sub {
69 0 ▶ my @row = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
70  
71 0 ▶ if ($self->{as_objects}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'as_objects'}) { } 0 0 0
72 0 push @data, {
73 string => $row[0],
74 context => $row[1],
75 hint => $row[2],
76 flagsref => $row[3],
77 lang => $row[4],
78 key => $row[5],
79 };
80 } else {
81 0 push @data, \@row;
82 }
83  
84 0 return $row[0]; # return original source string as translation (solely for parser debugging purposes)
85 0 });
86  
87 0 my $mode = lc($self->{output_mode});
88  
89 0 ▶ if ($mode eq 'dumper') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq 'dumper') { } 0 0 0
90 0 eval 'use Data::Dumper';
91 0 print Dumper(\@data);
92 } else {
93 0 eval 'use Config::Neat::Render';
94 0 my @order = qw(string context hint flagsref lang key);
95 0 my $r = Config::Neat::Render->new({
96 wrap_width => 256,
97 undefined_value => '',
98 sort => \@order
99 });
100 0 print $r->render({ data => \@data });
101 }
102  
103 0 return 0;
104 }
105  
106 1 1;
« lib/Serge/Engine/Plugin/parse_json_keyvalue.pm ↑ Summary lib/Serge/Engine/Plugin/test_language.pm »