#!perl

# Copyright (C) 2006-2008, The Perl Foundation.
# $Id: $

use strict;
use warnings;

use Test::More;
use File::Spec;

my $pipp_pbc = File::Spec->catfile( '..', '..', 'languages', 'pipp', 'pipp.pbc' );
plan skip_all => "Need to first run make in languages/pipp" if not -e $pipp_pbc;

plan tests => 14;

use_ok('Parrot::Embed') or exit;

my $module = 'Parrot::Interpreter';
my $interp = $module->new();
ok( $interp, 'new() should return a valid interpreter' );
isa_ok( $interp, $module );

my $result = eval { $interp->load_file($pipp_pbc) };
my $except = $@;
ok( $result, '... returning true if it could load the file' );
is( $except, '', '... throwing no exeption if so' );

my $pipp_x = $interp->find_global( 'Pipp' );
isa_ok( $pipp_x, 'Parrot::PMC' );
#can_ok($pipp_x, 'invoke');
#???

my $pipp = $interp->find_global( 'pipp', 'Pipp' );
isa_ok( $pipp, 'Parrot::PMC' );
can_ok($pipp, 'invoke');


my $code = <<'END_CODE';
<?php
function add($a, $b) {
	return $a+$b;
}
?>
END_CODE

# compile some PHP code
{
	my $pmc = $pipp->invoke( 'PS', $code );
	ok( $pmc, 'invoke() should return a PMC, given that signature' );
	is( $pmc->get_string(), 1, 'string returned in the PMC should be true?' );
}


# invoke a built-in php function
{
	my $pmc = $pipp->invoke( 'PS', 'strlen', 'some string' );
	ok( $pmc, 'invoke() should return a PMC, given that signature' );
	is( $pmc->get_string(), 11, 'value returned in the PMC' );
}

# invoke a php function
{
	my $pmc = $pipp->invoke( 'PS', 'add', 23, 19 );
	ok( $pmc, 'invoke() should return a PMC, given that signature' );
	is( $pmc->get_string(), 42, 'value returned in the PMC' );
}
