root/t/syntax/signature.t

Revision 21789, 2.8 kB (checked in by lwall, 5 months ago)

[STD vs t] more Mutually Assured Construction

  • Property svn:mime-type set to text/plain; charset=UTF-8
  • Property svn:eol-style set to native
Line 
1use v6;
2
3use Test;
4
5plan 29;
6
7# Signature values and pretty-printing
8# L<S06/"Parameters and arguments">
9{
10    # let's start with valid signatures whose canonical stringy form looks
11    # just like their source. I incidentally use different sigils, can't
12    # throw in the complete cartesian product here...
13    my @sigs =
14        ( ':($x)',                'single required positional'
15        , ':($x:)',               'invocant only'
16        , ':(@x, $y)',            'two required positionals'
17        , ':($x, %y?)',           'required and optional positionals'
18        , ':($x is rw is ref is lazy is moose)', # note order matters :/
19                                  'traits (including user defined)'
20        , ':($x, $y, :$z)',       'positional and named'
21        , ':($x, $y?, :$z)',      'optional positional and named'
22        , ':(:$x)',               'required named'
23        , ':(:$x?)',              'optional named'
24        , ':(:short($long))',     'long named'
25        , ':(:short($long)?)',    'optional long named'
26        , ':($ : %x)',            'dummy invocant'
27        , ':($x :($y))',          'unpacking(1)'
28        , ':($x :($y: $z))',      'unpacking(2)'
29        # add more here.
30        # We parse these correctly but don't pretty print them correctly yet.
31        , ':($x = 42)',           'positional with default'
32        , ':(@x = (1, 2))',       'positional array with default'
33        , ':(%x = (1 => 2))',     'positional hash with default'
34        , ':(:$x = 42)',          'named with default'
35        , ':(:@x = (1, 2))',      'named array with default'
36        , ':(:%x = (1 => 2))',    'named hash with default'
37        , ':(:x($y) = 42)',       'longnamed with default'
38        , ':(:x(@y) = (1, 2))',   'longnamed array with default'
39        , ':(:x(%y) = (1 => 2))', 'longnamed hash with default'
40        );
41    for @sigs -> $sig, $desc {
42        is eval("my \$s = $sig; qq[\$s]"), $sig, "signature stringifies - $desc";
43    }
44
45    # ("" ~ :() is just an interim hack to dispatch into pretty-newval. will be removed.)
46    # canonized version is different from source
47    is eval('""~:($x!)'),          ':($x)',      'required positional with hint';
48    is eval('""~:($x? = 42)'),     ':($x = 42)', 'positional with default and hint';
49    is eval('""~:(@y? = (1, 2))'), ':(@y = (1, 2))',
50                                                'named array with default and hint';
51
52    is eval('""~:($x is rw is ro is rw is copy is ro is rw)'),
53                                ':($x is rw)',  'last repeated trait wins'; # XXX: spec
54    is eval('""~:($x is moose is ref is ro is lazy)'), # 'is ro' is default thus not printed
55                                ':($x is ref is lazy is moose)',
56                                'interleaved traits'; # XXX spec this minor point?
57
58
59    # should die
60    eval_dies_ok ':($x! = 42)', "required params can't have a default";
61}
Note: See TracBrowser for help on using the browser.