Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to create a string with template values and unit test that the template is processing correctly

I am trying to create a test file that inputs template values into a string using the template toolkit but I don't know what check/tests to include to make sure the template toolkit is processing the string correctly. Here is my code:

#!/usr/bin/env perl

use lib ('./t/lib/');

use strict;
use warnings;

use Template;

use Test::More tests => 1;



# options/configuration for template
 my $config = {
     #PRE_PROCESS => 1, # means the templates processed can use the same global vars defined earlier
     #INTERPOLATE => 1,
     #EVAL_PERL => 1,
     RELATIVE => 1,
     OUTPUT_PATH => './out',

 };

my $template = Template->new($config);

# input string
my $text = "This is string number [%num%] ."; 

# template placeholder variables
my $vars = {
     num => "one",
 };


# processes imput string and inserts placeholder values 
my $create_temp = $template->process(\$text, $vars)
    || die "Template process failed: ", $template->error(), "\n";


#is(($template->process(\$text, $vars)), '1' , 'The template is processing correctly');

# If process method is executed successfully it should have a return value of 1
diag($template->process(\$text, $vars));

The diag function returns a value of 1, which from the documentation means that the string has been processed sucessfully, but I have been trying check what the stdout is so I can see the output string but I can get it to print. I have tried writing the stdout to a file from the terminal command but nothing appears in the file. I can write the stderr to a file though. I have also been trying different configuration for the template as seen in the code below. Is it not working because I am not running any tests, or am I using the Template Toolkit in the wrong way?

If there is any other required information to needed to answer this question just comment below.

like image 673
Paul Russell Avatar asked Feb 07 '23 06:02

Paul Russell


2 Answers

This answer assumes that the $template->process statement is really in your production code, and not in the unit test, and shows how to do it if you cannot just tell it to redirect the output into a variable, like Dave shows in his answer.

You can use Test::Output to check STDOUT.

use Test::Output;

stdout_is { $template->process( \$text, $vars ) } q{This is string number one .},
    q{Template generates the correct output};

An alternative could be Capture::Tiny and a two-step test.

use Capture::Tiny 'capture_stdout';

my $output = capture_stdout {
    ok $template->process( \$text, $vars ), q{Template processes successfully};
};
is $output, q{This is string number one .}, q{... and the output is correct};

Note that both solutions will eat the output, so it also doesn't mess with your terminal (it cannot mess with the TAP, as the Test::Harness only looks at STDOUT).

like image 164
simbabque Avatar answered Apr 09 '23 18:04

simbabque


Your main problem here is that process() sends its output to STDOUT and you're not capturing it. So you're not seeing the expanded output anythere.

The process() method takes an optional third argument which can take all sorts of useful values. You probably want to pass it a reference to an empty scalar variable, which then gets filled with the expanded template.

$template->process(\$text, $vars, \$output);
is($output, $expected_output);

But it's worth noting that the TT distribution includes Template::Test, which you'll probably find very useful.

like image 35
Dave Cross Avatar answered Apr 09 '23 18:04

Dave Cross