1use strict; 2use warnings; 3 4use Test2::IPC; 5use Test2::Tools::Tiny; 6 7use Test2::API qw/context intercept test2_stack/; 8 9ok(__PACKAGE__->can($_), "imported '$_\()'") for qw{ 10 ok 11 is isnt 12 like unlike 13 diag note 14 15 is_deeply 16 17 warnings 18 exception 19 20 plan 21 skip_all 22 done_testing 23}; 24 25ok(1, "'ok' Test"); 26is("foo", "foo", "'is' test"); 27is(undef, undef, "'is' undef test"); 28isnt("foo", "bar", "'isnt' test"); 29isnt("foo", undef, "'isnt' undef test 1"); 30isnt(undef, "foo", "'isnt' undef test 2"); 31like("foo", qr/o/, "'like' test"); 32unlike("foo", qr/a/, "'unlike' test"); 33 34note("Testing Note"); 35 36my $str = "abc"; 37is_deeply( 38 { a => 1, b => 2, c => { ref => \$str, obj => bless({x => 1}, 'XXX'), array => [1, 2, 3]}}, 39 { a => 1, b => 2, c => { ref => \$str, obj => {x => 1}, array => [1, 2, 3]}}, 40 "'is_deeply' test" 41); 42 43is_deeply( 44 warnings { warn "aaa\n"; warn "bbb\n" }, 45 [ "aaa\n", "bbb\n" ], 46 "Got warnings" 47); 48 49is_deeply( 50 warnings { 1 }, 51 [], 52 "no warnings" 53); 54 55is(exception { die "foo\n" }, "foo\n", "got exception"); 56is(exception { 1 }, undef, "no exception"); 57 58my $main_events = intercept { 59 plan 8; 60 61 ok(0, "'ok' Test"); 62 is("foo", "bar", "'is' test"); 63 isnt("foo", "foo", "'isnt' test"); 64 like("foo", qr/a/, "'like' test"); 65 unlike("foo", qr/o/, "'unlike' test"); 66 67 is_deeply( 68 { a => 1, b => 2, c => {}}, 69 { a => 1, b => 2, c => []}, 70 "'is_deeply' test" 71 ); 72}; 73 74my $other_events = intercept { 75 diag("Testing Diag"); 76 note("Testing Note"); 77}; 78 79my ($plan, $ok, $is, $isnt, $like, $unlike, $is_deeply) = grep {!$_->isa('Test2::Event::Diag')} @$main_events; 80my ($diag, $note) = @$other_events; 81 82ok($plan->isa('Test2::Event::Plan'), "got plan"); 83is($plan->max, 8, "planned for 8 oks"); 84 85ok($ok->isa('Test2::Event::Fail'), "got 'ok' result"); 86is($ok->facets->{assert}->pass, 0, "'ok' test failed"); 87 88ok($is->isa('Test2::Event::Fail'), "got 'is' result"); 89is($ok->facets->{assert}->pass, 0, "test failed"); 90 91ok($isnt->isa('Test2::Event::Fail'), "got 'isnt' result"); 92is($ok->facets->{assert}->pass, 0, "test failed"); 93 94ok($like->isa('Test2::Event::Fail'), "got 'like' result"); 95is($ok->facets->{assert}->pass, 0, "test failed"); 96 97ok($unlike->isa('Test2::Event::Fail'), "got 'unlike' result"); 98is($ok->facets->{assert}->pass, 0, "test failed"); 99 100ok($is_deeply->isa('Test2::Event::Fail'), "got 'is_deeply' result"); 101is($ok->facets->{assert}->pass, 0, "test failed"); 102 103ok($diag->isa('Test2::Event::Diag'), "got 'diag' result"); 104is($diag->message, "Testing Diag", "got diag message"); 105 106ok($note->isa('Test2::Event::Note'), "got 'note' result"); 107is($note->message, "Testing Note", "got note message"); 108 109my $events = intercept { 110 skip_all 'because'; 111 ok(0, "should not see me"); 112 die "should not happen"; 113}; 114 115is(@$events, 1, "1 event"); 116ok($events->[0]->isa('Test2::Event::Plan'), "got plan"); 117is($events->[0]->directive, 'SKIP', "plan is skip"); 118is($events->[0]->reason, 'because', "skip reason"); 119 120$events = intercept { 121 is(undef, ""); 122 is("", undef); 123 124 isnt(undef, undef); 125 126 like(undef, qr//); 127 unlike(undef, qr//); 128}; 129 130@$events = grep {!$_->isa('Test2::Event::Diag')} @$events; 131is(@$events, 5, "5 events"); 132ok(!$_->facets->{assert}->pass, "undef test - should not pass") for @$events; 133 134sub tool { context() }; 135 136my %params; 137my $ctx = context(level => -1); 138my $ictx; 139$events = intercept { 140 %params = @_; 141 142 $ictx = tool(); 143 $ictx->ok(1, 'pass'); 144 $ictx->ok(0, 'fail'); 145 my $trace = Test2::EventFacet::Trace->new( 146 frame => [ __PACKAGE__, __FILE__, __LINE__], 147 ); 148 $ictx->hub->finalize($trace, 1); 149}; 150 151@$events = grep {!$_->isa('Test2::Event::Diag')} @$events; 152 153is_deeply( 154 \%params, 155 { 156 context => { %$ctx, _is_canon => undef, _is_spawn => undef, _aborted => undef }, 157 hub => $ictx->hub, 158 }, 159 "Passed in some useful params" 160); 161 162ok($ctx != $ictx, "Different context inside intercept"); 163 164is(@$events, 3, "got 3 events"); 165 166$ctx->release; 167$ictx->release; 168 169# Test that a bail-out in an intercept does not exit. 170$events = intercept { 171 $ictx = tool(); 172 $ictx->bail("The world ends"); 173 $ictx->ok(0, "Should not see this"); 174}; 175 176is(@$events, 1, "got 1 event"); 177ok($events->[0]->isa('Test2::Event::Bail'), "got the bail"); 178 179$events = intercept { 180 $ictx = tool(); 181}; 182 183$ictx->release; 184 185like( 186 exception { intercept { die 'foo' } }, 187 qr/foo/, 188 "Exception was propagated" 189); 190 191$events = intercept { 192 test2_stack()->top->set_no_ending(0); 193 ok(1); 194}; 195 196is(@$events, 2, "2 events"); 197ok($events->[0]->isa('Test2::Event::Pass'), "got a pass"); 198ok($events->[1]->isa('Test2::Event::Plan'), "finalize was called"); 199 200$events = intercept { 201 test2_stack()->top->set_no_ending(0); 202 ok(1); 203 done_testing; 204}; 205 206is(@$events, 2, "2 events"); 207ok($events->[0]->isa('Test2::Event::Pass'), "got a pass"); 208ok($events->[1]->isa('Test2::Event::Plan'), "finalize was called (only 1 plan)"); 209 210done_testing; 211