t/bind_data.t

filter: Statementstmt 100% 25 / 25 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 100% 31 / 31 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/XML/BindData.pm ↑ Summary t/each_off_by_one.t »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Test::Warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use_ok 'XML::BindData';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 my $tests = [
10 [
11 '<foo tmpl-bind="foo"/>', { foo => 'bar' },
12 '<foo>bar</foo>', 'Single binding'
13 ],
14  
15 [
16 '<foo tmpl-bind="foo"/>', { foo => undef },
17 '<foo></foo>', 'Single binding, undefined - get empty string'
18 ],
19  
20 [
21 '<foo tmpl-bind="foo" tmpl-default="baz"/>', { foo => 'bar' },
22 '<foo>bar</foo>', 'Single binding, with default (unused)'
23 ],
24  
25 [
26 '<foo tmpl-bind="foo" tmpl-default="baz"/>', {},
27 '<foo>baz</foo>', 'Single binding, with default (used)'
28 ],
29  
30 [
31 '<foo><multi tmpl-each="foo"/></foo>', { foo => [(1) x 3] },
32 '<foo><multi></multi><multi></multi><multi></multi></foo>',
33 'Each over multiple entities'
34 ],
35  
36 [
37 '<foo><bar tmpl-each="bar" tmpl-bind="this"/></foo>',
38 { bar => [ 1, 2, 3 ] },
39 '<foo><bar>1</bar><bar>2</bar><bar>3</bar></foo>',
40 'This binds inside each'
41 ],
42  
43 [
44 '<foo tmpl-attr-map="bar:baz"/>', { baz => 'quux' },
45 '<foo bar="quux"></foo>', 'Attribute binds'
46 ],
47  
48 [
49 '<foo tmpl-attr-map="a:aaa,b:bbb"/>', { aaa => 1, bbb => 2 },
50 '<foo a="1" b="2"></foo>', 'Multiple attributes bind'
51 ],
52  
53 [
54 '<foo tmpl-attr-map="a:aaa,b:bbb,c:ccc"/>', { aaa => 1, bbb => 2 },
55 '<foo a="1" b="2"></foo>', 'Multiple attributes bind with undefined values'
56 ],
57  
58 [
59 '<foo tmpl-attr-map="a:aaa,b:bbb,d:ddd" tmpl-attr-defaults="a:zzz,c:123,d:456"/>', { aaa => 1, bbb => 2 },
60 '<foo a="1" b="2" c="123" d="456"></foo>', 'Attribute defaults'
61 ],
62  
63 [
64 '<foo tmpl-attr-map="a:aaa,b:bbb" tmpl-attr-defaults="a:zzz,c:0"/>', { aaa => 0, bbb => 2 },
65 '<foo a="0" b="2" c="0"></foo>', 'Attribute defaults with false values'
66 ],
67  
68 [
69 '<foo tmpl-attr-map="a:aaa,b:bbb,c:ccc,d:ddd" tmpl-attr-defaults="a:zzz,c:0"/>', { aaa => undef, bbb => 2 },
70 '<foo a="zzz" b="2" c="0"></foo>', 'Attribute defaults with undefined values'
71 ],
72  
73 [
74 '<foo tmpl-attr-map="a:aaa,b:bbb" tmpl-attr-defaults="a:zzz,c:foo\,bar,d:foo\:bar"/>', { aaa => 0, bbb => 2 },
75 '<foo a="0" b="2" c="foo,bar" d="foo:bar"></foo>', 'Attribute defaults with commas and colons'
76 ],
77  
78 [
79 '<foo><bar tmpl-each="bar"><baz tmpl-each="this" tmpl-bind="this"/></bar></foo>',
80 {
81 bar => [
82 [ qw/ 1 2 / ],
83 [ qw/ 3 4 / ],
84 ]
85 },
86 '<foo><bar><baz>1</baz><baz>2</baz></bar><bar><baz>3</baz><baz>4</baz></bar></foo>',
87 'Nested arrays'
88 ],
89  
90 [
91 '<foo><bar tmpl-each="bar"><id tmpl-bind="id"/></bar></foo>',
92 {
93 bar => [
94 { id => 1 },
95 { id => 2 },
96 ]
97 },
98 '<foo><bar><id>1</id></bar><bar><id>2</id></bar></foo>',
99 'Each uses individual items as context'
100 ],
101  
102 [
103 '<foo tmpl-bind="foo.bar.baz"/>',
104 { foo => { bar => { baz => 1 } } },
105 '<foo>1</foo>', 'Dot notation references nested hashes'
106 ],
107  
108 [
109 '<foo><bar tmpl-if="show">bar</bar></foo>', { show => 1 },
110 '<foo><bar>bar</bar></foo>', 'If true keeps node'
111 ],
112  
113 [
114 '<foo><bar tmpl-if="show">bar</bar></foo>', { show => undef },
115 '<foo></foo>', 'If false removes node'
116 ],
117  
118 [
119 '<foo><bar tmpl-if="show">bar</bar></foo>', { },
120 '<foo></foo>', 'If false removes node'
121 ],
122  
123 [
124 '<foo><bar tmpl-if="!show">bar</bar></foo>', { show => undef },
125 '<foo><bar>bar</bar></foo>', 'If not false keeps node'
126 ],
127  
128 [
129 '<foo><bar tmpl-if="!show">bar</bar></foo>', { show => 1 },
130 '<foo></foo>', 'If not true removes node'
131 ],
132  
133 [
134 '<foo><bar tmpl-if="show" tmpl-each="bar" tmpl-bind="this"/></foo>',
135 {
136 show => 1,
137 bar => [ 1, 2, 3 ],
138 },
139 '<foo><bar>1</bar><bar>2</bar><bar>3</bar></foo>',
140 'If + each + this all on one tag works'
141 ],
142  
143 [
144 '<foo><bar tmpl-each="list" tmpl-bind="this"/><baz tmpl-if="num" tmpl-bind="num" /></foo>',
145 {
146 list => [ 1, 2 ],
147 num => 3,
148 },
149 '<foo><bar>1</bar><bar>2</bar><baz>3</baz></foo>',
150 'XML order retained when using varying types',
151 ],
152 [
153 '<foo><bar tmpl-each="list" tmpl-bind="this"/><baz tmpl-if="num" tmpl-bind="num" /></foo>',
154 {
155 list => [ 0, 1 ],
156 num => 0,
157 },
158 '<foo><bar>0</bar><bar>1</bar><baz>0</baz></foo>',
159 'Number 0 is a valid value and passes if conditionals',
160 ],
161 [
162 '<foo><bar tmpl-bind="num" /><!-- preserve comment --></foo>',{ num => 4 },
163 '<foo><bar>4</bar><!-- preserve comment --></foo>',
164 'Preserve comments'
165 ]
166 ];
167  
168 1 foreach my $t (@$tests) {
169 25 my ($source_xml, $data, $output, $msg) = @$t;
170 25 is(XML::BindData->bind($source_xml, $data), $output, $msg);
171 }
172  
173 1 done_testing;
« blib/lib/XML/BindData.pm ↑ Summary t/each_off_by_one.t »