### ----------------------------------------------------
### -- Needle::Compile
### -- Licenses: Artistic-2.0
### -- Authors: Elizabeth Mattijsen
### -- File: t/03-file.rakutest
### ----------------------------------------------------
BEGIN %*ENV<RAKU_TEST_DIE_ON_FAIL> = 1;
use Test;
use Needle::Compile;

my $io   := $*PROGRAM.sibling("always");
my $path := $io.relative;

# Helper sub for simple tests
sub match-tests(&needle) {
    isa-ok &needle, Callable, "is it a Callable";

    is-deeply needle("foo"), True,  'matched "foo"';
    is-deeply needle("bar"), False, 'did not match "bar"';
}

# Adding a .type method to an object
my role Type {
    has $.type;
    method raku() { callsame() ~ " but Type('$!type')" }
}

# Ways to check "file"
my @file =
  "file:$path",
  $path but Type<file>,
  "file" => $path,
;

# Ways to check "not file"
my @notfile =
  "!file:$path",
  "not" => "file:$path",
  "not" => "file" => $path,
;

plan 3 + @file + @notfile;

# Test reading patterns from STDIN
{
    my $*IN = $io.open;
    match-tests compile-needle("file" => '-');
    $*IN.close;
}


for @file {
    subtest "file: $_.raku()" => {
        match-tests compile-needle($_);
    }
}

for @notfile {
    subtest "not file: $_.raku()" => {
        plan 3;

        my &needle := compile-needle($_);
        isa-ok &needle, Callable, "is it a Callable";

        is-deeply needle("foo"), False, 'did not match "foo"';
        is-deeply needle("bar"), True,  'matched "bar"';
    }
}

# vim: expandtab shiftwidth=4


