MooseX-Storage

 view release on metacpan or  search on metacpan

t/030_with_checksum.t  view on Meta::CPAN

use strict;
use warnings;

use Test::More;
use Test::Fatal;
use Test::Deep;

use Test::Needs qw(
    Digest::HMAC_SHA1
    JSON::MaybeXS
);
diag 'using JSON backend: ', JSON::MaybeXS->JSON;

plan tests => 25;

{
    package Foo;
    use Moose;
    use MooseX::Storage;

    with Storage(base => 'WithChecksum', format => "JSON");

    has 'number' => ( is => 'ro', isa => 'Int' );
    has 'string' => ( is => 'ro', isa => 'Str' );
    has 'float'  => ( is => 'ro', isa => 'Num' );
    has 'array'  => ( is => 'ro', isa => 'ArrayRef' );
    has 'hash'   => ( is => 'ro', isa => 'HashRef' );
    has 'object' => ( is => 'ro', isa => 'Foo' );
}

{
    my $foo = Foo->new(
        number => 10,
        string => 'foo',
        float  => 10.5,
        array  => [ 1 .. 10 ],
        hash   => { map { $_ => undef } ( 1 .. 10 ) },
        object => Foo->new( number => 2 ),
    );
    isa_ok( $foo, 'Foo' );

    my $packed = $foo->pack;

    cmp_deeply(
        $packed,
        {
            __CLASS__ => 'Foo',
            __DIGEST__  => re('[0-9a-f]+'),
            number    => 10,
            string    => 'foo',
            float     => 10.5,
            array     => [ 1 .. 10 ],
            hash      => { map { $_ => undef } ( 1 .. 10 ) },
            object    => {
                            __CLASS__ => 'Foo',
                            __DIGEST__  => re('[0-9a-f]+'),
                            number    => 2
                         },
        },
        '... got the right frozen class'
    );

    my $foo2;
    is( exception {
        $foo2 = Foo->unpack($packed);
    }, undef, '... unpacked okay');
    isa_ok($foo2, 'Foo');

    cmp_deeply(
        $foo2->pack,
        {
            __CLASS__ => 'Foo',
            __DIGEST__  => re('[0-9a-f]+'),
            number    => 10,
            string    => 'foo',
            float     => 10.5,
            array     => [ 1 .. 10 ],
            hash      => { map { $_ => undef } ( 1 .. 10 ) },
            object    => {
                            __CLASS__ => 'Foo',
                            __DIGEST__  => re('[0-9a-f]+'),
                            number    => 2
                         },
        },
        '... got the right frozen class'
    );
}



( run in 1.515 second using v1.01-cache-2.11-cpan-97f6503c9c8 )