1#!perl
2
3use strict ;
4use lib qw(t) ;
5use common ;
6
7use File::Slurp ;
8use Data::Dumper ;
9
10# these dirs must be in order to the deepest for rmdir to work properly
11
12my @tmpl_dirs = qw( templates templates/deeper templates/deeper/deepest ) ;
13
14my %tmpl_files = (
15
16	'templates/FOO.tmpl'	=> <<FOO,
17this loads bar <[%include BAR%]>
18FOO
19	'templates/deeper/BAR.tmpl'	=> <<BAR,
20{this should hide}
21BAR
22	'templates/deeper/deepest/BAR.tmpl'	=> <<BAR,
23[this should be hidden then revealed]
24BAR
25
26) ;
27
28my $tests = [
29	{
30		name	=> 'simple include',
31		skip	=> 0,
32		opts	=> {
33			templates => {
34				'foo'	=> 'bar',
35			}
36		},
37		data	=> {},
38		template => '[%INCLUDE foo%]',
39		expected => 'bar',
40	},
41	{
42		name	=> 'nested includes',
43		skip	=> 0,
44		opts	=> {
45			templates => {
46				foo	=> '[%include bar%]',
47				bar	=> 'quux',
48			},
49		},
50		data	=> {},
51		template => '[%INCLUDE foo%]',
52		expected => 'quux',
53	},
54	{
55		name	=> 'serial includes',
56		skip	=> 0,
57		opts	=> {
58			templates => {
59				foo	=> 'foo is here',
60				bar	=> 'bar is too',
61				quux	=> 'quux is on the drums',
62			},
63		},
64		data	=> {},
65		template => '[%INCLUDE foo%] [%INCLUDE bar%] [%INCLUDE quux%]',
66		expected => 'foo is here bar is too quux is on the drums',
67	},
68	{
69		name	=> 'missing include',
70		skip	=> 0,
71		data	=> {},
72		keep_obj => 1,
73		pretest	=> sub { $_[0]{obj}->delete_templates() },
74		error	=> qr/can't find/,
75	},
76	{
77		name	=> 'load include files',
78		skip	=> 0,
79		opts	=> {
80			search_dirs => [ qw(
81				templates
82				templates/deeper
83				templates/deeper/deepest
84			) ],
85		},
86		data	=> {},
87		template => '[%INCLUDE FOO%]',
88		expected => <<EXPECTED,
89this loads bar <{this should hide}
90>
91EXPECTED
92
93	},
94	{
95		name	=> 'use lower path',
96		skip	=> 0,
97		opts	=> {
98			include_paths => [ qw(
99				templates
100				templates/deeper/deepest
101			) ],
102		},
103		data	=> {},
104		expected => <<EXPECTED,
105this loads bar <[this should be hidden then revealed]
106>
107EXPECTED
108
109	},
110	{
111		name	=> 'delete covering file',
112		skip	=> 0,
113		opts	=> {
114			search_dirs => [ qw(
115				templates
116				templates/deeper
117				templates/deeper/deepest
118			) ],
119		},
120		pretest	=> sub { unlink 'templates/deeper/BAR.tmpl' },
121		posttest => sub { write_tmpl_files() },
122
123		data	=> {},
124		expected => <<EXPECTED,
125this loads bar <[this should be hidden then revealed]
126>
127EXPECTED
128
129	},
130] ;
131
132
133write_tmpl_files() ;
134
135template_tester( $tests ) ;
136
137#remove_tmpl_files() ;
138
139exit ;
140
141
142sub write_tmpl_files {
143
144	mkdir $_, 0755 for @tmpl_dirs ;
145
146	while( my( $file, $tmpl ) = each %tmpl_files ) {
147
148		write_file( $file, $tmpl ) ;
149	}
150}
151
152sub remove_tmpl_files {
153
154	unlink keys %tmpl_files ;
155	rmdir $_ for reverse @tmpl_dirs ;
156}
157