foo
[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 => 8;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('Moose::Autobox');
11 }
12
13 =pod
14
15 This comes from one of the examples in the Pugs distro.
16
17 =cut
18
19 {
20     package Units::Bytes;
21     use Moose::Role;
22     use Moose::Autobox;
23     
24     sub bytes     { $_[0]                   }    
25     sub kilobytes { $_[0] * 1024            }
26     sub megabytes { $_[0] * 1024->kilobytes }
27     sub gigabytes { $_[0] * 1024->megabytes }
28     sub terabytes { $_[0] * 1024->gigabytes }
29     
30     {
31         no warnings 'once'; # << squelch the stupid "used only once, maybe typo" warnings
32         *byte     = \&bytes;
33         *kilobyte = \&kilobytes;    
34         *megabyte = \&megabytes;    
35         *gigabyte = \&gigabytes;
36         *terabyte = \&terabytes;
37     }
38 }
39
40 {
41     package Moose::Autobox::SCALAR;
42     use Moose 'with';
43     with 'Units::Bytes';
44 }
45
46 sub testing_bytes {
47     ::dies_ok { 10->bytes } '... cannot do the autoboxing lexically';
48 }
49
50 {
51     use Moose::Autobox;
52
53     is(5->bytes,     5,             '... got 5 bytes');
54     is(5->kilobytes, 5120,          '... got 5 kilobytes');
55     is(2->megabytes, 2097152,       '... got 2 megabytes');
56     is(1->gigabyte,  1073741824,    '... got 1 gigabyte');
57     is(2->terabytes, 2199023255552, '... got 2 terabyte');
58     testing_bytes;
59 }
60
61 dies_ok { 5->bytes } '... no longer got 5 bytes';