# 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') };

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

# 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 $varspace = {};

my ( $err, $result, $wwobj );
foreach my $test ( @$tests ){
    $varspace = $test->{vspace}
      if ref( $test->{vspace} ) eq 'HASH';
    if( $wwobj = Text::Wigwam->new( @{$test->{params}}, varspace => $varspace ) ){
        if( $test->{terror} ){ # An error occurred when not expected
            fail( $test->{testid} );
            warn "\nExpected (but did not get) an error - $test->{result}\n"; 
            next;
        }
    }
    else{
        if( !$test->{terror} ){ # No error occurred when expected
            fail( $test->{testid} );
            warn "\nUnexpected error - $Text::Wigwam::ERROR\n"; 
            next;
        }
        $result = $Text::Wigwam::ERROR if $test->{terror};
    }
    ($err, $result) = $wwobj->execute();
    if( $err ){
        fail( $test->{testid} );
        warn "\nUnexpected parse error - $err";
    }
    elsif( $result =~ /$test->{result}/ ){
        pass( $test->{testid} );
    }
    else{
        fail( $test->{testid} );
        warn "Expect: $test->{result}\n"; 
        warn "Actual: $result\n\n";
    }
#    ok( $result =~ /$test->{result}/, $test->{testid} );
}

sub func_tests{
    return [
        {
            testid => 'IF',
            params => [ text => '[!!#IF 0 "Fail" #IF 1 "Pass"!!]' ],
            result => '^Pass$',
            vspace => {},
        },
        {
            testid => 'ELSE',
            params => [ text => '[!!#IF 0 "Fail" #ELSE "Pass1" #IF 1 "Pass2" #ELSE "Fail"!!]', ],
            result => '^Pass1Pass2$',
            vspace => {},
        },
        {
            testid => 'IF/ELSE (Nested)',
            params => [ text => '[!! #IF 1 { "Pass" #IF 0 "Fail1" } #ELSE "Fail2" !!]', ],
            result => '^Pass$',
            vspace => {},
        },
        {
            testid => 'ELSIF',
            params => [ text => '[!! #IF 0 "Fail" #ELSIF 0 "Fail" !!][!! #IF 0 "Fail" #ELSIF 1 "Pass1" !!][!! #IF 1 "Pass2" #ELSIF 0 "Fail" !!][!! #IF 1 "Pass3" #Elsif 1 "Fail" !!]' ],
            result => '^Pass1Pass2Pass3$',
            vspace => {},
        },
        {
            testid => 'ELSE',
            params => [ text => '[!! #IF 0 "Fail" #ELSE "Pass1" !!][!! #IF 1 "Pass2" #ELSE "Fail" !!][!! #IF 1 "Pass3" #ELSIF 1 "Fail" #ELSE "Fail" !!][!! #IF 1 "Pass4" #Elsif 0 "Fail" #ELSE "Fail"!!][!! #IF 0 "Fail" #ELSIF 0 "Fail" #ELSE "Pass5" !!][!! #IF 0 "Fail" #ELSIF 1 "Pass6" #ELSE "Fail" !!][!! #IF 1 "Pass7" #ELSIF 0 "Fail" #ELSE "Fail"!!][!! #IF 1 "Pass8" #ELSIF 1 "Fail" #ELSE "Fail"!!]' ],
            result => '^Pass1Pass2Pass3Pass4Pass5Pass6Pass7Pass8$',
            vspace => {},
        },
        {
            testid => 'IF/ELSIF/ELSE (Nested)',
            params => [ text => '[!! #IF 0 { "Fail" #IF 1 "Fail" } #ELSE "Pass1" !!][!! #IF 1 { "Pass2" #IF 0 "Fail" } #ELSE "Fail" !!][!! #IF 1 { "Pass3" #IF 0 "Fail" } #ELSIF 1 #IF 0 "Fail" #ELSE "Fail" !!][!! #IF 1 { "Pass4" #IF 0 "Fail" } #Elsif 0 "Fail" #ELSE "Fail" !!][!! #IF 0 #IF 1 "Fail" #ELSIF 0 #IF 0 "Fail" #ELSE "Pass5" !!][!! #IF 0 #IF 1 "Fail" #ELSIF 1 "Pass6" #ELSE "Fail" !!]' ],
            result => '^Pass1Pass2Pass3Pass4Pass5Pass6$',
            vspace => {},
        },
    ]
}

