1#!/usr/bin/perl -w
2
3use strict;
4
5use Test::More;
6
7use Params::Validate qw(validate validate_with);
8
9my @testset;
10
11# Generate test cases ...
12BEGIN
13{
14    my @lower_case_args = ( foo => 1 );
15    my @upper_case_args = ( FOO => 1 );
16    my @mixed_case_args = ( FoO => 1 );
17
18    my %lower_case_spec = ( foo => 1 );
19    my %upper_case_spec = ( FOO => 1 );
20    my %mixed_case_spec = ( FoO => 1 );
21
22    my %arglist = ( lower => \@lower_case_args,
23                    upper => \@upper_case_args,
24                    mixed => \@mixed_case_args
25                  );
26
27    my %speclist = ( lower => \%lower_case_spec,
28                     upper => \%upper_case_spec,
29                     mixed => \%mixed_case_spec
30                   );
31
32    # XXX - make subs such that user gets to see the error message
33    # when a test fails
34    my $ok_sub  =
35        sub { if ( $@ )
36              {
37                  print STDERR $@;
38              }
39              !$@; };
40
41    my $nok_sub =
42        sub { my $ok = ( $@ =~ /not listed in the validation options/ );
43              unless ($ok)
44              {
45                  print STDERR $@;
46              }
47              $ok; };
48
49    # generate testcases on the fly (I'm too lazy)
50    for my $ignore_case ( qw( 0 1 ) )
51    {
52        for my $args (keys %arglist)
53        {
54            for my $spec (keys %speclist)
55            {
56                push @testset, { params => $arglist{ $args },
57                                 spec   => $speclist{ $spec },
58                                 expect =>
59                                 ( $ignore_case
60                                   ? $ok_sub
61                                   : $args eq $spec
62                                   ? $ok_sub
63                                   : $nok_sub
64                                 ),
65                                 ignore_case => $ignore_case
66                               };
67            }
68        }
69    }
70}
71
72plan tests => (scalar @testset) * 2;
73
74{
75    # XXX - "called" will be all messed up, but what the heck
76    foreach my $case (@testset)
77    {
78        my %args =
79            eval { validate_with( params      => $case->{params},
80                                  spec        => $case->{spec},
81                                  ignore_case => $case->{ignore_case}
82                                ) };
83
84        ok( $case->{expect}->(%args) );
85    }
86
87    # XXX - make sure that it works from validation_options() as well
88    foreach my $case (@testset)
89    {
90        Params::Validate::validation_options
91            ( ignore_case => $case->{ignore_case} );
92
93        my %args = eval { my @args = @{ $case->{params} };
94                          validate( @args, $case->{spec} ) };
95
96        ok( $case->{expect}->(%args) );
97    }
98}
99
100