Regenerate test files
[gitmo/Mouse.git] / t / 040_type_constraints / 015_enum.t
1 #!/usr/bin/perl
2 # This is automatically generated by author/import-moose-test.pl.
3 # DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
4 use t::lib::MooseCompat;
5
6 use strict;
7 use warnings;
8
9 use Test::More;
10 $TODO = q{Mouse is not yet completed};
11 use Test::Exception;
12
13 use Scalar::Util ();
14
15 use Mouse::Util::TypeConstraints;
16
17 enum Letter => 'a'..'z', 'A'..'Z';
18 enum Language => 'Perl 5', 'Perl 6', 'PASM', 'PIR'; # any others? ;)
19 enum Metacharacter => ['*', '+', '?', '.', '|', '(', ')', '[', ']', '\\'];
20
21 my @valid_letters = ('a'..'z', 'A'..'Z');
22
23 my @invalid_letters = qw/ab abc abcd/;
24 push @invalid_letters, qw/0 4 9 ~ @ $ %/;
25 push @invalid_letters, qw/l33t st3v4n 3num/;
26
27 my @valid_languages = ('Perl 5', 'Perl 6', 'PASM', 'PIR');
28 my @invalid_languages = ('perl 5', 'Python', 'Ruby', 'Perl 666', 'PASM++');
29 # note that "perl 5" is invalid because case now matters
30
31 my @valid_metacharacters = (qw/* + ? . | ( ) [ ] /, '\\');
32 my @invalid_metacharacters = qw/< > & % $ @ ! ~ `/;
33 push @invalid_metacharacters, qw/.* fish(sticks)? atreides/;
34 push @invalid_metacharacters, '^1?$|^(11+?)\1+$';
35
36 Mouse::Util::TypeConstraints->export_type_constraints_as_functions();
37
38 ok(Letter($_), "'$_' is a letter") for @valid_letters;
39 ok(!Letter($_), "'$_' is not a letter") for @invalid_letters;
40
41 ok(Language($_), "'$_' is a language") for @valid_languages;
42 ok(!Language($_), "'$_' is not a language") for @invalid_languages;
43
44 ok(Metacharacter($_), "'$_' is a metacharacter") for @valid_metacharacters;
45 ok(!Metacharacter($_), "'$_' is not a metacharacter")
46     for @invalid_metacharacters;
47
48 # check anon enums
49
50 my $anon_enum = enum \@valid_languages;
51 isa_ok($anon_enum, 'Mouse::Meta::TypeConstraint');
52
53 is($anon_enum->name, '__ANON__', '... got the right name');
54 is($anon_enum->parent->name, 'Str', '... got the right parent name');
55
56 ok($anon_enum->check($_), "'$_' is a language") for @valid_languages;
57
58
59 ok( !$anon_enum->equals( enum [qw(foo bar)] ), "doesn't equal a diff enum" );
60 ok( $anon_enum->equals( $anon_enum ), "equals itself" );
61 ok( $anon_enum->equals( enum \@valid_languages ), "equals duplicate" );
62
63 ok( !$anon_enum->is_subtype_of('Object'), 'enum not a subtype of Object');
64 ok( !$anon_enum->is_a_type_of('Object'), 'enum not type of Object');
65
66 ok( !$anon_enum->is_subtype_of('ThisTypeDoesNotExist'), 'enum not a subtype of nonexistant type');
67 ok( !$anon_enum->is_a_type_of('ThisTypeDoesNotExist'), 'enum not type of nonexistant type');
68
69 # validation
70 throws_ok { Mouse::Meta::TypeConstraint->new(name => 'ZeroValues', values => []) }
71     qr/You must have at least two values to enumerate through/;
72
73 throws_ok { Mouse::Meta::TypeConstraint->new(name => 'OneValue', values => [ 'a' ]) }
74     qr/You must have at least two values to enumerate through/;
75
76 throws_ok { Mouse::Meta::TypeConstraint->new(name => 'ReferenceInEnum', values => [ 'a', {} ]) }
77     qr/Enum values must be strings, not 'HASH\(0x\w+\)'/;
78
79 throws_ok { Mouse::Meta::TypeConstraint->new(name => 'UndefInEnum', values => [ 'a', undef ]) }
80     qr/Enum values must be strings, not undef/;
81
82 throws_ok {
83     package Foo;
84     use Mouse;
85     use Mouse::Util::TypeConstraints;
86
87     has error => (
88         is      => 'ro',
89         isa     => enum ['a', 'aa', 'aaa'], # should be parenthesized!
90         default => 'aa',
91     );
92 } qr/enum called with an array reference and additional arguments\. Did you mean to parenthesize the enum call's parameters\?/;
93
94
95 done_testing;