### ----------------------------------------------------
### -- Pyrint
### -- Licenses: Artistic-2.0
### -- Authors: 
### -- File: t/01.rakutest
### ----------------------------------------------------
use Test;
use Pyrint;

lives-ok { &pyrint }, "function exported ok";

lives-ok { pyrint }, "no arg call ok";
lives-ok { pyrint "one" }, "one arg call ok";
lives-ok { pyrint "one", "two" }, "two args call ok";
lives-ok { pyrint ("one", "two") }, "one arg as list call ok";

lives-ok { pyrint "val", sep => "-" }, "call with sep ok";
lives-ok { pyrint "one", end => "\t" }, "call with end ok";
lives-ok { pyrint "one", sep => "z", end => ".\n" }, "call with sep and end ok";

is-deeply pyrint("something"), True, "returns True on success";

sub capture-pyrint(Code $block) {
    my Str $out;
    with class :: { method print($s) { $out = $s } }.new -> $*OUT {
        $block.()
    }
    $out
}

is capture-pyrint({ pyrint }), "\n", "no arg default";
is capture-pyrint({ pyrint end => "s" }), "s", "no arg with end";
is capture-pyrint({ pyrint sep => "--" }), "\n", "no arg with sep";
is capture-pyrint({ pyrint sep => "^", end => "v\n"}), "v\n", "no arg with sep and end";

is capture-pyrint({ pyrint "hi" }), "hi\n", "one arg default";
is capture-pyrint({ pyrint "yes", sep => ", " }), "yes\n", "one arg with sep";
is capture-pyrint({ pyrint "here", end => "after" }), "hereafter", "one arg with end";
is capture-pyrint({ pyrint "nottin", sep => "z", end => "gham\n"}), "nottingham\n", "one arg with sep and end";

my ($sep, $end) = " -- ", "<<\n\n";
for "hi".comb, "yes".comb, "long stuff".comb, ("w" x 100).comb {
    my @args = @$_;
    is capture-pyrint({ pyrint |@args}), @args.join(" ") ~ "\n", "@args.elems() args default";
    is capture-pyrint({ pyrint |@args, :$sep }), @args.join($sep) ~ "\n", "@args.elems() args with sep";
    is capture-pyrint({ pyrint |@args, :$end }), @args.join(" ") ~ $end, "@args.elems() args with end";
    is capture-pyrint({ pyrint |@args, :$sep, :$end }), @args.join($sep) ~ $end, "@args.elems() args with sep and end";
}

is capture-pyrint({ pyrint ("one", "two") }), "(one two)\n", "one arg as list isn't slurped";

is capture-pyrint({ pyrint class :: { method Str { "STR" }; method gist { "GIST" } }.new }),
   "GIST\n",
   ".gist is called, not .Str";

throws-like { pyrint ned => "<" },
            Exception,
            "nonbare pair stuff, i.e., other kwargs are erroneous to pass",
            message => /:i 'Unexpected named argument' /;

done-testing;


