blib/lib/App/Test/DWG/LibreDWG/JSON.pm

filter: Statementstmt 51.4% 52 / 101 Branchbran 3.8% 1 / 26 Conditioncond 11.1% 2 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 86.6% 13 / 15 Podpod 100% 2 / 2 totaltotal 40.4% 70 / 173 CC 20 SCAR 41.1
CC
20
Cov
38%
run
119.5
_exec
56.0
__ANON__
2.0
CRAP 61.1
↑ Summary
1 package App::Test::DWG::LibreDWG::JSON;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use CAD::AutoCAD::Detect qw(detect_dwg_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Capture::Tiny qw(capture);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use File::Path qw(mkpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Getopt::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use IO::Barf qw(barf);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 Readonly::Hash our %REL => (
17 'MC0.0' => 'r1.1',
18 'AC1.2' => 'r1.2',
19 'AC1.40' => 'r1.4',
20 'AC1.50' => 'r2.0',
21 'AC2.10' => 'r2.10',
22 'AC1001' => 'r2.4',
23 'AC1002' => 'r2.5',
24 'AC1003' => 'r2.6',
25 'AC1004' => 'r9',
26 'AC1006' => 'r10',
27 'AC1009' => 'r11',
28 'AC1012' => 'r13',
29 'AC1013' => 'r13c3',
30 'AC1014' => 'r14',
31 'AC1015' => 'r2000',
32 'AC1018' => 'r2004',
33 'AC1021' => 'r2007',
34 'AC1024' => 'r2010',
35 'AC1027' => 'r2013',
36 'AC1032' => 'r2018',
37 );
38 4 Readonly::Scalar our $DR => 'dwgread';
39 4 Readonly::Scalar our $DW => 'dwgwrite';
40  
41 4 our $VERSION = 0.05;
42  
43 # Constructor.
44 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
45 2 my ($class, @params) = @_;
46  
47 # Create object.
48 2 my $self = bless {}, $class;
49  
50 # Object.
51 2 return $self;
52 }
53  
54 # Run.
55 1 ▶sub run {
Subroutine run: called CC=14 SCAR=47.8
Pod documented
56 1 my $self = shift;
57  
58 # Process arguments.
59 1 $self->{'_opts'} = {
60 'd' => undef,
61 'h' => 0,
62 'i' => 0,
63 'v' => 0,
64 };
65 1 ▶ if (! getopts('d:hiv:', $self->{'_opts'}) || @ARGV < 1
Branch: 1 branch
BranchTrueFalseTotal
if (not &getopts("d:hiv:", $self->{"_opts"}) or @ARGV < 1 or $self->{"_opts"}{"h"}) 1 0 1
Condition33%2/6
not &getopts("d:hiv:", $self->{"_opts"}) or @ARGV < 1 or $self->{"_opts"}{"h"}
l!l&&r!l&&!r
100
not &getopts("d:hiv:", $self->{"_opts"}) or @ARGV < 1
l!l&&r!l&&!r
010
MC/DC0%0/3
not &getopts("d:hiv:", $self->{"_opts"}) or @ARGV < 1 or $self->{"_opts"}{"h"}
not &getopts("d:hiv:", $self->{"_opts"}) @ARGV < 1 $self->{"_opts"}{"h"}
Truth table25%1/4
not(A) or B or C
A= not &getopts("d:hiv:", $self->{"_opts"})B= @ARGV < 1C= $self->{"_opts"}{"h"}
ABCresult
1XX1
01X1
0011
0000
66 || $self->{'_opts'}->{'h'}) {
67  
68 1 print STDERR "Usage: $0 [-d test_dir] [-h] [-i] [-v level] [--version] dwg_file\n";
69 1 print STDERR "\t-d test_dir\tTest directory (default is directory in system tmp).\n";
70 1 print STDERR "\t-h\t\tPrint help.\n";
71 1 print STDERR "\t-i\t\tIgnore errors.\n";
72 1 print STDERR "\t-v level\tVerbosity level (default 0, max 9).\n";
73 1 print STDERR "\t--version\tPrint version.\n";
74 1 print STDERR "\tdwg_file\tAutoCAD DWG file to test.\n";
75 1 return 1;
76 }
77 0 $self->{'_dwg_file'} = shift @ARGV;
78  
79 0 my $tmp_dir = $self->{'_opts'}->{'d'};
80 0 ▶ if (defined $tmp_dir && ! -d $tmp_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tmp_dir and not -d $tmp_dir) 0 0 0
Condition0%0/3
defined $tmp_dir and not -d $tmp_dir
!ll&&!rl&&r
000
MC/DC0%0/2
defined $tmp_dir and not -d $tmp_dir
defined $tmp_dir not -d $tmp_dir
Truth table0%0/3
A and not(B)
A= defined $tmp_dirB= not -d $tmp_dir
ABresult
0X0
100
111
81 0 mkpath($tmp_dir);
82 }
83 0 ▶ if (! defined $tmp_dir || ! -d $tmp_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tmp_dir and -d $tmp_dir) 0 0 0
Condition0%0/3
defined $tmp_dir and -d $tmp_dir
!ll&&!rl&&r
000
MC/DC0%0/2
defined $tmp_dir and -d $tmp_dir
defined $tmp_dir -d $tmp_dir
Truth table0%0/3
A and B
A= defined $tmp_dirB= -d $tmp_dir
ABresult
0X0
100
111
84 0 $tmp_dir = tempdir(CLEANUP => 1);
85 }
86 0 $self->{'_tmp_dir'} = $tmp_dir;
87  
88 # Copy original file to dir.
89 0 my $dwg_file_first = catfile($tmp_dir, 'first.dwg');
90 0 copy($self->{'_dwg_file'}, $dwg_file_first);
91  
92 # Get magic string.
93 0 my $magic = detect_dwg_file($dwg_file_first);
94 0 ▶ if (! exists $REL{$magic}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $App::Test::DWG::LibreDWG::JSON::REL{$magic}) 0 0 0
95 0 print STDERR "dwgwrite for magic '$magic' doesn't supported.\n";
96 0 return 1;
97 }
98 0 my $dwgwrite_version = $REL{$magic};
99  
100 # Verbose level.
101 0 my $v = '-v'.$self->{'_opts'}->{'v'};
102  
103 0 ▶ my $dwgread = $ENV{'DWGREAD'} || $DR;
Condition0%0/3
$ENV{'DWGREAD'} || $App::Test::DWG::LibreDWG::JSON::DR
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'DWGREAD'} || $App::Test::DWG::LibreDWG::JSON::DR
$ENV{'DWGREAD'} $App::Test::DWG::LibreDWG::JSON::DR
Truth table0%0/3
A || B
A= $ENV{'DWGREAD'}B= $App::Test::DWG::LibreDWG::JSON::DR
ABresult
1X1
011
000
104 0 ▶ my $dwgwrite = $ENV{'DWGWRITE'} || $DW;
Condition0%0/3
$ENV{'DWGWRITE'} || $App::Test::DWG::LibreDWG::JSON::DW
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'DWGWRITE'} || $App::Test::DWG::LibreDWG::JSON::DW
$ENV{'DWGWRITE'} $App::Test::DWG::LibreDWG::JSON::DW
Truth table0%0/3
A || B
A= $ENV{'DWGWRITE'}B= $App::Test::DWG::LibreDWG::JSON::DW
ABresult
1X1
011
000
105  
106 # Convert dwg file to JSON.
107 0 my $json_file_first = catfile($tmp_dir, 'first.json');
108 0 my $dwg_to_json_first = "$dwgread $v -o $json_file_first $dwg_file_first";
109 0 ▶ if ($self->_exec($dwg_to_json_first, 'dwg_to_json')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_exec($dwg_to_json_first, "dwg_to_json")) 0 0 0
110 0 return 1;
111 }
112  
113 # Convert JSON to dwg file.
114 0 my $dwg_file_second = catfile($tmp_dir, 'second.dwg');
115 0 my $json_to_dwg_first = "$dwgwrite --as $dwgwrite_version $v -o $dwg_file_second $json_file_first";
116 0 ▶ if ($self->_exec($json_to_dwg_first, 'json_to_dwg')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_exec($json_to_dwg_first, "json_to_dwg")) 0 0 0
117 0 return 1;
118 }
119  
120 # Convert new dwg file to JSON.
121 0 my $json_file_second = catfile($tmp_dir, 'second.json');
122 0 my $dwg_to_json_second = "$dwgread $v -o $json_file_second $dwg_file_second";
123 0 ▶ if ($self->_exec($dwg_to_json_second, 'dwg_to_json_second')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_exec($dwg_to_json_second, "dwg_to_json_second")) 0 0 0
124 0 return 1;
125 }
126  
127 # Compare JSON files.
128 0 my $diff = "diff $json_file_first $json_file_second";
129 0 system($diff);
130  
131 0 return 0;
132 }
133  
134 0 ▶sub _exec {
Subroutine _exec: not called CC=7 SCAR=40.3
135 0 my ($self, $command, $log_prefix) = @_;
136  
137 0 ▶ my ($stdout, $stderr, $exit_code) = capture {
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 0 system($command);
139 0 };
140  
141 0 ▶ if (defined $log_prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $log_prefix) 0 0 0
142 0 ▶ if ($stdout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stdout) 0 0 0
143 0 my $stdout_file = catfile($self->{'_tmp_dir'},
144 $log_prefix.'-stdout.log');
145 0 barf($stdout_file, $stdout);
146 }
147 0 ▶ if ($stderr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stderr) 0 0 0
148 0 my $stderr_file = catfile($self->{'_tmp_dir'},
149 $log_prefix.'-stderr.log');
150 0 barf($stderr_file, $stderr);
151  
152 # Report errors.
153 0 ▶ if (! $self->{'_opts'}->{'i'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_opts"}{"i"}) 0 0 0
154 0 ▶ if (my @num = ($stderr =~ m/ERROR/gms)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@num) = $stderr =~ /ERROR/gms) 0 0 0
155 0 print STDERR "Command '$command' has ".scalar @num." ERRORs\n";
156 }
157 }
158 }
159 }
160  
161 0 ▶ if ($exit_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code) 0 0 0
162 0 print STDERR "Command '$command' exit with $exit_code.\n";
163 0 return 1;
164 }
165  
166 0 return 0;
167 }
168  
169 4 1;
170  
171  
172 __END__
↑ Summary