File: 20experimental-dispatch.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 (94 lines) | stat: -rw-r--r-- 2,028 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/perl

use v5.14;
use warnings;

use Test2::V0;

use Syntax::Keyword::Match qw( match :experimental(dispatch) );

# code stolen from benchmark.pl
sub match_case
{
   my ( $x ) = @_;
   match( $x : eq ) {
      case( "one"   ) { return 1; }
      case( "two"   ) { return 2; }
      case( "three" ) { return 3; }
      case( "four"  ) { return 4; }
      case( "five"  ) { return 5; }
      case( "six"   ) { return 6; }
      case( "seven" ) { return 7; }
      case( "eight" ) { return 8; }
      case( "nine"  ) { return 9; }
      default         { return 0; }
   }
}

# large stringy match
{
   my $i = 0;
   my %expect = map { $_ => $i++ } qw( zero one two three four five six seven eight nine );

   is( match_case( $_ ), $expect{$_}, "match_case($_)" )
      for sort keys %expect;
}

# overloaded 'eq' operator
{
   my $equal;
   package Greedy {
      use overload 'eq' => sub { $equal };
   }

   sub greedy_is_ten
   {
      match(bless [], "Greedy" : eq) {
         case("ten")    { return "YES" }
         case("twenty") { return "NO" }
         default        { return "NO" }
      }
   }

   $equal = 1;
   is( greedy_is_ten, "YES", 'Greedy is 10 when set' );

   $equal = 0;
   is( greedy_is_ten, "NO", 'Greedy is not 10 when unset' );
}

# mixed dispsatch + var
{
   my $four = 4;

   match( 2 : == ) {
      case(1)     { fail("No"); }
      case(2)     { pass("Mixed dispatch"); }
      case(3)     { fail("No"); }
      case($four) { fail("No"); }
      case(5)     { fail("No"); }
   }
}

# Various cornercases of our copy-pasted do_neq() function
{
   match(1 : ==) {
      case(1) { pass("IV == IV"); }
      default { fail("IV == IV"); }
   }

   match(2.2 : ==) {
      case(2.0) { fail("NV == NV first"); }
      case(2.2) { pass("NV == NV"); }
      default   { fail("NV == NV second"); }
   }

   match(~3 : ==) {
      case(3)  { fail("UV == UV a"); }
      case(~4) { fail("UV == UV b"); }
      case(~3) { pass("UV == UV"); }
      default  { fail("UV == UV c"); }
   }
}

done_testing;