5 use Test2::Tools::Tiny;
6 use Test2::API qw/context test2_ipc_drivers/;
7 use Test2::Util qw/CAN_FORK CAN_THREAD CAN_REALLY_FORK/;
10 package My::Formatter;
12 sub new { bless [], shift };
25 use base 'Test2::Event';
26 use Test2::Util::HashBase qw{msg};
30 my $hub = Test2::Hub->new(
31 formatter => My::Formatter->new,
34 my $send_event = sub {
36 my $e = My::Event->new(msg => $msg, trace => Test2::EventFacet::Trace->new(frame => ['fake', 'fake.t', 1]));
40 ok(my $e1 = $send_event->('foo'), "Created event");
41 ok(my $e2 = $send_event->('bar'), "Created event");
42 ok(my $e3 = $send_event->('baz'), "Created event");
44 my $old = $hub->format(My::Formatter->new);
46 ok($old->isa('My::Formatter'), "old formatter");
50 "Formatter got all events"
54 tests follow_ups => sub {
55 my $hub = Test2::Hub->new;
58 my $trace = Test2::EventFacet::Trace->new(
59 frame => [__PACKAGE__, __FILE__, __LINE__],
65 is_deeply($d, $trace, "Got trace");
66 is_deeply($h, $hub, "Got hub");
67 ok(!$hub->ended, "Hub state has not ended yet");
72 exception { $hub->follow_up('xxx') },
73 qr/follow_up only takes coderefs for arguments, got 'xxx'/,
74 "follow_up takes a coderef"
77 $hub->finalize($trace);
79 is($ran, 1, "ran once");
84 "Ended at the expected place."
87 eval { $hub->finalize($trace) };
89 is($ran, 1, "ran once");
95 my ($driver) = test2_ipc_drivers();
96 is($driver, 'Test2::IPC::Driver::Files', "Default Driver");
97 my $ipc = $driver->new;
98 my $hub = Test2::Hub->new(
99 formatter => My::Formatter->new,
103 my $build_event = sub {
105 return My::Event->new(msg => $msg, trace => Test2::EventFacet::Trace->new(frame => ['fake', 'fake.t', 1]));
108 my $e1 = $build_event->('foo');
109 my $e2 = $build_event->('bar');
110 my $e3 = $build_event->('baz');
121 my $old = $hub->format(My::Formatter->new);
123 ok($old->isa('My::Formatter'), "old formatter");
124 is(@$old, 3, "Formatter got all events ($name)");
125 ok($_->{hubs}, "Set the hubs") for @$old;
128 if (CAN_REALLY_FORK) {
130 die "Could not fork!" unless defined $pid;
133 is(waitpid($pid, 0), $pid, "waited properly");
134 ok(!$?, "child exited with success");
144 if (CAN_THREAD && $] ge '5.010') {
146 my $thr = threads->new(sub { $do_send->() });
149 $do_check->('Threads');
154 $do_check->('no IPC');
157 tests listen => sub {
158 my $hub = Test2::Hub->new();
162 my $it = $hub->listen(sub {
163 my ($h, $e, $count) = @_;
164 is_deeply($h, $hub, "got hub");
166 push @counts => $count;
170 my $it2 = $hub->listen(sub { $second++ });
172 my $ok1 = Test2::Event::Ok->new(
175 trace => Test2::EventFacet::Trace->new(
176 frame => [ __PACKAGE__, __FILE__, __LINE__ ],
180 my $ok2 = Test2::Event::Ok->new(
183 trace => Test2::EventFacet::Trace->new(
184 frame => [ __PACKAGE__, __FILE__, __LINE__ ],
188 my $ok3 = Test2::Event::Ok->new(
191 trace => Test2::EventFacet::Trace->new(
192 frame => [ __PACKAGE__, __FILE__, __LINE__ ],
203 is_deeply(\@counts, [1, 2], "Got counts");
204 is_deeply(\@events, [$ok1, $ok2], "got events");
205 is($second, 3, "got all events in listener that was not removed");
208 exception { $hub->listen('xxx') },
209 qr/listen only takes coderefs for arguments, got 'xxx'/,
210 "listen takes a coderef"
214 tests metadata => sub {
215 my $hub = Test2::Hub->new();
217 my $default = { foo => 1 };
218 my $meta = $hub->meta('Foo', $default);
219 is_deeply($meta, $default, "Set Meta");
221 $meta = $hub->meta('Foo', {});
222 is_deeply($meta, $default, "Same Meta");
224 $hub->delete_meta('Foo');
225 is($hub->meta('Foo'), undef, "No Meta");
227 $hub->meta('Foo', {})->{xxx} = 1;
228 is($hub->meta('Foo')->{xxx}, 1, "Vivified meta and set it");
231 exception { $hub->meta(undef) },
232 qr/Invalid META key: undef, keys must be true, and may not be references/,
233 "Cannot use undef as a meta key"
237 exception { $hub->meta(0) },
238 qr/Invalid META key: '0', keys must be true, and may not be references/,
239 "Cannot use 0 as a meta key"
243 exception { $hub->delete_meta(undef) },
244 qr/Invalid META key: undef, keys must be true, and may not be references/,
245 "Cannot use undef as a meta key"
249 exception { $hub->delete_meta(0) },
250 qr/Invalid META key: '0', keys must be true, and may not be references/,
251 "Cannot use 0 as a meta key"
255 tests filter => sub {
256 my $hub = Test2::Hub->new();
259 my $it = $hub->filter(sub {
261 is($h, $hub, "got hub");
267 my $it2 = $hub->filter(sub { $count++; $_[1] });
269 my $ok1 = Test2::Event::Ok->new(
272 trace => Test2::EventFacet::Trace->new(
273 frame => [ __PACKAGE__, __FILE__, __LINE__ ],
277 my $ok2 = Test2::Event::Ok->new(
280 trace => Test2::EventFacet::Trace->new(
281 frame => [ __PACKAGE__, __FILE__, __LINE__ ],
285 my $ok3 = Test2::Event::Ok->new(
288 trace => Test2::EventFacet::Trace->new(
289 frame => [ __PACKAGE__, __FILE__, __LINE__ ],
300 is_deeply(\@events, [$ok1, $ok2], "got events");
301 is($count, 3, "got all events, even after other filter was removed");
303 $hub = Test2::Hub->new();
306 $hub->filter(sub { undef });
316 ok(!@events, "Blocked events");
319 exception { $hub->filter('xxx') },
320 qr/filter only takes coderefs for arguments, got 'xxx'/,
321 "filter takes a coderef"
325 tests pre_filter => sub {
326 my $hub = Test2::Hub->new();
329 my $it = $hub->pre_filter(sub {
331 is($h, $hub, "got hub");
337 my $it2 = $hub->pre_filter(sub { $count++; $_[1] });
339 my $ok1 = Test2::Event::Ok->new(
342 trace => Test2::EventFacet::Trace->new(
343 frame => [ __PACKAGE__, __FILE__, __LINE__ ],
347 my $ok2 = Test2::Event::Ok->new(
350 trace => Test2::EventFacet::Trace->new(
351 frame => [ __PACKAGE__, __FILE__, __LINE__ ],
355 my $ok3 = Test2::Event::Ok->new(
358 trace => Test2::EventFacet::Trace->new(
359 frame => [ __PACKAGE__, __FILE__, __LINE__ ],
366 $hub->pre_unfilter($it);
370 is_deeply(\@events, [$ok1, $ok2], "got events");
371 is($count, 3, "got all events, even after other pre_filter was removed");
373 $hub = Test2::Hub->new();
376 $hub->pre_filter(sub { undef });
386 ok(!@events, "Blocked events");
389 exception { $hub->pre_filter('xxx') },
390 qr/pre_filter only takes coderefs for arguments, got 'xxx'/,
391 "pre_filter takes a coderef"
396 my $hub = Test2::Hub->new;
398 is($hub->count, 0, "count starts at 0");
399 is($hub->failed, 0, "failed starts at 0");
400 is($hub->is_passing, 1, "start off passing");
401 is($hub->plan, undef, "no plan yet");
404 is($hub->is_passing, 0, "Can Fail");
407 is($hub->is_passing, 1, "Passes again");
410 is($hub->count, 1, "Added a passing result");
411 is($hub->failed, 0, "still no fails");
412 is($hub->is_passing, 1, "Still passing");
416 is($hub->count, 2, "Added a result");
417 is($hub->failed, 1, "new failure");
418 is($hub->is_passing, 0, "Not passing");
421 is($hub->is_passing, 0, "is_passing always false after a failure");
425 is($hub->is_passing, 1, "Passes again");
428 is($hub->count, 2, "No new result");
429 is($hub->failed, 1, "new failure");
430 is($hub->is_passing, 0, "Not passing");
432 ok(!eval { $hub->plan('foo'); 1 }, "Could not set plan to 'foo'");
433 like($@, qr/'foo' is not a valid plan! Plan must be an integer greater than 0, 'NO PLAN', or 'SKIP'/, "Got expected error");
435 ok($hub->plan(5), "Can set plan to integer");
436 is($hub->plan, 5, "Set the plan to an integer");
438 $hub->set__plan(undef);
439 ok($hub->plan('NO PLAN'), "Can set plan to 'NO PLAN'");
440 is($hub->plan, 'NO PLAN', "Set the plan to 'NO PLAN'");
442 $hub->set__plan(undef);
443 ok($hub->plan('SKIP'), "Can set plan to 'SKIP'");
444 is($hub->plan, 'SKIP', "Set the plan to 'SKIP'");
446 ok(!eval { $hub->plan(5); 1 }, "Cannot change plan");
447 like($@, qr/You cannot change the plan/, "Got error");
449 my $trace = Test2::EventFacet::Trace->new(frame => ['Foo::Bar', 'foo.t', 42, 'blah']);
450 $hub->finalize($trace);
451 my $ok = eval { $hub->finalize($trace) };
455 is($err, <<" EOT", "Got expected error");
457 First End: foo.t line 42
458 Second End: foo.t line 42
461 $hub = Test2::Hub->new;
466 $hub->set_ended($trace);
467 $hub->set_bailed_out("foo");
468 $hub->set_skip_reason('xxx');
469 ok(!$hub->is_passing, "not passing");
473 ok(!$hub->plan, "no plan");
474 is($hub->count, 0, "count reset to 0");
475 is($hub->failed, 0, "reset failures");
476 ok(!$hub->ended, "not ended");
477 ok(!$hub->bailed_out, "did not bail out");
478 ok(!$hub->skip_reason, "no skip reason");