Array-To-Moose

 view release on metacpan or  search on metacpan

t/4d.t  view on Meta::CPAN

#!perl -w

use strict;

use Test::More;

BEGIN {
    eval "use Test::Exception";
      plan skip_all => "Test::Exception needed" if $@;
}

plan tests => 7;

# tests on single-level Objects with a ref attribute

use Array::To::Moose qw (:ALL);

eval 'use VarianReportsMoose qw(print_obj)';

use Data::Dumper;

use Carp;

package Person;
use Moose;
use namespace::autoclean;
use MooseX::StrictConstructor;
has last      => (is => 'ro', isa => 'Str');
has first     => (is => 'ro', isa => 'Str');
has hobbies   => (is => 'ro', isa => 'ArrayRef[Str]');

__PACKAGE__->meta->make_immutable;

package main;

sub Npat { Person->new(last => $_[0], first => $_[1], hobbies => $_[2] ) }

# person
my @p1 = ( "Smith", "John"  );
my @p2 = ( "Smith", "Alex"  );
my @p3 = ( "Green", "Helen" );

# hobbies
my $h1 = 'Walking';
my $h2 = 'Cooking';

my $h3 = 'Cycling';

my $h4 = 'Quilting';
my $h5 = 'Baking';
my $h6 = 'Running';

my @h1 = ( $h1, $h2      );
my @h2 = ( $h3           );
my @h3 = ( $h4, $h5, $h6 );


my $data = [
              [ @p1, $h1 ],
              [ @p1, $h2],

              [ @p2, $h3 ],

              [ @p3, $h4 ],
              [ @p3, $h5 ],
              [ @p3, $h6 ],
];

my $expected = [
    Npat(@p1, [ @h1 ] ),
    Npat(@p2, [ @h2 ] ),
    Npat(@p3, [ @h3 ] ),
];

# rows of @$data contain: last, first, hobbies
#           at positions: [0]   [1]    [2]
#

my $object;
lives_ok {
    $object = array_to_moose(
                          data => $data,
                          desc => {
                            class => 'Person',
                            last  => 0,
                            first => 1,
                            hobbies => [2],
                          }
                             )
         } "'Person' with 'hobbies'";



( run in 0.435 second using v1.01-cache-2.11-cpan-39bf76dae61 )