line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::TypeConstraints; |
2
|
5
|
|
|
5
|
|
146304
|
use strict; |
|
5
|
|
|
|
|
18
|
|
|
5
|
|
|
|
|
416
|
|
3
|
5
|
|
|
5
|
|
26
|
use warnings; |
|
5
|
|
|
|
|
8
|
|
|
5
|
|
|
|
|
135
|
|
4
|
5
|
|
|
5
|
|
131
|
use 5.008001; |
|
5
|
|
|
|
|
21
|
|
|
5
|
|
|
|
|
260
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.07'; |
6
|
5
|
|
|
5
|
|
25
|
use Exporter 'import'; |
|
5
|
|
|
|
|
8
|
|
|
5
|
|
|
|
|
161
|
|
7
|
5
|
|
|
5
|
|
3298
|
use Test::More; |
|
5
|
|
|
|
|
60883
|
|
|
5
|
|
|
|
|
51
|
|
8
|
5
|
|
|
5
|
|
2062
|
use Test::Builder; |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
95
|
|
9
|
5
|
|
|
5
|
|
4017
|
use Mouse::Util::TypeConstraints (); |
|
5
|
|
|
|
|
99828
|
|
|
5
|
|
|
|
|
121
|
|
10
|
5
|
|
|
5
|
|
56
|
use Scalar::Util (); |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
89
|
|
11
|
5
|
|
|
5
|
|
5993
|
use Data::Dumper; |
|
5
|
|
|
|
|
62396
|
|
|
5
|
|
|
|
|
4601
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our @EXPORT = qw/ type_isa type_does type_isnt type_doesnt /; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub type_isa { |
16
|
7
|
|
|
7
|
1
|
12270
|
my ($got, $type, @rest) = @_; |
17
|
|
|
|
|
|
|
|
18
|
7
|
|
|
|
|
35
|
my $tc = _make_type_constraint( |
19
|
|
|
|
|
|
|
$type, |
20
|
|
|
|
|
|
|
\&Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
7
|
|
|
|
|
549
|
local $Test::Builder::Level = $Test::Builder::Level + 1; |
24
|
7
|
|
|
|
|
27
|
return _type_constraint_ok( $got, $tc, @rest ); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub type_does { |
28
|
2
|
|
|
2
|
1
|
1278
|
my ($got, $type, @rest) = @_; |
29
|
|
|
|
|
|
|
|
30
|
2
|
|
|
|
|
10
|
my $tc = _make_type_constraint( |
31
|
|
|
|
|
|
|
$type, |
32
|
|
|
|
|
|
|
\&Mouse::Util::TypeConstraints::find_or_create_does_type_constraint |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
2
|
|
|
|
|
109
|
local $Test::Builder::Level = $Test::Builder::Level + 1; |
36
|
2
|
|
|
|
|
7
|
return _type_constraint_ok( $got, $tc, @rest ); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub type_isnt { |
40
|
1
|
|
|
1
|
1
|
7
|
my ($got, $type, @rest) = @_; |
41
|
|
|
|
|
|
|
|
42
|
1
|
|
|
|
|
4
|
my $tc = _make_type_constraint( |
43
|
|
|
|
|
|
|
$type, |
44
|
|
|
|
|
|
|
\&Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint |
45
|
|
|
|
|
|
|
); |
46
|
|
|
|
|
|
|
|
47
|
1
|
|
|
|
|
14
|
local $Test::Builder::Level = $Test::Builder::Level + 1; |
48
|
1
|
|
|
|
|
5
|
return _type_constraint_not_ok( $got, $tc, @rest ); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub type_doesnt { |
52
|
1
|
|
|
1
|
1
|
5
|
my ($got, $type, @rest) = @_; |
53
|
|
|
|
|
|
|
|
54
|
1
|
|
|
|
|
5
|
my $tc = _make_type_constraint( |
55
|
|
|
|
|
|
|
$type, |
56
|
|
|
|
|
|
|
\&Mouse::Util::TypeConstraints::find_or_create_does_type_constraint |
57
|
|
|
|
|
|
|
); |
58
|
|
|
|
|
|
|
|
59
|
1
|
|
|
|
|
30
|
local $Test::Builder::Level = $Test::Builder::Level + 1; |
60
|
1
|
|
|
|
|
4
|
return _type_constraint_not_ok( $got, $tc, @rest ); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub _make_type_constraint { |
64
|
11
|
|
|
11
|
|
24
|
my($type, $make_constraint) = @_; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# duck typing for (Mouse|Moose)::Meta::TypeConstraint |
67
|
11
|
100
|
66
|
|
|
71
|
if ( Scalar::Util::blessed($type) && $type->can("check") ) { |
68
|
1
|
|
|
|
|
4
|
return $type; |
69
|
|
|
|
|
|
|
} else { |
70
|
10
|
|
|
|
|
39
|
return $make_constraint->($type); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub _type_constraint_ok { |
75
|
9
|
|
|
9
|
|
28
|
my ($got, $tc, $test_name, %options) = @_; |
76
|
|
|
|
|
|
|
|
77
|
9
|
|
|
|
|
16
|
local $Test::Builder::Level = $Test::Builder::Level + 1; |
78
|
9
|
100
|
66
|
|
|
33
|
my $ret = ok(check_type($tc, $got, %options), $test_name || ( $tc->name . " types ok" ) ) |
79
|
|
|
|
|
|
|
or diag(sprintf('type: "%s" expected. but got %s', $tc->name, Dumper($got))); |
80
|
|
|
|
|
|
|
|
81
|
9
|
|
|
|
|
3955
|
return $ret; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub _type_constraint_not_ok { |
85
|
2
|
|
|
2
|
|
5
|
my ($got, $tc, $test_name, %options) = @_; |
86
|
|
|
|
|
|
|
|
87
|
2
|
|
|
|
|
4
|
local $Test::Builder::Level = $Test::Builder::Level + 1; |
88
|
2
|
50
|
33
|
|
|
6
|
my $ret = ok(!check_type($tc, $got, %options), $test_name || ( $tc->name . " types ok" ) ) |
89
|
|
|
|
|
|
|
or diag(sprintf('%s is not supposed to be of type "%s"', $tc->name, Dumper($got))); |
90
|
|
|
|
|
|
|
|
91
|
2
|
|
|
|
|
637
|
return $ret; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub check_type { |
95
|
11
|
|
|
11
|
0
|
24
|
my ($tc, $value, %options) = @_; |
96
|
|
|
|
|
|
|
|
97
|
11
|
100
|
|
|
|
115
|
return 1 if $tc->check($value); |
98
|
5
|
100
|
|
|
|
78
|
if ( my $coerce_check = $options{coerce} ) { |
99
|
2
|
|
|
|
|
14
|
my $new_val = $tc->coerce($value); |
100
|
2
|
100
|
|
|
|
179
|
$coerce_check->($new_val) if ref $coerce_check; |
101
|
2
|
50
|
|
|
|
498
|
return 1 if $tc->check($new_val); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
3
|
|
|
|
|
28
|
return 0; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
1; |
108
|
|
|
|
|
|
|
__END__ |