Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 164 lines (139 sloc) 4.226 kB
634795b make catalyst branch the new trunk
autarch authored
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use VegGuide::Config;
6
d752bd7 @autarch perltidy all code
autarch authored
7 BEGIN {
634795b make catalyst branch the new trunk
autarch authored
8 use Test::MockObject;
9 Test::MockObject->new()->fake_module('Geo::Coder::Google');
10 }
11
a38311f @autarch Make Geocoder.t pass
autarch authored
12 {
13
14 package Geo::Coder::Google;
15
16 our $VERSION = 42;
17
18 sub new {
19 return bless {}, shift;
20 }
21
22 sub geocode {
23 my $self = shift;
24 my $address = shift;
25
26 my @coords = (
27 [ qr/\Q2600 Emerson/ => [ 44.955565, -93.294425 ] ],
28 [ qr/\Q27 King/ => [ -25.335448, 135.745076 ] ],
29 [ qr/\Q254 Spadina/ => [ 43.651664, -79.397848 ] ],
30 [ qr/\Q中区央/ => [ 123, 456 ] ],
31 );
32
33 for my $pair (@coords) {
34 if ( $address =~ $pair->[0] ) {
35 return $self->_fake_return( @{ $pair->[1] } );
36 }
37 }
38
39 return $self->_fake_return();
40 }
41
42 sub _fake_return {
43 shift;
44 my $lat = shift;
45 my $long = shift;
46
47 return { Point => { coordinates => [ $long, $lat ] } };
48 }
49 }
50
634795b make catalyst branch the new trunk
autarch authored
51 require VegGuide::Geocoder;
52
53 my %addresses = address_test_data();
54
55 NEW:
56 {
d752bd7 @autarch perltidy all code
autarch authored
57 ok(
58 !VegGuide::Geocoder->new( country => 'does not exist' ),
59 'Cannot make Geocoder for an invalid country'
60 );
61
62 for my $c ( sort keys %addresses ) {
63 ok(
64 VegGuide::Geocoder->new( country => $c ),
65 "Can make Geocoder for $c"
66 );
634795b make catalyst branch the new trunk
autarch authored
67 }
68 }
69
70 GEOCODE:
71 {
d752bd7 @autarch perltidy all code
autarch authored
72 for my $c ( sort keys %addresses ) {
634795b make catalyst branch the new trunk
autarch authored
73 my $geocoder = VegGuide::Geocoder->new( country => $c );
74
a38311f @autarch Make Geocoder.t pass
autarch authored
75 my $result = $geocoder->geocode( %{ $addresses{$c}{address} } );
634795b make catalyst branch the new trunk
autarch authored
76
d752bd7 @autarch perltidy all code
autarch authored
77 is(
a38311f @autarch Make Geocoder.t pass
autarch authored
78 $result->latitude(), $addresses{$c}{expect}{lat},
d752bd7 @autarch perltidy all code
autarch authored
79 "lat for $c is $addresses{$c}{expect}{lat}"
80 );
81 is(
a38311f @autarch Make Geocoder.t pass
autarch authored
82 $result->longitude(), $addresses{$c}{expect}{long},
d752bd7 @autarch perltidy all code
autarch authored
83 "long for $c is $addresses{$c}{expect}{long}"
84 );
634795b make catalyst branch the new trunk
autarch authored
85 }
86 }
87
88 ADDRESS_PROCESSING:
89 {
d752bd7 @autarch perltidy all code
autarch authored
90 for my $c ( sort keys %addresses ) {
634795b make catalyst branch the new trunk
autarch authored
91 my $geocoder = VegGuide::Geocoder->new( country => $c );
92
d752bd7 @autarch perltidy all code
autarch authored
93 my $meth = $geocoder->{method};
634795b make catalyst branch the new trunk
autarch authored
94 my $address = $geocoder->$meth( %{ $addresses{$c}{address} } );
95
d752bd7 @autarch perltidy all code
autarch authored
96 is(
97 $address, $addresses{$c}{expect}{processed},
98 "address processing for $c"
99 );
634795b make catalyst branch the new trunk
autarch authored
100 }
101 }
102
a38311f @autarch Make Geocoder.t pass
autarch authored
103 done_testing();
104
d752bd7 @autarch perltidy all code
autarch authored
105 sub address_test_data {
106 return (
107 'United States' => {
108 address => {
a38311f @autarch Make Geocoder.t pass
autarch authored
109 address1 => '2600 Emerson Ave S',
d752bd7 @autarch perltidy all code
autarch authored
110 city => 'Minneapolis',
111 region => 'MN',
112 postal_code => '55408-1234',
113 },
114 expect => {
115 lat => 44.955565,
116 long => -93.294425,
a38311f @autarch Make Geocoder.t pass
autarch authored
117 processed => '2600 Emerson Ave S, 55408-1234, USA',
d752bd7 @autarch perltidy all code
autarch authored
118 },
119 },
120 'Australia' => {
121 address => {
a38311f @autarch Make Geocoder.t pass
autarch authored
122 address1 => '27 King Street',
d752bd7 @autarch perltidy all code
autarch authored
123 city => 'Sydney',
124 region => 'New South Wales',
125 postal_code => '2000'
126 },
127 expect => {
128 lat => -25.335448,
129 long => 135.745076,
a38311f @autarch Make Geocoder.t pass
autarch authored
130 processed => '27 King Street, 2000, Australia',
d752bd7 @autarch perltidy all code
autarch authored
131 },
132 },
a38311f @autarch Make Geocoder.t pass
autarch authored
133 'Canada',
134 {
d752bd7 @autarch perltidy all code
autarch authored
135 address => {
a38311f @autarch Make Geocoder.t pass
autarch authored
136 address1 => '254 Spadina Avenue',
d752bd7 @autarch perltidy all code
autarch authored
137 city => 'Toronto',
138 region => 'Ontario',
139 postal_code => 'M5T 2E2',
140 },
141 expect => {
142 lat => 43.651664,
143 long => -79.397848,
a38311f @autarch Make Geocoder.t pass
autarch authored
144 processed => '254 Spadina Avenue, M5T 2E2, Canada',
d752bd7 @autarch perltidy all code
autarch authored
145 },
146 },
147 'Japan' => {
148 address => {
a38311f @autarch Make Geocoder.t pass
autarch authored
149 address1 => 'Ginza Kosaka buildings 7-9F, 6-9-4 Ginza',
150 localized_address1 => '銀座小坂ビル7〜9F, 銀座',
151 city => 'Ginza',
152 localized_city => '銀座',
153 region => 'Chou-Ku',
154 localized_region => '中区央',
d752bd7 @autarch perltidy all code
autarch authored
155 },
156 expect => {
157 lat => 123,
158 long => 456,
159 processed => '中区央, 銀座',
160 },
161 },
162 );
634795b make catalyst branch the new trunk
autarch authored
163 }
Something went wrong with that request. Please try again.