1Test2::Compare::Custom(U3s)er Contributed Perl DocumentatTieosnt2::Compare::Custom(3)
2
3
4
6 Test2::Compare::Custom - Custom field check for comparisons.
7
9 Sometimes you want to do something complicated or unusual when
10 validating a field nested inside a deep data structure. You could pull
11 it out of the structure and test it separately, or you can use this to
12 embed the check. This provides a way for you to write custom checks for
13 fields in deep comparisons.
14
16 my $cus = Test2::Compare::Custom->new(
17 name => 'IsRef',
18 operator => 'ref(...)',
19 code => sub {
20 my %args = @_;
21 return $args{got} ? 1 : 0;
22 },
23 );
24
25 # Pass
26 is(
27 { a => 1, ref => {}, b => 2 },
28 { a => 1, ref => $cus, b => 2 },
29 "This will pass"
30 );
31
32 # Fail
33 is(
34 {a => 1, ref => 'notref', b => 2},
35 {a => 1, ref => $cus, b => 2},
36 "This will fail"
37 );
38
40 Your custom sub will be passed 4 arguments in a hash:
41
42 code => sub {
43 my %args = @_;
44 # provides got, exists, operator, name
45 return ref($args{got}) ? 1 : 0;
46 },
47
48 $_ is also localized to "got" to make it easier for those who need to
49 use regexes.
50
51 got
52 $_ The value to be checked.
53
54 exists
55 This will be a boolean. This will be true if "got" exists at all.
56 If "exists" is false then it means "got" is not simply undef, but
57 doesn't exist at all (think checking the value of a hash key that
58 does not exist).
59
60 operator
61 The operator specified at construction.
62
63 name
64 The name provided at construction.
65
67 $code = $cus->code
68 Returns the coderef provided at construction.
69
70 $name = $cus->name
71 Returns the name provided at construction.
72
73 $op = $cus->operator
74 Returns the operator provided at construction.
75
76 $bool = $cus->verify(got => $got, exists => $bool)
77
79 The source code repository for Test2-Suite can be found at
80 https://github.com/Test-More/Test2-Suite/.
81
83 Chad Granum <exodist@cpan.org>
84
86 Chad Granum <exodist@cpan.org>
87
89 Copyright 2018 Chad Granum <exodist@cpan.org>.
90
91 This program is free software; you can redistribute it and/or modify it
92 under the same terms as Perl itself.
93
94 See http://dev.perl.org/licenses/
95
96
97
98perl v5.32.1 2021-01-27 Test2::Compare::Custom(3)