Skip to content

Commit

Permalink
Added t/config.t
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelhorne committed Nov 21, 2024
1 parent 63151b4 commit ee179b4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Revision history for Geo-Coder-Free

0.38
Latest as_string return code from VWF
Added t/config.t

0.37 Wed Oct 23 10:09:54 EDT 2024
Allow new() to take HASH ref
Expand Down
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ t/admin2.t
t/bin.t
t/cities.t
t/comment-spelling.t
t/config.t
t/critic.t
t/dr5hn.t
t/eof.t
Expand Down
5 changes: 2 additions & 3 deletions lib/Geo/Coder/Free/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,11 @@ sub AUTOLOAD
our $AUTOLOAD;
my $self = shift;

return undef unless($self);

# Extract the method name from the AUTOLOAD variable
(my $key = $AUTOLOAD) =~ s/.*:://;

# Return undef if $self is not a hash reference
return undef unless(ref($self) eq 'HASH');

# Return the value of the corresponding hash key
return $self->{$key};
}
Expand Down
53 changes: 53 additions & 0 deletions t/config.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env perl

use strict;
use warnings;

use Test::Most tests => 4;
use File::Temp;
use File::Spec;

BEGIN {
use_ok('Geo::Coder::Free::Config');
$ENV{'HOME'} = File::Temp::tempdir(CLEANUP => 1);
}

# Test for creating a new object
subtest 'Object creation' => sub {
my $config_obj;

# Check it does not die
lives_ok(
sub {
$config_obj = Geo::Coder::Free::Config->new();
},
'Geo::Coder::Free::Config object created without errors'
);

isa_ok($config_obj, 'Geo::Coder::Free::Config', 'Correct object type');
};

# Test AUTOLOAD functionality
subtest 'AUTOLOAD method' => sub {
my $config_obj = Geo::Coder::Free::Config->new(config => { test_key => 'test_value' });

is($config_obj->test_key(), 'test_value', 'AUTOLOAD correctly retrieves a key-value pair');

is($config_obj->nonexistent_key(), undef, 'AUTOLOAD returns undef for non-existent keys');
};

# Test config file and environment variable overrides
subtest 'Configuration overrides' => sub {
my $custom_config = {
test_key => 'default_value',
override_key => 'default_override',
};

local $ENV{'override_key'} = 'env_override_value';

my $config_obj = Geo::Coder::Free::Config->new(config => $custom_config);

is($config_obj->{test_key}, 'default_value', 'Default configuration loaded correctly');

is($config_obj->{override_key}, 'env_override_value', 'Environment variable overrides configuration value');
};

0 comments on commit ee179b4

Please sign in to comment.