X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F003_p6_example.t;h=bead5703bffda0e4144ed07a3a47b80afc3dd139;hb=244bd352801e30561098541bd2a7313234374810;hp=4be1352d4cb64226997c3c6a91c885e5d933d791;hpb=e6bb88b0e1fff9794352dde571e623ae0f4e3a95;p=gitmo%2FMoose-Autobox.git diff --git a/t/003_p6_example.t b/t/003_p6_example.t index 4be1352..bead570 100644 --- a/t/003_p6_example.t +++ b/t/003_p6_example.t @@ -3,47 +3,55 @@ use strict; use warnings; -use Test::More no_plan => 1; +use Test::More tests => 8; +use Test::Exception; BEGIN { use_ok('Moose::Autobox'); } -use autobox; - -SCALAR->meta->add_method('bytes' => sub { - $_[0]; -}); - -SCALAR->meta->add_method('byte' => SCALAR->meta->get_method('bytes')); - -SCALAR->meta->add_method('kilobytes' => sub { - $_[0] * 1024; -}); - -SCALAR->meta->add_method('kilobyte' => SCALAR->meta->get_method('kilobytes')); - -SCALAR->meta->add_method('megabytes' => sub { - $_[0] * 1024->kilobytes; -}); - -SCALAR->meta->add_method('metabyte' => SCALAR->meta->get_method('megabytes')); - -SCALAR->meta->add_method('gigabytes' => sub { - $_[0] * 1024->megabytes; -}); +=pod + +This comes from one of the examples in the Pugs distro. + +=cut + +{ + package Units::Bytes; + use Moose::Role; + use Moose::Autobox; + + sub bytes { $_[0] } + sub kilobytes { $_[0] * 1024 } + sub megabytes { $_[0] * 1024->kilobytes } + sub gigabytes { $_[0] * 1024->megabytes } + sub terabytes { $_[0] * 1024->gigabytes } + + { + no warnings 'once'; # << squelch the stupid "used only once, maybe typo" warnings + *byte = \&bytes; + *kilobyte = \&kilobytes; + *megabyte = \&megabytes; + *gigabyte = \&gigabytes; + *terabyte = \&terabytes; + } +} -SCALAR->meta->add_method('gigabyte' => SCALAR->meta->get_method('gigabytes')); +Moose::Autobox->mixin_additional_role(SCALAR => 'Units::Bytes'); -SCALAR->meta->add_method('terabytes' => sub { - $_[0] * 1024->gigabytes; -}); +sub testing_bytes { + ::dies_ok { 10->bytes } '... cannot do the autoboxing lexically'; +} -SCALAR->meta->add_method('terabyte' => SCALAR->meta->get_method('terabytes')); +{ + use Moose::Autobox; -is(5->bytes, 5, '... got 5 bytes'); -is(5->kilobytes, 5120, '... got 5 kilobytes'); -is(2->megabytes, 2097152, '... got 2 megabytes'); -is(1->gigabyte, 1073741824, '... got 1 gigabyte'); -is(2->terabytes, 2199023255552, '... got 2 terabyte'); + is(5->bytes, 5, '... got 5 bytes'); + is(5->kilobytes, 5120, '... got 5 kilobytes'); + is(2->megabytes, 2097152, '... got 2 megabytes'); + is(1->gigabyte, 1073741824, '... got 1 gigabyte'); + is(2->terabytes, 2199023255552, '... got 2 terabyte'); + testing_bytes; +} +dies_ok { 5->bytes } '... no longer got 5 bytes';