FormMagick/Validator/Geography.pm untested

filter: Statementstmt 0.0% 0 / 23 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 46 CC 6 SCAR 21.7
CC
6
Cov
0%
iso_country_code
12.0
US_zipcode
12.0
US_state
6.0
CRAP 8.8
1 #!/usr/bin/perl -w
2 #
3 # FormMagick (c) 2000-2001 Kirrily Robert <skud@cpan.org>
4 # Copyright (c) 2000-2002 Mitel Networks Corporation
5 # This software is distributed under the same licenses as Perl itself;
6 # see the file COPYING for details.
7 #
8 # $Id: Geography.pm,v 1.4 2003/02/05 17:18:39 anoncvs_gtkglext Exp $
9 #
10  
11 package CGI::FormMagick::Validator;
12  
13 =pod
14  
15 =head1 NAME
16  
17 CGI::FormMagick::Validator::Geography - geography-related validation routines
18  
19 =head1 SYNOPSIS
20  
21 use CGI::FormMagick;
22  
23 =head1 DESCRIPTION
24  
25 See CGI::FormMagick::Validator for a general description.
26  
27 =for testing
28 BEGIN: {
29 use CGI::FormMagick::Validator;
30 }
31  
32 =head2 Validation routines provided:
33  
34 =over 4
35  
36 =item iso_country_code
37  
38 The data is a standard 2-letter ISO country code. Requires the Locale::Country
39 module to be installed.
40  
41 =begin testing
42  
43 SKIP: {
44 skip "Locale::Country not installed", 4
45 unless eval { require Locale::Country };
46  
47 ok( iso_country_code() ne "OK" , "undef is not a country");
48 ok( iso_country_code(undef, "") ne "OK" , "empty string is not a country");
49 ok( iso_country_code(undef, "00") ne "OK" , "00 is not a country");
50 ok( iso_country_code(undef, "au") eq "OK" , "au is a country");
51 }
52  
53 =end testing
54  
55 =cut
56  
57 sub iso_country_code {
58 my ($fm, $country) = @_;
59  
60 require Locale::Country;
61 my @countries = Locale::Country::all_country_codes();
62  
63 if ( not defined $country ) {
64 return "You must provide a country code";
65 } elsif ( grep /^$country$/, @countries ) {
66 return "OK";
67 } else {
68 return "This field does not contain an ISO country code";
69 }
70 }
71  
72 =pod
73  
74 =item US_state
75  
76 The data is a standard 2-letter US state abbreviation. Uses
77 Geography::State in non-strict mode, so this module must be installed
78 for it to work.
79  
80 =begin testing
81  
82 SKIP: {
83 skip "Geography::States broken, this needs figuring out", 5
84 unless eval { require Geography::States };
85  
86 ok( US_state(undef, "or") eq "OK" , "Oregon is a US state");
87 ok( US_state(undef, "OR") eq "OK" , "Oregon is a US state");
88 ok( US_state() ne "OK" , "undef is not a US state");
89 ok( US_state(undef, "") ne "OK" , "empty string is not a US state");
90 ok( US_state(undef, "zz") ne "OK" , "zz is not a US state");
91  
92 }
93  
94 =end testing
95  
96 =cut
97  
98 sub US_state {
99 my ($fm, $data) = @_;
100 require Geography::States;
101  
102 my $us = Geography::States->new('USA');
103  
104 if ($data && $us->state(uc($data))) {
105 return "OK";
106 } else {
107 return "This doesn't appear to be a valid 2-letter US state abbreviation"
108 }
109 }
110  
111  
112 =item US_zipcode
113  
114 The data looks like a valid US zipcode
115  
116 =for testing
117 ok( US_zipcode() ne "OK" , "undef is not a US zipcode");
118 ok( US_zipcode(undef, "") ne "OK" , "empty string is not a US zipcode");
119 ok( US_zipcode(undef, "abc") ne "OK" , "abc is not a US zipcode");
120 ok( US_zipcode(undef, "2210") ne "OK" , "2210 is not a US zipcode");
121 ok( US_zipcode(undef, "90210") eq "OK" , "90210 is a US zipcode");
122 ok( US_zipcode(undef, "a0210") ne "OK" , "a0210 is not a US zipcode");
123 ok( US_zipcode(undef, "123456789") eq "OK" , "123456789 is a valid US zipcode");
124 ok( US_zipcode(undef, "12345-6789") eq "OK" , "12345-6789 is a valid US zipcode");
125  
126 =cut
127  
128 sub US_zipcode {
129 my ($fm, $data) = @_;
130  
131 if (not $data) {
132 return "You must enter a US zip code";
133 } elsif ($data =~ /^\d{5}(-?\d{4})?$/) {
134 return "OK";
135 } else {
136 return "US zip codes must contain 5 or 9 numbers";
137 }
138 }
139  
140 =pod
141  
142 =head1 SEE ALSO
143  
144 The main perldoc for CGI::FormMagick
145  
146 =head1 AUTHOR
147  
148 Kirrily "Skud" Robert <skud@infotrope.net>
149  
150 More information about FormMagick may be found at
151 http://sourceforge.net/projects/formmagick/
152  
153 =cut
154  
155 return 1;