File: 18-depends.t

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 (131 lines) | stat: -rw-r--r-- 3,942 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
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
#!/usr/bin/perl -w

use strict;

use Params::Validate qw(validate validate_pos);
use Test::More tests => 19;

{
    my %spec = ( foo => { optional => 1, depends => 'bar' },
                 bar => { optional => 1 },
               );

    my @args = ( bar => 1 );

    eval { validate( @args,\%spec ) };

    is( $@, q{}, "validate() single depends(1): no depends, positive" );

    @args = ( foo => 1, bar => 1 );
    eval { validate( @args,\%spec ) };

    is( $@, q{}, "validate() single depends(2): with depends, positive" );

    @args = ( foo => 1 );
    eval { validate( @args, \%spec ) };

    ok( $@, "validate() single depends(3.a): with depends, negative" );
    like( $@,
          qr(^Parameter 'foo' depends on parameter 'bar', which was not given),
          "validate() single depends(3.b): check error string" );
}

{
    my %spec = ( foo => { optional => 1, depends => [ qw(bar baz) ] },
                 bar => { optional => 1 },
                 baz => { optional => 1 },
               );

    # positive, no depends (single, multiple)
    my @args = ( bar => 1 );
    eval { validate( @args, \%spec ) };
    is( $@, q{}, "validate() multiple depends(1): no depends, single arg, positive" );

    @args = ( bar => 1, baz => 1 );
    eval { validate( @args, \%spec ) };

    is( $@, q{},
	"validate() multiple depends(2): no depends, multiple arg, positive" );

    @args = ( foo => 1, bar => 1, baz => 1 );
    eval { validate( @args, \%spec ) };

    is( $@, q{}, "validate() multiple depends(3): with depends, positive" );

    @args = ( foo => 1, bar => 1 );
    eval { validate( @args, \%spec ) };

    ok( $@, "validate() multiple depends(4.a): with depends, negative, multiple missing" );
    like( $@,
          qr(^Parameter 'foo' depends on parameter 'baz', which was not given),
          "validate() multiple depends (4.b): check error string" );

    @args = ( foo => 1 );
    eval { validate( @args, \%spec ) };

    ok( $@, "validate() multiple depends(5.a): with depends, negative, multiple missing" );
    like( $@,
          qr(^Parameter 'foo' depends on parameter '(bar|baz)', which was not given),
          "validate() multiple depends (5.b): check error string" );
}

{
    # bad depends
    my %spec = ( foo => { optional => 1, depends => { 'bar' => 1 } },
                 bar => { optional => 1 },
               );

    my @args = ( foo => 1 );
    eval { validate( @args, \%spec ) };

    ok( $@, "validate() bad depends spec (1.a): depends is a hashref" );
    like( $@,
          qr(^Arguments to 'depends' must be a scalar or arrayref),
          "validate() bad depends spec (1.a): check error string" );
}

{
    my @spec = ( { optional => 1 } );

    my @args = qw(1);
    eval { validate_pos( @args, @spec ) };

    is( $@, q{}, "validate_pos() no depends, positive" );
}

{
    my @spec = ( { optional => 1, depends => 2 }, {optional => 1} );

    my @args = qw(1 1);
    eval { validate_pos( @args, @spec ) };

    is( $@, q{}, "validate_pos() single depends (1): with depends, positive" );
}

{
    my @spec = ( { optional => 1, depends => 4 },
                 { optional => 1 }, { optional => 1 },
                 { optional => 1 } );

    my @args = qw(1 0);
    eval { validate_pos( @args, @spec ) };

    ok( $@, "validate_pos() single depends (2.a): with depends, negative" );
    like( $@,
          qr(^Parameter #1 depends on parameter #4, which was not given),
          "validate_pos() single depends (2.b): check error" );
}

{
    my @spec = ( { optional => 1, depends => [ 2, 3 ] },
                 { optional => 1 },
                 0
               );
    my @args = qw(1);
    eval { validate_pos( @args, @spec ) };

    ok( $@, "validate_pos() multiple depends (1.a): with depends, bad args negative" );
    like( $@,
          qr{^Arguments to 'depends' for validate_pos\(\) must be a scalar},
          "validate_pos() multiple depends (1.b): check error" );
}