| 1 |
use Test2::Bundle::Extended -target => 'Test2::Compare::Custom'; |
| 2 |
use Test2::API qw(intercept); |
| 3 |
|
| 4 |
my $pass = $CLASS->new(code => sub { 1 }); |
| 5 |
my $fail = $CLASS->new(code => sub { 0 }); |
| 6 |
|
| 7 |
isa_ok($pass, $CLASS, 'Test2::Compare::Base'); |
| 8 |
isa_ok($fail, $CLASS, 'Test2::Compare::Base'); |
| 9 |
|
| 10 |
ok($pass->verify(got => "anything"), "always passes"); |
| 11 |
ok(!$fail->verify(got => "anything"), "always fails"); |
| 12 |
|
| 13 |
is($pass->operator, 'CODE(...)', "default operator"); |
| 14 |
is($pass->name, '<Custom Code>', "default name"); |
| 15 |
ok(!$pass->stringify_got, "default stringify_got"); |
| 16 |
|
| 17 |
{ |
| 18 |
package My::String; |
| 19 |
use overload '""' => sub { "xxx" }; |
| 20 |
} |
| 21 |
|
| 22 |
my $stringify = $CLASS->new(code => sub { 0 }, stringify_got => 1); |
| 23 |
ok($stringify->stringify_got, "custom stringify_got()"); |
| 24 |
like( |
| 25 |
intercept { |
| 26 |
my $object = bless {}, 'My::String'; |
| 27 |
is($object => $stringify); |
| 28 |
}, |
| 29 |
array { |
| 30 |
event Fail => sub { |
| 31 |
call info => array { |
| 32 |
item hash { |
| 33 |
field table => hash { |
| 34 |
field rows => [['', '', 'xxx', 'CODE(...)', '<Custom Code>']]; |
| 35 |
}; |
| 36 |
}; |
| 37 |
}; |
| 38 |
}; |
| 39 |
}, |
| 40 |
"stringified object in test output" |
| 41 |
); |
| 42 |
|
| 43 |
my $args; |
| 44 |
my $under; |
| 45 |
my $one = $CLASS->new(code => sub { $args = {@_}; $under = $_ }, name => 'the name', operator => 'the op'); |
| 46 |
$_ = undef; |
| 47 |
$one->verify(got => 'foo', exists => 'x'); |
| 48 |
is($_, undef, '$_ restored'); |
| 49 |
|
| 50 |
is($args, {got => 'foo', exists => 'x', operator => 'the op', name => 'the name'}, "Got the expected args"); |
| 51 |
is($under, 'foo', '$_ was set'); |
| 52 |
|
| 53 |
like( |
| 54 |
dies { $CLASS->new() }, |
| 55 |
qr/'code' is required/, |
| 56 |
"Need to provide code" |
| 57 |
); |
| 58 |
|
| 59 |
done_testing; |