Help language development. Donate to The Perl Foundation

Lines::Containing zef:lizmat last updated on 2022-07-23

t/01-basic.rakutest
use Test;
use Lines::Containing;

plan 34;

ok ::('&' ~ $_) ~~ Callable, "is $_ exported?"
  for <lines-containing has-word>;

sub test(\c, $expected, $comment, $count = $expected.elems) is test-assertion {
    is-deeply lines-containing(|c), $expected, $comment;
    is lines-containing(|c, :count-only), $count, "$comment (count-only)";
}

test \("foo\nbar\nbaz", "a"), <bar baz>, 'with an a';
test \("foo\nbar\nbaz", "a", :type<contains>),
  <bar baz>,
  'with an a and :type<contains>';
test \("foo\nbar\nbaz", "bar", :type<words>),
  ("bar",),
  'with a bar and :type<words>';
test \("foo\nbar\nbaz", "ba", :type<starts-with>),
  <bar baz>,
  'with a ba and :type<starts-with>';
test \("foo\nbar\nbaz", "ar", :type<ends-with>),
  ("bar",),
  'with a ar and :type<ends-with>';

test \("foo\nbar\nbaz", / r $/, :kv), (1, "bar"), 'ending on r', 1;
test \("foo\nbar\nbaz", "O", :p, :i), (0 => "foo",), 'with an O';
test \(1..100, / 0 $/), (10, 20...100), 'numbers ending on 0';

test \({:a<foo>, :b<bar>, :c<baz>},
  *.starts-with("f"), :p),
  (:a<foo>,), 'starting with f';

test \("foo\nbar\nbaz", *.starts-with("b"), :k, :offset(1)),
  (2,3),
  'line numbers of lines starting with "b", from 1';

test \("foo\nbar\nbaz", *.starts-with("b"), :k, :max-count(1)),
  (1,),
  'line numbers of lines starting with "b", up to 1';

test \("foo\nbar\nbaz", 'zippo', :invert-match),
  <foo bar baz>,
  'All line looking for non-existing with inverted match';

test \("foo\nbar\nbaz", 'bar', :invert-match),
  <foo baz>,
  'All line looking for non-existing with inverted match';

test \("foo\nbar\nbaz", / \w+ /, :invert-match),
  (),
  'No lines looking for words with inverted match';

test \("foo\nbar\n\nbaz", *.uc),
  ('FOO', 'BAR', '', 'BAZ'),
  'use matching for adaptation';

test \("foo\nbar\n\nbaz", { .uc if .contains("a") }),
  ('foo', 'BAR', '', 'BAZ'),
  'use matching for adaptation not touching Empty result';

# vim: expandtab shiftwidth=4