sub enum {
my($name, %valid);
- # enum ['small', 'medium', 'large']
- if (ref($_[0]) eq 'ARRAY') {
- %valid = map{ $_ => undef } @{ $_[0] };
- $name = sprintf '(%s)', join '|', sort @{$_[0]};
- }
- # enum size => 'small', 'medium', 'large'
- else{
- $name = shift;
- %valid = map{ $_ => undef } @_;
+ if(!(@_ == 1 && ref($_[0]) eq 'ARRAY')){
+ $name = shift;
}
+
+ %valid = map{ $_ => undef } (@_ == 1 && ref($_[0]) eq 'ARRAY' ? @{$_[0]} : @_);
+
return _create_type 'type', $name => (
optimized_as => sub{ defined($_[0]) && !ref($_[0]) && exists $valid{$_[0]} },
#!/usr/bin/env perl
use strict;
use warnings;
-use Test::More tests => 16;
+use Test::More tests => 19;
use Test::Exception;
do {
throws_ok { $class->new(size => ' small') } qr/^Attribute \(size\) does not pass the type constraint because: Validation failed for '\S+' failed with value small/;
}
+use Mouse::Util::TypeConstraints qw(enum);
+
+my $t = enum 'Foo', [qw(foo bar)];
+
+ok $t->check('foo'), 'enum $name, $array_ref';
+ok $t->check('bar');
+ok!$t->check('baz');
+