BEGIN {
use_ok(
'Text::MicroTemplate::File'
);
};
do
{
my
$mtf
= Text::MicroTemplate::File->new(
include_path
=>
't/07-file'
,
);
is
$mtf
->render_file(
'hello.mt'
,
'John'
)->as_string,
"Hello, John\n"
;
is
$mtf
->render_file(
'include.mt'
,
'John'
)->as_string,
"head\nHello, John\n\nfoot\n"
;
is
$mtf
->render_file(
'package.mt'
)->as_string,
"main\n"
,
'default package'
;
is
$mtf
->render_file(
'wrapped.mt'
)->as_string,
"abc\nheader\ndef\n\nfooter\nghi\n"
,
'wrapper'
;
is
$mtf
->render_file(
'wrapped2.mt'
)->as_string,
"abc\nheader\ndef\nheader\nghi\n\nfooter\njkl\n\nfooter\nmno\n"
,
'wrapper'
;
is
$mtf
->render_file(
'wrapped3.mt'
)->as_string,
"abc\nheader\n\ndef\n\nfooter\nghi\n"
,
'wrapper'
;
is
$mtf
->render_file(
'wrapped_escape.mt'
)->as_string,
"abc\nheader\n<def>\n\nfooter\nghi\n"
,
'wrapper'
;
is
$mtf
->render_file(
'pod.mt'
)->as_string,
"0\n"
,
'pod'
;
};
do
{
my
$mtf
= Text::MicroTemplate::File->new(
include_path
=>
't/07-file'
,
);
is_deeply
$mtf
->include_path, [
't/07-file'
];
is
$mtf
->open_layer,
':utf8'
;
is
$mtf
->use_cache,
'0'
;
is
$mtf
->package_name,
'main'
;
};
do
{
my
$mtf
= Text::MicroTemplate::File->new();
my
$filepath
= abs_path(
't/07-file/hello.mt'
);
is
$mtf
->render_file(
$filepath
,
'John'
)->as_string,
"Hello, John\n"
;
};
do
{
my
$mtf
= Text::MicroTemplate::File->new(
include_path
=>
't/07-file'
,
package_name
=>
'foo'
,
);
is
$mtf
->render_file(
'hello.mt'
,
'John'
)->as_string,
"Hello, John\n"
;
is
$mtf
->render_file(
'include.mt'
,
'John'
)->as_string,
"head\nHello, John\n\nfoot\n"
;
is
$mtf
->render_file(
'package.mt'
)->as_string,
"foo\n"
,
'package'
;
};
do
{
my
$dir
= tempdir(
CLEANUP
=> 1);
my
$rewrite
=
sub
{
open
my
$fh
,
'>'
,
"$dir/t.mt"
or
die
"cannot open $dir/t.mt:$!"
;
print
$fh
@_
;
close
$fh
;
};
my
$mtf
=Text::MicroTemplate::File->new(
include_path
=>
$dir
,
use_cache
=> 1,
);
$rewrite
->(1);
is
$mtf
->render_file(
't.mt'
)->as_string,
'1'
,
'cache=1 read'
;
is
$mtf
->render_file(
't.mt'
)->as_string,
'1'
,
'cache=1 retry'
;
sleep
2;
$rewrite
->(2);
is
$mtf
->render_file(
't.mt'
)->as_string,
'2'
,
'cache=1 update'
;
is
$mtf
->render_file(
't.mt'
)->as_string,
'2'
,
'cache=1 update retry'
;
$mtf
->use_cache(2);
unlink
"$dir/t.mt"
or
die
"failed to remove $dir/t.mt:$!"
;
is
$mtf
->render_file(
't.mt'
)->as_string,
'2'
,
'cache=2 read after cached'
;
};
do
{
my
$mtf
= Text::MicroTemplate::File->new(
include_path
=>
't/07-file'
,
);
my
$output
=
$mtf
->render_file(
'konnitiwa.mt'
,
'世界'
)->as_string;
is
$output
,
'こんにちは、世界'
,
'utf8 flagged render ok'
;
ok utf8::is_utf8(
$output
),
'utf8 flagged output ok'
;
};
SKIP: {
skip
"Perl < 5.8.3 has bug around join.see perl583delta.pod"
, 2
if
$] < 5.008003;
no
utf8;
my
$mtf
= Text::MicroTemplate::File->new(
include_path
=>
't/07-file'
,
open_layer
=>
''
,
);
my
$output
=
$mtf
->render_file(
'konnitiwa.mt'
,
'世界'
)->as_string;
is
$output
,
'こんにちは、世界'
,
'utf8 bytes render ok'
;
ok !utf8::is_utf8(
$output
),
'utf8 bytes output ok'
;
};