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