File: parameterized_with_coderefs.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 (82 lines) | stat: -rw-r--r-- 2,310 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
{
  package MooX::Types::MooseLike::Test;
  use strict;
  use warnings FATAL => 'all';
  use Moo;
  use MooX::Types::MooseLike::Base qw/ AnyOf AllOf Object Int ArrayRef HashRef InstanceOf HasMethods /;

  has any_of => (
    is  => 'ro',
    isa => AnyOf[Int, HashRef[Int], Object],
    );
  has all_of => (
    is  => 'ro',
    isa => AllOf[InstanceOf['IO::Handle'], HasMethods['print']],
    );
}
package main;
use strict;
use warnings FATAL => 'all';
use Test::More;
use Test::Fatal;
use IO::Handle;

# AnyOf
ok(MooX::Types::MooseLike::Test->new(any_of => IO::Handle->new ), 'value is AnyOf[Int, ArrayRef[Int], HashRef[Int], Object]');
ok(MooX::Types::MooseLike::Test->new(any_of =>  108  ), 'Int is AnyOf[Int, ArrayRef[Int], HashRef[Int], Object]');
ok(MooX::Types::MooseLike::Test->new(any_of => {auspicious_number => 108} ), 'HashRef[Int] is AnyOf[Int, ArrayRef[Int], HashRef[Int], Object]');
ok(MooX::Types::MooseLike::Test->new(any_of => {} ), 'HashRef is AnyOf[Int, ArrayRef[Int], HashRef[Int], Object]');
my $false_value;
like(
  exception {
    MooX::Types::MooseLike::Test->new(any_of => $false_value);
  },
  qr/is not any of/,
  'undef is not an any of the types given"'
  );
$false_value = { nada => undef };
like(
  exception {
    MooX::Types::MooseLike::Test->new(any_of => $false_value);
  },
  qr/is not any of/,
  'A HashRef with an undefined value is not an any of the types given'
  );
$false_value = [];
like(
  exception {
    MooX::Types::MooseLike::Test->new(any_of => $false_value);
  },
  qr/is not any of/,
  'ArrayRef is not an any of the types given'
  );
$false_value = 'peace_treaty';
like(
  exception {
    MooX::Types::MooseLike::Test->new(any_of => $false_value);
  },
  qr/is not any of/,
  'a string is not any of the types given'
  );

# AllOf
ok(MooX::Types::MooseLike::Test->new(all_of => IO::Handle->new ),
  "value is AllOf[InstanceOf('IO::Handle'), HasMethods['print']]");
$false_value = undef;
like(
  exception {
    MooX::Types::MooseLike::Test->new(all_of => $false_value);
  },
  qr/No instance given/,
  'undef is not an instance of IO::Handle"'
  );
$false_value = 'peace_treaty';
like(
  exception {
    MooX::Types::MooseLike::Test->new(all_of => $false_value);
  },
  qr/is not blessed/,
  'a string is not an instance of IO::Handle'
  );

done_testing;