File: 10-validation.t

package info (click to toggle)
libvalidation-class-perl 7.900057-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,572 kB
  • ctags: 355
  • sloc: perl: 21,493; makefile: 2
file content (58 lines) | stat: -rw-r--r-- 2,208 bytes parent folder | download | duplicates (5)
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
use Test::More tests => 27;

package MyVal;

use Validation::Class;

package main;

my $v = MyVal->new(
    fields => {foobar => {error => 'foobar error'}},
    params => {foobar => 'abc123456'}
);

ok $v, 'class initialized';
ok defined $v->fields->{foobar}, 'foobar field exists';
ok defined $v->params->{foobar}, 'foobar param exists';

# check min_length directive
$v->fields->{foobar}->{min_length} = 10;
ok !$v->validate('foobar'), 'error found as expected';
ok !$v->validate, 'alternate use of validation found error also';
ok $v->error_count == 1, 'error count is correct';
ok $v->errors_to_string eq 'foobar error', 'error message specified captured';

$v->fields->{foobar}->{min_length} = 5;
ok $v->validate('foobar'), 'foobar rule validates';
ok $v->validate, 'alternate use of validation validates';
ok $v->error_count == 0, 'error count is zero';
ok $v->errors_to_string eq '', 'no error messages found';

# check max_length directive
$v->fields->{foobar}->{max_length} = 8;
ok !$v->validate('foobar'), 'error found as expected';
ok !$v->validate, 'alternate use of validation found error also';
ok $v->error_count == 1, 'error count is correct';
ok $v->errors_to_string eq 'foobar error', 'error message specified captured';

$v->fields->{foobar}->{max_length} = 9;
ok $v->validate('foobar'), 'foobar rule validates';
ok $v->validate, 'alternate use of validation validates';
ok $v->error_count == 0, 'error count is zero';
ok $v->errors_to_string eq '', 'no error messages found';

# check pattern directive
$v->fields->{foobar}->{pattern} = 'XXX######';
ok $v->validate('foobar'), 'foobar rule validates';
ok $v->validate, 'alternate use of validation validates';
ok $v->error_count == 0, 'error count is zero';
ok $v->errors_to_string eq '', 'no error messages found';

# check pattern (telephone example) directive
delete $v->fields->{foobar}->{error};
$v->fields->{foobar}->{pattern} = '(###) ###-####';
$v->params->{foobar} = '111-1111';
ok !$v->validate('foobar'), 'foobar rule doesnt validate';
ok !$v->validate, 'alternate use of validation doesnt validate';
ok $v->error_count == 1, 'error count is correct';
ok $v->errors_to_string =~ 'not formatted', 'pattern error message found';