Commit | Line | Data |
e6bb88b0 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
feffe00c |
6 | use Test::More tests => 8; |
252ab1a2 |
7 | use Test::Exception; |
e6bb88b0 |
8 | |
9 | BEGIN { |
10 | use_ok('Moose::Autobox'); |
11 | } |
12 | |
1972aa1b |
13 | =pod |
14 | |
15 | This comes from one of the examples in the Pugs distro. |
16 | |
17 | =cut |
18 | |
252ab1a2 |
19 | { |
feffe00c |
20 | package Units::Bytes; |
252ab1a2 |
21 | use Moose::Role; |
7dad2765 |
22 | use Moose::Autobox; |
252ab1a2 |
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 | { |
260cc81f |
31 | no warnings 'once'; # << squelch the stupid "used only once, maybe typo" warnings |
252ab1a2 |
32 | *byte = \&bytes; |
33 | *kilobyte = \&kilobytes; |
34 | *megabyte = \&megabytes; |
35 | *gigabyte = \&gigabytes; |
36 | *terabyte = \&terabytes; |
37 | } |
38 | } |
e6bb88b0 |
39 | |
244bd352 |
40 | Moose::Autobox->mixin_additional_role(SCALAR => 'Units::Bytes'); |
feffe00c |
41 | |
42 | sub testing_bytes { |
43 | ::dies_ok { 10->bytes } '... cannot do the autoboxing lexically'; |
252ab1a2 |
44 | } |
e6bb88b0 |
45 | |
252ab1a2 |
46 | { |
7dad2765 |
47 | use Moose::Autobox; |
e6bb88b0 |
48 | |
252ab1a2 |
49 | is(5->bytes, 5, '... got 5 bytes'); |
50 | is(5->kilobytes, 5120, '... got 5 kilobytes'); |
51 | is(2->megabytes, 2097152, '... got 2 megabytes'); |
52 | is(1->gigabyte, 1073741824, '... got 1 gigabyte'); |
53 | is(2->terabytes, 2199023255552, '... got 2 terabyte'); |
feffe00c |
54 | testing_bytes; |
252ab1a2 |
55 | } |
e6bb88b0 |
56 | |
252ab1a2 |
57 | dies_ok { 5->bytes } '... no longer got 5 bytes'; |