8 my @types = qw/Any Item Bool Undef Defined Value Num Int Str ClassName
9 Ref ScalarRef ArrayRef HashRef CodeRef RegexpRef GlobRef
13 undef , [qw/Any Item Undef Bool/],
14 0 => [qw/Any Item Defined Bool Value Num Int Str/],
15 1 => [qw/Any Item Defined Bool Value Num Int Str/],
16 1.5 => [qw/Any Item Defined Value Num Str/],
17 '' => [qw/Any Item Defined Bool Value Str/],
18 't' => [qw/Any Item Defined Value Str/],
19 'f' => [qw/Any Item Defined Value Str/],
20 'undef' => [qw/Any Item Defined Value Str/],
21 'Test::More' => [qw/Any Item Defined Value Str ClassName/],
22 \undef => [qw/Any Item Defined Ref ScalarRef/],
23 \1 => [qw/Any Item Defined Ref ScalarRef/],
24 \"foo" => [qw/Any Item Defined Ref ScalarRef/],
25 [], => [qw/Any Item Defined Ref ArrayRef/],
26 [undef, \1] => [qw/Any Item Defined Ref ArrayRef/],
27 {} => [qw/Any Item Defined Ref HashRef/],
28 sub { die } => [qw/Any Item Defined Ref CodeRef/],
29 qr/.*/ => [qw/Any Item Defined Ref RegexpRef/],
30 \*main::ok => [qw/Any Item Defined Ref GlobRef/],
31 \*STDOUT => [qw/Any Item Defined Ref GlobRef FileHandle/],
32 IO::Handle->new => [qw/Any Item Defined Ref Object FileHandle/],
33 Test::Builder->new => [qw/Any Item Defined Ref Object/],
38 for (my $i = 1; $i < @type_values; $i += 2) {
39 my ($value, $valid_types) = @type_values[$i-1, $i];
40 my %is_invalid = map { $_ => 1 } @types;
41 delete @is_invalid{@$valid_types};
43 push @{ $values_for_type{$_}{invalid} }, $value
44 for grep { $is_invalid{$_} } @types;
46 push @{ $values_for_type{$_}{valid} }, $value
47 for grep { !$is_invalid{$_} } @types;
51 $plan += 5 * @{ $values_for_type{$_}{valid} || [] } for @types;
52 $plan += 4 * @{ $values_for_type{$_}{invalid} || [] } for @types;
61 for my $type (@types) {
69 can_ok(Class => @types);
71 for my $type (@types) {
72 for my $value (@{ $values_for_type{$type}{valid} }) {
74 my $via_new = Class->new($type => $value);
75 is_deeply($via_new->$type, $value, "correctly set a $type in the constructor");
79 my $via_set = Class->new;
80 is($via_set->$type, undef, "initially unset");
81 $via_set->$type($value);
82 is_deeply($via_set->$type, $value, "correctly set a $type in the setter");
86 for my $value (@{ $values_for_type{$type}{invalid} }) {
87 my $display = defined($value) ? $value : 'undef';
90 $via_new = Class->new($type => $value);
91 } qr/Attribute \($type\) does not pass the type constraint because: Validation failed for '$type' failed with value \Q$display\E/;
92 is($via_new, undef, "no object created");
94 my $via_set = Class->new;
96 $via_set->$type($value);
97 } qr/Attribute \($type\) does not pass the type constraint because: Validation failed for '$type' failed with value \Q$display\E/;
99 is($via_set->$type, undef, "value for $type not set");