X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F040_type_constraints%2F015_enum.t;h=ae6bef637131a39f2dc26a951a54b9521582d1c1;hb=be0ed15704fdad5f2d8517380a6f24687432c1dd;hp=28e73d95de3dc4f2697922ab297bcb6064677136;hpb=c4fe165f59754f765c60f046d084049005550fd8;p=gitmo%2FMoose.git diff --git a/t/040_type_constraints/015_enum.t b/t/040_type_constraints/015_enum.t index 28e73d9..ae6bef6 100644 --- a/t/040_type_constraints/015_enum.t +++ b/t/040_type_constraints/015_enum.t @@ -3,17 +3,16 @@ use strict; use warnings; -use Test::More tests => 97; +use Test::More; +use Test::Fatal; use Scalar::Util (); -BEGIN { - use_ok('Moose::Util::TypeConstraints'); -} +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'); @@ -22,7 +21,8 @@ push @invalid_letters, qw/0 4 9 ~ @ $ %/; push @invalid_letters, qw/l33t st3v4n 3num/; my @valid_languages = ('Perl 5', 'Perl 6', 'PASM', 'PIR'); -my @invalid_languages = ('Python', 'Ruby', 'Perl 666', 'PASM++'); +my @invalid_languages = ('perl 5', 'Python', 'Ruby', 'Perl 666', 'PASM++'); +# note that "perl 5" is invalid because case now matters my @valid_metacharacters = (qw/* + ? . | ( ) [ ] /, '\\'); my @invalid_metacharacters = qw/< > & % $ @ ! ~ `/; @@ -41,3 +41,51 @@ ok(Metacharacter($_), "'$_' is a metacharacter") for @valid_metacharacters; ok(!Metacharacter($_), "'$_' is not a metacharacter") for @invalid_metacharacters; +# check anon enums + +my $anon_enum = enum \@valid_languages; +isa_ok($anon_enum, 'Moose::Meta::TypeConstraint'); + +is($anon_enum->name, '__ANON__', '... got the right name'); +is($anon_enum->parent->name, 'Str', '... got the right parent name'); + +ok($anon_enum->check($_), "'$_' is a language") for @valid_languages; + + +ok( !$anon_enum->equals( enum [qw(foo bar)] ), "doesn't equal a diff enum" ); +ok( $anon_enum->equals( $anon_enum ), "equals itself" ); +ok( $anon_enum->equals( enum \@valid_languages ), "equals duplicate" ); + +ok( !$anon_enum->is_subtype_of('Object'), 'enum not a subtype of Object'); +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 +like exception { Moose::Meta::TypeConstraint::Enum->new(name => 'ZeroValues', values => []) }, + qr/You must have at least two values to enumerate through/; + +like exception { Moose::Meta::TypeConstraint::Enum->new(name => 'OneValue', values => [ 'a' ]) }, + qr/You must have at least two values to enumerate through/; + +like exception { Moose::Meta::TypeConstraint::Enum->new(name => 'ReferenceInEnum', values => [ 'a', {} ]) }, + qr/Enum values must be strings, not 'HASH\(0x\w+\)'/; + +like exception { Moose::Meta::TypeConstraint::Enum->new(name => 'UndefInEnum', values => [ 'a', undef ]) }, + qr/Enum values must be strings, not undef/; + +like exception { + 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;