blib/lib/Geo/Postcodes/NO/Check.pm

filter: Statementstmt 33.9% 57 / 168 Branchbran 14.8% 16 / 108 Conditioncond 12.8% 9 / 70 MC/DCmcdc 0.0% 0 / 44 Subroutinesub 41.1% 7 / 17 Podpod 100% 10 / 10 totaltotal 23.7% 99 / 417 CC 83 SCAR 50.3
CC
83
Cov
24%
read_records
224.7
read_records
224.7
check
132.0
CRAP 152.8
↑ Summary blib/lib/Geo/Postcodes/NO.pm »
1 package Geo::Postcodes::NO::Check;
2  
3 15 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 15 ▶use Encode qw(decode FB_CROAK);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 15 our $VERSION = '1.00';
11  
12 15 my $DATA_ENV = 'GEO_POSTCODES_NO_DATA';
13 15 my $DATA_ENCODING_ENV = 'GEO_POSTCODES_NO_ENCODING';
14 15 my $DEFAULT_ENCODING = 'cp1252';
15  
16 15 my %COUNTY = (
17 '03' => 'OSLO',
18 '11' => 'ROGALAND',
19 '15' => 'MØRE OG ROMSDAL',
20 '18' => 'NORDLAND',
21 '21' => 'SVALBARD',
22 '22' => 'JAN MAYEN',
23 '31' => 'ØSTFOLD',
24 '32' => 'AKERSHUS',
25 '33' => 'BUSKERUD',
26 '34' => 'INNLANDET',
27 '39' => 'VESTFOLD',
28 '40' => 'TELEMARK',
29 '42' => 'AGDER',
30 '46' => 'VESTLAND',
31 '50' => 'TRØNDELAG',
32 '55' => 'TROMS',
33 '56' => 'FINNMARK',
34 );
35  
36 15 ▶sub county_table {
Subroutine county_table: called CC=1 SCAR=1.2
Pod documented
37 0 ▶ return \%COUNTY;
Subroutine county_table: not called CC=1 SCAR=1.2
Pod documented
38 }
39  
40 0 ▶sub main {
Subroutine main: not called CC=6 SCAR=37.4
Pod documented
41 0 ▶ shift if @_ && defined $_[0] && $_[0] eq __PACKAGE__;
Subroutine main: not called CC=6 SCAR=37.4
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if @_ and defined $_[0] and $_[0] eq __PACKAGE__ 0 0 0
if @_ and defined $_[0] and $_[0] eq __PACKAGE__ 0 0 0
Condition0%0/12
@_ and defined $_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
000
@_ and defined $_[0]
!ll&&!rl&&r
000
@_ and defined $_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
000
@_ and defined $_[0]
!ll&&!rl&&r
000
MC/DC0%0/3
@_ and defined $_[0] and $_[0] eq __PACKAGE__
@_ defined $_[0] $_[0] eq __PACKAGE__
MC/DC0%0/3
@_ and defined $_[0] and $_[0] eq __PACKAGE__
@_ defined $_[0] $_[0] eq __PACKAGE__
Truth table0%0/4
A and B and C
A= @_B= defined $_[0]C= $_[0] eq __PACKAGE__
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B and C
A= @_B= defined $_[0]C= $_[0] eq __PACKAGE__
ABCresult
0XX0
10X0
1100
1111
42  
43 0 ▶ if (@_ != 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ != 1) 0 0 0
if (@_ != 1) 0 0 0
44 0 print STDERR "Usage: check-postcodes-no /path/to/postnummerregister-ansi.txt\n";
45 0 return 64;
46 }
47  
48 0 my $path = shift;
49 0 $ENV{$DATA_ENV} = $path;
50  
51 0 my $ok = eval { check($path); 1 };
52 0 ▶ if (!$ok) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($ok) 0 0 0
unless ($ok) 0 0 0
53 0 print STDERR $@;
54 0 return 1;
55 }
56  
57 0 return 0;
58 }
59  
60 0 ▶sub check {
Subroutine check: not called CC=11 SCAR=48.8
Pod documented
61 0 ▶ my $path = shift;
Subroutine check: not called CC=11 SCAR=48.8
Pod documented
62  
63 0 ▶ $ENV{$DATA_ENV} = $path if defined $path && length $path;
Branch: 2 branches
BranchTrueFalseTotal
if defined $path and length $path 0 0 0
if defined $path and length $path 0 0 0
Condition0%0/6
defined $path and length $path
!ll&&!rl&&r
000
defined $path and length $path
!ll&&!rl&&r
000
MC/DC0%0/2
defined $path and length $path
defined $path length $path
MC/DC0%0/2
defined $path and length $path
defined $path length $path
Truth table0%0/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
64 0 ▶ $path = $ENV{$DATA_ENV}
Branch: 2 branches
BranchTrueFalseTotal
$path = $ENV{$DATA_ENV} or die "$DATA_ENV is not set\n" 0 0 0
$path = $ENV{$DATA_ENV} or die "$DATA_ENV is not set\n" 0 0 0
65 or die "$DATA_ENV is not set\n";
66  
67 0 ▶ my $encoding = $ENV{$DATA_ENCODING_ENV} || $DEFAULT_ENCODING;
Condition0%0/6
$ENV{$DATA_ENCODING_ENV} || $DEFAULT_ENCODING
l!l&&r!l&&!r
000
$ENV{$DATA_ENCODING_ENV} || $DEFAULT_ENCODING
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{$DATA_ENCODING_ENV} || $DEFAULT_ENCODING
$ENV{$DATA_ENCODING_ENV} $DEFAULT_ENCODING
MC/DC0%0/2
$ENV{$DATA_ENCODING_ENV} || $DEFAULT_ENCODING
$ENV{$DATA_ENCODING_ENV} $DEFAULT_ENCODING
Truth table0%0/3
A || B
A= $ENV{$DATA_ENCODING_ENV}B= $DEFAULT_ENCODING
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $ENV{$DATA_ENCODING_ENV}B= $DEFAULT_ENCODING
ABresult
1X1
011
000
68 0 my ($records, $seen) = read_records($path, $encoding, county_prefixes => [ keys %COUNTY ]);
69 0 my $record_count = @$records;
70  
71 0 require Geo::Postcodes::NO;
72  
73 0 my $loaded_path = Geo::Postcodes::NO::data_file();
74 0 ▶ die "Geo::Postcodes::NO loaded '$loaded_path', expected '$path'\n"
Branch: 2 branches
BranchTrueFalseTotal
unless $loaded_path eq $path 0 0 0
unless $loaded_path eq $path 0 0 0
75 unless $loaded_path eq $path;
76  
77 0 my $loaded_encoding = Geo::Postcodes::NO::data_encoding();
78 0 ▶ die "Geo::Postcodes::NO used encoding '$loaded_encoding', expected '$encoding'\n"
Branch: 2 branches
BranchTrueFalseTotal
unless $loaded_encoding eq $encoding 0 0 0
unless $loaded_encoding eq $encoding 0 0 0
79 unless $loaded_encoding eq $encoding;
80  
81 0 my @loaded = Geo::Postcodes::NO::get_postcodes();
82 0 ▶ die "Geo::Postcodes::NO loaded " . scalar(@loaded) . " postcodes, expected $record_count\n"
Branch: 2 branches
BranchTrueFalseTotal
unless @loaded == $record_count 0 0 0
unless @loaded == $record_count 0 0 0
83 unless @loaded == $record_count;
84  
85 0 for my $postcode (sort keys %$seen) {
86 0 ▶ die "Postcode $postcode was validated but not loaded by Geo::Postcodes::NO\n"
Branch: 2 branches
BranchTrueFalseTotal
unless Geo::Postcodes::NO::valid($postcode) 0 0 0
unless Geo::Postcodes::NO::valid($postcode) 0 0 0
87 unless Geo::Postcodes::NO::valid($postcode);
88  
89 0 ▶ die "Postcode $postcode has no county in Geo::Postcodes::NO\n"
Branch: 2 branches
BranchTrueFalseTotal
unless defined Geo::Postcodes::NO::county_of($postcode) 0 0 0
unless defined Geo::Postcodes::NO::county_of($postcode) 0 0 0
90 unless defined Geo::Postcodes::NO::county_of($postcode);
91 }
92  
93 0 print "OK: $record_count postcodes loaded from $path using $encoding\n";
94 0 return $record_count;
95 }
96  
97 0 ▶sub validate_file {
Subroutine validate_file: not called CC=1 SCAR=6.9
Pod documented
98 0 ▶ my ($path, $encoding) = @_;
Subroutine validate_file: not called CC=1 SCAR=6.9
Pod documented
99 0 my ($records, $seen) = read_records($path, $encoding);
100 0 return (scalar @$records, $seen);
101 }
102  
103 15 ▶sub read_records {
Subroutine read_records: called CC=26 SCAR=54.1
Pod documented
104 0 ▶ my ($path, $encoding, %option) = @_;
Subroutine read_records: not called CC=26 SCAR=54.1
Pod documented
105 0 ▶ $encoding = $DEFAULT_ENCODING unless defined $encoding && length $encoding;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $encoding and length $encoding 0 15 15
unless defined $encoding and length $encoding 0 0 0
Condition16%1/6
defined $encoding and length $encoding
!ll&&!rl&&r
0015
defined $encoding and length $encoding
!ll&&!rl&&r
000
MC/DC0%0/2
defined $encoding and length $encoding
defined $encoding length $encoding
MC/DC0%0/2
defined $encoding and length $encoding
defined $encoding length $encoding
Truth table33%1/3
A and B
A= defined $encodingB= length $encoding
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $encodingB= length $encoding
ABresult
0X0
100
111
106  
107 0 ▶ open my $fh, '<:raw', $path or die "Cannot read postcode data $path: $!\n";
Branch: 2 branches
BranchTrueFalseTotal
open my $fh, "<:raw", $path or die "Cannot read postcode data ${path}: $!\n" 15 0 15
open my $fh, "<:raw", $path or die "Cannot read postcode data ${path}: $!\n" 0 0 0
108 0 my $raw = do { local $/; <$fh> };
109  
110 0 ▶ my $decode_subject = defined $option{decode_subject} ? $option{decode_subject} : $path;
Branch: 2 branches
BranchTrueFalseTotal
defined $option{'decode_subject'} ? : 15 0 15
defined $option{'decode_subject'} ? : 0 0 0
111 0 my $data = eval { decode($encoding, $raw, FB_CROAK) };
112 0 ▶ die "Cannot decode $decode_subject as $encoding: $@" if $@;
Branch: 2 branches
BranchTrueFalseTotal
if $@ 0 15 15
if $@ 0 0 0
113  
114 0 $data =~ s/\r\n?/\n/g;
115  
116 0 ▶ my %county_prefix = map { $_ => 1 } @{ $option{county_prefixes} || [] };
Condition25%1/4
$option{'county_prefixes'} || []
l!l
150
$option{'county_prefixes'} || []
l!l
00
MC/DC0%0/1
$option{'county_prefixes'} || []
$option{'county_prefixes'}
MC/DC0%0/1
$option{'county_prefixes'} || []
$option{'county_prefixes'}
Truth table50%1/2
A
A= $option{'county_prefixes'}
Aresult
11
00
Truth table0%0/2
A
A= $option{'county_prefixes'}
Aresult
11
00
117 0 my @records;
118 my %seen;
119 0 my %borough_name;
120 0 my $line_no = 0;
121  
122 0 for my $line (split /\n/, $data) {
123 0 ++$line_no;
124 0 ▶ next unless length $line;
Branch: 2 branches
BranchTrueFalseTotal
unless length $line 0 71569 71569
unless length $line 0 0 0
125  
126 0 my @fields = split /\t/, $line, -1;
127 0 ▶ _line_error($line_no, $path, $option{line_errors_include_path}, 'expected 5 tab-separated fields')
Branch: 2 branches
BranchTrueFalseTotal
unless @fields == 5 0 71569 71569
unless @fields == 5 0 0 0
128 unless @fields == 5;
129  
130 0 my ($postcode, $location, $borough_number, $borough, $category) = @fields;
131  
132 0 ▶ _line_error($line_no, $path, $option{line_errors_include_path}, "invalid postcode '$postcode'")
Branch: 2 branches
BranchTrueFalseTotal
unless defined $postcode and $postcode =~ /\A[0-9]{4}\z/ 0 71569 71569
unless defined $postcode and $postcode =~ /\A[0-9]{4}\z/ 0 0 0
Condition16%1/6
defined $postcode and $postcode =~ /\A[0-9]{4}\z/
!ll&&!rl&&r
0071569
defined $postcode and $postcode =~ /\A[0-9]{4}\z/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $postcode and $postcode =~ /\A[0-9]{4}\z/
defined $postcode $postcode =~ /\A[0-9]{4}\z/
MC/DC0%0/2
defined $postcode and $postcode =~ /\A[0-9]{4}\z/
defined $postcode $postcode =~ /\A[0-9]{4}\z/
Truth table33%1/3
A and B
A= defined $postcodeB= $postcode =~ /\A[0-9]{4}\z/
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $postcodeB= $postcode =~ /\A[0-9]{4}\z/
ABresult
0X0
100
111
133 unless defined $postcode && $postcode =~ /\A[0-9]{4}\z/;
134 0 ▶ _line_error($line_no, $path, $option{line_errors_include_path}, "empty location for postcode $postcode")
Branch: 2 branches
BranchTrueFalseTotal
unless defined $location and length $location 0 71569 71569
unless defined $location and length $location 0 0 0
Condition16%1/6
defined $location and length $location
!ll&&!rl&&r
0071569
defined $location and length $location
!ll&&!rl&&r
000
MC/DC0%0/2
defined $location and length $location
defined $location length $location
MC/DC0%0/2
defined $location and length $location
defined $location length $location
Truth table33%1/3
A and B
A= defined $locationB= length $location
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $locationB= length $location
ABresult
0X0
100
111
135 unless defined $location && length $location;
136 0 ▶ _line_error($line_no, $path, $option{line_errors_include_path}, "invalid borough number '$borough_number'")
Branch: 2 branches
BranchTrueFalseTotal
unless defined $borough_number and $borough_number =~ /\A[0-9]{4}\z/ 0 71569 71569
unless defined $borough_number and $borough_number =~ /\A[0-9]{4}\z/ 0 0 0
Condition16%1/6
defined $borough_number and $borough_number =~ /\A[0-9]{4}\z/
!ll&&!rl&&r
0071569
defined $borough_number and $borough_number =~ /\A[0-9]{4}\z/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $borough_number and $borough_number =~ /\A[0-9]{4}\z/
defined $borough_number $borough_number =~ /\A[0-9]{4}\z/
MC/DC0%0/2
defined $borough_number and $borough_number =~ /\A[0-9]{4}\z/
defined $borough_number $borough_number =~ /\A[0-9]{4}\z/
Truth table33%1/3
A and B
A= defined $borough_numberB= $borough_number =~ /\A[0-9]{4}\z/
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $borough_numberB= $borough_number =~ /\A[0-9]{4}\z/
ABresult
0X0
100
111
137 unless defined $borough_number && $borough_number =~ /\A[0-9]{4}\z/;
138 0 ▶ _line_error($line_no, $path, $option{line_errors_include_path}, "empty borough for postcode $postcode")
Branch: 2 branches
BranchTrueFalseTotal
unless defined $borough and length $borough 0 71569 71569
unless defined $borough and length $borough 0 0 0
Condition16%1/6
defined $borough and length $borough
!ll&&!rl&&r
0071569
defined $borough and length $borough
!ll&&!rl&&r
000
MC/DC0%0/2
defined $borough and length $borough
defined $borough length $borough
MC/DC0%0/2
defined $borough and length $borough
defined $borough length $borough
Truth table33%1/3
A and B
A= defined $boroughB= length $borough
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $boroughB= length $borough
ABresult
0X0
100
111
139 unless defined $borough && length $borough;
140 0 ▶ _line_error($line_no, $path, $option{line_errors_include_path}, "invalid category '$category'")
Branch: 2 branches
BranchTrueFalseTotal
unless defined $category and $category =~ /\A[GPBS]\z/ 0 71569 71569
unless defined $category and $category =~ /\A[GPBS]\z/ 0 0 0
Condition16%1/6
defined $category and $category =~ /\A[GPBS]\z/
!ll&&!rl&&r
0071569
defined $category and $category =~ /\A[GPBS]\z/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $category and $category =~ /\A[GPBS]\z/
defined $category $category =~ /\A[GPBS]\z/
MC/DC0%0/2
defined $category and $category =~ /\A[GPBS]\z/
defined $category $category =~ /\A[GPBS]\z/
Truth table33%1/3
A and B
A= defined $categoryB= $category =~ /\A[GPBS]\z/
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $categoryB= $category =~ /\A[GPBS]\z/
ABresult
0X0
100
111
141 unless defined $category && $category =~ /\A[GPBS]\z/;
142 _line_error($line_no, $path, $option{line_errors_include_path}, "duplicate postcode '$postcode'")
143 0 ▶ if $seen{$postcode}++;
Branch: 2 branches
BranchTrueFalseTotal
if $seen{$postcode}++ 0 71569 71569
if $seen{$postcode}++ 0 0 0
144 _line_error($line_no, $path, $option{line_errors_include_path},
145 "conflicting borough names for number '$borough_number': '$borough_name{$borough_number}' vs '$borough'")
146 0 ▶ if defined $borough_name{$borough_number} && $borough_name{$borough_number} ne $borough;
Branch: 2 branches
BranchTrueFalseTotal
if defined $borough_name{$borough_number} and $borough_name{$borough_number} ne $borough 0 71569 71569
if defined $borough_name{$borough_number} and $borough_name{$borough_number} ne $borough 0 0 0
Condition33%2/6
defined $borough_name{$borough_number} and $borough_name{$borough_number} ne $borough
!ll&&!rl&&r
5027665420
defined $borough_name{$borough_number} and $borough_name{$borough_number} ne $borough
!ll&&!rl&&r
000
MC/DC0%0/2
defined $borough_name{$borough_number} and $borough_name{$borough_number} ne $borough
defined $borough_name{$borough_number} $borough_name{$borough_number} ne $borough
MC/DC0%0/2
defined $borough_name{$borough_number} and $borough_name{$borough_number} ne $borough
defined $borough_name{$borough_number} $borough_name{$borough_number} ne $borough
Truth table66%2/3
A and B
A= defined $borough_name{$borough_number}B= $borough_name{$borough_number} ne $borough
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $borough_name{$borough_number}B= $borough_name{$borough_number} ne $borough
ABresult
0X0
100
111
147 0 $borough_name{$borough_number} = $borough;
148  
149 0 ▶ if (%county_prefix) {
Branch: 2 branches
BranchTrueFalseTotal
if (%county_prefix) 71569 0 71569
if (%county_prefix) 0 0 0
150 _line_error($line_no, $path, $option{line_errors_include_path}, "unknown county for borough number '$borough_number'")
151 0 ▶ unless $county_prefix{substr($borough_number, 0, 2)};
Branch: 2 branches
BranchTrueFalseTotal
unless $county_prefix{substr($borough_number, 0, 2)} 0 71569 71569
unless $county_prefix{substr($borough_number, 0, 2)} 0 0 0
152 }
153  
154 0 push @records, [ $postcode, $location, $borough_number, $borough, $category ];
155 }
156  
157 0 ▶ die "No postcode records found in $path\n" unless @records;
Branch: 2 branches
BranchTrueFalseTotal
unless @records 0 15 15
unless @records 0 0 0
158  
159 0 return (\@records, \%seen);
160 }
161  
162 0 ▶sub _line_error {
Subroutine _line_error: not called CC=2 SCAR=17.9
163 0 ▶ my ($line_no, $path, $include_path, $message) = @_;
Subroutine _line_error: not called CC=2 SCAR=17.9
164 0 ▶ my $where = $include_path ? "Line $line_no in $path" : "Line $line_no";
Branch: 2 branches
BranchTrueFalseTotal
$include_path ? : 0 0 0
$include_path ? : 0 0 0
165 0 die "$where: $message\n";
166 }
167  
168 15 1;
169  
170 __END__
↑ Summary blib/lib/Geo/Postcodes/NO.pm »