### ----------------------------------------------------
### -- App::RaCoCo
### -- Licenses: Artistic-2.0
### -- Authors: Mikhail Khorkov
### -- File: t/03-precomp-lookup.rakutest
### ----------------------------------------------------
use Test;
use lib 'lib';
use lib 't/lib';
use App::Racoco::Coverable::Precomp::PrecompLookup;
use App::Racoco::ModuleNames;
use App::Racoco::Paths;
use App::Racoco::X;
use Fixture;
use TestResources;
use TestHelper;

plan 4;

my ($sources, $paths, $file-name, $lookup);
sub setup($file, $lib-name) {
	plan $*plan;
	TestResources::prepare($*subtest);
	$sources = TestResources::exam-directory;
	$paths = Paths.from(lib => $sources.add($lib-name));
	$file-name = $file;
	$lookup = PrecompLookup.new(:$paths, compiler-id => -> {Fixture::compiler-id});
}

'01-lookup-in-precomp'.&test(:4plan, {
	setup('Module.rakumod', 'lib');
	my $expected = $paths.lib-precomp-path
		.add(Fixture::compiler-id())
    .add(file-precomp-path(lib => $paths.lib, path => $file-name));
  my $actual = $lookup.lookup(:$file-name);
  isa-ok $actual, IO::Path, 'lookup is io';
  ok $actual.e, 'lookup exists';
  ok $actual.Str.starts-with($paths.lib), 'lookup under lib';
  is $actual, $expected, 'lookup ok';
});

'02-lookup-not-exist'.&test(:1plan, {
	setup('NotExists.rakumod', 'lib');
	nok $lookup.lookup(:$file-name).DEFINITE, 'cannot lookup precomp file';
});

'03-lookup-two-precomp-dir'.&test(:1plan, {
	setup('Module.rakumod', 'lib');
	my $expected = $paths.lib-precomp-path
		.add(Fixture::compiler-id())
    .add(file-precomp-path(lib => $paths.lib, path => $file-name));
  my $actual = $lookup.lookup(:$file-name);
  is $actual, $expected, 'lookup ok';
});

'04-lookup-our-precomp'.&test(:1plan, {
	setup('Module2.rakumod', 'lib');
	my $expected = $paths.our-precomp-path
			.add(file-precomp-path(lib => $paths.lib, path => $file-name));
  is $lookup.lookup(:$file-name).IO, $expected, 'lookup in our precomp';
});


