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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
use Test2::Bundle::Extended -target => 'Test2::Tools::ClassicCompare';
BEGIN { $ENV{TABLE_TERM_SIZE} = 80 }
use Test2::Util::Stash qw/purge_symbol/;
BEGIN {
purge_symbol('&is');
purge_symbol('&like');
purge_symbol('&unlike');
purge_symbol('&isnt');
purge_symbol('&cmp_ok');
not_imported_ok(qw/is is_deeply like unlike isnt cmp_ok/);
}
use Test2::Tools::ClassicCompare;
imported_ok(qw/is is_deeply like cmp_ok unlike isnt/);
my $ref = {};
is(undef, undef, "undef is undef");
is("foo", "foo", 'foo check');
is($ref, "$ref", "flat check, ref as string right");
is("$ref", $ref, "flat check, ref as string left");
isnt("bar", "foo", 'not foo check');
isnt({}, "$ref", "negated flat check, ref as string right");
isnt("$ref", {}, "negated flat check, ref as string left");
like('aaa', qr/a/, "have an a");
like('aaa', 'a', "have an a, not really a regex");
unlike('bbb', qr/a/, "do not have an a");
unlike('bbb', 'a', "do not have an a, not really a regex");
# Failures
my $events = intercept {
def ok => (!is('foo', undef, "undef check"), "undef check");
def ok => (!is(undef, 'foo', "undef check"), "undef check");
def ok => (!is('foo', 'bar', "string mismatch"), "string mismatch");
def ok => (!isnt('foo', 'foo', "undesired match"), "undesired match");
def ok => (!like('foo', qr/a/, "no match"), "no match");
def ok => (!unlike('foo', qr/o/, "unexpected match"), "unexpected match");
};
do_def;
is_deeply(
$events,
array {
filter_items { grep { !$_->isa('Test2::Event::Diag') } @_ };
event Fail => { };
event Fail => { };
event Fail => { };
event Fail => { };
event Fail => { };
event Fail => { };
end;
},
"got failure events"
);
# is_deeply uses the same algorithm as the 'Compare' plugin, so it is already
# tested over there.
is_deeply(
{foo => 1, bar => 'baz'},
{foo => 1, bar => 'baz'},
"Deep compare"
);
{
package Foo;
use overload '""' => sub { 'xxx' };
}
my $foo = bless({}, 'Foo');
like($foo, qr/xxx/, "overload");
my $thing = bless {}, 'Foo::Bar';
# Test cmp_ok in a separate package so we have access to the better tools.
package main2;
use Test2::Bundle::Extended;
BEGIN { main::purge_symbol('&cmp_ok') }
use Test2::Tools::ClassicCompare qw/cmp_ok/;
use Test2::Util::Table();
sub table { join "\n" => Test2::Util::Table::table(@_) }
use Test2::Util::Ref qw/render_ref/;
cmp_ok('x', 'eq', 'x', 'string pass');
cmp_ok(5, '==', 5, 'number pass');
cmp_ok(5, '==', 5.0, 'float pass');
my $file = __FILE__;
my $line = __LINE__ + 2;
like(
warnings { cmp_ok(undef, '==', undef, 'undef pass') },
[
qr/uninitialized value.*at \(eval in cmp_ok\) \Q$file\E line $line/,
],
"got expected warnings (number)"
);
$line = __LINE__ + 2;
like(
warnings { cmp_ok(undef, 'eq', undef, 'undef pass') },
[
qr/uninitialized value.*at \(eval in cmp_ok\) \Q$file\E line $line/,
],
"got expected warnings (string)"
);
like(
intercept { cmp_ok('x', 'ne', 'x', 'string fail', 'extra diag') },
array {
fail_events Ok => sub {
call pass => 0;
call name => 'string fail';
};
event Diag => sub {
call message => table(
header => [qw/GOT OP CHECK/],
rows => [
[qw/x ne x/],
],
);
};
event Diag => { message => 'extra diag' };
end;
},
"Got 1 string fail event"
);
like(
intercept { cmp_ok(5, '==', 42, 'number fail', 'extra diag') },
array {
fail_events Ok => sub {
call pass => 0;
call name => 'number fail';
};
event Diag => sub {
call message => table(
header => [qw/GOT OP CHECK/],
rows => [
[qw/5 == 42/],
],
);
};
event Diag => { message => 'extra diag' };
end;
},
"Got 1 number fail event"
);
my $warning;
$line = __LINE__ + 2;
like(
intercept { $warning = main::warning { cmp_ok(5, '&& die', 42, 'number fail', 'extra diag') } },
array {
event Exception => { error => qr/42 at \(eval in cmp_ok\) \Q$file\E line $line/ };
fail_events Ok => sub {
call pass => 0;
call name => 'number fail';
};
event Diag => sub {
call message => table(
header => [qw/GOT OP CHECK/],
rows => [
['5', '&& die', '<EXCEPTION>'],
],
);
};
event Diag => { message => 'extra diag' };
end;
},
"Got exception in test"
);
like(
$warning,
qr/operator '&& die' is not supported \(you can add it to %Test2::Tools::ClassicCompare::OPS\)/,
"Got warning about unsupported operator"
);
{
package Overloaded::Foo42;
use overload
'fallback' => 1,
'0+' => sub { 42 },
'""' => sub { 'foo' };
}
$foo = bless {}, 'Overloaded::Foo42';
cmp_ok($foo, '==', 42, "numeric compare with overloading");
cmp_ok($foo, 'eq', 'foo', "string compare with overloading");
like(
intercept {
local $ENV{TS_TERM_SIZE} = 10000;
cmp_ok($foo, 'ne', $foo, 'string fail', 'extra diag')
},
array {
fail_events Ok => sub {
call pass => 0;
call name => 'string fail';
};
event Diag => sub {
call message => table(
header => [qw/TYPE GOT OP CHECK/],
rows => [
['str', 'foo', 'ne', 'foo'],
['orig', render_ref($foo), '', render_ref($foo)],
],
);
};
event Diag => { message => 'extra diag' };
end;
},
"Failed string compare, overload"
);
like(
intercept {
local $ENV{TS_TERM_SIZE} = 10000;
cmp_ok($foo, '!=', $foo, 'number fail', 'extra diag')
},
array {
fail_events Ok => sub {
call pass => 0;
call name => 'number fail';
};
event Diag => sub {
call message => table(
header => [qw/TYPE GOT OP CHECK/],
rows => [
['num', '42', '!=', '42'],
['orig', render_ref($foo), '', render_ref($foo)],
],
);
};
event Diag => { message => 'extra diag' };
end;
},
"Failed number compare, overload"
);
$line = __LINE__ + 2;
like(
intercept {
local $ENV{TS_TERM_SIZE} = 10000;
$warning = main::warning {
cmp_ok($foo, '&& die', $foo, 'overload exception', 'extra diag')
}
},
array {
event Exception => { error => T() };
fail_events Ok => sub {
call pass => 0;
call name => 'overload exception';
};
event Diag => sub {
call message => table(
header => [qw/TYPE GOT OP CHECK/],
rows => [
['unsupported', 'foo', '&& die', '<EXCEPTION>'],
['orig', render_ref($foo), '', render_ref($foo)],
],
);
};
event Diag => { message => 'extra diag' };
end;
},
"Got exception in test"
);
note "cmp_ok() displaying good numbers"; {
my $have = 1.23456;
my $want = 4.5678;
like(
intercept {
cmp_ok($have, '>', $want);
},
array {
fail_events Ok => sub {
call pass => 0;
};
event Diag => sub {
call message => table(
header => [qw/GOT OP CHECK/],
rows => [
[$have, '>', $want],
],
);
};
end;
},
);
}
my $warnings;
note "cmp_ok() displaying bad numbers"; {
my $have = "zero";
my $want = "3point5";
like(
intercept {
$warnings = warnings { cmp_ok($have, '>', $want) };
},
array {
fail_events Ok => sub {
call pass => 0;
};
event Diag => sub {
call message => table(
header => [qw/TYPE GOT OP CHECK/],
rows => [
['num', 0, '>', '3'],
['orig', $have, '', $want],
],
);
};
end;
},
);
}
done_testing;
|