return undef for ->get() with invalid property

This commit is contained in:
Graham Knop 2009-10-22 10:20:34 -05:00
parent 8b975cc810
commit 9b31593dae
2 changed files with 12 additions and 1 deletions

View file

@ -166,7 +166,10 @@ sub _gen_get {
my $self = shift;
if (@_) {
my $prop = shift;
return $self->$prop;
if ($self->can($prop)) {
return $self->$prop;
}
return undef;
}
my @all_properties = $self->getProperties;
my %props;

View file

@ -13,6 +13,8 @@ use warnings;
no warnings qw(uninitialized);
use Test::More 'no_plan'; #tests => 1;
use Test::Exception;
my $called_getProperties;
{
package WGT::Class;
@ -97,6 +99,12 @@ is_deeply $object->get, { property1 => 'property 1 value' },
is_deeply $subclass_object->get, { property1 => undef, a_property => ' - BLAH', property2 => 'property 2 value' },
'get returns hash with correct properties';
is $object->get('property1'), 'property 1 value',
'get with parameter returns value from accessor';
is $object->get('nonExistantProperty'), undef,
'get with non-existant parameter returns undef';
is_deeply $object->getProperty('property1'), { label => 'property1 label', defaultValue => $object },
'getProperty returns correct hash for object';
is_deeply $subclass_object->getProperty('property2'), { label => 'property2 label', defaultValue => 'dynamic value' },