forked from sympa-community/sympa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_templates.t
88 lines (77 loc) · 2.62 KB
/
parse_templates.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
# -*- indent-tabs-mode: nil; -*-
# vim:ft=perl:et:sw=4
# $Id$
use strict;
use warnings;
use Cwd qw();
use English qw(-no_match_vars);
use Test::More;
use XML::LibXML;
use Sympa::Template;
my $params = {
all_lists => {size => 2},
languages => {size => 2},
total_group => 2,
rows => 2,
reply_to_header => {value => 'all', other_email => 'xxx@xxx',},
list_request_date_epoch => 0,
tpl_lang => 'en',
current_subscriber => {lang => 'en'},
};
my @def_tt2 = _templates('default', '*.tt2 sympa.wsdl');
my @list_tt2 = _templates('default/create_list_templates', '*/*.tt2');
my @mail_tt2 = _templates('default/mail_tt2', '*.tt2 */*.tt2');
my @web_tt2 = _templates('default/web_tt2', '*.tt2 */*.tt2');
my @g_task = _templates('default/global_task_models', '*.task');
my @l_task = _templates('default/list_task_models', '*.task');
plan tests => scalar @def_tt2 +
scalar @list_tt2 +
scalar @mail_tt2 +
scalar @web_tt2 +
scalar @g_task +
scalar @l_task;
map { is _do_test('default', $_), '', $_ } @def_tt2;
map { is _do_test('default/create_list_templates', $_), '', $_ } @list_tt2;
map { is _do_test('default/mail_tt2', $_), '', $_ } @mail_tt2;
map { is _do_test('default/web_tt2', $_), '', $_ } @web_tt2;
map { is _do_test('default/global_task_models', $_, '[ ]'), '', $_ } @g_task;
map { is _do_test('default/list_task_models', $_, '[ ]'), '', $_ } @l_task;
sub _templates {
my $dir = shift;
my $pattern = shift || '*.tt2';
my $cwd = Cwd::getcwd();
chdir $dir or die $ERRNO;
my @files = glob $pattern;
chdir $cwd;
return @files;
}
sub _do_test {
my $dir = shift;
my $tpl = shift;
my $tags = shift;
if ($tags) {
open my $fh, '<', $dir . '/' . $tpl;
$tpl = do { local $RS; <$fh> };
close $fh;
$tpl = "[% TAGS $tags %]$tpl";
$tpl = [split /(?<=\n)/, $tpl];
} elsif ($tpl eq 'mhonarc-ressources.tt2') {
open my $fh, '<', $dir . '/' . $tpl;
$tpl = do { local $RS; <$fh> };
close $fh;
$tpl =~ s/\$(PAGENUM|NUMOFPAGES)\$/2/g;
$tpl = [split /(?<=\n)/, $tpl];
}
my $template = Sympa::Template->new('*', include_path => [$dir]);
my $scalar;
if ($template->parse($params, $tpl, \$scalar)) {
# Also check XML syntax.
if (not ref $tpl and $tpl eq 'sympa.wsdl') {
eval { XML::LibXML->load_xml(string => $scalar) }
or return $EVAL_ERROR;
}
return '';
} else {
return $template->{last_error};
}
}