### ----------------------------------------------------
### -- Test::Stream
### -- Licenses: Artistic-2.0
### -- Authors: 
### -- File: t/300-predicates.rakutest
### ----------------------------------------------------
### -- Chunk 2 of 2
 class  => Test::Stream::Event::Skip,
#                attributes => ${
#                    reason => 'because',
#                    count  => 1,
#                },
#            }
#        );
#    };
#
#    my-subtest 'skip( 2, reason )', {
#        skip( 'none', 2 );
#        test-event-stream(
#            $listener,
#            ${
#                class  => Test::Stream::Event::Skip,
#                attributes => ${
#                    reason => 'none',
#                    count  => 2,
#                },
#            }
#        );
#    };

    my-subtest 'diag(...)', {
        diag('some text');
        test-event-stream(
            $listener,
            ${
                class  => Test::Stream::Event::Diag,
                attributes => ${
                    message => 'some text',
                },
            }
        );
    };

    my-subtest 'subtest( subname, &block ) with passing subtest', {
        my-is(
            subtest(
                'subname',
                sub {
                    ok(True),
                }
            ),
            True,
            'subtest returns Bool'
        );
        test-event-stream(
            $listener,
            ${
                class  => Test::Stream::Event::Suite::Start,
                attributes => ${
                    name => 'subname',
                },
            },
            ${
                class  => Test::Stream::Event::Test,
                attributes => ${
                    passed     => True,
                    name       => (Str),
                    diagnostic => (Test::Stream::Diagnostic)
                },
            },
            ${
                class  => Test::Stream::Event::Suite::End,
                attributes => ${
                    name => 'subname',
                },
            },
        );
    };

    my-subtest 'subtest( subname, &block ) with failing subtest', {
        my-is(
            subtest(
                'subname',
                sub {
                    ok(False),
                }
            ),
            False,
            'subtest returns Bool'
        );
        test-event-stream(
            $listener,
            ${
                class  => Test::Stream::Event::Suite::Start,
                attributes => ${
                    name => 'subname',
                },
            },
            ${
                class  => Test::Stream::Event::Test,
                attributes => ${
                    passed     => False,
                    name       => (Str),
                    diagnostic => (Test::Stream::Diagnostic)
                },
            },
            ${
                class  => Test::Stream::Event::Suite::End,
                attributes => ${
                    name => 'subname',
                },
            },
        );
    };

    my-subtest 'nested subtests', {
        my-is(
            subtest(
                'subname',
                sub {
                    subtest( 'nested', sub { ok(False) } );
                    ok(True),
                }
            ),
            False,
            'subtest returns Bool'
        );
        test-event-stream(
            $listener,
            ${
                class  => Test::Stream::Event::Suite::Start,
                attributes => ${
                    name => 'subname',
                },
            },
            ${
                class  => Test::Stream::Event::Suite::Start,
                attributes => ${
                    name => 'nested',
                },
            },
            ${
                class  => Test::Stream::Event::Test,
                attributes => ${
                    passed     => False,
                    name       => (Str),
                    diagnostic => (Test::Stream::Diagnostic)
                },
            },
            ${
                class  => Test::Stream::Event::Suite::End,
                attributes => ${
                    name => 'nested',
                },
            },
            ${
                class  => Test::Stream::Event::Test,
                attributes => ${
                    passed     => True,
                    name       => (Str),
                    diagnostic => (Test::Stream::Diagnostic)
                },
            },
            ${
                class  => Test::Stream::Event::Suite::End,
                attributes => ${
                    name => 'subname',
                },
            },
        );
    };

    my-subtest 'subtest that runs no tests returns false', {
        my-is(
            subtest( 'no tests', sub { } ), False,
            'subtest returns Bool'
        );
        test-event-stream(
            $listener,
            ${
                class  => Test::Stream::Event::Suite::Start,
                attributes => ${
                    name => 'no tests',
                },
            },
            ${
                class  => Test::Stream::Event::Suite::End,
                attributes => ${
                    name => 'no tests',
                },
            },
        );
    };

    my-subtest 'subtest with wrong plan returns false', {
        my-is(
            subtest(
                'plan is wrong',
                sub {
                    plan(2);
                    ok(True);
                }
            ),
            False,
            'subtest returns Bool'
        );
        test-event-stream(
            $listener,
            ${
                class  => Test::Stream::Event::Suite::Start,
                attributes => ${
                    name => 'plan is wrong',
                },
            },
            ${
                class  => Test::Stream::Event::Plan,
                attributes => ${
                    planned => 2,
                },
            },
            ${
                class  => Test::Stream::Event::Test,
                attributes => ${
                    passed     => True,
                    name       => (Str),
                    diagnostic => (Test::Stream::Diagnostic)
                },
            },
            ${
                class  => Test::Stream::Event::Suite::End,
                attributes => ${
                    name => 'plan is wrong',
                },
            },
        );
    };

    my-subtest 'nested subtest with wrong plan returns false', {
        my-is(
            subtest(
                'plan is correct',
                sub {
                    plan(2);
                    subtest(
                        'plan is wrong',
                        sub {
                            plan(2);
                            ok(True);
                        }
                    );
                    ok(True);
                }
            ),
            False,
            'subtest returns Bool'
        );
        test-event-stream(
            $listener,
            ${
                class  => Test::Stream::Event::Suite::Start,
                attributes => ${
                    name => 'plan is correct',
                },
            },
            ${
                class  => Test::Stream::Event::Plan,
                attributes => ${
                    planned => 2,
                },
            },
            ${
                class  => Test::Stream::Event::Suite::Start,
                attributes => ${
                    name => 'plan is wrong',
                },
            },
            ${
                class  => Test::Stream::Event::Plan,
                attributes => ${
                    planned => 2,
                },
            },
            ${
                class  => Test::Stream::Event::Test,
                attributes => ${
                    passed     => True,
                    name       => (Str),
                    diagnostic => (Test::Stream::Diagnostic)
                },
            },
            ${
                class  => Test::Stream::Event::Suite::End,
                attributes => ${
                    name => 'plan is wrong',
                },
            },
            ${
                class  => Test::Stream::Event::Test,
                attributes => ${
                    passed     => True,
                    name       => (Str),
                    diagnostic => (Test::Stream::Diagnostic)
                },
            },
            ${
                class  => Test::Stream::Event::Suite::End,
                attributes => ${
                    name => 'plan is correct',
                },
            },
        );
    };
}

{
    my $listener = listener();

    my-subtest 'skip-all()', {
        skip-all();
        test-event-stream(
            $listener,
            ${
                class      => Test::Stream::Event::Suite::Start,
                attributes => ${ name => '300-predicates.rakutest' },
            },
            ${
                class  => Test::Stream::Event::SkipAll,
                attributes => ${
                    reason => (Str),
                },
            }
        );
    };
}

{
    my $listener = listener();

    my-subtest 'skip-all()', {
        skip-all('why not');
        test-event-stream(
            $listener,
            ${
                class      => Test::Stream::Event::Suite::Start,
                attributes => ${ name => '300-predicates.rakutest' },
            },
            ${
                class  => Test::Stream::Event::SkipAll,
                attributes => ${
                    reason => 'why not',
                },
            }
        );
    };
}

Test::Predicates::clear-instance-violently-for-test-stream-tests;

my-done-testing;

sub listener (--> Test::Stream::Recorder:D) {
    Test::Stream::Hub.clear-instance-violently-for-test-stream-tests;
    Test::Predicates::clear-instance-violently-for-test-stream-tests;
    my $hub = Test::Stream::Hub.instance;
    my $listener = Test::Stream::Recorder.new;
    $hub.add-listener($listener);

    return $listener;
}

# vim: expandtab shiftwidth=4


