X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F026-auto-deref.t;fp=t%2F026-auto-deref.t;h=0aa021e0a9069e59663d03999a96b04901214ae0;hb=615d5d5fa75cb9c4d1445184b63dae565dcc790e;hp=0000000000000000000000000000000000000000;hpb=9694b71b9fb63978f813900224f556ad62da729f;p=gitmo%2FMouse.git diff --git a/t/026-auto-deref.t b/t/026-auto-deref.t new file mode 100644 index 0000000..0aa021e --- /dev/null +++ b/t/026-auto-deref.t @@ -0,0 +1,76 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More tests => 13; +use Test::Exception; + +do { + package Class; + use Mouse; + + has array => ( + is => 'rw', + isa => 'ArrayRef', + auto_deref => 1, + ); + + has hash => ( + is => 'rw', + isa => 'HashRef', + auto_deref => 1, + ); +}; + +my $obj; +lives_ok { + $obj = Class->new; +} qr/auto_deref without defaults don't explode on new/; + +my ($array, @array, $hash, %hash); +lives_ok { + @array = $obj->array; + %hash = $obj->hash; + $array = $obj->array; + $hash = $obj->hash; + + $obj->array; + $obj->hash; +} qr/auto_deref without default doesn't explode on get/; + +is($obj->array, undef, "array without value is undef in scalar context"); +is($obj->hash, undef, "hash without value is undef in scalar context"); + +TODO: { + local $TODO = "auto_deref not implemented"; + is(@array, 0, "array without value is empty in list context"); + is(keys %hash, 0, "hash without value is empty in list context"); +}; + +@array = $obj->array([1, 2, 3]); +%hash = $obj->hash({foo => 1, bar => 2}); + +TODO: { + local $TODO = "auto_deref not implemented"; + is_deeply(\@array, [1, 2, 3], "setter returns the dereferenced list"); + is_deeply(\%hash, {foo => 1, bar => 2}, "setter returns the dereferenced hash"); +}; + +lives_ok { + @array = $obj->array; + %hash = $obj->hash; + $array = $obj->array; + $hash = $obj->hash; + + $obj->array; + $obj->hash; +} qr/auto_deref without default doesn't explode on get/; + +is_deeply($array, [1, 2, 3], "auto_deref in scalar context gives the reference"); +is_deeply($hash, {foo => 1, bar => 2}, "auto_deref in scalar context gives the reference"); + +TODO: { + local $TODO = "auto_deref not implemented"; + is_deeply(\@array, [1, 2, 3], "auto_deref in list context gives the list"); + is_deeply(\%hash, {foo => 1, bar => 2}, "auto_deref in list context gives the hash"); +}; +