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 stringify_got => 1,
20 code => sub {
21 my %args = @_;
22 return $args{got} ? 1 : 0;
23 },
24 );
25
26 # Pass
27 is(
28 { a => 1, ref => {}, b => 2 },
29 { a => 1, ref => $cus, b => 2 },
30 "This will pass"
31 );
32
33 # Fail
34 is(
35 {a => 1, ref => 'notref', b => 2},
36 {a => 1, ref => $cus, b => 2},
37 "This will fail"
38 );
39
41 Your custom sub will be passed 4 arguments in a hash:
42
43 code => sub {
44 my %args = @_;
45 # provides got, exists, operator, name
46 return ref($args{got}) ? 1 : 0;
47 },
48
49 $_ is also localized to "got" to make it easier for those who need to
50 use regexes.
51
52 got
53 $_ The value to be checked.
54
55 exists
56 This will be a boolean. This will be true if "got" exists at all.
57 If "exists" is false then it means "got" is not simply undef, but
58 doesn't exist at all (think checking the value of a hash key that
59 does not exist).
60
61 operator
62 The operator specified at construction.
63
64 name
65 The name provided at construction.
66
68 $code = $cus->code
69 Returns the coderef provided at construction.
70
71 $name = $cus->name
72 Returns the name provided at construction.
73
74 $op = $cus->operator
75 Returns the operator provided at construction.
76
77 $stringify = $cus->stringify_got
78 Returns the stringify_got flag provided at construction.
79
80 $bool = $cus->verify(got => $got, exists => $bool)
81
83 The source code repository for Test2-Suite can be found at
84 https://github.com/Test-More/Test2-Suite/.
85
87 Chad Granum <exodist@cpan.org>
88
90 Chad Granum <exodist@cpan.org>
91 Daniel Böhmer <dboehmer@cpan.org>
92
94 Copyright 2018 Chad Granum <exodist@cpan.org>.
95
96 This program is free software; you can redistribute it and/or modify it
97 under the same terms as Perl itself.
98
99 See http://dev.perl.org/licenses/
100
101
102
103perl v5.38.0 2023-07-21 Test2::Compare::Custom(3)