File: Callbacks.pm

package info (click to toggle)
libparams-validate-perl 0.91-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 524 kB
  • ctags: 729
  • sloc: perl: 2,596; makefile: 52
file content (83 lines) | stat: -rw-r--r-- 1,651 bytes parent folder | download
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
package PVTests::Callbacks;

use strict;
use warnings;

use Params::Validate qw(:all);

use PVTests;
use Test::More;


sub run_tests
{
    plan tests => 3;

    my %allowed = ( foo => 1, baz => 1 );
    eval
    {
        my @a = ( foo => 'foo' );
        validate( @a, { foo => { callbacks =>
                                 { is_allowed => sub { $allowed{ lc $_[0] } } },
                               }
                      } );
    };
    is( $@, q{} );

    eval
    {
        my @a = ( foo => 'aksjgakl' );

        validate( @a, { foo => { callbacks =>
                                 { is_allowed => sub { $allowed{ lc $_[0] } } },
                               }
                      } );
    };

    if ( $ENV{PERL_NO_VALIDATION} )
    {
        is( $@, q{} );
    }
    else
    {
        like( $@, qr/is_allowed/ );
    }

    # duplicates code from Lingua::ZH::CCDICT that revealad bug fixed in
    # 0.56.
    eval
    {
        Foo->new( storage => 'InMemory', file => 'something' );
    };
    is( $@, q{} )
}


package Foo;

use Params::Validate qw(:all);

my %storage = map { lc $_ => $_ } ( qw( InMemory XML BerkeleyDB ) );

sub new
{
    my $class = shift;

    local $^W = 1;

    my %p = validate_with( params => \@_,
                           spec   =>
                           { storage =>
                             { callbacks =>
                               { 'is a valid storage type' =>
                                 sub { $storage{ lc $_[0] } } },
                             },
                           },
                           allow_extra => 1,
                         );

    return 1;
}


1;