]> git.vpit.fr Git - perl/modules/Lexical-Types.git/blob - t/10-args.t
Allow skipping declarations by returning an empty list from the mangler
[perl/modules/Lexical-Types.git] / t / 10-args.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 12 + 6;
7
8 {
9  package Lexical::Types::Test::LTT;
10
11  sub TYPEDSCALAR {
12   $_[1] = (caller(0))[2];
13   Test::More::is($_[2], 'LTT', 'original type is ok');
14   ();
15  }
16
17  no warnings 'once';
18  *TS = \&TYPEDSCALAR;
19 }
20
21 {
22  package Lexical::Types::Test::LTT2;
23
24  sub TYPEDSCALAR { 1 .. 2 }
25 }
26
27 {
28  package LTT;
29  
30  no warnings 'once';
31  *ts = \&Lexical::Types::Test::LTT::TYPEDSCALAR
32 }
33
34 {
35  use Lexical::Types as => 'Lexical::Types::Test';
36  my LTT $x;
37  is $x, __LINE__-1, 'as => string, without trailing ::';
38
39  no Lexical::Types;
40  my LTT $y;
41  is $y, undef, 'after no';
42 }
43
44 {
45  use Lexical::Types as => 'Lexical::Types::Test::';
46  my LTT $x;
47  is $x, __LINE__-1, 'as => string, with trailing ::';
48 }
49
50 {
51  use Lexical::Types as => sub { return };
52  my LTT $x;
53  is $x, undef, 'as => code, returning nothing';
54 }
55
56 {
57  use Lexical::Types as => sub { 'Lexical::Types::Test::LTT' };
58  my LTT $x;
59  is $x, __LINE__-1, 'as => code, returning package name';
60 }
61
62 {
63  use Lexical::Types as => sub { 'Lexical::Types::Test::LTT', undef };
64  my LTT $x;
65  is $x, __LINE__-1, 'as => code, returning package name and undef';
66 }
67
68 {
69  use Lexical::Types as => sub { undef, 'ts' };
70  my LTT $x;
71  is $x, __LINE__-1, 'as => code, returning undef and method name';
72 }
73
74 {
75  use Lexical::Types as => sub { 'Lexical::Types::Test::LTT', 'TS' };
76  my LTT $x;
77  is $x, __LINE__-1, 'as => code, returning package and method name';
78 }
79
80 {
81  my $expect = qr/^Invalid ARRAY reference/;
82  local $@;
83  eval q[
84   use Lexical::Types as => [ qw/a b c/ ];
85   my LTT $x;
86  ];
87  like $@, $expect, 'as => array';
88 }
89
90 {
91  my $expect = qr/^Lexical::Types mangler should return zero, one or two scalars, but got 3/;
92  diag 'This will throw two warnings' if $] >= 5.008008 and $] < 5.009;
93  local $@;
94  eval q[
95   use Lexical::Types as => sub { qw/a b c/ };
96   my LTT $x;
97  ];
98  like $@, $expect, 'as => code, returning three scalars';
99 }
100
101 {
102  my $expect = qr/^Typed scalar initializer method should return zero or one scalar, but got 2/;
103  local $@;
104  eval q[
105   use Lexical::Types as => sub { 'Lexical::Types::Test::LTT2' };
106   my LTT $x;
107  ];
108  like $@, $expect, 'as => code, initializing by returning two scalars';
109 }
110
111 my LTT $x;
112 is $x, undef, 'out of scope';