blib/lib/Punk/Plugin/I18n.pm

filter: Statementstmt 100% 53 / 53 Branchbran 66.6% 14 / 18 (4 uncoverable) Conditioncond 100% 2 / 5 (3 uncoverable) MC/DCmcdc 100% 0 / 3 (3 uncoverable) Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 93.1% 78 / 88 (10 uncoverable) CC 13 SCAR 21.9
CC
13
Cov
92%
register
13.2
BEGIN
1.0
BEGIN
1.0
CRAP 9.0
« blib/lib/Punk/Test.pm ↑ Summary blib/lib/Punk/Generate.pm »
1 package Punk::Plugin::I18n;
2  
3 13 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 our $VERSION = '0.70';
8  
9 13 ▶use Punk ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use File::Raw ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 # Loaded for its side effect: it registers the 'json' plugin that
13 # File::Raw::slurp uses below. Nothing is imported.
14 13 ▶use File::Raw::JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 266 ▶sub register {
Subroutine register: called CC=13 SCAR=25.8
Pod documented
17 266 my ($class, $app, $opts) = @_;
18 # uncoverable condition right
19 266 ▶ $opts ||= {};
Condition100%1/2
$opts ||= {}
l!l
2660
MC/DC100%0/1
$opts ||= {}
$opts
Truth table100%1/2
A
A= $opts
Aresult
11
00
20 # uncoverable branch true
21 266 ▶ ref $opts eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $opts eq "HASH" or die "Punk::Plugin::I18n: options must be a hashref\n" 266 0 266
22 or die "Punk::Plugin::I18n: options must be a hashref\n";
23  
24 266 my $dir = $opts->{dir};
25 # uncoverable branch true
26 # uncoverable condition left
27 # uncoverable condition right
28 266 ▶ defined $dir && length $dir
Branch: 1 branch
BranchTrueFalseTotal
defined $dir and length $dir or die "Punk::Plugin::I18n: `dir` is required - it is where the catalogues live (plugin 'I18n' => { dir => 'i18n', default => 'en' })\n" 266 0 266
Condition100%1/3
defined $dir and length $dir
!ll&&!rl&&r
00266
MC/DC100%0/2
defined $dir and length $dir
defined $dir length $dir
Truth table100%1/3
A and B
A= defined $dirB= length $dir
ABresult
0X0
100
111
29 or die "Punk::Plugin::I18n: `dir` is required - it is where the "
30 . "catalogues live (plugin 'I18n' => { dir => 'i18n', "
31 . "default => 'en' })\n";
32 266 ▶ -d $dir
Branch: 1 branch
BranchTrueFalseTotal
-d $dir or die "Punk::Plugin::I18n: the catalogue directory '${dir}' does not " . 'exist - a missing directory is a deploy that shipped without ' . "its translations, and it should fail here rather than serve " . "every page in the default language\n" 155 111 266
33 or die "Punk::Plugin::I18n: the catalogue directory '$dir' does not "
34 . "exist - a missing directory is a deploy that shipped without "
35 . "its translations, and it should fail here rather than serve "
36 . "every page in the default language\n";
37  
38 # uncoverable branch true
39 155 ▶ opendir my $dh, $dir
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or die "Punk::Plugin::I18n: cannot read '${dir}': $!\n" 155 0 155
40 or die "Punk::Plugin::I18n: cannot read '$dir': $!\n";
41 155 my @files = sort grep { /\.json\z/ } readdir $dh;
42 155 closedir $dh;
43  
44 @files
45 155 ▶ or die "Punk::Plugin::I18n: no *.json catalogues in '$dir' - a plugin "
Branch: 1 branch
BranchTrueFalseTotal
@files or die "Punk::Plugin::I18n: no *.json catalogues in '${dir}' - a plugin " . 'with nothing to translate is a typo in `dir`, not a working ' . "default\n" 154 1 155
46 . "with nothing to translate is a typo in `dir`, not a working "
47 . "default\n";
48  
49 154 my %cats;
50 154 for my $f (@files) {
51 202 (my $tag = $f) =~ s/\.json\z//;
52 202 $tag =~ tr/A-Z/a-z/;
53 202 my $path = File::Spec->catfile($dir, $f);
54  
55 202 my $data = eval { File::Raw::slurp($path, plugin => 'json') };
56 202 ▶ if (!defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 1 201 202
57 1 my $why = $@;
58 1 $why =~ s/\s+\z//;
59 # uncoverable branch false
60 1 ▶ die -r $path
Branch: 1 branch
BranchTrueFalseTotal
-r $path ? : 1 0 1
61 ? "Punk::Plugin::I18n: '$path' will not parse: $why\n"
62 : "Punk::Plugin::I18n: cannot read '$path': $!\n";
63 }
64 201 ▶ ref $data eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq "HASH" or die "Punk::Plugin::I18n: '${path}' is not a JSON object - a " . "catalogue is a map of key to translation\n" 200 1 201
65 or die "Punk::Plugin::I18n: '$path' is not a JSON object - a "
66 . "catalogue is a map of key to translation\n";
67 200 $cats{$tag} = $data;
68 }
69  
70 152 my $ok = eval { $class->_build($app, \%cats, $opts); 1 };
71 152 ▶ return 1 if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 34 118 152
72  
73 118 my $why = $@;
74 118 $why =~ s/\s+\z//;
75 118 die "$why (catalogues from '$dir')\n";
76 }
77  
78 13 1;
79  
80 __END__
« blib/lib/Punk/Test.pm ↑ Summary blib/lib/Punk/Generate.pm »