#!/usr/bin/ruby

# Tests for the `Perl` class.

# Number eval
var perl_code = <<'EOT';
    my $x = 12;
    my $y = 16;

    sqrt($x**2 + $y**2);
EOT

var num = Perl.eval(perl_code);
assert_eq(num, 20)

# Array eval
var array = Perl.eval(<<'EOT');
    my @factorials;

    sub factorial {
        my ($n) = @_;
        return 1 if $n <= 1;
        push @factorials, $n * factorial($n - 1);
        $factorials[-1];
    }

    factorial(5);
    \@factorials;
EOT

assert_eq(array, [2, 6, 24, 120])

#=====================
# Transparent sub-call
#=====================

do {
    var f = %perl{
        sub {
            my ($x, $y) = @_;
            return ($x+1, $y+2);
        }
    }

    var (a,b) = f(5, 6)

    assert_eq(a, 5+1)
    assert_eq(b, 6+2)
}

#=================================
# Transparent sub-call with module
#=================================

do {
    require('Text::Wrap')

    var str = %perl{
        sub {
            local $Text::Wrap::columns = 50;
            Text::Wrap::wrap('', '', @_);
        }
    }(File(__FILE__).read.words...)

    assert(str.lines.all { .len <= 50 })
}

say "** Test passed!"
