Commit | Line | Data |
a30fa891 |
1 | BEGIN { |
2 | use strict; |
3 | use warnings; |
4 | use Test::More tests=>26; |
5 | use Test::Exception; |
6 | } |
7 | |
8 | { |
9 | package Test::MooseX::Meta::TypeConstraint::Structured::Tuple; |
10 | |
11 | use Moose; |
12 | use MooseX::Types::Structured qw(Tuple); |
13 | use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef Maybe); |
14 | use MooseX::Types -declare => [qw(MyString)]; |
15 | |
16 | subtype MyString, |
17 | as 'Str', |
18 | where { $_=~m/abc/}; |
19 | |
20 | #use Data::Dump qw/dump/; warn dump Tuple; |
21 | |
22 | has 'tuple' => (is=>'rw', isa=>Tuple[Int, Str, MyString]); |
23 | has 'tuple_with_param' => (is=>'rw', isa=>Tuple[Int, Str, ArrayRef[Int]]); |
24 | has 'tuple_with_maybe' => (is=>'rw', isa=>Tuple[Int, Str, Maybe[Int], Object]); |
25 | has 'tuple_with_maybe2' => (is=>'rw', isa=>Tuple[Int, Str, Maybe[Int]]); |
26 | has 'tuple_with_union' => (is=>'rw', isa=>Tuple[Int,Str,Int|Object,Int]); |
27 | has 'tuple2' => (is=>'rw', isa=>Tuple[Int,Str,Int]); |
28 | has 'tuple_with_parameterized' => (is=>'rw', isa=>Tuple[Int,Str,Int,ArrayRef[Int]]); |
29 | } |
30 | |
31 | ## Instantiate a new test object |
32 | |
33 | ok my $record = Test::MooseX::Meta::TypeConstraint::Structured::Tuple->new |
34 | => 'Instantiated new Record test class.'; |
35 | |
36 | isa_ok $record => 'Test::MooseX::Meta::TypeConstraint::Structured::Tuple' |
37 | => 'Created correct object type.'; |
38 | |
39 | ## Test Tuple type constraint |
40 | |
41 | lives_ok sub { |
42 | $record->tuple([1,'hello', 'test.abc.test']); |
43 | } => 'Set tuple attribute without error'; |
44 | |
45 | is $record->tuple->[0], 1 |
46 | => 'correct set the tuple attribute index 0'; |
47 | |
48 | is $record->tuple->[1], 'hello' |
49 | => 'correct set the tuple attribute index 1'; |
50 | |
51 | is $record->tuple->[2], 'test.abc.test' |
52 | => 'correct set the tuple attribute index 2'; |
53 | |
54 | throws_ok sub { |
55 | $record->tuple([1,'hello', 'test.xxx.test']); |
56 | }, qr/Attribute \(tuple\) does not pass the type constraint/ |
57 | => 'Properly failed for bad value in custom type constraint'; |
58 | |
59 | throws_ok sub { |
60 | $record->tuple(['asdasd',2, 'test.abc.test']); |
61 | }, qr/Attribute \(tuple\) does not pass the type constraint/ |
62 | => 'Got Expected Error for violating constraints'; |
63 | |
64 | ## Test tuple_with_maybe |
65 | |
66 | lives_ok sub { |
67 | $record->tuple_with_maybe([1,'hello', 1, $record]); |
68 | } => 'Set tuple attribute without error'; |
69 | |
70 | throws_ok sub { |
71 | $record->tuple_with_maybe([1,'hello', 'a', $record]); |
72 | }, qr/Attribute \(tuple_with_maybe\) does not pass the type constraint/ |
73 | => 'Properly failed for bad value parameterized constraint'; |
74 | |
75 | lives_ok sub { |
76 | $record->tuple_with_maybe([1,'hello',undef, $record]); |
77 | } => 'Set tuple attribute without error skipping optional parameter'; |
78 | |
79 | ## Test tuple_with_maybe2 |
80 | |
81 | lives_ok sub { |
82 | $record->tuple_with_maybe2([1,'hello', 1]); |
83 | } => 'Set tuple attribute without error'; |
84 | |
85 | throws_ok sub { |
86 | $record->tuple_with_maybe2([1,'hello', 'a']); |
87 | }, qr/Attribute \(tuple_with_maybe2\) does not pass the type constraint/ |
88 | => 'Properly failed for bad value parameterized constraint'; |
89 | |
90 | lives_ok sub { |
91 | $record->tuple_with_maybe2([1,'hello',undef]); |
92 | } => 'Set tuple attribute without error skipping optional parameter'; |
93 | |
94 | throws_ok sub { |
95 | $record->tuple_with_maybe2([1,'hello']); |
96 | }, qr/Attribute \(tuple_with_maybe2\) does not pass the type constraint/ |
97 | => 'Properly fails for missing maybe (needs to be at least undef)'; |
98 | |
99 | ## Test Tuple with parameterized type |
100 | |
101 | lives_ok sub { |
102 | $record->tuple_with_param([1,'hello', [1,2,3]]); |
103 | } => 'Set tuple attribute without error'; |
104 | |
105 | throws_ok sub { |
106 | $record->tuple_with_param([1,'hello', [qw/a b c/]]); |
107 | }, qr/Attribute \(tuple_with_param\) does not pass the type constraint/ |
108 | => 'Properly failed for bad value parameterized constraint'; |
109 | |
110 | ## Test tuple2 (Tuple[Int,Str,Int]) |
111 | |
112 | ok $record->tuple2([1,'hello',3]) |
113 | => "[1,'hello',3] properly suceeds"; |
114 | |
115 | throws_ok sub { |
116 | $record->tuple2([1,2,'world']); |
117 | }, qr/Attribute \(tuple2\) does not pass the type constraint/ => "[1,2,'world'] properly fails"; |
118 | |
119 | throws_ok sub { |
120 | $record->tuple2(['hello1',2,3]); |
121 | }, qr/Attribute \(tuple2\) does not pass the type constraint/ => "['hello',2,3] properly fails"; |
122 | |
123 | throws_ok sub { |
124 | $record->tuple2(['hello2',2,'world']); |
125 | }, qr/Attribute \(tuple2\) does not pass the type constraint/ => "['hello',2,'world'] properly fails"; |
126 | |
127 | |
128 | ## Test tuple_with_parameterized (Tuple[Int,Str,Int,ArrayRef[Int]]) |
129 | |
130 | ok $record->tuple_with_parameterized([1,'hello',3,[1,2,3]]) |
131 | => "[1,'hello',3,[1,2,3]] properly suceeds"; |
132 | |
133 | throws_ok sub { |
134 | $record->tuple_with_parameterized([1,2,'world']); |
135 | }, qr/Attribute \(tuple_with_parameterized\) does not pass the type constraint/ |
136 | => "[1,2,'world'] properly fails"; |
137 | |
138 | throws_ok sub { |
139 | $record->tuple_with_parameterized(['hello1',2,3]); |
140 | }, qr/Attribute \(tuple_with_parameterized\) does not pass the type constraint/ |
141 | => "['hello',2,3] properly fails"; |
142 | |
143 | throws_ok sub { |
144 | $record->tuple_with_parameterized(['hello2',2,'world']); |
145 | }, qr/Attribute \(tuple_with_parameterized\) does not pass the type constraint/ |
146 | => "['hello',2,'world'] properly fails"; |
147 | |
148 | throws_ok sub { |
149 | $record->tuple_with_parameterized([1,'hello',3,[1,2,'world']]); |
150 | }, qr/Attribute \(tuple_with_parameterized\) does not pass the type constraint/ |
151 | => "[1,'hello',3,[1,2,'world']] properly fails"; |