### ----------------------------------------------------
### -- highlighter
### -- Licenses: Artistic-2.0
### -- Authors: Elizabeth Mattijsen
### -- File: t/01-basic.rakutest
### ----------------------------------------------------
BEGIN %*ENV<RAKU_TEST_DIE_ON_FAIL> = 1;

use Test;
use highlighter;

plan 81;

my $haystack = "foo bar baz bar zop";

my sub test-hi($needle, $capture, $result, $description) is test-assertion {
    is highlighter($haystack, $needle, |$capture),
      $result,
      $description;

    is highlighter($haystack, ($needle, "frobnicate"), |$capture),
      $result,
      "$description (list first)";

    is highlighter($haystack, ("frobnicate", $needle), |$capture),
      $result,
      "$description (list last)";
}

test-hi "bar", \("<em>", "</em>", :type<words>),
  "foo <em>bar</em> baz <em>bar</em> zop",
  'did "bar" get highlighted as a word';

test-hi "bar" but Type<words>, \("<em>", "</em>"),
  "foo <em>bar</em> baz <em>bar</em> zop",
  'did "bar"<words> get highlighted as a word';

test-hi "BAR", \("<em>", "</em>", :type<words>, :ignorecase),
  "foo <em>bar</em> baz <em>bar</em> zop",
  'did "BAR" get highlighted as a word case-insensitively';

test-hi "ar", \("<em>", "</em>", :type<words>),
  $haystack,
  'did "ar" NOT get highlighted as a word';

test-hi "ba", \("<em>", "</em>", :type<contains>),
  "foo <em>ba</em>r <em>ba</em>z <em>ba</em>r zop",
  'did "ba" get highlighted as a string';

test-hi "BA", \("<em>", "</em>", :type<contains>, :ignorecase),
  "foo <em>ba</em>r <em>ba</em>z <em>ba</em>r zop",
  'did "BA" get highlighted as a string case-insensitively';

test-hi "zz", \("<em>", "</em>", :type<words>),
  $haystack,
  'did "zz" NOT get highlighted as a string';

test-hi "foo", \("<em>", "</em>", :type<starts-with>),
  "<em>foo</em> bar baz bar zop",
  'did "foo" get highlighted at beginning';

test-hi "FOO", \("<em>","</em>",:type<starts-with>,:ignorecase),
  "<em>foo</em> bar baz bar zop",
  'did "FOO" get highlighted at beginning case-insensitively';

test-hi "zz", \("<em>", "</em>", :type<starts-with>),
  $haystack,
  'did "zz" NOT get highlighted at beginning';

test-hi "zop", \("<em>", "</em>", :type<ends-with>),
  "foo bar baz bar <em>zop</em>",
  'did "foo" get highlighted at end';

test-hi "ZOP", \("<em>","</em>",:type<ends-with>,:ignorecase),
  "foo bar baz bar <em>zop</em>",
  'did "ZOP" get highlighted at end case-insensitively';

test-hi "zz", \("<em>", "</em>", :type<ends-with>),
  $haystack,
  'did "zz" NOT get highlighted at end';

test-hi / << b.r >> /, \("<em>", "</em>"),
  "foo <em>bar</em> baz <em>bar</em> zop",
  'did "b.r" get highlighted as a match';

test-hi rx:i/ B.R /, \("<em>", "</em>"),
  "foo <em>bar</em> baz <em>bar</em> zop",
  'did "B.R" get highlighted as a match case-insensitively';

test-hi / zz /, \("<em>", "</em>"),
  $haystack,
  'did "zz" NOT get highlighted as a match';

for
  \("<em>", "</em>"),
  \("<em>", "</em>", :smartcase),
  \("<em>", "</em>", :ignorecase),
  \("<em>", "</em>", :smartmark),
  \("<em>", "</em>", :ignoremark)
-> $capture {
    test-hi 'ar', $capture,
      "foo b<em>ar</em> baz b<em>ar</em> zop",
      "did 'ar' get highlighted $capture.keys()";
}

test-hi / <( ba \w )> /, \("<em>", "</em>"),
  'foo <em>bar</em> <em>baz</em> <em>bar</em> zop',
  'did we get correct highlighting with capture markers';

test-hi / <( ba \w )> /, \("", "\n", :only),
  "bar\nbaz\nbar\n",
  'did we get correct highlighting with :only';

for *.contains("foo") -> $needle {
    is highlighter($haystack, $needle),
      $haystack,
      'did we get no highlighting at all';

    is highlighter($haystack, ($needle, { False })),
      $haystack,
      'did we get no highlighting at all (list first)';

    is highlighter($haystack, ({ False }, $needle)),
      $haystack,
      'did we get no highlighting at all (list last)';
}

test-hi $haystack, \("<em>", "</em>", :type<equal>),
  '<em>' ~ $haystack ~ '</em>',
  'did haystack get highlighted completely';

test-hi $haystack.uc, \("<em>", "</em>", :type<equal>, :ignorecase),
  '<em>' ~ $haystack ~ '</em>',
  'did haystack.uc get highlighted completely';

test-hi $haystack,
  \("<em>", "</em>", :type<equal>, :summary-if-larger-than(10)),
  '<em>foo bar...</em>',
  'did haystack get highlighted completely';

# vim: expandtab shiftwidth=4


