File: constraints_invalid_once_only.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 (44 lines) | stat: -rw-r--r-- 931 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
#!/usr/bin/perl

# this test checks that a failing constraint is only marked as invalid once

use Test::More tests => 1;
use Data::FormValidator;
use strict;

sub check_passwords {
	my ( $dfv, $val ) = @_;
	my $passwords = $dfv->{__INPUT_DATA}->{password};
	if( ref( $passwords ) eq 'ARRAY' ) {
		if( $$passwords[0] eq $$passwords[1] ) {
			return 1;
		}
		return 0;
	}
	return 1;
}

my %data = (
  'password'         => ['123456','123457'],
);

my %profile = (
    optional => [qw/password/],
    constraint_methods => {
            password => \&check_passwords,
    },
);

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

my $invalid = $results->{invalid};
my $duplicated = {};
my $has_duplicates;
foreach ( @{$invalid->{password}} ) {
    if( exists $duplicated->{$_} ) {
        $has_duplicates = 1;
        last;
    }
    $duplicated->{$_} = 1;
}
ok(!$has_duplicates, 'constraint marked as invalid only once');