]> Dogcows Code - chaz/p5-CGI-Ex/blob - t/1_validate_12_change.t
e577b0dad1912f7846704f5931a4bcaa84f4c798
[chaz/p5-CGI-Ex] / t / 1_validate_12_change.t
1 # -*- Mode: Perl; -*-
2
3 use strict;
4
5 $^W = 1;
6
7 ### determine number of tests
8 seek(DATA,0,0);
9 my $prog = join "", <DATA>;
10 my @tests = ($prog =~ /&print_ok\(/g);
11 my $tests = @tests;
12 print "1..$tests\n";
13
14 require CGI::Ex::Validate;
15
16 my ($N, $v, $e, $ok) = (0);
17
18 sub validate {
19 return scalar &CGI::Ex::Validate::validate(@_);
20 }
21 sub print_ok {
22 my $ok = shift;
23 $N ++;
24 warn "Test failed at line ".(caller)[2]."\n" if ! $ok;
25 print "" . ($ok ? "" : "not ") . "ok $N\n";
26 }
27 &print_ok(1);
28
29 ###----------------------------------------------------------------###
30
31 $v = [
32 {
33 foo => {
34 max_len => 10,
35 replace => 's/[^\d]//g',
36 },
37 },
38 ];
39
40 $e = &validate({
41 foo => '123-456-7890',
42 }, $v);
43 &print_ok(! $e);
44
45
46 my $form = {
47 key1 => 'Bu-nch @of characte#rs^',
48 key2 => '123 456 7890',
49 };
50
51
52 $v = {
53 key1 => {
54 replace => 's/[^\s\w]//g',
55 },
56 };
57
58 $e = &validate($form, $v);
59 &print_ok(! $e && $form->{key1} eq 'Bunch of characters');
60
61 $v = {
62 key2 => {
63 replace => 's/(\d{3})\D*(\d{3})\D*(\d{4})/($1) $2-$3/g',
64 },
65 };
66
67 $e = &validate($form, $v);
68 &print_ok(! $e && $form->{key2} eq '(123) 456-7890');
69
70
71 $v = {
72 key2 => {
73 replace => 's/.+//g',
74 required => 1,
75 },
76 };
77
78 $e = &validate($form, $v);
79 &print_ok($e && $form->{key2} eq '');
80
81 __DATA__
This page took 0.033941 seconds and 3 git commands to generate.