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

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

plan 7;

# is-run has been checked to work in 005-bootstrap.t. Use it to diagnose
is-run q<pass "test passes">,
       "simple pass test, plan is output at the end",
       :compiler-args(@default-args),
       :out("ok 1 - test passes\n1..1\n"),
       :err('');

is-run q<plan 1; pass "test passes">,
       "simple pass test, plan is output at the start",
        :compiler-args(@default-args),
        :out("1..1\nok 1 - test passes\n")
        :err('');

is-run q<pass "test passes"; done-testing; pass "not allowed">,
       "no testing beyond done-testing",
        :compiler-args(@default-args),
        :out("ok 1 - test passes\n1..1\n"),
        :err(/:s^^A test tool \`pass\` called after done\-testing /);

is-run q<flunk "test fails">,
       "test fails, plan is at the end",
        :compiler-args(@default-args),
        :out(/^"not ok 1 - test fails\n# Failed test 'test fails'\n# at " .+? " line 1\n1..1\n# You failed 1 test of 1\n"/),
        :err(''),
        :exitcode(1);

is-run q<flunk "test 1"; flunk "test 2";>,
       "exit code reflects the number of tests failed",
        :compiler-args(@default-args),
        :err(''),
        :exitcode(2);

is-run q:to/TEST_CODE/,
            plan 4; plan 2;
            TEST_CODE
       "two plans result in bail out",
       :compiler-args(@default-args),
       :out(/^"1..4\n# FAILURE: It is too late to change plan at " .+? " at line 1\n# You planned 4 tests, but ran 0\n# You failed 4 tests of 0\n"/),
       :err(''),
       :exitcode(4); # Exit code is the number of initially planned tests

is-run q:to/TEST_CODE/,
            plan 1;
            pass "one pass";
            abort-testing;
            flunk "this never happens";
            TEST_CODE
       "abort-testing works at the top-level",
       :compiler-args(@default-args),
       :out("1..1\nok 1 - one pass\n"),
       :err(''),
       :exitcode(0); # Exit code is the number of initially planned tests

done-testing;


