summaryrefslogtreecommitdiff
path: root/t/15-case.t
blob: ff02112add756d76ea6a7835fc6ebfa4626894aa (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
use strict;
use warnings;

use Test::More;

use Params::Validate qw(validate validate_with);

my @testset;

# Generate test cases ...
BEGIN {
    my @lower_case_args = ( foo => 1 );
    my @upper_case_args = ( FOO => 1 );
    my @mixed_case_args = ( FoO => 1 );

    my %lower_case_spec = ( foo => 1 );
    my %upper_case_spec = ( FOO => 1 );
    my %mixed_case_spec = ( FoO => 1 );

    my %arglist = (
        lower => \@lower_case_args,
        upper => \@upper_case_args,
        mixed => \@mixed_case_args
    );

    my %speclist = (
        lower => \%lower_case_spec,
        upper => \%upper_case_spec,
        mixed => \%mixed_case_spec
    );

    # XXX - make subs such that user gets to see the error message
    # when a test fails
    my $ok_sub = sub {
        if ($@) {
            print STDERR $@;
        }
        !$@;
    };

    my $nok_sub = sub {
        my $ok = ( $@ =~ /not listed in the validation options/ );
        unless ($ok) {
            print STDERR $@;
        }
        $ok;
    };

    # generate testcases on the fly (I'm too lazy)
    for my $ignore_case (qw( 0 1 )) {
        for my $args ( keys %arglist ) {
            for my $spec ( keys %speclist ) {
                push @testset, {
                    params => $arglist{$args},
                    spec   => $speclist{$spec},
                    expect => (
                          $ignore_case   ? $ok_sub
                        : $args eq $spec ? $ok_sub
                        :                  $nok_sub
                    ),
                    ignore_case => $ignore_case
                    };
            }
        }
    }
}

plan tests => ( scalar @testset ) * 2;

{

    # XXX - "called" will be all messed up, but what the heck
    foreach my $case (@testset) {
        my %args = eval {
            validate_with(
                params      => $case->{params},
                spec        => $case->{spec},
                ignore_case => $case->{ignore_case}
            );
        };

        ok( $case->{expect}->(%args) );
    }

    # XXX - make sure that it works from validation_options() as well
    foreach my $case (@testset) {
        Params::Validate::validation_options(
            ignore_case => $case->{ignore_case} );

        my %args = eval {
            my @args = @{ $case->{params} };
            validate( @args, $case->{spec} );
        };

        ok( $case->{expect}->(%args) );
    }
}