File: 10-attribute-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 (86 lines) | stat: -rw-r--r-- 2,022 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
BEGIN {

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

}

use utf8;
use Test::More;

{

    # testing the attribute method
    # this method is designed to add accessors to the calling class
    # this method can also be referred to as has()

    package MyApp;

    use Validation::Class;

    attribute name => 'Thomas';
    attribute sex  => 'M';

    has age => 23;

    attribute type => sub {
        my ($self) = @_;
        if ($self->age > 21) {
            if ($self->sex eq 'M') {
                return 'Man';
            }
            if ($self->sex eq 'M') {
                return 'Woman';
            }
        }
        else {
            if ($self->sex eq 'M') {
                return 'Boy';
            }
            if ($self->sex eq 'M') {
                return 'Girl';
            }
        }
    };

    has slang_type => sub {
        my ($self) = @_;
        if ($self->age > 21) {
            if ($self->sex eq 'M') {
                return 'Oldhead';
            }
            if ($self->sex eq 'M') {
                return 'Oldjawn';
            }
        }
        else {
            if ($self->sex eq 'M') {
                return 'Youngbawl';
            }
            if ($self->sex eq 'M') {
                return 'Youngjawn';
            }
        }
    };

    __PACKAGE__->attribute(class => 'upper')
      ;    # override the existing class method

    package main;

    my $class = "MyApp";
    my $self  = $class->new;

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

    ok 'Thomas' eq $self->name, 'The name attribute has the correct value';
    ok 'M'      eq $self->sex,  'The sex attribute has the correct value';
    ok 23       eq $self->age,  'The age attribute has the correct value';
    ok 'Man'    eq $self->type, 'The type attribute has the correct value';
    ok 'Oldhead' eq $self->slang_type,
      'The slang_type attribute has the correct value';
    ok 'upper' eq $self->class, 'The class attribute has the correct value';

}

done_testing;