From: Shawn M Moore Date: Mon, 9 Feb 2009 20:03:31 +0000 (+0000) Subject: Support for anonymous enums: enum [elements] X-Git-Tag: 0.19~36 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=019047233180625898edb43732965d0dbda7e4b0;p=gitmo%2FMouse.git Support for anonymous enums: enum [elements] --- diff --git a/Changes b/Changes index af351aa..a701790 100644 --- a/Changes +++ b/Changes @@ -3,6 +3,8 @@ Revision history for Mouse 0.16 * Implement get_all_method_names + * Support for anonymous enums: enum [elements] + 0.15 Thu Feb 5 11:44:05 2009 * Don't export Mouse's sugar into the package 'main' diff --git a/lib/Mouse/Util/TypeConstraints.pm b/lib/Mouse/Util/TypeConstraints.pm index 885788a..e811ad5 100644 --- a/lib/Mouse/Util/TypeConstraints.pm +++ b/lib/Mouse/Util/TypeConstraints.pm @@ -161,7 +161,19 @@ sub typecast_constraints { return $value; } +my $serial_enum = 0; sub enum { + # enum ['small', 'medium', 'large'] + if (ref($_[0]) eq 'ARRAY') { + my @elements = @{ shift @_ }; + + my $name = 'Mouse::Util::TypeConstaints::Enum::Serial::' + . ++$serial_enum; + enum($name, @elements); + return $name; + } + + # enum size => 'small', 'medium', 'large' my $name = shift; my %is_valid = map { $_ => 1 } @_; diff --git a/t/041-enum.t b/t/041-enum.t index 731dcc1..5546f4f 100644 --- a/t/041-enum.t +++ b/t/041-enum.t @@ -1,7 +1,7 @@ #!/usr/bin/env perl use strict; use warnings; -use Test::More tests => 8; +use Test::More tests => 16; use Test::Exception; do { @@ -15,15 +15,26 @@ do { is => 'rw', isa => 'Size', ); + + package Shirt::Anon; + use Mouse; + use Mouse::Util::TypeConstraints 'enum'; + + has size => ( + is => 'rw', + isa => enum ['small', 'medium', 'large'], + ); }; -ok(Shirt->new(size => 'small')); -ok(Shirt->new(size => 'medium')); -ok(Shirt->new(size => 'large')); +for my $class ('Shirt', 'Shirt::Anon') { + ok($class->new(size => 'small')); + ok($class->new(size => 'medium')); + ok($class->new(size => 'large')); -throws_ok { Shirt->new(size => 'extra small') } qr/^Attribute \(size\) does not pass the type constraint because: Validation failed for 'Size' failed with value extra small/; -throws_ok { Shirt->new(size => 'Small') } qr/^Attribute \(size\) does not pass the type constraint because: Validation failed for 'Size' failed with value Small/; -throws_ok { Shirt->new(size => '') } qr/^Attribute \(size\) does not pass the type constraint because: Validation failed for 'Size' failed with value /; -throws_ok { Shirt->new(size => 'small ') } qr/^Attribute \(size\) does not pass the type constraint because: Validation failed for 'Size' failed with value small /; -throws_ok { Shirt->new(size => ' small') } qr/^Attribute \(size\) does not pass the type constraint because: Validation failed for 'Size' failed with value small/; + throws_ok { $class->new(size => 'extra small') } qr/^Attribute \(size\) does not pass the type constraint because: Validation failed for '\S+' failed with value extra small/; + throws_ok { $class->new(size => 'Small') } qr/^Attribute \(size\) does not pass the type constraint because: Validation failed for '\S+' failed with value Small/; + throws_ok { $class->new(size => '') } qr/^Attribute \(size\) does not pass the type constraint because: Validation failed for '\S+' failed with value /; + throws_ok { $class->new(size => 'small ') } qr/^Attribute \(size\) does not pass the type constraint because: Validation failed for '\S+' failed with value small /; + throws_ok { $class->new(size => ' small') } qr/^Attribute \(size\) does not pass the type constraint because: Validation failed for '\S+' failed with value small/; +}