[Moose-commits] r7552 - in Mouse/trunk: lib/Mouse/Util t
sartak at code2.0beta.co.uk
sartak at code2.0beta.co.uk
Thu Feb 5 02:23:54 GMT 2009
Author: sartak
Date: 2009-02-04 18:23:53 -0800 (Wed, 04 Feb 2009)
New Revision: 7552
Added:
Mouse/trunk/t/041-enum.t
Modified:
Mouse/trunk/lib/Mouse/Util/TypeConstraints.pm
Log:
Implement basic usage of "enum" type constraints
Modified: Mouse/trunk/lib/Mouse/Util/TypeConstraints.pm
===================================================================
--- Mouse/trunk/lib/Mouse/Util/TypeConstraints.pm 2009-02-05 02:07:30 UTC (rev 7551)
+++ Mouse/trunk/lib/Mouse/Util/TypeConstraints.pm 2009-02-05 02:23:53 UTC (rev 7552)
@@ -7,7 +7,7 @@
use Scalar::Util qw/blessed looks_like_number openhandle/;
our @EXPORT = qw(
- as where message from via type subtype coerce class_type role_type
+ as where message from via type subtype coerce class_type role_type enum
);
my %TYPE;
@@ -161,6 +161,15 @@
return $value;
}
+sub enum {
+ my $name = shift;
+ my %is_valid = map { $_ => 1 } @_;
+
+ subtype(
+ $name => where => sub { $is_valid{$_} }
+ );
+}
+
1;
__END__
Added: Mouse/trunk/t/041-enum.t
===================================================================
--- Mouse/trunk/t/041-enum.t (rev 0)
+++ Mouse/trunk/t/041-enum.t 2009-02-05 02:23:53 UTC (rev 7552)
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More tests => 8;
+use Test::Exception;
+
+do {
+ package Shirt;
+ use Mouse;
+ use Mouse::Util::TypeConstraints 'enum';
+
+ enum 'Size' => qw(small medium large);
+
+ has size => (
+ is => 'rw',
+ isa => 'Size',
+ );
+};
+
+ok(Shirt->new(size => 'small'));
+ok(Shirt->new(size => 'medium'));
+ok(Shirt->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/;
+
More information about the Moose-commits
mailing list