Revert most of the conversion to Test::Fatal so we can redo it
[gitmo/Moose.git] / t / 040_type_constraints / 015_enum.t
index 162905e..92c0062 100644 (file)
@@ -4,6 +4,7 @@ use strict;
 use warnings;
 
 use Test::More;
+use Test::Exception;
 
 use Scalar::Util ();
 
@@ -11,7 +12,7 @@ use Moose::Util::TypeConstraints;
 
 enum Letter => 'a'..'z', 'A'..'Z';
 enum Language => 'Perl 5', 'Perl 6', 'PASM', 'PIR'; # any others? ;)
-enum Metacharacter => '*', '+', '?', '.', '|', '(', ')', '[', ']', '\\';
+enum Metacharacter => ['*', '+', '?', '.', '|', '(', ')', '[', ']', '\\'];
 
 my @valid_letters = ('a'..'z', 'A'..'Z');
 
@@ -28,11 +29,6 @@ my @invalid_metacharacters = qw/< > & % $ @ ! ~ `/;
 push @invalid_metacharacters, qw/.* fish(sticks)? atreides/;
 push @invalid_metacharacters, '^1?$|^(11+?)\1+$';
 
-plan tests => @valid_letters        + @invalid_letters
-            + @valid_languages      + @invalid_languages
-            + @valid_metacharacters + @invalid_metacharacters
-            + @valid_languages      + 10;
-
 Moose::Util::TypeConstraints->export_type_constraints_as_functions();
 
 ok(Letter($_), "'$_' is a letter") for @valid_letters;
@@ -66,3 +62,30 @@ ok( !$anon_enum->is_a_type_of('Object'), 'enum not type of Object');
 ok( !$anon_enum->is_subtype_of('ThisTypeDoesNotExist'), 'enum not a subtype of nonexistant type');
 ok( !$anon_enum->is_a_type_of('ThisTypeDoesNotExist'), 'enum not type of nonexistant type');
 
+# validation
+throws_ok { Moose::Meta::TypeConstraint::Enum->new(name => 'ZeroValues', values => []) }
+    qr/You must have at least two values to enumerate through/;
+
+throws_ok { Moose::Meta::TypeConstraint::Enum->new(name => 'OneValue', values => [ 'a' ]) }
+    qr/You must have at least two values to enumerate through/;
+
+throws_ok { Moose::Meta::TypeConstraint::Enum->new(name => 'ReferenceInEnum', values => [ 'a', {} ]) }
+    qr/Enum values must be strings, not 'HASH\(0x\w+\)'/;
+
+throws_ok { Moose::Meta::TypeConstraint::Enum->new(name => 'UndefInEnum', values => [ 'a', undef ]) }
+    qr/Enum values must be strings, not undef/;
+
+throws_ok {
+    package Foo;
+    use Moose;
+    use Moose::Util::TypeConstraints;
+
+    has error => (
+        is      => 'ro',
+        isa     => enum ['a', 'aa', 'aaa'], # should be parenthesized!
+        default => 'aa',
+    );
+} qr/enum called with an array reference and additional arguments\. Did you mean to parenthesize the enum call's parameters\?/;
+
+
+done_testing;