### ----------------------------------------------------
### -- Test::Async
### -- Licenses: Artistic-2.0
### -- Authors: Vadim Belman <vrurg@cpan.org>
### -- File: t/180-dying-subtest.rakutest
### ----------------------------------------------------
use v6;
use Test::Async;

plan 2;

my @default-args = '-I' ~ $?FILE.IO.parent(2), '-MTest::Async';

is-run q:to/CODE/,
           class X::DieFast is Exception { method message { "right now" } };
           subtest "Dying" => { X::DieFast.new.throw }
           CODE
   "exception in an immediate subtest is caught and reported",
   :compiler-args(@default-args),
   :timeout(10),
   :exitcode(1),
   :out( /^^ \s+ '# ===SORRY!=== ' .* ' X::DieFast' .*?
   ^^ \s+ '#' \s+ 'right now'
   /),
   :async,
   :err("");

is-run q:to/CODE/,
           plan 2, :parallel;
           class X::MyTest is Exception {
               method message { "This one would be The Bad Guy" }
           }
           subtest "Throw in subtest" => {
               plan 10, :parallel;

               for ^10 -> $n {
                   test-flunks if $n == 7;
                   subtest "pass $n" => {
                       pass "test";
                       X::MyTest.new.throw if $n == 7;
                   }
               }
           }

           subtest "Not throwing" => {
               plan 2;
               pass "good 1";
               pass "good 2";
           }
           CODE
       "exception in a parallel subtest is caught and reported",
       :compiler-args(@default-args),
       :exitcode(1),
       :out(
            /^^ \s+ '# ===SORRY!=== ' .* ' X::MyTest' .*?
             ^^ \s+ '#' \s+ 'This one would be The Bad Guy'
            /
       ),
       :err("");

done-testing;


