File: 10context.t

package info (click to toggle)
libsyntax-keyword-match-perl 0.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 236 kB
  • sloc: perl: 902; ansic: 34; makefile: 3
file content (60 lines) | stat: -rw-r--r-- 1,003 bytes parent folder | download
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
60
#!/usr/bin/perl

use v5.14;
use warnings;

use Test2::V0;

use Syntax::Keyword::Match;

# in scalar context
{
   my $ret = do {
      my $topic = "two";
      match($topic : eq) {
         case("one")   { 1 }
         case("two")   { 2 }
         case("three") { 3 }
      }
   };
   is( $ret, 2, 'match/case in scalar context' );
}

{
   sub yieldsnumber { 99 }

   my $ret = do {
      match(1 : ==) {
         case(1) { yieldsnumber }
         case(2) { 0 }
      }
   };
   is( $ret, 99, 'match/case yields value of entersub' );
}

# in list context
{
   my @ret = do {
      my $topic = "x,y";
      match($topic : eq) {
         case("a,b") { "a", "b" }
         case("x,y") { "x", "y" }
      }
   };
   is( \@ret, [ "x", "y" ], 'match/case in list context' );
}

# as final-expr
{
   sub func {
      match($_[0] : ==) {
         case(1) { "one" }
         case(2) { "two" }
         case(3) { "three" }
      }
   }

   is( scalar func(2), "two", 'match/case as final-expr' );
}

done_testing;