Debian Codegraph @

t/check_data_structure.t

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #!/usr/bin/perl
2
3 use Test::More;
4
5 use File::Spec::Functions qw(catfile);
6
7 subtest 'setup' => sub {
8 use_ok( 'Business::ISBN::Data' );
9 ok( %Business::ISBN::country_data );
10 };
11
12 subtest 'included_range_message' => sub {
13 # Test with included RangeMessage.xml in the same spot as the module
14 like( $Business::ISBN::country_data{_source}, qr/RangeMessage\.xml/ );
15 like( $Business::ISBN::country_data{_source}, qr/blib/ );
16 foreach my $isbn_prefix ("978", "979") {
17 foreach my $key ( sort { $a <=> $b } grep { ! /\A_/ } keys %{ $Business::ISBN::country_data{$isbn_prefix} } ) {
18 my $value = $Business::ISBN::country_data{$isbn_prefix}->{$key};
19 isa_ok( $value, ref [], "Value is array ref for country $key" );
20
21 my( $country, $ranges ) = @$value;
22
23 my $count = @$ranges;
24
25 ok( ($count % 2) == 0, "Even number of elements ($count) for country $key" );
26 }
27 }
28 };
29
30
31 subtest 'env_range_message' => sub {
32 # Test with RangeMessage.xml set in ISBN_RANGE_MESSAGE
33 local $ENV{ISBN_RANGE_MESSAGE} = catfile( qw(lib Business ISBN RangeMessage.xml) );
34 local %Business::ISBN::country_data = Business::ISBN::Data::_get_data();
35 ok( -e $ENV{ISBN_RANGE_MESSAGE}, 'Alternate RangeMessage.xml exists' );
36 unlike( $Business::ISBN::country_data{_source}, qr/blib/ );
37 like( $Business::ISBN::country_data{_source}, qr/RangeMessage\.xml/ );
38 foreach my $isbn_prefix ("978", "979") {
39 foreach my $key ( sort { $a <=> $b } grep { ! /\A_/ } keys %{ $Business::ISBN::country_data{$isbn_prefix} } ) {
40 my $value = $Business::ISBN::country_data{$isbn_prefix}->{$key};
41 isa_ok( $value, ref [], "Value is array ref for country $key" );
42
43 my( $country, $ranges ) = @$value;
44
45 my $count = @$ranges;
46
47 ok( ($count % 2) == 0, "Even number of elements ($count) for country $key" );
48 }
49 }
50 };
51
52 subtest 'missing_range_message' => sub {
53 # Test with RangeMessage.xml set in ISBN_RANGE_MESSAGE
54
55 my $file = catfile( qw(blib lib Business ISBN RangeMessage.xml) );
56 my $out_of_the_way = $file . '.hidden';
57
58 rename $file => $out_of_the_way;
59
60 ok( ! -e $file, 'RangeMessage.xml is out of the way' );
61 local %Business::ISBN::country_data = Business::ISBN::Data::_get_data();
62
63 like( $Business::ISBN::country_data{_source}, qr/\bData\.pm/, 'Data source is the default data structure' );
64
65 rename $out_of_the_way => $file;
66 };
67
68 subtest 'default_data' => sub {
69 # Test with default data
70 local %Business::ISBN::country_data = Business::ISBN::Data::_default_data();
71 like( $Business::ISBN::country_data{_source}, qr/Data\.pm/ );
72
73 foreach my $isbn_prefix ("978", "979") {
74 foreach my $key ( sort { $a <=> $b } grep { ! /\A_/ } keys %{ $Business::ISBN::country_data{$isbn_prefix} } ) {
75 my $value = $Business::ISBN::country_data{$isbn_prefix}->{$key};
76 isa_ok( $value, ref [], "Value is array ref for country $key" );
77
78 my( $country, $ranges ) = @$value;
79
80 my $count = @$ranges;
81
82 ok( ($count % 2) == 0, "Even number of elements ($count) for country $key" );
83 }
84 }
85 };
86
87 done_testing();