60cd2485b03e6ecb9a6373517488822e44871005
[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 BEGIN {
10     use_ok('Moose::Autobox');
11 }
12
13 {
14     package Bytes;
15     use Moose::Role;
16     use autobox;
17     
18     sub bytes     { $_[0]                   }    
19     sub kilobytes { $_[0] * 1024            }
20     sub megabytes { $_[0] * 1024->kilobytes }
21     sub gigabytes { $_[0] * 1024->megabytes }
22     sub terabytes { $_[0] * 1024->gigabytes }
23     
24     {
25         no warnings; # << squelch the stupid "used only once, maybe typo" warnings
26         *byte     = \&bytes;
27         *kilobyte = \&kilobytes;    
28         *megabyte = \&megabytes;    
29         *gigabyte = \&gigabytes;
30         *terabyte = \&terabytes;
31     }
32 }
33
34 {
35     package SCALAR;
36     use Moose;
37     with 'Bytes';
38 }
39
40 {
41     use autobox;
42
43     is(5->bytes,     5,             '... got 5 bytes');
44     is(5->kilobytes, 5120,          '... got 5 kilobytes');
45     is(2->megabytes, 2097152,       '... got 2 megabytes');
46     is(1->gigabyte,  1073741824,    '... got 1 gigabyte');
47     is(2->terabytes, 2199023255552, '... got 2 terabyte');
48 }
49
50 dies_ok { 5->bytes } '... no longer got 5 bytes';