File: 22-creditcard.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 (78 lines) | stat: -rw-r--r-- 1,872 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use Test::More;

package main;

use utf8;
use Validation::Class::Simple;

my $r = Validation::Class::Simple->new(fields => {visa_number => {creditcard => 1}});

sub should_fail {
    my ($name, @numbers) = @_;
    for (@numbers) {
        $r->params->{$name} = $_;
        ok !$r->validate(), "$_ is an invalid $name param";
    }
}

sub should_pass {
    my ($name, @numbers) = @_;
    for (@numbers) {
        $r->params->{$name} = $_;
        ok $r->validate(), "$_ is a valid $name param";
    }
}

# failures

diag 'validating bad visa numbers';
should_fail visa_number => qw(
    1111111111111111
    0000000000000000
    1234567887654321
);

diag 'now validating visa numbers against mastercard validator';
$r->fields->get('visa_number')->creditcard('mastercard');
should_fail visa_number => qw(
    1111111111111111
    0000000000000000
    1234567887654321
);

# successes

diag 'now validating bad visa numbers which are properly formatted';
$r->fields->get('visa_number')->creditcard(1);
should_pass visa_number => qw(
    4222222222222
    4111111111111111
    4012888888881881
);

diag 'now passing the "visa" argument to the creditcard directive';
$r->fields->get('visa_number')->creditcard('visa');
should_pass visa_number => qw(
    4222222222222
    4111111111111111
    4012888888881881
);

diag 'now using mc numbers and passing the "mastercard" argument to the creditcard directive';
$r->fields->get('visa_number')->creditcard('mastercard');
should_pass visa_number => qw(
    5105105105105100
    5555555555554444
);

diag 'now using mc and visa numbers and passing the ["visa", "mastercard"] argument to the creditcard directive';
$r->fields->get('visa_number')->creditcard(['visa', 'mastercard']);
should_pass visa_number => qw(
    4222222222222
    4111111111111111
    4012888888881881
    5105105105105100
    5555555555554444
);

done_testing;