### ----------------------------------------------------
### -- highlighter
### -- Licenses: Artistic-2.0
### -- Authors: Elizabeth Mattijsen
### -- File: t/04-matches.rakutest
### ----------------------------------------------------
use Test;
use highlighter;

plan 75;

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

my sub test-ma($needle, $capture, \result, $description) is test-assertion {
    is-deeply matches($haystack, $needle, |$capture).List,
      result,
      $description;

    is-deeply matches($haystack, ($needle, "frobnicate"), |$capture).List,
      result,
      "$description (list first)";

    is-deeply matches($haystack, ("frobnicate", $needle), |$capture).List,
      result,
      "$description (list last)";
}

test-ma "bar", \(:type<words>),
  <bar bar>,
  'did "bar" get matched multiple times';

test-ma "bar" but Type<words>, \(),
  <bar bar>,
  'did "bar"<words> get matched multiple times';

test-ma "BAR", \(:type<words>, :ignorecase),
  <bar bar>,
  'did "BAR" get matched word case-insensitively';

test-ma "ar", \(:type<words>),
  (),
  'did "ar" NOT get matched as a word';

test-ma "ba", \(:type<contains>),
  <ba ba ba>,
  'did "ba" get matched as a string';

test-ma "BA", \(:type<contains>, :ignorecase),
  <ba ba ba>,
  'did "BA" get matched as a string case-insensitively';

test-ma "zz", \(:type<words>),
  (),
  'did "zz" NOT get matched as a string';

for
  \(:type<starts-with>),
  \(:type<starts-with>, :smartcase),
  \(:type<starts-with>, :ignorecase),
  \(:type<starts-with>, :smartmark),
  \(:type<starts-with>, :ignoremark)
-> $capture {
    test-ma "foo", $capture,
      ("foo",),
      "did 'foo' get matched at beginning $capture.keys()";
}

test-ma "FOO", \(:type<starts-with>, :ignorecase),
  ("foo",),
  'did "FOO" get matched at beginning case-insensitively';

test-ma "zz", \(:type<starts-with>),
  (),
  'did "zz" NOT get matched at beginning';

test-ma "zop", \(:type<ends-with>),
  ("zop",),
  'did "zop" get matched at end';

test-ma "ZOP", \(:type<ends-with>, :ignorecase),
  ("zop",),
  'did "ZOP" get matched at end case-insensitively';

test-ma "zz", \(:type<ends-with>),
  (),
  'did "zz" NOT get matched at end';

test-ma / << b.r >> /, \(),
  <bar bar>,
  'did "b.r" get matched as a match';

test-ma rx:i/ B.R /, \(),
  <bar bar>,
  'did "B.R" get matched as a match case-insensitively';

test-ma / zz /, \(),
  (),
  'did "zz" NOT get matched as a match';

test-ma 'ar', \(),
  <ar ar>,
  'did "ar" get matched';

test-ma *.contains("foo"), \(),
  (),
  'did we get no highlighting at all';

test-ma / <( ba \w )> /, \(),
  <bar baz bar>,
  'did we get correct highlighting with capture markers';

test-ma $haystack, \(:type<equal>),
  ($haystack,),
  'was haystack found';

test-ma $haystack.uc, \(:type<equal>, :ignorecase),
  ($haystack,),
  'was haystack found with ignorecase';

# vim: expandtab shiftwidth=4


