foo
[gitmo/Moose-Autobox.git] / t / 003_p6_example.t
CommitLineData
e6bb88b0 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
feffe00c 6use Test::More tests => 8;
252ab1a2 7use Test::Exception;
e6bb88b0 8
9BEGIN {
10 use_ok('Moose::Autobox');
11}
12
252ab1a2 13{
feffe00c 14 package Units::Bytes;
252ab1a2 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}
e6bb88b0 33
252ab1a2 34{
35 package SCALAR;
36 use Moose;
feffe00c 37 with 'Units::Bytes';
38}
39
40sub testing_bytes {
41 ::dies_ok { 10->bytes } '... cannot do the autoboxing lexically';
252ab1a2 42}
e6bb88b0 43
252ab1a2 44{
45 use 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 55dies_ok { 5->bytes } '... no longer got 5 bytes';