App-DuckPAN

 view release on metacpan or  search on metacpan

t/templates.t  view on Meta::CPAN

is $pm_file_content, <<EOT, 'template: generated file content is as expected';
package $package_name;
my \$id = '$ia_id';
EOT

#########################################
# File generation errors from templates #
#########################################

# overwriting file
throws_ok {
		$template_map{pm}->generate(\%vars);
	} qr/already exists/,
	'template: overwriting generated file throws error';

# input file not present
throws_ok { $template_map{no_input}->generate(\%vars) }
	qr/not found/,
	'template: non-existent template file throws error';


# create a directory with no write access
mkdir "$TEMPLATE_OUT/readonly", 0500;
throws_ok {
		$template_map{no_write_perm}->generate(\%vars)
	}
	qr/Error creating output/,
	'template: failure creating template output file throws error';

{
	my $got_warning;
	local $SIG{__WARN__} = sub { $got_warning = 1 };

	clear_output_directory();

	# all variables were passed to template; no warnings should be shown
	$template_map{pm}->generate(\%vars);
	ok !$got_warning, 'template: no warning when all variables are passed to template';

	clear_output_directory();

	# some or all variables were missing; show warnings
	$template_map{pm}->generate({});
	ok $got_warning, 'template: show warning when not all variables are passed to template';
}

######################################
# File generation from template sets #
######################################
my %generate_res;

clear_output_directory();
%generate_res = $template_set_map{required_and_optional}->generate(\%vars, [ $template_map{js} ]);

is_deeply [ sort @{$generate_res{created_files}} ],
	[ $pm_out_file, $js_out_file, $test_out_file ],
	'template set: return value has all the created files';

ok !$generate_res{error}, 'template set: no errors when succesfully generating files';

# verify all required files have been generated
ok -f $_, "template set: required file '$_' generated from template set"
	for ($pm_out_file, $test_out_file);

ok  -f $js_out_file,  "template set: selected optional file '$js_out_file' generated from template set";
ok !-f $css_out_file, "template set: unselected optional file '$css_out_file' not generated from template set";

#############################################
# File generation errors from template sets #
#############################################

clear_output_directory();
throws_ok {
		$template_set_map{required_and_optional}->generate(\%vars, [ $template_map{pm} ])
	}
	qr/Unknown template/,
	'template set: die when invalid templates passed in optional templates list';

# individual template errors while generating output
%generate_res = $template_set_map{errors}->generate(\%vars, [ $template_map{no_input} ]);

# required files are generated before optional files, thus we get the .pm file
# in the created files list. Feel free to change this behaviour and update the
# test if necessary.
is_deeply $generate_res{created_files}, [ $pm_out_file ],
	'template set: successfully generated file added to "created_files" list';

# the optional template failed to generate the output
ok scalar($generate_res{error} =~ /Template input file.*not found/),
	'template set: error message set for failed template';

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

clear_output_directory();

done_testing;



( run in 1.770 second using v1.01-cache-2.11-cpan-5a3173703d6 )