File: 12-open_mode.t

package info (click to toggle)
libhtml-template-perl 2.97-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 740 kB
  • sloc: perl: 2,572; makefile: 8
file content (68 lines) | stat: -rw-r--r-- 1,891 bytes parent folder | download | duplicates (6)
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
use strict;
use warnings;
use File::Temp qw(tempdir);
use Test::More ($] < 5.007001 ? (skip_all => 'open_mode needs at least perl 5.7.1') : (tests => 5));

use_ok('HTML::Template');
my $cache_dir = tempdir(CLEANUP => 1);

# test with raw encoding
my $tmpl = HTML::Template->new(
    path      => 'templates',
    filename  => 'utf8-test.tmpl',
    open_mode => '<:raw',
);
my $output = $tmpl->output;
is(sprintf('%vd', $output), "195.164.10", 'correct raw bytes');

# test with UTF-8 encoding
$tmpl = HTML::Template->new(
    path      => 'templates',
    filename  => 'utf8-test.tmpl',
    open_mode => '<:encoding(utf-8)',
);
$output = $tmpl->output;
chomp $output;
is($output, chr(228), 'correct UTF8 encoded character');

# same as before, this time we test file_cache
$tmpl = HTML::Template->new(
    path           => 'templates',
    filename       => 'utf8-test.tmpl',
    open_mode      => '<:encoding(utf-8)',
    cache          => 0,
    file_cache     => 1,
    file_cache_dir => $cache_dir,
);

# trigger cache storage:
$output = $tmpl->output;

# this time it will implicitly read from the cache
$tmpl = HTML::Template->new(
    path           => 'templates',
    filename       => 'utf8-test.tmpl',
    open_mode      => '<:encoding(utf-8)',
    cache          => 0,
    file_cache     => 1,
    file_cache_dir => $cache_dir,
);

$output = $tmpl->output;
chomp $output;

is($output, chr(228), 'correct UTF8 encoded character from cache');

# this time it will implicitly read from the cache w/out open_mode
# which means it won't be correct UTF8.
$tmpl = HTML::Template->new(
    path           => 'templates',
    filename       => 'utf8-test.tmpl',
    cache          => 0,
    file_cache     => 1,
    file_cache_dir => $cache_dir,
);

$output = $tmpl->output;
chomp $output;
is(sprintf('%vd', $output), "195.164", 'correct non-UTF8 bytes: different open_mode, no cache');