File: mx_types_structured.t

package info (click to toggle)
libtrycatch-perl 1.003000-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 464 kB
  • sloc: perl: 2,590; ansic: 19; makefile: 15
file content (37 lines) | stat: -rw-r--r-- 814 bytes parent folder | download | duplicates (4)
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
use strict;
use warnings;
use Test::More;

use Scope::Upper qw/unwind/;
BEGIN {
  eval { require MooseX::Types::Structured; };
  if ($@) {
    plan skip_all => "This test requires MooseX::Types::Structured"
  } else {
    plan tests => 3;
  }
}

BEGIN { use_ok "TryCatch" or BAIL_OUT("Cannot load TryCatch") };

use MooseX::Types::Structured qw/Dict/;

sub throw_struct {
  my @args = @_;
  try {
    die { code => $args[0] };
  }
  catch (Dict[code => Int] $err where { $_->{code} >= 200 } ) {
    return "Code over 200";
  }
  catch (Dict[code => Int] $err where { $_->{code} < 200 } ) {
    return "Code less than 200";
  }
  catch {
    return "otherwise";
  }
  return "no error";
}

is throw_struct(200), "Code over 200", "where condition 1";
is throw_struct(100), "Code less than 200", "where condition 2";