File: 02-synopsis.t

package info (click to toggle)
libclass-type-enum-perl 0.014-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 136 kB
  • sloc: perl: 178; makefile: 2
file content (48 lines) | stat: -rw-r--r-- 1,287 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
use warnings;
use strict;
use Test::More;

BEGIN {
  eval "require Moo; require Type::Tiny;";
  plan skip_all => "Synopsis test requires Moo and Type::Tiny" if $@;
};

{
  package Toast::Status;
  use Class::Type::Enum values => ['bread', 'toasting', 'toast', 'burnt'];
}
 
{
  package Toast;
  # Don't let this show up as a dependency:
  BEGIN { 
    Moo->import();
  }
 
  has status => (
    is       => 'rw',
    required => 1,
    isa      => Toast::Status->type_constraint,
    coerce   => 1,
    handles => [ Toast::Status->list_is_methods ],
  );
}
 
my @toast = map { Toast->new(status => $_) } qw( toast burnt bread bread toasting toast );
 
my @trashcan = grep { $_->is_burnt } @toast;
my @plate    = grep { $_->is_toast } @toast;
 
my $ready_status   = Toast::Status->new('toast');
my @eventual_toast = grep { $_->status < $ready_status } @toast;
my @eventual_toast_cmp = grep { $_->status lt 'toast' } @toast;

is( scalar(@trashcan), 1, "Found one burnt toast" );
is( scalar(@plate),    2, "Found two actual toast" );
is( scalar(@eventual_toast),     3, "And three on the way" );
is( scalar(@eventual_toast_cmp), 3, "Even with string compare" );

eval { Toast->new(status => 'ack') };
ok( index($@, 'Value [ack] is not valid') > 0, 'Type constraint works' );

done_testing;