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 } @_;
#!/usr/bin/env perl
use strict;
use warnings;
-use Test::More tests => 8;
+use Test::More tests => 16;
use Test::Exception;
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/;
+}