File: 06_regexp_map.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 (71 lines) | stat: -rw-r--r-- 1,933 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
use Test::More tests => 5;
use strict;

$^W = 1;

use Data::FormValidator;

my $input_profile = {
		       required => [ qw( email_1  email_ok) ],
		       optional => [qw/ extra first_name last_name /],
		       constraint_regexp_map => {
				      '/^email/'  => "email",
				   },
			   field_filter_regexp_map => {
			   		  '/_name$/'	  => 'ucfirst',
			   }
			};

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

my $input_hashref = {
   email_1  => 'invalidemail',
   email_ok => 'mark@stosberg.com', 
   extra    => 'unrelated field',
   first_name => 'mark',
   last_name  => 'stosberg',
};

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

eval{
  ($valids, $missings, $invalids, $unknowns) = $validator->validate($input_hashref, 'default');
};
ok (not $@);
ok ($invalids->[0] eq 'email_1');

ok ($valids->{'email_ok'}) ;
ok ($valids->{'extra'});
ok ($valids->{'first_name'} eq 'Mark' and $valids->{'last_name'} eq 'Stosberg');

# Tests below added 04/24/03 to test adding constraints to fields with existing constraints 
eval {
	my ($valids,$missings,$invalids) = Data::FormValidator->validate(
		# input
		{
			with_no_constraint	=> 'f1 text',	
			with_one_constraint	=> 'f2 text',	
			with_mult_constraint	=> 'f2 text',	
		},
		# profile
		{
			required=>[qw/with_no_constraint with_one_constraint with_mult_constraint/],
			constraints=> {
				with_one_constraint => 'email',
				with_mult_constraint => ['email','american_phone'],
			},
			constraint_regexp_map => {
			   '/^with/'	=> 'state',
			},
			msgs=>{},
		}
	)
};

TODO: {
	local $TODO = 'rewrite when message system is rebuilt';
	#ok (not $@) ir diag $@;
	#like($invalids->{with_no_constraint}, qr/Invalid/ ,   '...with no existing constraints');
	#ok(scalar @{ $invalids->{with_one_constraint} } eq 2, '...with one existing constraint');
	#ok(scalar @{ $invalids->{with_mult_constraint} } eq 3,'...with two existing constraints');
};