### ----------------------------------------------------
### -- Template::Nest::Fast
### -- Licenses: ISC
### -- Authors: Andinus <andinus@nand.sh>
### -- File: t/02-die-on-bad-params.rakutest
### ----------------------------------------------------
use Test;
use Template::Nest::Fast;

plan 6;

my IO $template-dir = $*PROGRAM.IO.parent.add('templates');
my $nest = Template::Nest::Fast.new( :$template-dir, :die-on-bad-params );
my $nest-underscored = Template::Nest::Fast.new( :$template-dir, :die_on_bad_params );
my $nest-bad = Template::Nest::Fast.new( :$template-dir, die-on-bad-params => False );

my %simple-page = %(
    TEMPLATE => '00-simple-page',
    variable => 'Simple Variable',
    simple_component => %(
        TEMPLATE => '01-simple-component',
        variable => 'Simple Variable in Simple Component'
    )
);

my %simple-page-bad-params = %(
    TEMPLATE => '00-simple-page',
    variable => 'Simple Variable',
    simple_component => 'Simple Component',
    bad_param => 'Bad Param'
);

# Testing with a bad parameter but with same number of keys as
# %simple-page.
my %simple-page-bad-params-more = %(
    TEMPLATE => '00-simple-page',
    variable => 'Simple Variable',
    bad_param => 'Simple Component',
);

throws-like(
    { $nest-underscored.render(%simple-page-bad-params) }, Exception,
    "Dies on bad params by default on underscored option.",
    message => /"All variables in template hash must be valid if die-on-bad-params is True"/
);

throws-like(
    { $nest.render(%simple-page-bad-params) }, Exception,
    "Dies on bad params by default.",
    message => /"All variables in template hash must be valid if die-on-bad-params is True"/
);
throws-like(
    { $nest.render(%simple-page-bad-params-more) }, Exception,
    "Dies on bad params with same number of keys.",
    message => /"All variables in template hash must be valid if die-on-bad-params is True"/
);

lives-ok { $nest.render(%simple-page) }, "Lives on good params if die-on-bad-params is True.";

lives-ok { $nest-bad.render(%simple-page) }, "Lives on good params.";
lives-ok { $nest-bad.render(%simple-page-bad-params) }, "Lives on bad params if `die-on-bad-params' is False.";


