File: 91_func_errors.t

package info (click to toggle)
liblist-compare-perl 0.55-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,012 kB
  • sloc: perl: 3,080; makefile: 2
file content (196 lines) | stat: -rw-r--r-- 7,804 bytes parent folder | download | duplicates (2)
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
# perl
#$Id$
# 91_func_errors.t
use strict;
use Test::More tests => 176;
use List::Compare::Functional qw(:originals :aliases);
use lib ("./t");
use Test::ListCompareSpecial qw( :seen :func_wrap :arrays :hashes :results );
use Capture::Tiny q|:all|;

my @pred = ();
my %seen = ();
my %pred = ();
my @unpred = ();
my (@unique, @complement, @intersection, @union, @symmetric_difference, @bag);
my ($unique_ref, $complement_ref, $intersection_ref, $union_ref,
$symmetric_difference_ref, $bag_ref);
my ($LR, $RL, $eqv, $disj, $return, $vers);
my (@nonintersection, @shared);
my ($nonintersection_ref, $shared_ref);
my ($memb_hash_ref, $memb_arr_ref, @memb_arr);
my ($unique_all_ref, $complement_all_ref);
my @args;

my $error = q{--bad-string};
my %badhash1 = (
    alpha   => 1,
    beta    => q{omega},
);
my %badhash2 = (
    gamma   => 1,
    delta   => q{psi},
);
my $bad_lists_msg = q{If argument is single hash ref, you must have a 'lists' key whose value is an array ref};

I_class_func_tests(\&get_union, q{get_union});
I_class_func_tests(\&get_union_ref, q{get_union_ref});
I_class_func_tests(\&get_intersection, q{get_intersection});
I_class_func_tests(\&get_intersection_ref, q{get_intersection_ref});
I_class_func_tests(\&get_shared, q{get_shared});
I_class_func_tests(\&get_shared_ref, q{get_shared_ref});
I_class_func_tests(\&get_nonintersection, q{get_nonintersection});
I_class_func_tests(\&get_nonintersection_ref, q{get_nonintersection_ref});
I_class_func_tests(\&get_symmetric_difference, q{get_symmetric_difference});
I_class_func_tests(\&get_symmetric_difference_ref,
    q{get_symmetric_difference_ref});
I_class_func_tests(\&get_symdiff, q{get_symdiff});
I_class_func_tests(\&get_symdiff_ref, q{get_symdiff_ref});
I_class_func_tests(\&get_bag, q{get_bag});
I_class_func_tests(\&get_bag_ref, q{get_union_ref});

II_class_func_tests(\&get_unique, q{get_unique});
II_class_func_tests(\&get_unique_ref, q{get_unique_ref});
II_class_func_tests(\&get_complement, q{get_complement});
II_class_func_tests(\&get_complement_ref, q{get_complement_ref});

III_class_func_tests(\&is_LsubsetR, q{is_LsubsetR});
III_class_func_tests(\&is_RsubsetL, q{is_RsubsetL});
III_class_func_tests(\&is_LequivalentR, q{is_LequivalentR});
III_class_func_tests(\&is_LeqvlntR, q{is_LeqvlntR});
III_class_func_tests(\&is_LdisjointR, q{is_LdisjointR});

IV_class_func_tests(\&is_member_which, q{is_member_which});
IV_class_func_tests(\&is_member_which_ref, q{is_member_which_ref});
IV_class_func_tests(\&is_member_any, q{is_member_any});

V_class_func_tests(\&are_members_which, q{are_members_which});
V_class_func_tests(\&are_members_any, q{are_members_any});

sub I_class_func_tests {
    my $sub = shift;
    my $name = shift;
    my @results;
    # Assume we have access to imported globals such as @a0, %h1, etc.

    eval { @results = $sub->( { key => 'value' } ); };
    like($@, qr/^$bad_lists_msg/,
        "$name:  Got expected error message for bad single hash ref");
    
    eval { @results = $sub->( { lists => 'not a reference' } ); };
    like($@, qr/^$bad_lists_msg/,
        "$name:  Got expected error message for bad single hash ref");
    
    eval { @results = $sub->( $error, [ \@a0, \@a1 ] ); };
    like($@, qr/^'$error' must be an array ref/,
        "$name:  Got expected error message for bad non-ref argument");
    
    eval { @results = $sub->( '-u', $error, [ \@a0, \@a1 ] ); };
    like($@, qr/^'$error' must be an array ref/,
        "$name:  Got expected error message for bad non-ref argument");
    
    eval { @results = $sub->( [ \%h0, \@a1 ] ); };
    like($@,
        qr/Arguments must be either all array references or all hash references/,
        "$name:  Got expected error message for mixing array refs and hash refs");
    
    eval { @results = $sub->( [ \%badhash1, \%badhash2 ] ); };
    like($@,
        qr/Values in a 'seen-hash' must be numeric/s,
        "$name:  Got expected error message for bad seen-hash");
    like($@,
        qr/Key:\s+beta\s+Value:\s+omega/s,
        "$name:  Got expected error message for bad seen-hash");
}

sub II_class_func_tests {
    my $sub = shift;
    my $name = shift;
    I_class_func_tests($sub, $name);
    my @results;
    eval { @results = $sub->( $error, [ \@a0, \@a1 ], [2], [3] ); };
    like($@, qr/Subroutine call requires 1 or 2 references as arguments/,
        "$name:  Got expected error message for wrong number of arguments");

    eval { @results = $sub->( $error, [ \%h0, \%h1 ], [2], [3] ); };
    like($@, qr/Subroutine call requires 1 or 2 references as arguments/,
        "$name:  Got expected error message for wrong number of arguments");
}

sub III_class_func_tests {
    my $sub = shift;
    my $name = shift;
    my $result;
    # Assume we have access to imported globals such as @a0, %h1, etc.

    eval { $result = $sub->( { key => 'value' } ); };
    like($@, qr/^$bad_lists_msg/,
        "$name:  Got expected error message for bad single hash ref");

    eval { $result = $sub->( { lists => 'not a reference' } ); };
    like($@, qr/^$bad_lists_msg/,
        "$name:  Got expected error message for bad single hash ref");
    
    my $i = 2;
    eval { $result = $sub->( [ \@a0, \@a1 ], [ $i, 0 ] ); };
    like($@, qr/No element in index position $i in list of list references passed as first argument to function/,
        "$name:  Got expected error message for non-existent index position");

    eval { $result = $sub->( [ \@a0, \@a1 ], [ $i ] ); };
    like($@, qr/Must provide index positions corresponding to two lists/,
        "$name:  Got expected error message for non-existent index position");
}
    
sub IV_class_func_tests {
    my $sub = shift;
    my $name = shift;
    my @results;
    # Assume we have access to imported globals such as @a0, %h1, etc.

    eval { @results = $sub->( { item  => 'value' } ); };
    like($@, qr/^$bad_lists_msg/,
        "$name:  Got expected error message for single hash ref lacking 'lists' key");

    eval { @results = $sub->( { lists => 'not a reference' } ); };
    like($@, qr/^$bad_lists_msg/,
        "$name:  Got expected error message for bad single hash ref");
    
    eval { @results = $sub->( { lists  => [ \@a0, \@a1 ] } ); };
    like($@, qr/^If argument is single hash ref, you must have an 'item' key/,
        "$name:  Got expected error message for single hash ref lacking 'item' key");

    eval { @results = $sub->( [ \@a0, \@a1 ] ); };
    like($@, qr/^Subroutine call requires 2 references as arguments/,
        "$name:  Got expected error message for lack of second argument");
}

sub V_class_func_tests {
    my $sub = shift;
    my $name = shift;
    my $result;
    # Assume we have access to imported globals such as @a0, %h1, etc.

    eval { $result = $sub->( { items  => 'value' } ); };
    like($@, qr/^$bad_lists_msg/,
        "$name:  Got expected error message for single hash ref lacking 'lists' key");

    eval { $result = $sub->( { lists => 'not a reference' } ); };
    like($@, qr/^$bad_lists_msg/,
        "$name:  Got expected error message for bad single hash ref");
    
    eval { $result = $sub->( { lists  => [ \@a0, \@a1 ] } ); };
    like($@, qr/^If argument is single hash ref, you must have an 'items' key/,
        "$name:  Got expected error message for single hash ref lacking 'items' key");
    
    eval { $result = $sub->( {
        lists  => [ \@a0, \@a1 ],
        items  => 'not a reference',
    } ); };
    like($@, qr/^If argument is single hash ref, you must have an 'items' key/,
        "$name:  Got expected error message for single hash ref with improper 'items' key");

    eval { $result = $sub->( [ \@a0, \@a1 ] ); };
    like($@, qr/^Subroutine call requires 2 references as arguments/,
        "$name:  Got expected error message for lack of second argument");
}