Subject: | New Attribute style Type: dies with subs or objects |
From t/15-type.t added:
my @cc1 :Field
:Acc(cc1)
:Type(sub{ shift > 0 });
Got error:
Show quoted text
>perl -w 15-type.t
OIO::Attribute error: Malformed attribute in package 'My::Class'
Error: Odd number of elements in anonymous hash
Attribute: Type(sub{ shift > 0 })
Package: main
File: 15-type.t
Line: 49
Trace begun at 15-type.t line 49
# Looks like your test died before it could output anything.
Show quoted text >Exit code: 255
I have attached a revised test file using the new attribute technique.
Not much, but it might save you five minutes. Note that currently all
the bad declaration forms are commented out so as to figure out which
ones were working.
Version info: OIO 2.14
This is perl, v5.8.8 built for MSWin32-x86-multi-thread
(with 33 registered patches, see perl -V for more detail)
Binary build 819 [267479] provided by ActiveState
Subject: | 15a-type.t |
use strict;
use warnings;
use Test::More 'no_plan';
package My::Class; {
use Object::InsideOut;
sub is_scalar : Private { return (! ref(shift)); }
sub is_int {
my $arg = $_[0];
return (Scalar::Util::looks_like_number($arg) &&
(int($arg) == $arg));
}
my @aa : Field
:Acc('aa')
:Type('array');
my @ar : Field
: Acc('ar')
: Type('array_ref');
# my @cc : Field({'acc'=>'cc', 'type' => sub{ shift > 0 } });
# my @cc : Field
# : Acc(cc1)
# : Type(sub{ shift > 0 });
my @hh : Field
:Acc('hh')
:Type('hash');
my @hr : Field
:Acc('hr')
:Type('hashref');
# my @mc : Field
# :Acc('mc')
# :Type('My::Class');
my @nn : Field
:Acc('nn')
:Type('num');
# my @ss : Field
# : Acc('ss')
# : Type(\&My::Class::is_scalar);
my %init_args :InitArgs = (
'DATA' => {
'Field' => \@nn,
'Type' => \&is_int,
},
'INFO' => {
'Type' => sub { $_[0] }
},
'BAD' => {
'Type' => sub { shift > 0 }
},
);
}
package main;
MAIN:
{
my $obj = My::Class->new('DATA' => 5);
$obj->aa('test');
is_deeply($obj->aa(), ['test'] => 'Array single value');
$obj->aa('zero', 5);
is_deeply($obj->aa(), ['zero', 5] => 'Array multiple values');
$obj->aa(['x', 42, 'z']);
is_deeply($obj->aa(), ['x', 42, 'z'] => 'Array ref value');
eval { $obj->ar('test'); };
like($@->message, qr/Wrong type/ => 'Not array ref');
$obj->ar([3, [ 'a' ]]);
is_deeply($obj->ar(), [3, [ 'a' ]] => 'Array ref');
$obj->cc(12);
is($obj->cc(), 12 => 'Type sub');
eval { $obj->cc(-5); };
like($@->message, qr/failed type check/ => 'Type failure');
eval { $obj->cc('hello'); };
like($@->message, qr/Problem with type check routine/ => 'Type sub failure');
$obj->hh('x' => 5);
is_deeply($obj->hh(), {'x'=>5} => 'Hash single pair');
$obj->hh('a' => 'z', '0' => '9');
is_deeply($obj->hh(), {'a'=>'z','0'=>'9'} => 'Hash multiple pairs');
$obj->hh({'2b'=>'not'});
is_deeply($obj->hh(), {'2b'=>'not'} => 'Hash ref value');
eval { $obj->hr('test'); };
like($@->message, qr/Wrong type/ => 'Not hash ref');
$obj->hr({'frog'=>{'prince'=>'John'}});
is_deeply($obj->hr(), {'frog'=>{'prince'=>'John'}} => 'Hash ref');
my $obj2 = My::Class->new();
$obj->mc($obj2);
my $obj3 = $obj->mc();
isa_ok($obj3, 'My::Class' => 'Object');
is($$obj3, $$obj2 => 'Objects equal');
eval { $obj2->mc('test'); };
like($@->message, qr/Wrong type/ => 'Not object');
$obj->nn(99);
is_deeply($obj->nn(), 99 => 'Numeric');
eval { $obj->nn('x'); };
like($@->message, qr/Bad argument/ => 'Numeric failure');
$obj->ss('hello');
is($obj->ss(), 'hello' => 'Scalar');
eval { $obj->ss([1]); };
like($@->message, qr/failed type check/ => 'Scalar failure');
eval { $obj2 = My::Class->new('DATA' => 'hello'); };
like($@->message, qr/failed type check/ => 'Type failure');
eval { $obj2 = My::Class->new('INFO' => ''); };
like($@->message, qr/failed type check/ => 'Type failure');
eval { $obj2 = My::Class->new('BAD' => ''); };
like($@->message, qr/Problem with type check routine/ => 'Type sub failure');
}
exit(0);
# EOF