### ----------------------------------------------------
### -- L10N
### -- Licenses: Artistic-2.0
### -- Authors: Elizabeth Mattijsen
### -- File: xt/basic.rakutest
### ----------------------------------------------------
use v6.e.PREVIEW;
use Test;

#-------------------------------------------------------------------------------
# Stolen from the Trap ecosystem module
class Trap {
    has str @!text;

    method print(*@_ --> True) {
        @!text.push: @_.join
    }
    method say(*@_ --> True) {              # older versions of Raku
        @!text.push: @_.join ~ "\n"
    }
    method printf($format, *@_ --> True) {  # older versions of Raku
        @!text.push: sprintf($format, @_)
    }

    method text(--> str) { @!text.join }

    multi method CALL-ME(Trap:U: $one is raw) {
        $one = self.new
    }
    multi method CALL-ME(Trap:U: $one is raw, $two is raw) {
        $one = $two = self.new
    }
}
#-------------------------------------------------------------------------------
# Capture all output from running a given AST

sub output($ast) {
    my $merged = Trap(my $*OUT, my $*ERR);
    $ast.EVAL;
    $merged.text
}

#-------------------------------------------------------------------------------

my $REPO = $*REPO;
my $localizations := $*PROGRAM.parent.parent.add("localizations");
my @localizations  = $localizations.dir.map({ .basename if .d }).sort;
plan 4 * @localizations;

# Where we keep the source code to be tested
my $sources := $*PROGRAM.sibling("sources");

# The basic source file, human written
my $basic := $sources.add("basic").slurp;

# The RakuAST of the basic source file
my $basic-ast := $basic.AST;

# Expected output when running the basic source file
my $basic-out := output($basic-ast);

for @localizations {

    # Make sure we get the localization module locally
    my $*REPO := CompUnit::Repository::FileSystem.new(
      prefix    => $localizations.add($_).add("lib"),
      next-repo => $REPO
    );

    my $expected := $sources.add("basic.$_").slurp;
    is $basic-ast.DEPARSE($_), $expected, "$_: did the deparsing match";

    my $ast := $expected.AST($_);
    is-deeply $ast, $basic-ast,
      "$_: did the source create the same AST with (:slang)";

    $ast := "use L10N::$_;\n$expected".AST;
    # need to ignore the "use" statement itself
    is-deeply $ast.statements[1..*], $basic-ast.statements,
      "$_: did the source create the same AST with 'use'";

    is output($ast), $basic-out, "$_: did it generate the same output";
}

# vim: expandtab shiftwidth=4


