Debian Codegraph @

cpan/Test2-Suite/t/modules/Compare/Delta.t

raw
1 use Test2::Bundle::Extended -target => 'Test2::Compare::Delta';
2
3 BEGIN { $ENV{TABLE_TERM_SIZE} = 80 }
4
5 can_ok($CLASS, qw/check/);
6 is(
7 $CLASS->can('chk'),
8 $CLASS->can('check'),
9 "chk is aliased to check"
10 );
11
12 my $one = $CLASS->new();
13 isa_ok($one, $CLASS);
14
15 my $check1 = Test2::Compare::String->new(input => 'x');
16 my $check2 = Test2::Compare::String->new(input => 'y');
17
18 $one = $CLASS->new(check => $check1);
19 ref_is($one->chk, $check1, "Got our check");
20 ref_is($one->check, $check1, "Got our check aliased");
21
22 $one = $CLASS->new(chk => $check2);
23 ref_is($one->chk, $check2, "Got our check");
24 ref_is($one->check, $check2, "Got our check aliased");
25
26 like(
27 dies { $CLASS->new(check => $check1, chk => $check2) },
28 qr/Cannot specify both 'check' and 'chk' as arguments/,
29 "Cannot specify both chk and check"
30 );
31
32 subtest render_got => sub {
33 my $one = $CLASS->new;
34
35 is($one->render_got, '<UNDEF>', "'got' is undef");
36
37 $one->set_exception('foo');
38 is($one->render_got, '<EXCEPTION: foo>', "Exception always wins");
39
40 $one->set_exception(undef);
41 $one->set_dne('got');
42 is($one->render_got, '<DOES NOT EXIST>', "'got' does not exist");
43
44 $one->set_dne('check');
45 is($one->render_got, '<UNDEF>', "'got' does not exist");
46
47 $one->set_dne(undef);
48 $one->set_got('a');
49 is($one->render_got, 'a', "'got' value");
50
51 $one->set_got({});
52 like($one->render_got, qr/HASH\(.*\)/, "'got' ref value");
53 };
54
55 subtest render_check => sub {
56 my $one = $CLASS->new;
57 my $check = Test2::Compare::String->new(input => 'xyz');
58
59 is($one->render_check, '<UNDEF>', "check is undef");
60
61 $one->set_dne('got');
62 is($one->render_check, '<UNDEF>', "check is undef and dne is 'got'");
63
64 $one->set_dne('check');
65 is($one->render_check, '<DOES NOT EXIST>', "check does not exit");
66
67 $one->set_dne(undef);
68 $one->set_check($check);
69 is($one->render_check, $check->render, "valid check is rendered");
70 };
71
72 subtest _full_id => sub {
73 my $fid = $CLASS->can('_full_id');
74
75 is($fid->(undef, 'xxx'), '<xxx>', "no type means <ID>");
76 is($fid->('META', 'xxx'), '<xxx>', "META type means <ID>");
77 is($fid->('SCALAR', '$*'), '$*', "SCALAR type means ID is unchanged");
78 is($fid->('HASH', 'xxx'), '{xxx}', "HASH type means ID is wrapped in {}");
79 is($fid->('ARRAY', '12'), '[12]', "ARRAY type means ID is wrapped in []");
80 is($fid->('METHOD', 'foo'), 'foo()', "METHOD type gets () postfix");
81 };
82
83 subtest _arrow_id => sub {
84 my $aid = $CLASS->can('_arrow_id');
85
86 is($aid->('xxx', undef), ' ', "undef gets a space, not an arrow");
87 is($aid->('xxx', 'META'), ' ', "Meta gets a space, not an arrow");
88 is($aid->('xxx', 'METHOD'), '->', "Method always needs an arrow");
89 is($aid->('xxx', 'SCALAR'), '->', "Scalar always needs an arrow");
90 is($aid->('xxx', 'HASH'), '->', "Hash usually needs an arrow");
91 is($aid->('xxx', 'ARRAY'), '->', "Array usually needs an arrow");
92 is($aid->('{xxx}', 'HASH'), '', "Hash needs no arrow after hash");
93 is($aid->('{xxx}', 'ARRAY'), '', "Array needs no arrow after hash");
94 is($aid->('[xxx]', 'HASH'), '', "Hash needs no arrow after array");
95 is($aid->('[xxx]', 'ARRAY'), '', "Array needs no arrow after array");
96 is($aid->('<xxx>', 'xxx'), '->', "Need an arrow after meta, or after a method");
97 is($aid->('xxx()', 'xxx'), '->', "Need an arrow after meta, or after a method");
98 is($aid->('$VAR', 'xxx'), '->', "Need an arrow after the initial ref");
99 is($aid->('xxx', ''), ' ', "space");
100 is($aid->('', ''), '', "No arrow needed");
101 };
102
103 subtest _join_id => sub {
104 my $jid = $CLASS->can('_join_id');
105
106 is($jid->('{path}', [undef, 'id']), "{path} <id>", "Hash + undef");
107 is($jid->('[path]', [undef, 'id']), "[path] <id>", "Array + undef");
108 is($jid->('path', [undef, 'id']), "path <id>", "path + undef");
109 is($jid->('<path>', [undef, 'id']), "<path> <id>", "meta + undef");
110 is($jid->('path()', [undef, 'id']), "path() <id>", "meth + undef");
111 is($jid->('$VAR', [undef, 'id']), '$VAR <id>', '$VAR + undef');
112 is($jid->('', [undef, 'id']), "<id>", "empty + undef");
113
114 is($jid->('{path}', ['META', 'id']), "{path} <id>", "hash + meta");
115 is($jid->('[path]', ['META', 'id']), "[path] <id>", "array + meta");
116 is($jid->('path', ['META', 'id']), "path <id>", "path + meta");
117 is($jid->('<path>', ['META', 'id']), "<path> <id>", "meta + meta");
118 is($jid->('path()', ['META', 'id']), "path() <id>", "meth + meta");
119 is($jid->('$VAR', ['META', 'id']), '$VAR <id>', '$VAR + meta');
120 is($jid->('', ['META', 'id']), "<id>", "empty + meta");
121
122 is($jid->('{path}', ['SCALAR', '$*']), '{path}->$*', "Hash + scalar");
123 is($jid->('[path]', ['SCALAR', '$*']), '[path]->$*', "Array + scalar");
124 is($jid->('path', ['SCALAR', '$*']), 'path->$*', "Path + scalar");
125 is($jid->('<path>', ['SCALAR', '$*']), '<path>->$*', "Meta + scalar");
126 is($jid->('path()', ['SCALAR', '$*']), 'path()->$*', "Meth + scalar");
127 is($jid->('$VAR', ['SCALAR', '$*']), '$VAR->$*', '$VAR + scalar');
128 is($jid->('', ['SCALAR', '$*']), '$*', "Empty + scalar");
129
130 is($jid->('{path}', ['HASH', 'id']), "{path}{id}", "Hash + hash");
131 is($jid->('[path]', ['HASH', 'id']), "[path]{id}", "Array + hash");
132 is($jid->('path', ['HASH', 'id']), "path->{id}", "Path + hash");
133 is($jid->('<path>', ['HASH', 'id']), "<path>->{id}", "Meta + hash");
134 is($jid->('path()', ['HASH', 'id']), "path()->{id}", "Meth + hash");
135 is($jid->('$VAR', ['HASH', 'id']), '$VAR->{id}', '$VAR + hash');
136 is($jid->('', ['HASH', 'id']), "{id}", "Empty + hash");
137
138 is($jid->('{path}', ['ARRAY', '12']), "{path}[12]", "Hash + array");
139 is($jid->('[path]', ['ARRAY', '12']), "[path][12]", "Array + array");
140 is($jid->('path', ['ARRAY', '12']), "path->[12]", "Path + array");
141 is($jid->('<path>', ['ARRAY', '12']), "<path>->[12]", "Meta + array");
142 is($jid->('path()', ['ARRAY', '12']), "path()->[12]", "Meth + array");
143 is($jid->('$VAR', ['ARRAY', '12']), '$VAR->[12]', '$VAR + array');
144 is($jid->('', ['ARRAY', '12']), "[12]", "Empty + array");
145
146 is($jid->('{path}', ['METHOD', 'id']), "{path}->id()", "Hash + method");
147 is($jid->('[path]', ['METHOD', 'id']), "[path]->id()", "Array + method");
148 is($jid->('path', ['METHOD', 'id']), "path->id()", "Path + method");
149 is($jid->('<path>', ['METHOD', 'id']), "<path>->id()", "Meta + method");
150 is($jid->('path()', ['METHOD', 'id']), "path()->id()", "Meth + method");
151 is($jid->('$VAR', ['METHOD', 'id']), '$VAR->id()', '$VAR + method');
152 is($jid->('', ['METHOD', 'id']), "id()", "Empty + method");
153 };
154
155 subtest should_show => sub {
156 my $one = $CLASS->new(verified => 0);
157 ok($one->should_show, "not verified, always show");
158
159 $one->set_verified(1);
160 ok(!$one->should_show, "verified, do not show");
161
162 my $check = Test2::Compare::String->new(input => 'xyz');
163 $one->set_chk($check);
164 ok(!$one->should_show, "verified, check is uninteresting");
165
166 $check->set_lines([1,2]);
167 ok(!$one->should_show, "verified, check has lines but no file");
168
169 $check->set_file('foo');
170 ok(!$one->should_show, "verified, check has lines different file");
171
172 $check->set_file(__FILE__);
173 ok($one->should_show, "Have lines and same file, should show for debug purposes");
174 };
175
176 subtest filter_visible => sub {
177 my $root = $CLASS->new(verified => 1);
178 my $child1 = $CLASS->new(verified => 0, id => [HASH => 'a']);
179 my $child2 = $CLASS->new(verified => 1, id => [HASH => 'b']);
180 my $grand1 = $CLASS->new(verified => 0, id => [ARRAY => 0], children => []);
181 my $grand2 = $CLASS->new(verified => 0, id => [ARRAY => 1], children => []);
182
183 $root->set_children([$child1, $child2]);
184 $child2->set_children([$grand1, $grand2]);
185
186 is(
187 $root->filter_visible,
188 [
189 ['{a}', $child1],
190 ['{b}[0]', $grand1],
191 ['{b}[1]', $grand2],
192 ],
193 "Got visible ones"
194 );
195 };
196
197 subtest table_header => sub {
198 is($CLASS->table_header, [qw/PATH LNs GOT OP CHECK LNs/], "got header");
199 };
200
201 subtest table_op => sub {
202 my $one = $CLASS->new(verified => 0);
203 is($one->table_op, '!exists', "no op if there is no check");
204
205 my $check = Test2::Compare::String->new(input => 'xyz');
206 $one->set_chk($check);
207 $one->set_got('foo');
208 is($one->table_op, 'eq', "got op");
209
210 $one->set_dne('anything');
211 is($one->table_op, 'eq', "got op when dne is set to something other than 'got'");
212
213 $one->set_dne('got');
214 is($one->table_op, '', "Called check->operator without args since dne is 'got'");
215 };
216
217 subtest table_check_lines => sub {
218 my $one = $CLASS->new(verified => 0);
219 is($one->table_check_lines, '', 'no lines without a check');
220
221 my $check = Test2::Compare::String->new(input => 'xyz');
222 $one->set_chk($check);
223 is($one->table_check_lines, '', 'check has no lines');
224
225 $check->set_lines([]);
226 is($one->table_check_lines, '', 'check has lines, but it is empty');
227
228 $check->set_lines([2, 4, 6]);
229 is($one->table_check_lines, '2, 4, 6', 'got lines');
230 };
231
232 subtest table_got_lines => sub {
233 my $one = $CLASS->new(verified => 0);
234 is($one->table_got_lines, '', "no lines without a check");
235
236 my $check = Test2::Compare::String->new(input => 'xyz');
237 $one->set_chk($check);
238 $one->set_dne('got');
239 is($one->table_got_lines, '', "no lines when 'got' is dne");
240
241 $one->set_dne('anything');
242 is($one->table_got_lines, '', "no lines found with other dne");
243
244 $one->set_dne('');
245 is($one->table_got_lines, '', "no lines found by check");
246
247 my $c = mock 'Test2::Compare::Base' => (
248 override => [
249 got_lines => sub {(2, 4, 6)},
250 ],
251 );
252
253 is($one->table_got_lines, '2, 4, 6', "got lines");
254 };
255
256 subtest table_rows => sub {
257 my $one = $CLASS->new(verified => 0);
258
259 # These are tested above, mocking here for simplicity
260 my $mock = mock $CLASS => (
261 override => [
262 filter_visible => sub { [['{foo}', $one], ['{bar}', $one]] },
263 render_check => sub { 'CHECK!' },
264 render_got => sub { 'GOT!' },
265 table_op => sub { 'OP!' },
266 table_check_lines => sub { 'CHECK LINES!' },
267 table_got_lines => sub { 'GOT LINES!' },
268 ],
269 );
270
271 my $rows = $one->table_rows;
272 $mock = undef;
273
274 is(
275 $rows,
276 [
277 ['{foo}', 'GOT LINES!', 'GOT!', 'OP!', 'CHECK!', 'CHECK LINES!'],
278 ['{bar}', 'GOT LINES!', 'GOT!', 'OP!', 'CHECK!', 'CHECK LINES!'],
279 ],
280 "got rows"
281 );
282 };
283
284 subtest table => sub {
285 local $ENV{TS_MAX_DELTA} = 10;
286 my $rows;
287 my $mock = mock $CLASS => (override => [table_rows => sub { return $rows }]);
288 my $one = $CLASS->new();
289
290 $rows = [
291 ['{foo}', 'GOT LINES!', 'GOT!', 'OP!', 'CHECK!', 'CHECK LINES!'],
292 ['{bar}', 'GOT LINES!', 'GOT!', 'OP!', 'CHECK!', 'CHECK LINES!'],
293 ['{baz}', 'GOT LINES!', 'GOT!', 'OP!', 'CHECK!', 'CHECK LINES!'],
294 ['{bat}', 'GOT LINES!', 'GOT!', 'OP!', 'CHECK!', 'CHECK LINES!'],
295 ];
296
297 def is => (
298 [split /\n/, $one->table->as_string],
299 [
300 '+-------+------------+------+-----+--------+--------------+',
301 '| PATH | LNs | GOT | OP | CHECK | LNs |',
302 '+-------+------------+------+-----+--------+--------------+',
303 '| {foo} | GOT LINES! | GOT! | OP! | CHECK! | CHECK LINES! |',
304 '| {bar} | GOT LINES! | GOT! | OP! | CHECK! | CHECK LINES! |',
305 '| {baz} | GOT LINES! | GOT! | OP! | CHECK! | CHECK LINES! |',
306 '| {bat} | GOT LINES! | GOT! | OP! | CHECK! | CHECK LINES! |',
307 '+-------+------------+------+-----+--------+--------------+',
308 ],
309 "Got expected table"
310 );
311
312 $rows = [
313 ['{foo}', 'GOT LINES!', 'GOT!', 'OP!', 'CHECK!', 'CHECK LINES!'],
314 ['{bar}', 'GOT LINES!', 'GOT!', 'OP!', 'CHECK!', 'CHECK LINES!'],
315 ['{baz}', 'GOT LINES!', 'GOT!', 'OP!', 'CHECK!', 'CHECK LINES!'],
316 ['{bat}', 'GOT LINES!', 'GOT!', 'OP!', 'CHECK!', 'CHECK LINES!'],
317 ];
318
319 $ENV{TS_MAX_DELTA} = 2;
320 def is => (
321 [split /\n/, $one->table->as_string],
322 [
323 '+-------+------------+------+-----+--------+--------------+',
324 '| PATH | LNs | GOT | OP | CHECK | LNs |',
325 '+-------+------------+------+-----+--------+--------------+',
326 '| {foo} | GOT LINES! | GOT! | OP! | CHECK! | CHECK LINES! |',
327 '| {bar} | GOT LINES! | GOT! | OP! | CHECK! | CHECK LINES! |',
328 '+-------+------------+------+-----+--------+--------------+',
329 '************************************************************',
330 '* Stopped after 2 differences. *',
331 '* Set the TS_MAX_DELTA environment var to raise the limit. *',
332 '* Set it to 0 for no limit. *',
333 '************************************************************',
334 ],
335 "Got expected table and warning"
336 );
337
338 $ENV{TS_MAX_DELTA} = 25;
339 $rows = [
340 ['{foo}', '', '', '', '', ''],
341 ['{bar}', '', '', '', '', ''],
342 ['{baz}', '', '', '', '', ''],
343 ['{bat}', '', '', '', '', ''],
344 ];
345
346 def is => (
347 [split /\n/, $one->table->as_string],
348 [
349 '+-------+-----+-------+',
350 '| PATH | GOT | CHECK |',
351 '+-------+-----+-------+',
352 '| {foo} | | |',
353 '| {bar} | | |',
354 '| {baz} | | |',
355 '| {bat} | | |',
356 '+-------+-----+-------+',
357 ],
358 "'GOT' and 'CHECK' never collapse"
359 );
360
361 $mock = undef;
362 delete $ENV{TS_MAX_DELTA};
363
364 do_def();
365 };
366
367 subtest custom_columns => sub {
368 my $conv = Test2::Compare->can('strict_convert');
369 my $comp = Test2::Compare->can('compare');
370
371 my $cmp = sub {
372 my $ctx = context();
373 my $delta = $comp->(@_, $conv);
374 my $table = $delta->table;
375 $ctx->release;
376 return [split /\n/, $table->as_string];
377 };
378
379 $CLASS->add_column('V' => sub {
380 my ($d) = @_;
381 return $d->verified ? '*' : '';
382 });
383
384 my $table = $cmp->(
385 { foo => ['x', 'y'] },
386 hash {
387 field foo => array {
388 item 'a';
389 item 'b';
390 };
391 },
392 );
393
394 like(
395 $table,
396 [
397 qr/\Q+---+\E$/,
398 qr/\Q| V |\E$/,
399 qr/\Q+---+\E$/,
400 qr/\Q| * |\E$/,
401 qr/\Q| * |\E$/,
402 qr/\Q| |\E$/,
403 qr/\Q| |\E$/,
404 qr/\Q+---+\E$/,
405 DNE()
406 ],
407 "Got new column, it is last"
408 );
409
410 $table = $cmp->(
411 ['x', 'y'],
412 ['a', 'b'],
413 );
414
415 is($table->[1], mismatch qr/\Q| V |\E/, "Column not shown, it is empty");
416
417 is($CLASS->remove_column('V'), 1, "Removed the column");
418 is($CLASS->remove_column('V'), 0, "No column to remove");
419
420 $CLASS->add_column(
421 'V',
422 value => sub {
423 my ($d) = @_;
424 return $d->verified ? '*' : '';
425 },
426 alias => '?',
427 no_collapse => 1,
428 prefix => 1,
429 );
430
431 $table = $cmp->(
432 { foo => ['x', 'y'] },
433 hash {
434 field foo => array {
435 item 'a';
436 item 'b';
437 };
438 },
439 );
440
441 like(
442 $table,
443 [
444 qr/^\Q+---+\E/,
445 qr/^\Q| ? |\E/,
446 qr/^\Q+---+\E/,
447 qr/^\Q| * |\E/,
448 qr/^\Q| * |\E/,
449 qr/^\Q| |\E/,
450 qr/^\Q| |\E/,
451 qr/^\Q+---+\E/,
452 DNE()
453 ],
454 "Got new column, it is first"
455 );
456
457 $table = $cmp->(
458 ['x', 'y'],
459 ['a', 'b'],
460 );
461
462 like(
463 $table,
464 [
465 qr/^\Q+---+\E/,
466 qr/^\Q| ? |\E/,
467 qr/^\Q+---+\E/,
468 qr/^\Q| |\E/,
469 qr/^\Q| |\E/,
470 qr/^\Q+---+\E/,
471 DNE()
472 ],
473 "Did not collapse"
474 );
475
476 is($CLASS->remove_column('V'), 1, "Removed the column");
477 is($CLASS->remove_column('V'), 0, "No column to remove");
478
479 like(
480 dies { $CLASS->add_column },
481 qr/Column name is required/,
482 "Column name is required"
483 );
484
485 like(
486 dies { $CLASS->add_column('FOO') },
487 qr/You must specify a 'value' callback/,
488 "Need value callback"
489 );
490
491 like(
492 dies { $CLASS->add_column('FOO', 'foo') },
493 qr/'value' callback must be a CODE reference/,
494 "Need value callback"
495 );
496
497 $CLASS->add_column('FOO' => sub { '' });
498 like(
499 dies { $CLASS->add_column('FOO' => sub { '' }) },
500 qr/Column 'FOO' is already defined/,
501 "No duplicates"
502 );
503
504 is($CLASS->remove_column('FOO'), 1, "Removed the column");
505 };
506
507 subtest set_column_alias => sub {
508 $CLASS->set_column_alias(PATH => ' ');
509 is(
510 $CLASS->table_header,
511 [' ', qw/LNs GOT OP CHECK LNs/],
512 "hide column name"
513 );
514
515 $CLASS->set_column_alias(GLNs => 'Now This');
516 is(
517 $CLASS->table_header,
518 [' ', 'Now This', qw/GOT OP CHECK LNs/],
519 "column name with spaces"
520 );
521
522 $CLASS->add_column('NEW' => sub { '' });
523 $CLASS->set_column_alias(NEW => 'OLD');
524 is(
525 $CLASS->table_header,
526 [' ', 'Now This', qw/GOT OP CHECK LNs OLD/],
527 "change added column name"
528 );
529
530 like(
531 dies { $CLASS->set_column_alias('OP') },
532 qr/Missing alias/,
533 'Missing alias'
534 );
535
536 like(
537 dies { $CLASS->set_column_alias(DNE => 'NOPE') },
538 qr/Tried to alias a non-existent column/,
539 'Needs existing column name'
540 );
541 };
542
543 subtest overload => sub {
544 no warnings 'once';
545 {
546 package Overload::Foo;
547 use overload
548 '""' => sub { 'FOO' },
549 '0+' => sub { 42 };
550
551 package Overload::Bar;
552 use overload
553 '""' => sub { 'BAR' },
554 '0+' => sub { 99 };
555 }
556
557 my $foo = bless \*FOO, 'Overload::Foo';
558 my $bar = bless \*BAR, 'Overload::Bar';
559
560 is("$foo", "FOO", "overloaded string form FOO");
561 is("$bar", "BAR", "overloaded string form BAR");
562 is(int($foo), 42, "overloaded number form FOO");
563 is(int($bar), 99, "overloaded number form BAR");
564
565 my $conv = Test2::Compare->can('strict_convert');
566 my $comp = Test2::Compare->can('compare');
567 my $cmp = sub {
568 my $ctx = context();
569 my $delta = $comp->(@_, $conv);
570 my $table = $delta->table;
571 $ctx->release;
572 return [split /\n/, $table->as_string];
573 };
574
575 my $table = $cmp->($foo, $bar);
576
577 # On some systems the memory address is long enough to cause this to wrap.
578 my @checks;
579 if (@$table == 5) {
580 @checks = (
581 qr/^\| Overload::Foo=GLOB\(.+\)\s+\| ==\s+\| Overload::Bar=GLOB\(.+\)\s+\|$/,
582 );
583 }
584 else {
585 @checks = (
586 qr/^\| Overload::Foo=GLOB\(.+\s+\| ==\s+\| Overload::Bar=GLOB\(.+\s+\|$/,
587 qr/^\| .*\)\s+\| \s+\| .*\)\s+\|$/,
588 );
589 }
590
591 like(
592 $table,
593 [
594 T(), # Border
595 T(), # Header
596 T(), # Border
597 @checks,
598 T(), # Border
599 DNE(), # END
600 ],
601 "Showed type+mem address, despire overloading"
602 );
603 };
604
605 done_testing;