Skip to content

Commit

Permalink
Improved handling of scalar then array call on same method
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelhorne committed Oct 11, 2019
1 parent 1ec690f commit 654b1f7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 30 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 Class::Simple::Readonly::Cached

0.04
Object shouldn't be instantiated if cache argument is not given
Improved handling of scalar then array call on same method

0.03 Sun Sep 29 05:12:19 PDT 2019
Added the object() method
Expand Down
17 changes: 13 additions & 4 deletions lib/Class/Simple/Readonly/Cached.pm
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ sub new {

=head2 object
Return the encapsulated objet
Return the encapsulated object
=cut

Expand Down Expand Up @@ -113,7 +113,7 @@ sub AUTOLOAD {
if($param eq 'DESTROY') {
if($cache) {
if(ref($cache) eq 'HASH') {
foreach my $key(keys %{$cache}) {
while(my($key, $value) = each %{$cache}) {
delete $cache->{$key};
}
return;
Expand Down Expand Up @@ -145,13 +145,22 @@ sub AUTOLOAD {
$rc = $cache->get($key);
}
if($rc) {
die $key if($rc eq 'never');
if(ref($rc) eq 'ARRAY') {
return @{$rc};
my @foo = @{$rc};
if(wantarray) {
die $key if($foo[0] eq __PACKAGE__ . ">UNDEF<");
die $key if($foo[0] eq 'never');
return @{$rc};
}
return pop @foo;
}
if($rc eq __PACKAGE__ . '>UNDEF<') {
return;
}
return $rc;
if(!wantarray) {
return $rc;
}
}
if(wantarray) {
my @rc = $object->$func(@_);
Expand Down
68 changes: 44 additions & 24 deletions t/hash.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use strict;
use warnings;
use Test::Most tests => 37;
use Test::Most tests => 41;
use Test::NoWarnings;
use CHI;

Expand All @@ -12,43 +12,59 @@ BEGIN {

HASH: {
my $cache = {};
my $l = new_ok('Class::Simple::Readonly::Cached' => [ cache => $cache, object => x->new() ]);

ok($l->calls() == 0);
ok($l->barney('betty') eq 'betty');
ok($l->calls() == 1);
ok($l->barney() eq 'betty');
ok($l->calls() == 1);
ok($l->barney() eq 'betty');
ok($l->calls() == 1);
my @abc = $l->abc();
my $cached = new_ok('Class::Simple::Readonly::Cached' => [ cache => $cache, object => x->new() ]);

ok($cached->calls() == 0);
ok($cached->barney('betty') eq 'betty');
ok($cached->calls() == 1);
ok($cached->barney() eq 'betty');
ok($cached->calls() == 1);
ok($cached->barney() eq 'betty');
ok($cached->calls() == 1);
my @abc = $cached->abc();
ok(scalar(@abc) == 3);
ok($abc[0] eq 'a');
ok($abc[1] eq 'b');
ok($abc[2] eq 'c');
@abc = $l->abc();
@abc = $cached->abc();
ok(scalar(@abc) == 3);
ok($abc[0] eq 'a');
ok($abc[1] eq 'b');
ok($abc[2] eq 'c');
my @a = $l->a();

my $uncached = x->new();

# Check reading scalar after reading array
my $abc = $cached->abc();
my $abc2 = $uncached->abc();
ok($abc eq $abc2);

# Check reading array after reading scalar
my $def = $cached->def();
ok($def eq 'f');
my $def2 = $uncached->def();
ok($def eq $def2);
my @def = $cached->def();
ok(scalar(@def) == 3);

my @a = $cached->a();
ok(scalar(@a) == 1);
ok($a[0] eq 'a');
@a = $l->a();
@a = $cached->a();
ok(scalar(@a) == 1);
ok($a[0] eq 'a');

ok($l->echo('foo') eq 'foo');
ok($l->echo('foo') eq 'foo');
ok($l->echo('bar') eq 'bar');
ok($l->echo('bar') eq 'bar');
ok($l->echo('foo') eq 'foo');
ok($cached->echo('foo') eq 'foo');
ok($cached->echo('foo') eq 'foo');
ok($cached->echo('bar') eq 'bar');
ok($cached->echo('bar') eq 'bar');
ok($cached->echo('foo') eq 'foo');

my @empty = $l->empty();
my @empty = $cached->empty();
ok(scalar(@empty) == 0);

ok(!defined($l->empty()));
ok(!defined($l->empty()));
ok(!defined($cached->empty()));
ok(!defined($cached->empty()));

# White box test the cache
ok($cache->{'barney::'} eq 'betty');
Expand All @@ -57,10 +73,10 @@ HASH: {
ok($cache->{'echo::bar'} eq 'bar');
my $a = $cache->{'a::'};
ok(ref($a) eq 'ARRAY');
my $abc = $cache->{'abc::'};
$abc = $cache->{'abc::'};
ok(ref($abc) eq 'ARRAY');

ok(ref($l->object()) eq 'x');
ok(ref($cached->object()) eq 'x');

# foreach my $key(sort keys %{$cache}) {
# diag($key);
Expand Down Expand Up @@ -89,6 +105,10 @@ sub abc {
return ('a', 'b', 'c');
}

sub def {
return ('d', 'e', 'f');
}

sub a {
return 'a';
}
Expand Down
4 changes: 2 additions & 2 deletions t/vars.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use warnings;

use Test::Most;

if(not $ENV{RELEASE_TESTING}) {
if(not $ENV{AUTHOR_TESTING}) {
plan(skip_all => 'Author tests not required for installation');
}

eval "use Test::Vars";

plan skip_all => "Test::Vars required for detecting unused variables" if $@;
plan(skip_all => 'Test::Vars required for detecting unused variables')if $@;

all_vars_ok(ignore_vars => { '$self' => 0 });

0 comments on commit 654b1f7

Please sign in to comment.