stop using Module::Install
[gitmo/Moose-Autobox.git] / t / 003_p6_example.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 7;
7 use Test::Exception;
8
9 require Moose::Autobox;
10
11 =pod
12
13 This comes from one of the examples in the Pugs distro.
14
15 =cut
16
17 {
18     package Units::Bytes;
19     use Moose::Role;
20     use Moose::Autobox;
21     
22     sub bytes     { $_[0]                   }    
23     sub kilobytes { $_[0] * 1024            }
24     sub megabytes { $_[0] * 1024->kilobytes }
25     sub gigabytes { $_[0] * 1024->megabytes }
26     sub terabytes { $_[0] * 1024->gigabytes }
27     
28     {
29         no warnings 'once'; # << squelch the stupid "used only once, maybe typo" warnings
30         *byte     = \&bytes;
31         *kilobyte = \&kilobytes;    
32         *megabyte = \&megabytes;    
33         *gigabyte = \&gigabytes;
34         *terabyte = \&terabytes;
35     }
36 }
37
38 Moose::Autobox->mixin_additional_role(SCALAR => 'Units::Bytes');
39
40 sub testing_bytes {
41     ::dies_ok { 10->bytes } '... cannot do the autoboxing lexically';
42 }
43
44 {
45     use Moose::Autobox;
46
47     is(5->bytes,     5,             '... got 5 bytes');
48     is(5->kilobytes, 5120,          '... got 5 kilobytes');
49     is(2->megabytes, 2097152,       '... got 2 megabytes');
50     is(1->gigabyte,  1073741824,    '... got 1 gigabyte');
51     is(2->terabytes, 2199023255552, '... got 2 terabyte');
52     testing_bytes;
53 }
54
55 dies_ok { 5->bytes } '... no longer got 5 bytes';