1package main;
2
3use strict;
4use warnings;
5
6use Test::More 0.88;
7
8use constant HASH_REF	=> ref {};
9
10my $module = 'Geo::WebService::Elevation::USGS';
11
12require_ok($module)
13    or BAIL_OUT ("Can not continue without loading $module");
14
15my $ele = eval {$module->new()};
16isa_ok($ele, $module)
17    or BAIL_OUT ("Can not continue without instantiating $module");
18
19is($ele->get('units'), 'FEET', 'Units default to feet');
20$ele->set(units => 'METERS');
21is($ele->get('units'), 'METERS', 'Units can be set to meters');
22$ele->set(units => 'FEET');
23is($ele->get('units'), 'FEET', 'Units can be set back to feet');
24ok($ele->get('croak'), 'Croak defaults to true');
25$ele->set(croak => undef);
26ok(!$ele->get('croak'), 'Croak can be set false');
27ok(!defined($ele->get('places')), 'Places defaults to undefined');
28$ele->set(places => 2);	# USGS returns insane precision
29is($ele->get('places'), 2, 'Places can be set to 2');
30my $rslt = eval {$ele->attributes()};
31ok($rslt, 'attributes() returned something');
32is(ref $rslt, HASH_REF, 'attributes() returned a hash reference');
33is($rslt->{places}, 2, 'attributes() returned places => 2');
34$rslt->{places} = undef;
35is($ele->get('places'), 2,
36    'Manipulating attributes() return does not affect attributes');
37eval {$ele->set(places => 'fubar')};
38like($@, qr{ \A\QAttribute places must be an unsigned integer}smx,
39    'Setting places to a non-integer should blow up,');
40is($ele->get('places'), 2, 'and not change the value of places');
41eval {$ele->set(places => undef)};
42ok(!$@, 'Setting places to undef should work');
43is($ele->get('places'), undef, 'and yield undef for places');
44$ele->set(places => 2);	# For subsequent testing
45
46{
47    my %rslt = eval {$ele->attributes()};
48    ok(scalar %rslt, 'attributes() returned something in list context');
49    is($rslt{places}, 2, 'attributes() returned places => 2');
50    my $bogus = eval {Geo::WebService::Elevation::USGS::new()};
51    my $msg = $@ || '';
52    ok(!$bogus, 'Function call to new() returned nothing');
53    like($msg, qr{ \A\QNo class name specified}smx,
54	'Function call to new() threw an error');
55    $rslt = eval{$ele->get('fubar')};
56    like($@, qr{ \A\QNo such attribute as 'fubar'}smx,
57	"Can't get attribute 'fubar'");
58    $rslt = eval{$ele->set(fubar => 'baz')};
59    like($@, qr{ \A\QNo such attribute as 'fubar'}smx,
60	"Can't set 'fubar' either");
61    $ele->{_bogus} = 'really';
62    $rslt = eval{$ele->get('_bogus')};
63    like($@, qr{  \A\QNo such attribute as '_bogus'}smx,
64	"Can't get attribute '_bogus'");
65    $rslt = eval{$ele->set(_bogus => 'baz')};
66    like($@, qr{ \A\QNo such attribute as '_bogus'}smx,
67	"Can't set '_bogus' either");
68    %rslt = eval {$ele->attributes()};
69    ok(!exists $rslt{_bogus},
70	"'_bogus' should not appear in attributes() output");
71}
72
73$rslt = eval {$module->is_valid(0)};
74ok(!$@, 'is_valid(0) should succeed');
75ok($rslt, 'is_valid(0) should be true');
76$rslt = eval {$module->is_valid('bogus')};
77ok(!$@, 'is_valid(\'bogus\') should succeed');
78ok(!$rslt, 'is_valid(\'bogus\') should be false');
79$rslt = eval {$module->is_valid(undef)};
80ok(!$@, 'is_valid(undef) should succeed');
81ok(!$rslt, 'is_valid(undef) should be false');
82$rslt = eval {$module->is_valid(-1e310)};
83ok(!$@, 'is_valid(-1e310) should succeed');
84ok(!$rslt, 'is_valid(-1e310) should be false');
85$rslt = eval {$ele->is_valid({Elevation => 0})};
86ok(!$@, 'is_valid({Elevation => 0}) should succeed');
87ok($rslt, 'is_valid({Elevation => 0}) should be true');
88$rslt = eval {$ele->is_valid([])};
89like ($@, qr{ \A\QARRAY reference not understood}smx,
90    'is_valid() should croak when passed an array reference');
91
92done_testing;
93
941;
95