[Moose-commits] r7590 - in Mouse/trunk: . lib/Mouse/Util t
sartak at code2.0beta.co.uk
sartak at code2.0beta.co.uk
Mon Feb 9 20:03:31 GMT 2009
Author: sartak
Date: 2009-02-09 12:03:31 -0800 (Mon, 09 Feb 2009)
New Revision: 7590
Modified:
Mouse/trunk/Changes
Mouse/trunk/lib/Mouse/Util/TypeConstraints.pm
Mouse/trunk/t/041-enum.t
Log:
Support for anonymous enums: enum [elements]
Modified: Mouse/trunk/Changes
===================================================================
--- Mouse/trunk/Changes 2009-02-09 03:51:00 UTC (rev 7589)
+++ Mouse/trunk/Changes 2009-02-09 20:03:31 UTC (rev 7590)
@@ -3,6 +3,8 @@
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'
Modified: Mouse/trunk/lib/Mouse/Util/TypeConstraints.pm
===================================================================
--- Mouse/trunk/lib/Mouse/Util/TypeConstraints.pm 2009-02-09 03:51:00 UTC (rev 7589)
+++ Mouse/trunk/lib/Mouse/Util/TypeConstraints.pm 2009-02-09 20:03:31 UTC (rev 7590)
@@ -161,7 +161,19 @@
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 } @_;
Modified: Mouse/trunk/t/041-enum.t
===================================================================
--- Mouse/trunk/t/041-enum.t 2009-02-09 03:51:00 UTC (rev 7589)
+++ Mouse/trunk/t/041-enum.t 2009-02-09 20:03:31 UTC (rev 7590)
@@ -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 @@
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/;
+}
More information about the Moose-commits
mailing list