### ----------------------------------------------------
### -- String::Utils
### -- Licenses: Artistic-2.0
### -- Authors: Elizabeth Mattijsen
### -- File: t/01-basic.rakutest
### ----------------------------------------------------
use Test;
use String::Utils;

plan 111;

is after("foobar","foo"),       "bar", 'after(foo) ok?';
is "foobar".&after("foo"),      "bar", '.&after(foo) ok?';
is-deeply after("foobar","goo"), Nil,  'after(goo) ok?';

is around("foobarbaz","ob","rb"),   "foaz",      'around(foo,baz) ok?';
is "foobarbaz".&around("ob","rb"),  "foaz",      '.around(foo,baz) ok?';
is around("foobarbaz","goo","baz"), "foobarbaz", 'around(goo,baz) ok?';

is before("foobar","bar"),        "foo", 'before(bar) ok?';
is "foobar".&before("bar"),       "foo", '.&before(bar) ok?';
is-deeply before("foobar","baz"), Nil,   'before(goo) ok?';

is between("foobarbaz","foo","baz"),        "bar", 'between(foo,baz) ok?';
is "foobarbaz".&between("foo","baz"),       "bar", '.between(foo,baz) ok?';
is-deeply between("foobarbaz","goo","baz"), Nil,   'between(goo,baz) ok?';

is between-included("foobarbaz","oo","baz"), "oobarbaz",
  'between-included(o,baz) ok?';
is "foobarbaz".&between-included("oo","baz"), "oobarbaz",
  '.between-included(o,baz) ok?';
is-deeply between("foobarbaz","goo","baz"), Nil,
  'between-included(goo,baz) ok?';

is chomp-needle("foobarbaz","baz"),  "foobar", 'chomp-needle(baz) ok?';
is "foobarbaz".&chomp-needle("baz"), "foobar", '.chomp-needle(foo,baz) ok?';
is chomp-needle("foobarbaz","bar"),  "foobarbaz", 'chomp-needle(bar) ok?';

is (root <abcd abce abde>), "ab",   'root abcd abce abde ok?';
is (root <abcd abcd abcd>), "abcd", 'root abcd abcd abcd ok?';
is (root <foo bar baz>),    "",     'root foo bar baz ok?';
is (root <foo>),            "foo",  'root foo ok?';
is (root ()),               "",     'root ok?';

is (leaf <a.t b.t cafe.t>), ".t",   'leaf a.t b.t cafe.t ok?';
is (leaf <a.tt a.tt a.tt>), "a.tt", 'leaf a.tt a.tt a.tt ok?';
is (leaf <a.t b.c d.c>),    "",     'leaf a.t b.c d.c ok?';
is (leaf <foo>),            "foo",  'leaf foo ok?';
is (leaf ()),               "",     'leaf ok?';

is-deeply (is-sha1 "356A192B7913B04C54574D18C28D46E6395428AB"), True,
  'is SHA1 ok';
is-deeply (is-sha1 "356A192B7913B04C54574D18C28D46E6395428AG"), False,
  'is SHA1 not ok (1)';
is-deeply (is-sha1 "356A192B7913B04C54574D18C28D46E6395428A"), False,
  'is SHA1 not ok (2)';
is-deeply (is-sha1 "356A192B7913B04C54574D18C28D46E6395428ABC"), False,
  'is SHA1 not ok (3)';

is-deeply stem("foo.tar.gz"), 'foo', 'stem without number of extensions';
is-deeply stem("foo.tar.gz", 1), 'foo.tar', 'stem, 1 extension removed';
is-deeply stem("foo.tar.gz", *), 'foo', 'stem, all extensions removed';

is-deeply ngram("foobar", 3), <foo oob oba bar>,
  'did trigrams work';
is-deeply ngram("foobar", 4, :partial), <foob ooba obar bar ar r>,
  'did quadgrams work with partial';

is-deeply non-word("foobar"), False, 'all word characters';
is-deeply non-word("foo/bar"), True, 'at least one non-word character';

is-deeply letters("//foo:bar"), "foobar", 'did we get foobar in letters';
is-deeply letters("//fóö:bar"), "fóöbar", 'did we get fóöbar in letters';
is-deeply letters("//:"), "", 'did we get empty string in letters';
is-deeply letters(""), "", 'did we keep empty string in letters';

is-deeply has-marks("foo👩🏽‍💻bar"), False, 'no marks in f👩🏽‍💻oobar';
is-deeply has-marks("fóöbar"), True, 'marks in fóöbar';
is-deeply has-marks("óö"), True, 'marks in óö';
is-deeply has-marks(""), False, 'no marks in empty string';

is-deeply leading-whitespace("foo"), "", 'no leading whitespace';
is-deeply leading-whitespace(" \t foo"), " \t ", 'leading whitespace';
is-deeply leading-whitespace(" \t "), " \t ", 'all whitespace';

is-deeply trailing-whitespace("bar"), "", 'no trailing whitespace';
is-deeply trailing-whitespace("bar \t "), " \t ", 'trailing whitespace';
is-deeply trailing-whitespace(" \t "), " \t ", 'all whitespace';

is-deeply is-whitespace("\t \n"), True,  'whitespace only';
is-deeply is-whitespace("\ta\n"), False, 'non-whitespace';
is-deeply is-whitespace(""),      True,  'whitespace empty';

is-deeply is-uppercase("FOOBAR"), True,  'uppercase only';
is-deeply is-uppercase("FooBar"), False, 'non-uppercase';
is-deeply is-uppercase(""),       True,  'uppercase empty';

is-deeply is-lowercase("foobar"), True,  'lowercase only';
is-deeply is-lowercase("FooBar"), False, 'non-lowercase';
is-deeply is-lowercase(""),       True,  'lowercase empty';

is-deeply consists-of("aaabbcc", "abc"), True, 'consists-of ok';
is-deeply consists-of("aaadbcc", "abc"), False, 'consists-of NOT ok';
is-deeply consists-of("", "abc"), True, 'consists-of empty ok';

is-deeply all-same("aaaaaa"), "a", 'all-same ok';
is-deeply all-same("aaaaba"), Nil, 'all-same NOT ok';
is-deeply all-same(""), Nil, 'all-same empty NOT ok';

is-deeply nomark("élève"), "eleve", 'nomark with marks ok';
is-deeply nomark("eleve"), "eleve", 'nomark without marks ok';
is-deeply nomark(""),      "",      'nomark empty ok';

is-deeply paragraphs("a\n\nb\n\n\nc"),
  (0 => "a\n", 2 => "b\n\n", 5 => "c"),
  'paragraphs from string ok';

is-deeply paragraphs("\na\n\nb\n\n\nc", 1),
  (1 => "", 2 => "a\n", 4 => "b\n\n", 7 => "c"),
  'paragraphs from string ok with initial line number set';

my $text = "\na\n\nb\n\n\nc";
is-deeply paragraphs($text).map(*.value).join("\n"), $text,
  'can we round-trip with .join("\n")?';

my str $base = "a\tbb\tccc\tdddd\teeeee";
is-deeply expand-tab($base,0), "abbcccddddeeeee", '0 removes tabs';
is-deeply expand-tab($base,1), "a bb ccc dddd eeeee", '1 replaces tab w/space';
is-deeply expand-tab($base,4), "a   bb  ccc dddd    eeeee", '4 is ok';
is-deeply expand-tab("foo",4), "foo", 'no tabs ok';

my str $string = "foo bar baz";
is-deeply word-at($string, $_), Empty, "no word at $_" for -1, 12;
is-deeply word-at($string, $_), (0,3,0), "'foo' at $_" for 0 .. 3;
is-deeply word-at($string, $_), (4,3,1), "'bar' at $_" for 4 .. 7;
is-deeply word-at($string, $_), (8,3,2), "'baz' at $_" for 8 .. 11;

$string = " foo  bar baz";
is-deeply word-at($string, $_), Empty, "no word at $_" for -1, 0, 5, 14;
is-deeply word-at($string, $_), ( 1,3,0), "'foo' at $_" for 1 .. 4;
is-deeply word-at($string, $_), ( 6,3,1), "'bar' at $_" for 6 .. 9;
is-deeply word-at($string, $_), (10,3,2), "'baz' at $_" for 10 .. 13;

is-deeply abbrev(<yes no>), <n no no no y yes ye yes yes yes>.Map,
  "did yes/no abbreviate correctly";
is-deeply abbrev(<foo bar baz>), <bar bar baz baz f foo fo foo foo foo>.Map,
  "did foo/bar/baz abbreviate correctly";

is paragraphs($*PROGRAM.lines).elems, 33, 'reading from file lazily';

# vim: expandtab shiftwidth=4


