File: 12return.t

package info (click to toggle)
libsyntax-keyword-try-perl 0.31-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 264 kB
  • sloc: perl: 898; makefile: 3
file content (110 lines) | stat: -rw-r--r-- 1,890 bytes parent folder | download | duplicates (2)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/perl

use v5.14;
use warnings;

use Test2::V0;

use Syntax::Keyword::Try;

# return from try
{
   my $after;
   ( sub {
      try { return }
      catch ($e) {}
      $after++;
   } )->();
   ok( !$after, 'code after try{return} in void context is not invoked' );
}

# return SCALAR from try
{
   is(
      scalar ( sub {
         try { return "result" }
         catch ($e) {}
         return "nope";
      } )->(),
      "result",
      'return SCALAR in try yields correct value'
   );
}

# return LIST from try
{
   is(
      [ sub {
         try { return qw( A B C ) } catch ($e) {}
      }->() ],
      [qw( A B C )],
      'return LIST in try yields correct values'
   );
}

# return from two nested try{}s
{
   my $after;

   is(
      ( sub {
         try {
            try { return "result" }
            catch ($e) {}
         }
         catch ($e) {}
         $after++;
         return "nope";
      } )->(),
      "result",
      'return in try{try{}} leaves containing function'
   );
   ok( !$after, 'code after try{try{return}} is not invoked' );
}

# return inside eval{} inside try{}
{
   is(
      ( sub {
         my $two;
         try {
            my $one = eval { return 1 };
            $two = $one + 1;
         }
         catch ($e) {}
         return $two;
      } )->(),
      2,
      'return in eval{} inside try{} behaves as expected'
   );
}

# return inside try{} inside eval{}
{
   is(
      ( sub {
         my $ret = eval {
            try { return "part"; }
            catch ($e) {}
         };
         return "($ret)";
      } )->(),
      "(part)",
      'return in try{} inside eval{}'
   );
}

# return from catch
{
   is(
      ( sub {
         try { die "oopsie" }
         catch ($e) { return "result" }
         return "nope";
      } )->(),
      "result",
      'return in catch leaves containing function'
   );
}

done_testing;