]> git.vpit.fr Git - perl/modules/Lexical-Types.git/blob - t/10-base.t
Add more basic tests
[perl/modules/Lexical-Types.git] / t / 10-base.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 2 + 3 * 4 + 5 * 2
7                       + 2 + 3 * 2 + 5 * 2
8                       + 2 + 3 * 4 + 5 * 2;
9
10 our $calls;
11
12 sub Int::TYPEDSCALAR { ++$calls; (caller(0))[2] }
13
14 {
15  use Lexical::Types;
16
17  {
18   my $desc = 'single uninit';
19   local $calls = 0;
20   my Int $x;
21   is $calls, 1,          "$desc: correct number of calls";
22   is $x,     __LINE__-2, "$desc: initialized correctly";
23  }
24
25  {
26   my $desc = 'double uninit';
27   local $calls = 0;
28   my Int ($x, $y);
29   is $calls, 2,      "$desc: correct number of calls";
30   is $x, __LINE__-2, "$desc: initialized \$x correctly";
31   is $y, __LINE__-3, "$desc: initialized \$y correctly";
32  }
33
34  {
35   my $desc = 'double uninit with undef begin';
36   local $calls = 0;
37   my Int (undef, $x, $y);
38   is $calls, 2,      "$desc: correct number of calls";
39   is $x, __LINE__-2, "$desc: initialized \$x correctly";
40   is $y, __LINE__-3, "$desc: initialized \$y correctly";
41  }
42
43  {
44   my $desc = 'double uninit with undef middle';
45   local $calls = 0;
46   my Int ($x, undef, $y);
47   is $calls, 2,      "$desc: correct number of calls";
48   is $x, __LINE__-2, "$desc: initialized \$x correctly";
49   is $y, __LINE__-3, "$desc: initialized \$y correctly";
50  }
51
52  {
53   my $desc = 'double uninit with undef end';
54   local $calls = 0;
55   my Int ($x, undef, $y);
56   is $calls, 2,      "$desc: correct number of calls";
57   is $x, __LINE__-2, "$desc: initialized \$x correctly";
58   is $y, __LINE__-3, "$desc: initialized \$y correctly";
59  }
60
61  {
62   my $desc = 'quadruple uninit with parens';
63   local $calls = 0;
64   my Int ($x, ($y, $z), $t);
65   is $calls, 4,      "$desc: correct number of calls";
66   is $x, __LINE__-2, "$desc: initialized \$x correctly";
67   is $y, __LINE__-3, "$desc: initialized \$y correctly";
68   is $z, __LINE__-4, "$desc: initialized \$z correctly";
69   is $t, __LINE__-5, "$desc: initialized \$t correctly";
70  }
71
72  {
73   my $desc = 'quadruple uninit with parens and undef';
74   local $calls = 0;
75   my Int ($x, undef, ($y, undef, $z), undef, $t);
76   is $calls, 4,      "$desc: correct number of calls";
77   is $x, __LINE__-2, "$desc: initialized \$x correctly";
78   is $y, __LINE__-3, "$desc: initialized \$y correctly";
79   is $z, __LINE__-4, "$desc: initialized \$z correctly";
80   is $t, __LINE__-5, "$desc: initialized \$t correctly";
81  }
82
83  {
84   my $desc = 'single init';
85   local $calls = 0;
86   my Int $x = 'x';
87   is $calls, 1,   "$desc: correct number of calls";
88   is $x,     'x', "$desc: initialized correctly";
89  }
90
91  {
92   my $desc = 'double init';
93   local $calls = 0;
94   my Int ($x, $y) = ('x', 'y');
95   is $calls, 2,   "$desc: correct number of calls";
96   is $x,     'x', "$desc: initialized \$x correctly";
97   is $y,     'y', "$desc: initialized \$y correctly";
98  }
99
100  {
101   my $desc = 'double partial init';
102   local $calls = 0;
103   my Int ($x, undef, $y) = ('x');
104   is $calls, 2,     "$desc: correct number of calls";
105   is $x,     'x',   "$desc: initialized \$x correctly";
106   is $y,     undef, "$desc: initialized \$y correctly";
107  }
108
109  {
110   my $desc = 'quadruple init with parens';
111   local $calls = 0;
112   my Int ($x, ($y, $z), $t) = ('x', 'y');
113   is $calls, 4,      "$desc: correct number of calls";
114   is $x, 'x',   "$desc: initialized \$x correctly";
115   is $y, 'y',   "$desc: initialized \$y correctly";
116   is $z, undef, "$desc: initialized \$z correctly";
117   is $t, undef, "$desc: initialized \$t correctly";
118  }
119
120  {
121   my $desc = 'quadruple init with parens and undef';
122   local $calls = 0;
123   my Int ($x, ($y, undef, $z), $t) = ('x', 'y');
124   is $calls, 4,      "$desc: correct number of calls";
125   is $x, 'x',   "$desc: initialized \$x correctly";
126   is $y, 'y',   "$desc: initialized \$y correctly";
127   is $z, undef, "$desc: initialized \$z correctly";
128   is $t, undef, "$desc: initialized \$t correctly";
129  }
130
131  {
132   my $desc = 'for';
133   local $calls = 0;
134   for my Int $x (0) {
135    is $calls, 0, "$desc: correct number of calls";
136    is $x,     0, "$desc: initialized correctly";
137   }
138  }
139
140  {
141   my $desc = 'argument list';
142   local $calls = 0;
143   sub {
144    my Int ($x, $y) = @_;
145    is $calls, 2,   "$desc: correct number of calls";
146    is $x,     'x', "$desc: initialized \$x correctly";
147    is $y,     'y', "$desc: initialized \$y correctly";
148   }->('x', 'y');
149  }
150
151  {
152   my $desc = 'argument list with undef begin';
153   local $calls = 0;
154   sub {
155    my Int (undef, $x, $y) = @_;
156    is $calls, 2,     "$desc: correct number of calls";
157    is $x,     'y',   "$desc: initialized \$x correctly";
158    is $y,     undef, "$desc: initialized \$y correctly";
159   }->('x', 'y');
160  }
161
162  {
163   my $desc = 'argument list with undef middle';
164   local $calls = 0;
165   sub {
166    my Int ($x, undef, $y) = @_;
167    is $calls, 2,     "$desc: correct number of calls";
168    is $x,     'x',   "$desc: initialized \$x correctly";
169    is $y,     undef, "$desc: initialized \$y correctly";
170   }->('x', 'y');
171  }
172
173  {
174   my $desc = 'argument list with undef end';
175   local $calls = 0;
176   sub {
177    my Int ($x, $y, undef) = @_;
178    is $calls, 2,   "$desc: correct number of calls";
179    is $x,     'x', "$desc: initialized \$x correctly";
180    is $y,     'y', "$desc: initialized \$y correctly";
181   }->('x', 'y');
182  }
183
184  {
185   my $desc = 'argument list with parens';
186   local $calls = 0;
187   sub {
188    my Int ($x, ($y, $z), $t) = @_;
189    is $calls, 4,   "$desc: correct number of calls";
190    is $x,     'x', "$desc: initialized \$x correctly";
191    is $y,     'y', "$desc: initialized \$y correctly";
192    is $z,     'z', "$desc: initialized \$z correctly";
193    is $t,     't', "$desc: initialized \$t correctly";
194   }->('x', 'y', 'z', 't');
195  }
196
197  {
198   my $desc = 'argument list with parens';
199   local $calls = 0;
200   sub {
201    my Int ($x, ($y, undef, $z), $t) = @_;
202    is $calls, 4,     "$desc: correct number of calls";
203    is $x,     'x',   "$desc: initialized \$x correctly";
204    is $y,     'y',   "$desc: initialized \$y correctly";
205    is $z,     'z',   "$desc: initialized \$z correctly";
206    is $t,     undef, "$desc: initialized \$t correctly";
207   }->('x', 'y', 'foo', 'z');
208  }
209 }