1Test::Stream::Compare::UCsuesrtoCmo(n3t)ributed Perl DocTuemsetn:t:aSttiroenam::Compare::Custom(3)
2
3
4
6 Test::Stream::Compare::Custom - Custom field check for comparisons.
7
9 This distribution is deprecated in favor of Test2, Test2::Suite, and
10 Test2::Workflow.
11
12 See Test::Stream::Manual::ToTest2 for a conversion guide.
13
15 Sometimes you want to do something complicated or unusual when
16 validating a field nested inside a deep data structure. You could pull
17 it out of the structure and test it separately, or you can use this to
18 embed the check. This provides a way for you to write custom checks for
19 fields in deep comparisons.
20
22 my $cus = Test::Stream::Compare::Custom->new(
23 name => 'IsRef',
24 operator => 'ref(...)',
25 code => sub {
26 my ($got, $exists, $operator, $name) = @_;
27 return ref($got) ? 1 : 0;
28 },
29 );
30
31 # Pass
32 is(
33 { a => 1, ref => {}, b => 2 },
34 { a => 1, ref => $cus, b => 2 },
35 "This will pass"
36 );
37
38 # Fail
39 is(
40 {a => 1, ref => 'notref', b => 2},
41 {a => 1, ref => $cus, b => 2},
42 "This will fail"
43 );
44
46 Your custom sub will get 4 arguments:
47
48 code => sub {
49 my ($got, $exists, $operator, $name) = @_;
50 return ref($got) ? 1 : 0;
51 },
52
53 $_ is also localized to $got to make it easier for those who need to
54 use regexes.
55
56 $got
57 $_ This is the value to be checked.
58
59 $exists
60 This will be a boolean. This will be true if $got exists at all. If
61 $exists is false then it means $got is not simply undef, but
62 doesn't exist at all (think checking the value of a hash key that
63 does not exist).
64
65 $operator
66 This is the operator specified at construction.
67
68 $name
69 This is the name provided at construction.
70
72 $code = $cus->code
73 Get the coderef provided at construction.
74
75 $name = $cus->name
76 Get the name provided at construction.
77
78 $op = $cus->operator
79 Get the operator provided at construction
80
81 $bool = $cus->verify(got => $got, exists => $bool)
82
84 The source code repository for Test::Stream can be found at
85 http://github.com/Test-More/Test-Stream/.
86
88 Chad Granum <exodist@cpan.org>
89
91 Chad Granum <exodist@cpan.org>
92
94 Copyright 2015 Chad Granum <exodist7@gmail.com>.
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.32.0 2020-07-28 Test::Stream::Compare::Custom(3)