### ----------------------------------------------------
### -- Test::Stream
### -- Licenses: Artistic-2.0
### -- Authors: 
### -- File: t/400-tap-formatter.rakutest
### ----------------------------------------------------
### -- Chunk 2 of 2
            event => Test::Stream::Event::SkipAll,
            args  => ${
                reason => 'feeling tired',
            },
            expect => ${
                output         => qq{    1..0 # Skipped: feeling tired\n},
                todo-output    => q{},
                failure-output => q{},
            },
        },
        ${
            event => 'suite-end',
            args  => ${
                name => 'will skip',
            },
            expect => ${
                output         => "ok 2 - will skip\n",
                todo-output    => q{},
                failure-output => q{},
            },
        },
        ${
            event => Test::Stream::Event::Test,
            args  => ${
                passed => True,
                name   => 'post-subtest',
            },
            expect => ${
                output         => "ok 3 - post-subtest\n",
                todo-output    => q{},
                failure-output => q{},
            },
        },
        ${
            event => 'suite-end',
            args  => ${
                name => $*PROGRAM-NAME,
            },
            expect => ${
                output         => "1..3\n",
                todo-output    => q{},
                failure-output => q{},
            },
        },
    ],
);

my-done-testing;

sub test-formatter ($name, @event-tests) {
    my %outputs = (
        output         => My::IO::String.new,
        todo-output    => My::IO::String.new,
        failure-output => My::IO::String.new,
    );
    my $tap = Test::Stream::Formatter::TAP12.new(|%outputs);
    my $hub = Test::Stream::Hub.new;
    $hub.add-listener($tap);

    $hub.set-context;
    LEAVE { $hub.release-context; }

    my-subtest $name, {
        for @event-tests -> $test {
            test-event-output(
                $hub,
                $test,
                %outputs,
            );
            $_.clear for %outputs.values;
        }
    };
}

sub test-event-output (
    Test::Stream::Hub:D $hub,
    $test,
    %outputs,
) {
    given $test<event> {
        when 'suite-start' {
            my-lives-ok(
                { $hub.start-suite( |$test<args> ) },
                'sending start-suite to formatter',
            );
        }
        when 'suite-end' {
            my-lives-ok(
                { $hub.end-suite( |$test<args> ) },
                'sending end-suite to formatter',
            );
        }
        default {
            my-lives-ok(
                { $hub.send-event( $test<event>.new( |$test<args> ) ) },
                "sending {$test<event>.^name} to formatter",
            );
        }
    };

    for < output todo-output failure-output > -> $h {
        compare-outputs( $h, %outputs, $test<expect> );
    }
}

sub compare-outputs (Str:D $key, %outputs, %expect) {
    if %expect{$key} ~~ Regex {
        my-like(
            %outputs{$key}, %expect{$key},
            "got expected $key"
        );
    }
    else {
        my-is(
            %outputs{$key}, %expect{$key},
            "got expected $key"
        );
    }
}

# vim: expandtab shiftwidth=4


