/
render_file.t
117 lines (97 loc) · 2.65 KB
/
render_file.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
eval "use Data::Section::Simple";
plan skip_all => 'Data::Section::Simple required for this test!' if $@;
use Text::Haml;
use IO::File;
use URI::Escape ();
use File::Spec;
use File::Temp qw/ tempdir /;
my $file = IO::File->new;
# cache_dir
my $tempdir = tempdir(CLEANUP => 1);
# haml template
$file->open(File::Spec->catfile('t', 'render1.haml'), 'r') or die $!;
my $haml1_content = do { local $/; <$file> };
$file->close;
# create haml file for test
my $render_haml = File::Spec->catfile($tempdir, 'render.haml');
$file->open($render_haml, 'w') or die $!;
print $file $haml1_content;
$file->close;
my $haml = Text::Haml->new(
path => $tempdir,
cache_dir => $tempdir,
cache => 1,
);
my $output = $haml->render_file('render.haml', title => 'RENDER_FILE_TEST');
$file->open(File::Spec->catfile('t', 'render1.html'), 'r') or die $!;
my $expected1 = do { local $/; <$file> };
$file->close;
is($output, $expected1);
$output = $haml->render_file( File::Spec->rel2abs(File::Spec->catfile('t', 'render1.haml')), title => 'RENDER_FILE_TEST');
is($output, $expected1);
my $uri_escaped = URI::Escape::uri_escape($tempdir);
my $cache_dir = File::Spec->catdir($tempdir, $uri_escaped);
note($cache_dir);
ok(-d $cache_dir);
my $cache_path = File::Spec->catfile($cache_dir, 'render.haml.pl');
ok(-f $cache_path);
cmp_ok(-s $cache_path, '>', 1);
# overwrite haml file for test
$file->open($render_haml, 'w') or die $!;
print $file <<'EOF';
%p.title= $title
%p
TEST
EOF
$file->close;
# change mtime
my $mtime = time;
$mtime += 1000;
utime $mtime, $mtime, $render_haml;
$haml = Text::Haml->new(
path => [$tempdir],
cache_dir => $tempdir,
cache => 2, # using already exists cache
);
$output = $haml->render_file('render.haml', title => 'RENDER_FILE_TEST');
# same output test 1
is($output, $expected1);
# rendering same file again works just the same
$output = $haml->render_file('render.haml', title => 'RENDER_FILE_TEST');
is($output, $expected1);
is($haml->error,undef);
$haml = Text::Haml->new(
path => $tempdir,
cache_dir => $tempdir,
cache => 1,
);
$output = $haml->render_file('render.haml', title => 'RENDER_FILE_TEST');
is($output, <<'EOF');
<p class='title'>RENDER_FILE_TEST</p>
<p>
TEST
</p>
EOF
# Test virtual path
my $vpath = Data::Section::Simple->new()->get_data_section();
$haml = Text::Haml->new(
path => [$vpath],
cache_dir => $tempdir,
cache => 1,
);
$output = $haml->render_file('index.haml', title => 'RENDER_DATA_SECTION_TEST');
is($output, <<'EOF');
<p class='title'>RENDER_DATA_SECTION_TEST</p>
<p>TEST</p>
EOF
done_testing;
__DATA__
@@ index.haml
%p(class = 'title') #{$title}
%p TEST
@@ header.haml
%header hello