File: subtype.t

package info (click to toggle)
libmoox-types-mooselike-perl 0.27-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 192 kB
  • ctags: 14
  • sloc: perl: 492; makefile: 2
file content (84 lines) | stat: -rw-r--r-- 2,113 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
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
use strict;
use warnings;
use Test::More;
use Test::Fatal;

BEGIN {
  package Local::TypeLibrary;
  use MooX::Types::MooseLike;
  use Exporter 5.57 'import';
  MooX::Types::MooseLike::register_types([{
        name    => 'BaseType',
        test    => sub { defined $_[0] && !ref $_[0] },
        message => sub { 'not a simple string' },
      }], __PACKAGE__);
  MooX::Types::MooseLike::register_types([{
        name    => 'SubType',
        subtype_of => 'BaseType',
        from    => __PACKAGE__,
        test    => sub { length $_[0] },
        message => sub { 'not a string length > 0' },
      }], __PACKAGE__);
  MooX::Types::MooseLike::register_types([{
        name    => 'SubSubType',
        subtype_of => SubType(),
        test    => sub { $_[0] },
        message => sub { 'not a true value' },
      }], __PACKAGE__);
  $INC{'Local/TypeLibrary.pm'} = __FILE__;
}

{
  package MooX::Types::MooseLike::Test;
  use strict;
  use warnings FATAL => 'all';
  use Moo;
  use Local::TypeLibrary qw/BaseType SubType SubSubType/;

  has string => (
    is  => 'ro',
    isa => BaseType,
    );
  has string_w_length => (
    is  => 'ro',
    isa => SubType,
    );
  has true_string => (
    is  => 'ro',
    isa => SubSubType,
    );
}

ok(MooX::Types::MooseLike::Test->new(string => ''), 'empty string is a string');
ok(MooX::Types::MooseLike::Test->new(string_w_length => '0'), '0 is a string with length');
ok(MooX::Types::MooseLike::Test->new(true_string => '1'), '1 is true string');
like(
  exception {
    MooX::Types::MooseLike::Test->new(string => undef);
  },
  qr/not a simple string/,
  'undef is not a simple string',
  );
like(
  exception {
    MooX::Types::MooseLike::Test->new(string_w_length => '');
  },
  qr/not a string length > 0/,
  'empty string has no length',
  );
like(
  exception {
    MooX::Types::MooseLike::Test->new(string_w_length => {});
  },
  qr/not a string length > 0/,
  'hashref is not a simple string',
);
like(
  exception {
    MooX::Types::MooseLike::Test->new(true_string => '0');
  },
  qr/not a true value/,
  '0 is not a true value',
  );

done_testing;