From ee179b44a9648daa5ec58ad810de8a612390dfda Mon Sep 17 00:00:00 2001 From: Nigel Horne Date: Thu, 21 Nov 2024 15:45:36 -0500 Subject: [PATCH] Added t/config.t --- Changes | 1 + MANIFEST | 1 + lib/Geo/Coder/Free/Config.pm | 5 ++-- t/config.t | 53 ++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 t/config.t diff --git a/Changes b/Changes index 8720c70..92a3188 100644 --- a/Changes +++ b/Changes @@ -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 diff --git a/MANIFEST b/MANIFEST index 3a9261b..c2daa82 100644 --- a/MANIFEST +++ b/MANIFEST @@ -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 diff --git a/lib/Geo/Coder/Free/Config.pm b/lib/Geo/Coder/Free/Config.pm index c84170c..8ef90e4 100644 --- a/lib/Geo/Coder/Free/Config.pm +++ b/lib/Geo/Coder/Free/Config.pm @@ -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}; } diff --git a/t/config.t b/t/config.t new file mode 100644 index 0000000..1cc82f5 --- /dev/null +++ b/t/config.t @@ -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'); +};