File: 05-filters-usage.t

package info (click to toggle)
libvalidation-class-perl 7.900056-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,564 kB
  • ctags: 355
  • sloc: perl: 21,474; makefile: 2
file content (131 lines) | stat: -rw-r--r-- 3,538 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use FindBin;
use Test::More;

use utf8;
use strict;
use warnings;

{

    package TestClass::FiltersUsage;

    use Validation::Class;

    filter 'flatten' => sub {
        $_[0] =~ s/[\t\r\n]+/ /g;
        return $_[0];
    };

    field 'biography' => {
        filters => ['trim', 'strip', 'flatten'],
        alias   => ['bio']
    };

    1;

    package main;

    my $biography = <<'TEXT';
    1. In arcu mi, sagittis vel pretium sit amet, tempor ac risus.
    2. Integer facilisis, ante ac tincidunt euismod, metus tortor.
    3. Suscipit erat, nec porta arcu urna eu nisl.
TEXT

    my $self;
    my $class = "TestClass::FiltersUsage";

    $self  = $class->new(biography => $biography);

    ok $class eq ref $self, "$class instantiated";
    is_deeply $self->fields->biography->filters, ['trim', 'strip', 'flatten'],
        "$class has biography field with filters trim, strip and flatten";
    ok $self->params->get('biography') =~ /^[^\n]+$/,
        "$class biography filter executed as expected";

    $self = $class->new(bio => $biography);

    ok $class eq ref $self, "$class instantiated";
    is_deeply $self->fields->biography->filters, ['trim', 'strip', 'flatten'],
        "$class has biography field with filters trim, strip and flatten";
    ok $self->params->get('biography') =~ /^[^\n]+$/,
        "$class biography filter executed as expected";

}

{

    package TestClass::FiltersAliasUsage::A;

    use Validation::Class;

    field 'full_name' => {
        filters => ['trim', 'strip', 'titlecase'],
        alias   => ['name']
    };

    1;

    package main;

    my $self;
    my $class = "TestClass::FiltersAliasUsage::A";

    $self = $class->new(full_name => 'elliot    ');

    ok $class eq ref $self, "$class instantiated";
    is_deeply $self->fields->full_name->filters, ['trim', 'strip', 'titlecase'],
        "$class has full_name field with filters trim, strip and titlecase";
    ok $self->param('full_name') =~ /^Elliot$/,
        "$class full_name filter executed as expected";

    $self = $class->new(name => '   elliot   ');

    ok $class eq ref $self, "$class instantiated";
    is_deeply $self->fields->full_name->filters, ['trim', 'strip', 'titlecase'],
        "$class has full_name field with filters trim, strip and titlecase";
    ok $self->param('full_name') =~ /^Elliot$/,
        "$class full_name filter executed as expected";

}

{

    package TestClass::FiltersAliasUsage::B;

    use Validation::Class;

    field 'full_name' => {
        filters => ['trim', 'strip', 'titlecase'],
        alias   => ['name']
    };

    1;

    package main;

    my $self;
    my $class = "TestClass::FiltersAliasUsage::B";

    $self = $class->new;
    $self->params->add({name => 'elliot    '});
    $self->prototype->normalize($self);

    ok $class eq ref $self, "$class instantiated";
    is_deeply $self->fields->full_name->filters, ['trim', 'strip', 'titlecase'],
        "$class has full_name field with filters trim, strip and titlecase";
    ok $self->full_name =~ /^Elliot$/,
        "$class full_name filter executed as expected";

    $self = $class->new;
    $self->params->add({name => '   elliot   '});
    $self->prototype->normalize($self);

    ok $class eq ref $self, "$class instantiated";
    is_deeply $self->fields->full_name->filters, ['trim', 'strip', 'titlecase'],
        "$class has full_name field with filters trim, strip and titlecase";
    ok $self->full_name =~ /^Elliot$/,
        "$class full_name filter executed as expected";

}

done_testing;