File: 17_multi_valued_keys.t

package info (click to toggle)
libdata-formvalidator-perl 4.66-1%2Bsqueeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 588 kB
  • ctags: 127
  • sloc: perl: 2,756; makefile: 2
file content (88 lines) | stat: -rw-r--r-- 2,266 bytes parent folder | download | duplicates (6)
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
85
86
87
88
# This script tests validating keys with multiple data
use strict;
use lib ('.','../t');

$^W = 1;

use Test::More tests => 8;

my $input_hash = { 
	single_value => ' Just One ',
	multi_values => [' One ', ' Big ', ' Happy ', ' Family '],
	re_multi_test => [qw/at the circus/],
	constraint_multi_test => [qw/12345 22234 oops/],
};


use Data::FormValidator;

my $input_profile = {
	required => [qw/single_value multi_values re_multi_test constraint_multi_test/],
	filters => [qw/trim/],
	field_filters => {
		single_value => 'lc',
		multi_values => 'uc',
	},
	field_filter_regexp_map => {
		'/_multi_test$/'      => 'ucfirst',
	},
	constraints => {
		constraint_multi_test => 'zip',
	},
};

my $validator = new Data::FormValidator({default => $input_profile});

my ($valids, $missings, $invalids, $unknowns);
eval{
  ($valids, $missings, $invalids, $unknowns) = $validator->validate($input_hash, 'default');
};

is($valids->{single_value},'just one',
 'inconditional filters still work with single values'
);

is(lc $valids->{multi_values}->[0],lc 'one',
 'inconditional filters work with multi values'
);

is($valids->{multi_values}->[0],'ONE',
 'field filters work with multiple values'
);

is($valids->{re_multi_test}->[0] ,'At',
 'Test the filters applied to multiple values by RE work'
);

ok(!$valids->{constraint_multi_test},
'If any of the values fail the constraint, the field becomes invalid'
);

my $r;
eval { $r = Data::FormValidator->check({ undef_multi => [undef] }, { required => 'undef_multi' }) };
diag "error: $@" if $@;
ok($r->missing('undef_multi'), 'multi-valued field containing only undef should be missing'); 

my $v;
eval { $v = $r->valid('undef_multi'); };
diag "error: $@" if $@;
ok(!$v, 'multiple valued fields containing only undefined values should not be valid');


###

eval { $r = Data::FormValidator->check({ 
            cc_type => ['Check'],
        },
        {
            required => 'cc_type',
            dependencies => {
                cc_type => {
                    Check   => [qw( cc_num )],
                    Visa => [qw( cc_num cc_exp cc_name )],
                },
            },
        }) };
diag "error: $@" if $@;

ok($r->missing('cc_num'), 'a single valued array should still trigger the dependency check');