File: provider.t

package info (click to toggle)
libmojolicious-plugin-templatetoolkit-perl 0.006-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 204 kB
  • sloc: perl: 266; makefile: 2
file content (154 lines) | stat: -rw-r--r-- 5,891 bytes parent folder | download
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use Mojo::Base -strict;

use Test::More;
use Mojo::Util 'encode';
use Template;
use Template::Context;
use Template::Provider::Mojo;

use Mojolicious::Lite;

my $renderer = app->renderer;
my $provider = Template::Provider::Mojo->new({MOJO_RENDERER => $renderer});
my $context = Template::Context->new({LOAD_TEMPLATES => [$provider]});
my $tt = Template->new({CONTEXT => $context});

# Basic templates
my $output;
ok($tt->process(\'[% foo %] ☃', { foo => 'bar' }, \$output), 'processed inline template')
	or diag $tt->error;
is $output, 'bar ☃', 'right template output';

undef $output;
ok($tt->process('data_section.html.tt2', { foo => 'bar' }, \$output), 'processed data template')
	or diag $tt->error;
is $output, "bar ☃\n", 'right template output';

undef $output;
ok($tt->process('data_empty.html.tt2', { foo => 'bar' }, \$output), 'processed empty data template')
	or diag $tt->error;
is $output, '', 'right template output';

undef $output;
ok($tt->process('tmpl_file.html.tt2', { foo => 'bar' }, \$output), 'processed file template')
	or diag $tt->error;
is $output, "bar ☃\n", 'right template output';

# Includes from inline
undef $output;
ok($tt->process(\'[% INCLUDE data_section.html.tt2 %]baz', { foo => 'bar' }, \$output),
	'processed inline template with included data template') or diag $tt->error;
is $output, "bar ☃\nbaz", 'right template output';

undef $output;
ok($tt->process(\'[% INCLUDE data_empty.html.tt2 %]baz', { foo => 'bar' }, \$output),
	'processed inline template with included empty data template') or diag $tt->error;
is $output, "baz", 'right template output';

undef $output;
ok($tt->process(\'[% INCLUDE tmpl_file.html.tt2 %]baz', { foo => 'bar' }, \$output),
	'processed inline template with included file template') or diag $tt->error;
is $output, "bar ☃\nbaz", 'right template output';

# Includes from data section
undef $output;
ok($tt->process('data_include_data.html.tt2', { foo => 'bar' }, \$output),
	'processed data template with included data template') or diag $tt->error;
is $output, "bar ☃\nbaz\n", 'right template output';

undef $output;
ok($tt->process('data_include_empty.html.tt2', { foo => 'bar' }, \$output),
	'processed data template with included empty data template') or diag $tt->error;
is $output, "baz\n", 'right template output';

undef $output;
ok($tt->process('data_include_file.html.tt2', { foo => 'bar' }, \$output),
	'processed data template with included file template') or diag $tt->error;
is $output, "bar ☃\nbaz\n", 'right template output';

# Includes from file template
undef $output;
ok($tt->process('file_include_data.html.tt2', { foo => 'bar' }, \$output),
	'processed file template with included data template') or diag $tt->error;
is $output, "bar ☃\nbaz\n", 'right template output';

undef $output;
ok($tt->process('file_include_empty.html.tt2', { foo => 'bar' }, \$output),
	'processed file template with included empty data template') or diag $tt->error;
is $output, "baz\n", 'right template output';

undef $output;
ok($tt->process('file_include_file.html.tt2', { foo => 'bar' }, \$output),
	'processed file template with included file template') or diag $tt->error;
is $output, "bar ☃\nbaz\n", 'right template output';

my $inserted = encode $renderer->encoding, "[% foo %] ☃\n";

# Inserts from inline
undef $output;
ok($tt->process(\'[% INSERT data_section.html.tt2 %]baz', { foo => 'bar' }, \$output),
	'processed inline template with inserted data template') or diag $tt->error;
is $output, "${inserted}baz", 'right template output';

undef $output;
ok($tt->process(\'[% INSERT data_empty.html.tt2 %]baz', { foo => 'bar' }, \$output),
	'processed inline template with inserted empty data template') or diag $tt->error;
is $output, "baz", 'right template output';

undef $output;
ok($tt->process(\'[% INSERT tmpl_file.html.tt2 %]baz', { foo => 'bar' }, \$output),
	'processed inline template with inserted file template') or diag $tt->error;
is $output, "${inserted}baz", 'right template output';

# Inserts from data section
undef $output;
ok($tt->process('data_insert_data.html.tt2', { foo => 'bar' }, \$output),
	'processed data template with inserted data template') or diag $tt->error;
is $output, "${inserted}baz\n", 'right template output';

undef $output;
ok($tt->process('data_insert_empty.html.tt2', { foo => 'bar' }, \$output),
	'processed data template with inserted empty data template') or diag $tt->error;
is $output, "baz\n", 'right template output';

undef $output;
ok($tt->process('data_insert_file.html.tt2', { foo => 'bar' }, \$output),
	'processed data template with inserted file template') or diag $tt->error;
is $output, "${inserted}baz\n", 'right template output';

# Inserts from file template
undef $output;
ok($tt->process('file_insert_data.html.tt2', { foo => 'bar' }, \$output),
	'processed file template with inserted data template') or diag $tt->error;
is $output, "${inserted}baz\n", 'right template output';

undef $output;
ok($tt->process('file_insert_empty.html.tt2', { foo => 'bar' }, \$output),
	'processed file template with inserted empty data template') or diag $tt->error;
is $output, "baz\n", 'right template output';

undef $output;
ok($tt->process('file_insert_file.html.tt2', { foo => 'bar' }, \$output),
	'processed file template with inserted file template') or diag $tt->error;
is $output, "${inserted}baz\n", 'right template output';

done_testing;

__DATA__

@@ data_section.html.tt2
[% foo %] ☃
@@ data_empty.html.tt2
@@ data_include_data.html.tt2
[% INCLUDE data_section.html.tt2 %]baz
@@ data_include_empty.html.tt2
[% INCLUDE data_empty.html.tt2 %]baz
@@ data_include_file.html.tt2
[% INCLUDE tmpl_file.html.tt2 %]baz
@@ data_insert_data.html.tt2
[% INSERT data_section.html.tt2 %]baz
@@ data_insert_empty.html.tt2
[% INSERT data_empty.html.tt2 %]baz
@@ data_insert_file.html.tt2
[% INSERT tmpl_file.html.tt2 %]baz
__END__