### ----------------------------------------------------
### -- _
### -- Licenses: Artistic-2.0
### -- Authors: Daniel Sockwell
### -- File: t/TextParagraphs/02-basic.rakutest
### ----------------------------------------------------
use _;
use Test;
plan 3;

sub tmp-file(:(:$_ = $?FILE.IO)) {
    "test-data.from.{.parent.basename}.{.basename}.ln$(callframe(1).line.Str).txt" }

subtest "Basic ¶ with bullets", {
    my $in = q:to/§eof/;
    Here are some available books, in alphabetical order:
      * Learning Raku, by brian d foy
      * Learning to program with Raku: First Steps, by JJ Merelo
      * Metagenomics, by Ken Youens-Clark
      * Parsing with Perl 6 Regexes and Grammars, by Moritz Lenz
      * Perl 6 at a Glance, by Andrew Shitov
      * Raku Fundamentals, by Moritz Lenz
      * Perl 6 Deep Dive, by Andrew Shitov
      * Think Perl 6: How to Think Like a Computer Scientist, by Laurent Rosenfeld.
    A list of books published or in progress is maintained in raku.org.
    §eof

    $in.lines[1..^*-1].join("\n").&paragraphs.&is-deeply:
        $in.lines[1..^*-1],                "Splits bullets into ¶¶";
    $in.&paragraphs.&is-deeply: $in.lines, "Splits bullets and headers into ¶¶";

    $in.&paragraphs(:limit(3)).&is-deeply:
        $in.lines[^3],                     ":limit works when spliting bullets and headers into ¶¶";
    $in.&paragraphs(:!chomp).&is-deeply:
        $in.lines(:!chomp),                ":!chomp keeps ␤ in bullets";
    $in.&paragraphs(:!chomp:limit(4)).&is-deeply:
        $in.lines(:!chomp)[^4],            ":!chomp and :limit can be used together with bullets";

    indir $*TMPDIR, {
        my $path = tmp-file;
        spurt $path, $in;
        $path.IO.&paragraphs.&is-deeply: $in.lines,      "Spliting bullets and headers works with a Path";
        $path.IO.open.&paragraphs.&is-deeply: $in.lines, "Spliting bullets and headers works with a Handle";
        $path.IO.&paragraphs(:!chomp).&is-deeply:
            $in.lines(:!chomp),                          "Keeping ␤ in bullets with :!chomp works with a Path";
        $path.IO.open.&paragraphs(:!chomp).&is-deeply:
            $in.lines(:!chomp),                          "Keeping ␤ in bullets with :!chomp works with a Handle";
        unlink $path; }
}




subtest "Basic ¶, seperated by blank lines", {
    my $in = q:to/§eof/;
        The sub form, which takes $*ARGFILES by default, will apply the lines method to the
        object that's the first argument, and pass it the rest of the arguments.

        The method will return a Seq each element of which is a line from the handle (that is
        chunks delineated by .nl-in). If the handle's .chomp attribute is set to True, then
        characters specified by .nl-in will be stripped from each line.

        Reads up to $limit lines, where $limit can be a non-negative Int, Inf, or Whatever
        (which is interpreted to mean Inf). If :$close is set to True, will close the handle
        when the file ends or $limit is reached. Subroutine form defaults to $*ARGFILES, if
        no handle is provided.

        Attempting to call this method when the handle is in binary mode will result in
        X::IO::BinaryMode exception being thrown.

        §eof

    $in.&paragraphs.&is-deeply: $in.trim.split("\n\n", :skip-empty), "Splits basic ¶";
    $in.&paragraphs(:!chomp).&is-deeply: $in.comb(/.*?\n\n/),        "Keeps ␤ with :!chomp";

    indir $*TMPDIR, {
        my $path = tmp-file;
        spurt $path, $in;
        $path.IO.&paragraphs.&is-deeply:
            $in.trim.split("\n\n", :skip-empty),  "Spliting basic ¶ works with a Path";
        $path.IO.&paragraphs(:limit(2)).&is-deeply:
            $in.trim.split("\n\n", :skip-empty)[^2],  ":limit(2) works when spliting basic ¶ with a Path";
        $path.IO.&paragraphs(:limit(3)).&is-deeply:
            $in.trim.split("\n\n", :skip-empty)[^3],  ":limit(3) works when spliting basic ¶ with a Path";
        $path.IO.open.&paragraphs.&is-deeply:
            $in.trim.split("\n\n", :skip-empty),  "Spliting basic ¶ works with a Handle";
        $path.IO.&paragraphs(:!chomp).&is-deeply:
            $in.comb(/.*?\n\n/),                  "Keeping ␤ in basic ¶ with :!chomp works with a Path";
        $path.IO.open.&paragraphs(:!chomp).&is-deeply:
            $in.comb(/.*?\n\n/),                  "Keeping ␤ in basic ¶ with :!chomp works with a Handle";
        unlink $path; }
}

subtest "Basic ¶, seperated by indentation", {
    my $in = q:to/§eof/;
            The sub form, which takes $*ARGFILES by default, will apply the lines method to the
        object that's the first argument, and pass it the rest of the arguments.
            The method will return a Seq each element of which is a line from the handle (that is
        chunks delineated by .nl-in). If the handle's .chomp attribute is set to True, then
        characters specified by .nl-in will be stripped from each line.
            Reads up to $limit lines, where $limit can be a non-negative Int, Inf, or Whatever
        (which is interpreted to mean Inf). If :$close is set to True, will close the handle
        when the file ends or $limit is reached. Subroutine form defaults to $*ARGFILES, if
        no handle is provided.
            Attempting to call this method when the handle is in binary mode will result in
        X::IO::BinaryMode exception being thrown.

        §eof

    $in.&paragraphs.&is-deeply:
         $in.split("    ", :skip-empty)».trim-trailing, "Splits basic indented ¶";
    $in.&paragraphs(:!chomp).&is-deeply:
         $in.split("    ", :skip-empty),                "Keeps ␤ with :!chomp in basic indented ¶";

    indir $*TMPDIR, {
        my $path = tmp-file;
        spurt $path, $in;
        $path.IO.&paragraphs.&is-deeply:
            $in.split("    ", :skip-empty)».trim-trailing, "Splits basic indented ¶ with a Path";
        $path.IO.&paragraphs(:limit(3)).&is-deeply:
            $in.split("    ", :skip-empty)».trim-trailing[^3], ":limit(3) works with basic indented ¶ with a Path";
        $path.IO.open.&paragraphs.&is-deeply:
            $in.split("    ", :skip-empty)».trim-trailing, "Splits basic indented ¶ with a Handle";
        $path.IO.&paragraphs(:!chomp).&is-deeply:
            $in.split("    ", :skip-empty),                "Keeps ␤ with :!chomp in basic indented ¶ with a Path";
        $path.IO.open.&paragraphs(:!chomp).&is-deeply:
            $in.split("    ", :skip-empty),                "Keeps ␤ with :!chomp in basic indented ¶ with a Handle";
        unlink $path; }
}


