# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl 1.t'

#########################

use strict;
use Test::More;

my $tests = &func_tests;
plan tests => scalar @$tests;

BEGIN { use_ok('Text::Wigwam') };

my $engine = 'Fusion';

#########################

# Insert your test code below, the Test::More module is use()ed here so read
# its man page ( perldoc Test::More ) for help writing this test script.

my $args = {};

sub do_fail{
    my( $test, $result, $message ) = @_;
    warn "\n";
    fail( $test->{testid} );
    warn "*** $message ***";
    warn ( $test->{perror} ? "Expected run-time error: " : $test->{terror} ? "Expected template error: " : "Expected parsed results: " );
    warn $test->{result};
    warn "Actual template results: $result\n" if $result;
}

foreach my $test ( @$tests ){
    my ($perror, $result);
     # Keep the same args for each successive test until it's redefined
    $args = $test->{args}
      if ref( $test->{args} );
    if( my $wwobj = Text::Wigwam->new( @{$test->{params}} ) ){
          # No error was encountered.
        do_fail( $test, undef, "No template error was encountered when it was expected." ), next if( $test->{terror} );
        ( $perror, $result ) = $wwobj->execute( $args );
        if( $perror ){
             # A parse error was encountered
            if( !$test->{perror} ){ do_fail( $test, undef, "Encountered an unexpected parse error - $perror" ); }
            elsif( $perror =~ /$test->{result}/ ){ pass( $test->{testid} ); }
            else{ do_fail( $test, $perror, "The resulting parse error does not match the parse error that was expected - result: $perror" ); }
            next;
        }
        elsif( $test->{perror} ){
            do_fail( $test, undef, "No parse error was encountered when it was expected." );
            next;
        }
        elsif( $result =~ /$test->{result}/ ){ pass( $test->{testid} ); }
        else{ do_fail( $test, $result, "The results of the parsed template does not match what was expected." ); }
    }
    else{
        do_fail( $test, undef, "Unexpected template object creation error - $Text::Wigwam::ERROR" ), next if( !$test->{terror} );
        if( $Text::Wigwam::ERROR =~ /$test->{result}/ ){ pass( $test->{testid} ); }
        else{ do_fail( $test, $result, "The resulting template error does not match what was expected - $Text::Wigwam::ERROR." ); }
    }
}

sub func_tests{
    return [
        {
            testid => ' Null Filename',
            params => [ file => '', Restrict => { engine => $engine} ],
            terror => 1,
            result => '\bNull filename\b',
            args =>  {},
        },
        {
            testid => ' Whitespace Filename',
            params => [ file => ' ' , Restrict => { engine => $engine} ],
            terror => 1,
            result => '\bNull filename\b',
            args =>  {},
        },
        {
            testid => ' Nonexistent File',
            params => [ file => '.nonexistentfile.wehope' , Restrict => { engine => $engine}],
            terror => 1,
            result => '\bError opening\b',
            args =>  {},
        },
        {
            testid => ' Array Traversal',
            params => [ text => '[!! #params < articles > #args articles:0 !!]' , Restrict => { engine => $engine} ],
            result => '^blah blah blah$',
            args =>  {
                articles => [
                    "blah blah blah",
                    "yadda yadda yadda",
                ]
            },
        },
        {
            testid => ' Common Varspace',
            params => [ text => '[!! #params < articles > #args articles:1 !!]', Restrict => { engine => $engine} ],
            result => '^yadda yadda yadda$',
        },
#        {
#            testid => ' Private Varspace',
#            params => [ text => '[!!test!!]', Restrict => { engine => $engine}, Settings => { local_varspace => 1 } ],
#            result => '^$',
#            args => { test => 'Fail' },
#        },
    ]
}

