summaryrefslogtreecommitdiff
path: root/lib/Params/Check/t/01_Params-Check.t
blob: e868d13169d309f7a642988e12f8c3329d72e079 (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
use strict;
use Test::More 'no_plan';

### use && import ###
BEGIN {
    use_ok( 'Params::Check' );
    Params::Check->import(qw|check last_error allow|);
}    

### verbose is good for debugging ###
$Params::Check::VERBOSE = $Params::Check::VERBOSE = $ARGV[0] ? 1 : 0;

### basic things first, allow function ###

use constant FALSE  => sub { 0 };
use constant TRUE   => sub { 1 };

### allow tests ###
{   ok( allow( 42, qr/^\d+$/ ), "Allow based on regex" );
    ok( allow( $0, $0),         "   Allow based on string" );
    ok( allow( 42, [0,42] ),    "   Allow based on list" );
    ok( allow( 42, [50,sub{1}]),"   Allow based on list containing sub");
    ok( allow( 42, TRUE ),      "   Allow based on constant sub" );
    ok(!allow( $0, qr/^\d+$/ ), "Disallowing based on regex" );
    ok(!allow( 42, $0 ),        "   Disallowing based on string" );
    ok(!allow( 42, [0,$0] ),    "   Disallowing based on list" );
    ok(!allow( 42, [50,sub{0}]),"   Disallowing based on list containing sub");
    ok(!allow( 42, FALSE ),     "   Disallowing based on constant sub" );

    ### check that allow short circuits where required 
    {   my $sub_called;
        allow( 1, [ 1, sub { $sub_called++ } ] );
        ok( !$sub_called,       "Allow short-circuits properly" );
    }        

    ### check if the subs for allow get what you expect ###
    for my $thing (1,'foo',[1]) {
        allow( $thing, 
           sub { is_deeply(+shift,$thing,  "Allow coderef gets proper args") } 
        );
    }
}
### default tests ###
{   
    my $tmpl =  {
        foo => { default => 1 }
    };
    
    ### empty args first ###
    {   my $args = check( $tmpl, {} );

        ok( $args,              "check() call with empty args" );
        is( $args->{'foo'}, 1,  "   got default value" );
    }
    
    ### now provide an alternate value ###
    {   my $try  = { foo => 2 };
        my $args = check( $tmpl, $try );
        
        ok( $args,              "check() call with defined args" );
        is_deeply( $args, $try, "   found provided value in rv" );
    }

    ### now provide a different case ###
    {   my $try  = { FOO => 2 };
        my $args = check( $tmpl, $try );
        ok( $args,              "check() call with alternate case" );
        is( $args->{foo}, 2,    "   found provided value in rv" );
    }

    ### now see if we can strip leading dashes ###
    {   local $Params::Check::STRIP_LEADING_DASHES = 1;
        my $try  = { -foo => 2 };
        my $get  = { foo  => 2 };
        
        my $args = check( $tmpl, $try );
        ok( $args,              "check() call with leading dashes" );
        is_deeply( $args, $get, "   found provided value in rv" );
    }
}

### preserve case tests ###
{   my $tmpl = { Foo => { default => 1 } };
    
    for (1,0) {
        local $Params::Check::PRESERVE_CASE = $_;
        
        my $expect = $_ ? { Foo => 42 } : { Foo => 1 };
        
        my $rv = check( $tmpl, { Foo => 42 } );
        ok( $rv,                "check() call using PRESERVE_CASE: $_" );
        is_deeply($rv, $expect, "   found provided value in rv" );
    }             
}


### unknown tests ###
{   
    ### disallow unknowns ###
    {        
        my $rv = check( {}, { foo => 42 } );
    
        is_deeply( $rv, {},     "check() call with unknown arguments" ); 
        like( last_error(), qr/^Key 'foo' is not a valid key/,
                                "   warning recorded ok" );
    }
    
    ### allow unknown ###
    {
        local   $Params::Check::ALLOW_UNKNOWN = 1;
        my $rv = check( {}, { foo => 42 } );        
        
        is_deeply( $rv, { foo => 42 },
                                "check call() with unknown args allowed" );
    }
}

### store tests ###
{   my $foo;
    my $tmpl = {
        foo => { store => \$foo }
    };

    ### with/without store duplicates ###
    for( 1, 0 ) {
        local   $Params::Check::NO_DUPLICATES = $_;
        
        my $expect = $_ ? undef : 42;
        
        my $rv = check( $tmpl, { foo => 42 } );
        ok( $rv,                    "check() call with store key, no_dup: $_" );
        is( $foo, 42,               "   found provided value in variable" );
        is( $rv->{foo}, $expect,    "   found provided value in variable" );
    }
}    

### no_override tests ###
{   my $tmpl = {
        foo => { no_override => 1, default => 42 },
    };
    
    my $rv = check( $tmpl, { foo => 13 } );        
    ok( $rv,                    "check() call with no_override key" );
    is( $rv->{'foo'}, 42,       "   found default value in rv" );

    like( last_error(), qr/^You are not allowed to override key/, 
                                "   warning recorded ok" );
}

### strict_type tests ###
{   my @list = (
        [ { strict_type => 1, default => [] },  0 ],
        [ { default => [] },                    1 ],
    );

    ### check for strict_type global, and in the template key ###
    for my $aref (@list) {

        my $tmpl = { foo => $aref->[0] };
        local   $Params::Check::STRICT_TYPE = $aref->[1];
                
        ### proper value ###    
        {   my $rv = check( $tmpl, { foo => [] } );
            ok( $rv,                "check() call with strict_type enabled" );
            is( ref $rv->{foo}, 'ARRAY',
                                    "   found provided value in rv" );
        }
        
        ### improper value ###
        {   my $rv = check( $tmpl, { foo => {} } );
            ok( !$rv,               "check() call with strict_type violated" );
            like( last_error(), qr/^Key 'foo' needs to be of type 'ARRAY'/, 
                                    "   warning recorded ok" );
        }
    }
}          

### required tests ###
{   my $tmpl = {
        foo => { required => 1 }
    };
    
    ### required value provided ###
    {   my $rv = check( $tmpl, { foo => 42 } );
        ok( $rv,                    "check() call with required key" );
        is( $rv->{foo}, 42,         "   found provided value in rv" );
    }
    
    ### required value omitted ###
    {   my $rv = check( $tmpl, { } );
        ok( !$rv,                   "check() call with required key omitted" );
        like( last_error, qr/^Required option 'foo' is not provided/,
                                    "   warning recorded ok" );            
    }
}

### defined tests ###
{   my @list = (
        [ { defined => 1, default => 1 },  0 ],
        [ { default => 1 },                1 ],
    );

    ### check for strict_type global, and in the template key ###
    for my $aref (@list) {

        my $tmpl = { foo => $aref->[0] };
        local   $Params::Check::ONLY_ALLOW_DEFINED = $aref->[1];
                
        ### value provided defined ###
        {   my $rv = check( $tmpl, { foo => 42 } );
            ok( $rv,                "check() call with defined key" );
            is( $rv->{foo}, 42,     "   found provided value in rv" );
        }
        
        ### value provided undefined ###
        {   my $rv = check( $tmpl, { foo => undef } );
            ok( !$rv,               "check() call with defined key undefined" );
            like( last_error, qr/^Key 'foo' must be defined when passed/,
                                    "   warning recorded ok" );
        }                                             
    }
}

### check + allow tests ###
{   ### check if the subs for allow get what you expect ###
    for my $thing (1,'foo',[1]) {
        my $tmpl = {
            foo => { allow =>
                    sub { is_deeply(+shift,$thing,  
                                    "   Allow coderef gets proper args") } 
            }
        };
        
        my $rv = check( $tmpl, { foo => $thing } );
        ok( $rv,                    "check() call using allow key" );  
    }
}

### invalid key tests 
{   my $tmpl = { foo => { allow => sub { 0 } } };
    
    for my $val ( 1, 'foo', [], bless({},__PACKAGE__) ) {
        my $rv      = check( $tmpl, { foo => $val } );
        my $text    = "Key 'foo' ($val) is of invalid type";
        my $re      = quotemeta $text;
        
        ok(!$rv,                    "check() fails with unalllowed value" );
        like(last_error(), qr/$re/, "   $text" );
    }
}

### warnings fatal test
{   my $tmpl = { foo => { allow => sub { 0 } } };

    local $Params::Check::WARNINGS_FATAL = 1;

    eval { check( $tmpl, { foo => 1 } ) };      

    ok( $@,             "Call dies with fatal toggled" );
    like( $@,           qr/invalid type/,
                            "   error stored ok" );
}

### store => \$foo tests
{   ### quell warnings
    local $SIG{__WARN__} = sub {};
    
    my $tmpl = { foo => { store => '' } };
    check( $tmpl, {} );
    
    my $re = quotemeta q|Store variable for 'foo' is not a reference!|;
    like(last_error(), qr/$re/, "Caught non-reference 'store' variable" );
}    

### edge case tests ###
{   ### if key is not provided, and value is '', will P::C treat
    ### that correctly? 
    my $tmpl = { foo => { default => '' } };
    my $rv   = check( $tmpl, {} );
    
    ok( $rv,                    "check() call with default = ''" );
    ok( exists $rv->{foo},      "   rv exists" );
    ok( defined $rv->{foo},     "   rv defined" );
    ok( !$rv->{foo},            "   rv false" );
    is( $rv->{foo}, '',         "   rv = '' " );
}

### big template test ###
{
    my $lastname;
    
    ### the template to check against ###
    my $tmpl = {
        firstname   => { required   => 1, defined => 1 },
        lastname    => { required   => 1, store => \$lastname },
        gender      => { required   => 1,
                         allow      => [qr/M/i, qr/F/i],
                    },
        married     => { allow      => [0,1] },
        age         => { default    => 21,
                         allow      => qr/^\d+$/,
                    },
        id_list     => { default        => [],
                         strict_type    => 1
                    },
        phone       => { allow          => sub { 1 if +shift } },
        bureau      => { default        => 'NSA',
                         no_override    => 1
                    },
    };

    ### the args to send ###
    my $try = {
        firstname   => 'joe',
        lastname    => 'jackson',
        gender      => 'M',
        married     => 1,
        age         => 21,
        id_list     => [1..3],
        phone       => '555-8844',
    };

    ### the rv we expect ###
    my $get = { %$try, bureau => 'NSA' };

    my $rv = check( $tmpl, $try );
    
    ok( $rv,                "elaborate check() call" );
    is_deeply( $rv, $get,   "   found provided values in rv" );
    is( $rv->{lastname}, $lastname, 
                            "   found provided values in rv" );
}

### $Params::Check::CALLER_DEPTH test
{
    sub wrapper { check  ( @_ ) };
    sub inner   { wrapper( @_ ) };
    sub outer   { inner  ( @_ ) };
    outer( { dummy => { required => 1 }}, {} );

    like( last_error, qr/for .*::wrapper by .*::inner$/,
                            "wrong caller without CALLER_DEPTH" );

    local $Params::Check::CALLER_DEPTH = 1;
    outer( { dummy => { required => 1 }}, {} );

    like( last_error, qr/for .*::inner by .*::outer$/,
                            "right caller with CALLER_DEPTH" );
}