File: multiple_constraints.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 (90 lines) | stat: -rw-r--r-- 2,109 bytes parent folder | download | duplicates (7)
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
89
90

use Data::FormValidator;
use Test::More tests => 8;
use strict;
use lib ('.','../t');

my $input_profile = {
	required => ['my_zipcode_field'],
	constraints => {
		my_zipcode_field => [
			'zip',
			{ 
				constraint =>  '/^406/', 
				name 	   =>  'starts_with_406',
			}
		],
	},
};

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

my $input_hashref = {
	my_zipcode_field => '402015', # born to lose
};

my ($valids, $missings, $invalids, $unknowns);

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

ok(!$@, 'survived eval');

ok((grep { (ref $_) eq 'ARRAY' } @$invalids));


# Test that the array ref in the invalids array contains three elements,
my @zip_failures;
for (@$invalids) {
	if (ref $_ eq 'ARRAY') {
		if (scalar @$_ == 3) {	 
			@zip_failures = @$_;	
            # This is cheesy, and could be further refactored.
            ok(1);
			last;
		}
	}
}

# Test that the first element of the array is 'my_zipcode_field'
my $t = shift @zip_failures;

ok($t eq 'my_zipcode_field');

# Test that the two elements are 'zip' and 'starts_with_406'
ok(eq_set(\@zip_failures, [qw/zip starts_with_406/]));



# The next three tests are to confirm that an input field is deleted
# from the valids under the following conditions

# 1. the input field has multiple constraints
# 2. one or more constraint fails

my %data = (
  multiple => 'to fail',
  #multiple => [qw{this multi-value input will fail on the constraint below}],
  single   => 'to pass',
);

my %profile = (
  required => [qw/
    multiple
    single 
    /],
  constraints => {
      multiple => [
        { name => 'constraint_1', constraint => qr/\w/ }, # pass
        { name => 'constraint_2', constraint => qr/\d/ }, # force fail 
      ],
  },
);


my $results = Data::FormValidator->check(\%data, \%profile);

ok(!$results->valid('multiple'), "expect 'multiple' not to appear in valid");
is_deeply($results->invalid('multiple'), ['constraint_2'], "list of failed constraints for 'multiple'");
is($results->valid('single'), 'to pass', "single is valid");