File: STRICT.t

package info (click to toggle)
libcontextual-return-perl 0.004014-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 348 kB
  • sloc: perl: 1,453; makefile: 2
file content (42 lines) | stat: -rw-r--r-- 1,473 bytes parent folder | download | duplicates (5)
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
use Contextual::Return;

sub bar {
    return 'in bar';
}

sub foo {
    return STRICT
        PUREBOOL  { 1 }
        BOOL      { 0 }
        LIST      { 1,2,3 }
        NUM       { 42 }
        STR       { 'forty-two' }
        REF       { [] }
        DEFAULT   { {} }
    ;
}

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

is_deeply [ ::foo() ], [1,2,3]                         => 'LIST context';

is do{ ::foo() ? 'true' : 'false' }, 'true'            => 'PURE BOOLEAN context';

is do{ (my $x = ::foo()) ? 'true' : 'false' }, 'false' => 'BOOLEAN context';

is 0+::foo(), 42                                       => 'NUMERIC context';

is "".::foo(), 'forty-two'                             => 'STRING context';

ok !eval { ::foo(); 1 }                                => 'No VOID context';
like $@, qr{Can't call main::foo in a void context}    => '...with correct error msg';

ok !eval { my $scalar = ${::foo()}; 1 }                => 'No SCALARREF context';
like $@, qr{Call to main::foo didn't return a scalar reference, as required} => '...with correct error msg';

ok !eval { my @list = @{::foo()}; 1 }                             => 'No ARRAYREF context';
like $@, qr{Call to main::foo didn't return an array reference, as required} => '...with correct error msg';

ok !eval { my %hash = %{::foo()}; 1 }                             => 'No HASHREF context';
like $@, qr{Call to main::foo didn't return a hash reference, as required}   => '...with correct error msg';