1use strict;
2use warnings;
3use Encode qw(decode encode);
4use Geo::Coder::PlaceFinder;
5use Test::More;
6
7unless ($ENV{YAHOO_APPID}) {
8    plan skip_all => 'YAHOO_APIPID environment variable must be set';
9}
10else {
11    plan tests => 10;
12}
13
14my $debug = $ENV{GEO_CODER_PLACEFINDER_DEBUG};
15unless ($debug) {
16    diag "Set GEO_CODER_PLACEFINDER_DEBUG to see request/response data";
17}
18
19my $geocoder = Geo::Coder::PlaceFinder->new(
20    appid => $ENV{YAHOO_APPID},
21    debug => $debug,
22);
23
24{
25    my $address = 'Hollywood & Highland, Los Angeles, CA';
26    my $location = $geocoder->geocode($address);
27    like($location->{postal}, qr/^90028/, "correct zip code for $address");
28}
29{
30    my @locations = $geocoder->geocode('Main Street, Los Angeles, CA');
31    ok(@locations > 1, 'there are many Main Streets in Los Angeles, CA');
32}
33{
34    my $address = qq(Ch\xE2teau d Uss\xE9, 37420);
35
36    my $location = $geocoder->geocode($address);
37    ok($location, 'latin1 bytes');
38    is($location->{countrycode}, 'FR', 'latin1 bytes');
39
40    $location = $geocoder->geocode(location => decode('latin1', $address));
41    ok($location, 'UTF-8 characters');
42    is($location->{countrycode}, 'FR', 'UTF-8 characters');
43
44    $location = $geocoder->geocode(
45        location => encode('utf-8', decode('latin1', $address))
46    );
47    ok($location, 'UTF-8 bytes');
48    is($location->{countrycode}, 'FR', 'UTF-8 bytes');
49}
50
51# Multi-line format.
52{
53    my @address = (
54        line1 => '701 First Ave.',
55        line2 => 'Sunnyvale, CA 94089',
56        line3 => 'USA',
57    );
58    my $location = $geocoder->geocode(@address);
59    like(
60        $location->{postal}, qr/^94089/,
61        "correct zip code for multi-line address"
62    );
63}
64
65# Fully-parsed format.
66{
67    my @address = (
68        house    => 701,
69        street   => 'First Ave.',
70        xstreet  => 'Mathilda Ave.',
71        postal   => 94089,
72        city     => 'Sunnyvale',
73        county   => 'Santa Clara',
74        country  => 'USA',
75    );
76    my $location = $geocoder->geocode(@address);
77    like(
78        $location->{postal}, qr/^94089/,
79        "correct zip code for fully-parsed address"
80    );
81}
82