File: args.t

package info (click to toggle)
libcontextual-return-perl 0.003001-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 324 kB
  • ctags: 52
  • sloc: perl: 1,187; makefile: 2
file content (59 lines) | stat: -rw-r--r-- 1,661 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use Contextual::Return;

sub foo {
    return
        VOID      { $_[1] = 99 }
        BOOL      { @_ > 0 }
        LIST      { (@_) x 2 }
        NUM       { scalar @_ }
        STR       { join '|', @_ }
        SCALAR    { $_[0] }
        SCALARREF { my $var = $_[0]; \$var }
        HASHREF   { { args => \@_} }
        ARRAYREF  { \@_ }
    ;
}

package Other;
use Test::More 'no_plan';

my @arg_lists = (
    [99],
    [],
    [99..101],
);

for my $arg_list (@arg_lists) {
    my $call = 'foo(' . join(q{,}, @{$arg_list}) . ')';

    is_deeply [ ::foo(@{$arg_list}) ],
              [(@{$arg_list})x2]         
                                                => "list test on $call";

    is do{ ::foo(@{$arg_list}) ? 'true' : 'false' },
       do{ @{$arg_list} ? 'true' : 'false' }
                                                => "boolean test on $call";

    is 0+::foo(@{$arg_list}), 0+@{$arg_list}
                                                => "number test on $call";

    is "" . ::foo(@{$arg_list}),
       join('|',@{$arg_list})
                                                => "string test on $call";

    is ${::foo(@{$arg_list})},
       $arg_list->[0]
                                                => "scalar test on $call";

    is_deeply \%{::foo(@{$arg_list})},
              { args => $arg_list }
                                                => "hash test on $call";

    is_deeply \@{::foo(@{$arg_list})},
              \@{$arg_list}
                                                => "array test on $call";
}

my @real_args = 1..3;
::foo(@real_args);
is_deeply \@real_args, [1,99,3]                 => "arg changes stick"