### ----------------------------------------------------
### -- Log::Dispatch
### -- Licenses: Artistic-2.0
### -- Authors: Vadim Belman
### -- File: t/010-generic.rakutest
### ----------------------------------------------------
use Test;
use Log::Dispatch;
use Log::Dispatch::Source;
use Log::Dispatch::Destination;

plan 2;

my @msg-obj;
my @msg-line;

my class LogDest does Log::Dispatch::Destination {
    method report(Log::Dispatch::Msg:D $message) {
        @msg-obj.push: $message;
        @msg-line.append: |$message.fmt-lines;
    }
}

my class LogSrc does Log::Dispatch::Source {
    has Str $.lsn;
    method log-source-name { $!lsn }
}

subtest "No Source Name" => {
    plan 14;
    @msg-obj = [];
    @msg-line = [];
    my $logger = Log::Dispatch.new;
    my $inst = LogSrc.new;

    nok $inst.attached, "log source is not attached yet";

    $logger.add: $inst;
    $logger.add: LogDest;

    ok $inst.attached, "log source is attached";

    $inst.log: "1";
    $inst.log: 2, :warning;
    $inst.log: "pi\n", 3.14, :alert;

    is +@msg-obj, 3, "all log events got through";
    is +@msg-line, 4, "log lines";

    $inst.log: "Bad boy", :debug; # Must not get through because destination max-level is NOTICE by default

    is +@msg-obj, 3, "too detailed log entry is skipped";

    $logger.shutdown;

    nok @msg-obj[0].source.defined, "no source name is set for a message object";

    isa-ok @msg-obj[0].msg, Str:D, "message type is Str";
    is @msg-obj[0].msg, "1", "message content 1";
    is @msg-obj[1].msg, "2", "message content 2";
    is @msg-obj[2].msg, "pi\n3.14", "message content 3";

    like @msg-line[0], /'[     INFO] 1' $/;
    like @msg-line[1], /'[  WARNING] 2' $/;
    like @msg-line[2], /'[    ALERT] pi' $/;
    like @msg-line[3], /'[    ALERT] 3.14' $/;
}

subtest "With Source Name" => {
    plan 7;
    @msg-obj = [];
    @msg-line = [];
    my $logger = Log::Dispatch.new;
    my $inst = LogSrc.new: :lsn<MySource>;
    $logger.add: $inst;
    $logger.add: LogDest;

    $inst.log: "1";
    $inst.log: 2, :warning;
    $inst.log: "pi\n", 3.14, :alert;

    is +@msg-obj, 3, "all log events got through";
    is +@msg-line, 4, "log lines";

    $logger.shutdown;

    is @msg-obj[0].source, "MySource", "a message object source";

    like @msg-line[0], /'[     INFO] [MySource] 1' $/;
    like @msg-line[1], /'[  WARNING] [MySource] 2' $/;
    like @msg-line[2], /'[    ALERT] [MySource] pi' $/;
    like @msg-line[3], /'[    ALERT] [MySource] 3.14' $/;
}

done-testing;


