line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/env perl |
2
|
|
|
|
|
|
|
|
3
|
35
|
|
|
35
|
|
41303
|
use strict; |
|
35
|
|
|
|
|
70
|
|
|
35
|
|
|
|
|
1259
|
|
4
|
35
|
|
|
35
|
|
183
|
use warnings; |
|
35
|
|
|
|
|
59
|
|
|
35
|
|
|
|
|
2059
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package Test::Validator::Declarative; |
7
|
|
|
|
|
|
|
{ |
8
|
|
|
|
|
|
|
$Test::Validator::Declarative::VERSION = '1.20130722.2105'; |
9
|
|
|
|
|
|
|
} |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# ABSTRACT: Tests for declarative parameters validation |
12
|
|
|
|
|
|
|
|
13
|
35
|
|
|
35
|
|
227
|
use Exporter; |
|
35
|
|
|
|
|
66
|
|
|
35
|
|
|
|
|
2214
|
|
14
|
35
|
|
|
35
|
|
68602
|
use Test::More; |
|
35
|
|
|
|
|
1144455
|
|
|
35
|
|
|
|
|
465
|
|
15
|
35
|
|
|
35
|
|
64219
|
use Test::Exception; |
|
35
|
|
|
|
|
179196
|
|
|
35
|
|
|
|
|
184
|
|
16
|
35
|
|
|
35
|
|
73035
|
use Data::Dumper; |
|
35
|
|
|
|
|
491457
|
|
|
35
|
|
|
|
|
3303
|
|
17
|
35
|
|
|
35
|
|
33489
|
use Validator::Declarative; |
|
35
|
|
|
|
|
150
|
|
|
35
|
|
|
|
|
44314
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our @ISA = qw/ Exporter /; |
20
|
|
|
|
|
|
|
our @EXPORT_OK = qw/ check_type_validation check_converter_validation /; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub check_type_validation { |
23
|
33
|
|
|
33
|
1
|
339352
|
my %param = @_; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# for lives_ok + is_deeply + throws_ok + message for each error |
26
|
33
|
|
|
|
|
97
|
plan tests => 4 + 1 + scalar( @{ $param{bad} } ); |
|
33
|
|
|
|
|
318
|
|
27
|
|
|
|
|
|
|
|
28
|
33
|
|
|
|
|
25853
|
my ( $type, $aliased_type, $values, @result, $type_name, $stringified_type ); |
29
|
|
|
|
|
|
|
|
30
|
33
|
|
|
|
|
148
|
$type = $param{type}; |
31
|
33
|
|
100
|
|
|
1091
|
$aliased_type = $param{aliased_to} || ''; |
32
|
|
|
|
|
|
|
|
33
|
33
|
50
|
|
|
|
462
|
($type_name) = # there should be exactly one k/v pair |
|
|
100
|
|
|
|
|
|
34
|
|
|
|
|
|
|
ref($type) eq 'HASH' ? keys(%$type) |
35
|
|
|
|
|
|
|
: ref($type) eq 'ARRAY' ? $type->[0] |
36
|
|
|
|
|
|
|
: $type; |
37
|
|
|
|
|
|
|
|
38
|
33
|
|
|
|
|
176
|
$stringified_type = _struct_to_str($type); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# |
41
|
|
|
|
|
|
|
# check type validation pass |
42
|
|
|
|
|
|
|
# |
43
|
33
|
|
|
|
|
3992
|
$values = $param{good}; |
44
|
|
|
|
|
|
|
lives_ok { |
45
|
33
|
|
|
33
|
|
1643
|
@result = Validator::Declarative::validate( [undef] => [ "param_${type_name}_0" => [ 'optional', $type ] ] ); |
46
|
|
|
|
|
|
|
} |
47
|
33
|
|
|
|
|
478
|
"type 'optional,$stringified_type' lives on undef"; |
48
|
33
|
|
|
|
|
25955
|
is_deeply( \@result, [undef], "type 'optional,$stringified_type' returns expected result" ); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
lives_ok { |
51
|
140057
|
|
|
|
|
523675
|
@result = Validator::Declarative::validate( |
52
|
33
|
|
|
33
|
|
12283
|
$values => [ map { sprintf( "param_${type_name}_%02d", $_ ) => $type, } 1 .. scalar @$values ] ); |
53
|
|
|
|
|
|
|
} |
54
|
33
|
|
|
|
|
25558
|
"type $stringified_type lives on correct parameters"; |
55
|
33
|
|
|
|
|
73113
|
is_deeply( \@result, $values, "type $stringified_type returns expected result" ); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# |
58
|
|
|
|
|
|
|
# check type validation fail |
59
|
|
|
|
|
|
|
# |
60
|
33
|
|
|
|
|
1155187
|
$values = $param{bad}; |
61
|
|
|
|
|
|
|
throws_ok { |
62
|
3174
|
|
|
|
|
9544
|
Validator::Declarative::validate( |
63
|
33
|
|
|
33
|
|
10455
|
$values => [ map { sprintf( "param_${type_name}_%02d", $_ ) => $type, } 1 .. scalar @$values ] ); |
64
|
|
|
|
|
|
|
} |
65
|
33
|
|
|
|
|
479
|
'Error::Simple', "type $stringified_type throws on incorrect parameters"; |
66
|
|
|
|
|
|
|
|
67
|
33
|
|
|
|
|
26116
|
my $error_text = "$@"; |
68
|
33
|
|
|
|
|
2104
|
for ( 1 .. scalar @$values ) { |
69
|
3174
|
|
|
|
|
1593927
|
my $param = sprintf( "param_${type_name}_%02d", $_ ); |
70
|
3174
|
|
66
|
|
|
23696
|
my $regexp = sprintf( "%s: .* does not satisfy %s", $param, uc( $aliased_type || $type_name ) ); |
71
|
3174
|
|
|
|
|
80169
|
like $error_text, qr/^$regexp/m, "message about $param"; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub check_converter_validation { |
77
|
2
|
|
|
2
|
1
|
72
|
my %param = @_; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# for lives_ok + is_deeply + throws_ok + message for each error |
80
|
2
|
|
|
|
|
5
|
plan tests => 2 * values %{ $param{result} }; |
|
2
|
|
|
|
|
22
|
|
81
|
|
|
|
|
|
|
|
82
|
2
|
|
|
|
|
1297
|
my ( $type, $aliased_type, @result, $type_name, $stringified_type ); |
83
|
|
|
|
|
|
|
|
84
|
2
|
|
|
|
|
7
|
$type = $param{type}; |
85
|
2
|
|
50
|
|
|
18
|
$aliased_type = $param{aliased_to} || ''; |
86
|
|
|
|
|
|
|
|
87
|
2
|
50
|
|
|
|
16
|
($type_name) = # there should be exactly one k/v pair |
|
|
50
|
|
|
|
|
|
88
|
|
|
|
|
|
|
ref($type) eq 'HASH' ? keys(%$type) |
89
|
|
|
|
|
|
|
: ref($type) eq 'ARRAY' ? $type->[0] |
90
|
|
|
|
|
|
|
: $type; |
91
|
|
|
|
|
|
|
|
92
|
2
|
|
|
|
|
10
|
$stringified_type = _struct_to_str($type); |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# |
95
|
|
|
|
|
|
|
# check type validation pass |
96
|
|
|
|
|
|
|
# |
97
|
2
|
|
|
|
|
203
|
while ( my ( $result, $values ) = each %{ $param{result} } ) { |
|
6
|
|
|
|
|
3513
|
|
98
|
4
|
50
|
33
|
|
|
51
|
if ( $ENV{DEBUG} || $ENV{TEST_DEBUG} || $ENV{DEBUG_TEST} ) { |
|
|
|
33
|
|
|
|
|
99
|
0
|
|
|
|
|
0
|
diag( 'Processing values: ' . _struct_to_str($values) ); |
100
|
0
|
|
|
|
|
0
|
diag( 'Expected result(s): ' . _struct_to_str($result) ); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
4
|
50
|
|
|
|
16
|
$values = [$values] if ref($values) ne 'ARRAY'; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
lives_ok { |
106
|
46
|
|
|
|
|
155
|
@result = |
107
|
|
|
|
|
|
|
Validator::Declarative::validate( |
108
|
4
|
|
|
4
|
|
122
|
$values => [ map { sprintf( "param_${type_name}_%02d", $_ ) => $type } 1 .. scalar @$values ] ); |
109
|
|
|
|
|
|
|
} |
110
|
4
|
|
|
|
|
60
|
"converter $stringified_type lives on correct parameters for $result"; |
111
|
|
|
|
|
|
|
|
112
|
4
|
|
|
|
|
2371
|
is_deeply( |
113
|
|
|
|
|
|
|
\@result, [ ($result) x scalar(@$values) ], |
114
|
|
|
|
|
|
|
"converter $stringified_type returns as expected for $result" |
115
|
|
|
|
|
|
|
); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub _struct_to_str { |
120
|
35
|
|
|
35
|
|
288
|
my ( $struct, $maxdepth, $use_deparse ) = @_; |
121
|
|
|
|
|
|
|
|
122
|
35
|
|
50
|
|
|
263
|
$maxdepth ||= 3; |
123
|
35
|
|
50
|
|
|
395
|
$use_deparse ||= 0; |
124
|
|
|
|
|
|
|
|
125
|
35
|
|
|
|
|
107
|
local $Data::Dumper::Deparse = $use_deparse; |
126
|
35
|
|
|
|
|
282
|
local $Data::Dumper::Indent = 0; |
127
|
35
|
|
|
|
|
96
|
local $Data::Dumper::Maxdepth = $maxdepth; |
128
|
35
|
|
|
|
|
260
|
local $Data::Dumper::Quotekeys = 1; |
129
|
35
|
|
|
|
|
164
|
local $Data::Dumper::Sortkeys = 1; |
130
|
35
|
|
|
|
|
117
|
local $Data::Dumper::Terse = 1; |
131
|
35
|
|
|
|
|
83
|
local $Data::Dumper::Useqq = 0; |
132
|
|
|
|
|
|
|
|
133
|
35
|
|
|
|
|
432
|
return Data::Dumper::Dumper($struct); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
1; # End of Test::Validator::Declarative |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
__END__ |