File: 10-initialize-method.t

package info (click to toggle)
libvalidation-class-perl 7.900057-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 1,616 kB
  • sloc: perl: 21,493; makefile: 2
file content (107 lines) | stat: -rw-r--r-- 1,673 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
BEGIN {

    use FindBin;
    use lib $FindBin::Bin . "/myapp/lib";

}

use utf8;
use Test::More;

{

 # testing the initialize method
 # this method is designed to allow the Validation::Class framework to wrap
 # an existing class configuration, most useful with OO systems like Moose, etc

    package MyApp;

    sub new {

        my ($class) = @_;

        my $self = bless {}, $class;

        return $self;

    }

    use Validation::Class 'field';

    field name => {

        required => 1

    };

    package main;

    my $class = "MyApp";

    my $self = $class->new(name => "...");

    ok $class eq ref $self, "$class instantiated";

    eval { $self->name };

    ok !$@, "$class has a name field";

    eval { $self->initialize_validator };

    ok !$@, "$class has initialized with no errors";

}

{

    # testing initialization and parameter handling

    package MyApp::A;

    use Validation::Class;

    field numbers => {

        required => 1,
        filters  => ['numeric']

    };

    package main;

    my $class = "MyApp::A";

    my $self = $class->new(params => {numbers => [2, 1]});

    ok "ARRAY" eq ref $self->numbers, "numbers method has an arrayref";

    ok $self->numbers->[0] == 2, "numbers array #0 is correct";
    ok $self->numbers->[1] == 1, "numbers array #1 is correct";

}

{

    # testing initialization and parameter handling

    package MyApp::B;

    use Validation::Class;

    field numbers => {

        required => 1

    };

    package main;

    my $class = "MyApp::A";

    my $self = $class->new(numbers => 12345);

    ok 12345 == $self->numbers, "numbers method has 12345 as expected";

}

done_testing;