File: constraint_method.t

package info (click to toggle)
libdata-formvalidator-perl 4.70-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 676 kB
  • sloc: perl: 2,839; makefile: 2
file content (49 lines) | stat: -rw-r--r-- 1,604 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
#!/usr/bin/perl

use Test::More qw/no_plan/;

use Data::FormValidator;

my $result = Data::FormValidator->check({ field => 'value' }, {
        required => 'field',
        constraints => {
            field => {
                constraint_method => sub { 
                    my $dfv = shift;
                    my $name = $dfv->get_current_constraint_name;
                    is($name, 'test_name', "get_current_constraint_name works");
                },
                name => 'test_name',
            }
        },
    });

{
    my $result = Data::FormValidator->check({
            to_pass      => 'value', 
            to_fail      => 'value', 
            map_to_pass  => 'value',  
            map_to_fail  => 'value',
        }, {
            required => [qw/
                 to_pass 
                 to_fail 
                 map_to_pass 
                 map_to_fail
                 /],
            constraint_methods => {
                to_pass =>  qr/value/,
                to_fail =>  qr/wrong/,
            },
            constraint_method_regexp_map => {
                qr/map_to_p.*/   => qr/value/,
                qr/map_to_f.*/   => qr/fail/,

            },
        });

    ok ( $result->invalid('to_fail'), "using qr with constraint_method fails as expected");
    ok ( $result->valid('to_pass'), "using qr with constraint_method succeeds as expected");
    ok ( $result->invalid('map_to_fail'), "using qr with constraint_method_regexp_map fails as expected");
    ok ( $result->valid('map_to_pass'), "using qr with constraint_method_regexp_map succeeds as expected");
}