File: node_basics.t

package info (click to toggle)
libxml-easy-perl 0.011-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 520 kB
  • sloc: perl: 2,200; makefile: 3
file content (373 lines) | stat: -rw-r--r-- 12,057 bytes parent folder | download | duplicates (4)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
use warnings;
use strict;

BEGIN { unshift @INC, "./t/lib"; }
use Params::Classify qw(is_ref);
use t::DataSets (map { ("COUNT_$_", "foreach_$_") } qw(
	yes_name
	yes_attributes
	yes_content_twine
	yes_content
	yes_element
));
use t::ErrorCases (map { ("COUNT_$_", "test_$_") } qw(
	error_type_name
	error_attribute_name
	error_attributes
	error_content
	error_element
	error_content_item
));

use Test::More tests => 67 +
	9*COUNT_yes_content_twine + 2*COUNT_yes_name + 4*COUNT_yes_attributes +
	COUNT_error_content_item*12 +
	COUNT_error_type_name*4 + COUNT_error_attributes*5 +
	COUNT_error_content*2 + COUNT_error_element*6 +
	COUNT_error_attribute_name +
	4*36 + 4*COUNT_yes_content + 4*COUNT_yes_element +
	COUNT_error_content*4 + COUNT_error_element*4 +
	3*17;

BEGIN { $SIG{__WARN__} = sub { die "WARNING: $_[0]" }; }

BEGIN {
	use_ok "XML::Easy::NodeBasics", qw(
		xml_content_object xml_content_twine xml_element
		xml_c_content_object xml_c_content_twine
		xml_e_type_name
		xml_e_attributes xml_e_attribute
		xml_e_content_object xml_e_content_twine
		xml_c_equal xml_e_equal xml_c_unequal xml_e_unequal
	);
}

my($ca, $cb);
$cb = $ca = [""];
xml_element("foo", $cb);
ok $cb == $ca;
$cb = $ca = [""];
xml_content_twine($cb);
ok $cb == $ca;
$cb = $ca = [""];
xml_content_object($cb);
ok $cb == $ca;

my $c0 = xml_content_object("bop");
is_deeply xml_content_object("b", "op"), $c0;
is_deeply xml_content_object(["bop"]), $c0;
is_deeply xml_content_object("bo", "", "p"), $c0;
is_deeply xml_content_object(["bo"], [""], "p"), $c0;
is_deeply xml_content_object($c0), $c0;
is_deeply xml_content_object("", $c0, ""), $c0;

is_deeply xml_content_twine("bop"), ["bop"];
is_deeply xml_content_twine(["bop"]), ["bop"];
is_deeply xml_content_twine("b", "op"), ["bop"];
is_deeply xml_content_twine("bo", "", "p"), ["bop"];
is_deeply xml_content_twine(["bo"], [""], "p"), ["bop"];
is_deeply xml_content_twine($c0), ["bop"];
is_deeply xml_content_twine("", $c0, ""), ["bop"];

my $a0 = { bar=>"baz", quux=>"wibble" };
my $e0 = xml_element("foo", $a0, "bop");
is_deeply xml_element("foo", {bar=>"baz"}, "bop", {quux=>"wibble"}), $e0;
is_deeply xml_element("foo", {}, {quux=>"wibble"}, {bar=>"baz"}, "bop"), $e0;
is_deeply xml_element("foo", ["bop"], $a0), $e0;
is_deeply xml_element("foo", "b", "op", $a0), $e0;
is_deeply xml_element("foo", "bo", $a0, "", "p"), $e0;
is_deeply xml_element("foo", $a0, ["bo"], [""], "p"), $e0;
is_deeply xml_element("foo", $c0, $a0), $e0;
is_deeply xml_element("foo", "", $c0, $a0, ""), $e0;

my $c1 = xml_content_object("a", $e0, "b");
is_deeply xml_content_object(["a", $e0, "b"]), $c1;
is_deeply xml_content_object(["a"], $e0, "", "b"), $c1;
is_deeply xml_content_object("a", ["", $e0, ""], "b"), $c1;
is_deeply xml_content_object("a", xml_content_object($e0, "b")), $c1;

is_deeply xml_content_twine("a", $e0, "b"), ["a", $e0, "b"];
is_deeply xml_content_twine(["a", $e0, "b"]), ["a", $e0, "b"];
is_deeply xml_content_twine(["a"], $e0, "", "b"), ["a", $e0, "b"];
is_deeply xml_content_twine("a", ["", $e0, ""], "b"), ["a", $e0, "b"];
is_deeply xml_content_twine("a", xml_content_object($e0, "b")), ["a", $e0, "b"];

my $e1 = xml_element("bar", "a", $e0, "b");
is_deeply xml_element("bar", ["a", $e0, "b"], {}), $e1;
is_deeply xml_element("bar", ["a"], $e0, "", "b"), $e1;
is_deeply xml_element("bar", {}, "a", {}, ["", $e0, ""], "b"), $e1;
is_deeply xml_element("bar", "a", {}, xml_content_object($e0, "b")), $e1;

is_deeply xml_content_twine(), xml_content_twine("");
is_deeply xml_element("foo"), xml_element("foo", "");

foreach_yes_content_twine sub { my($twine) = @_;
	my $c = xml_content_object($twine);
	is ref($c), "XML::Easy::Content";
	is_deeply $c->twine, $twine;
	is_deeply xml_content_object(@$twine), $c;
	is_deeply xml_content_twine($twine), $twine;
	is_deeply xml_content_twine(@$twine), $twine;
};
foreach_yes_content_twine sub { my($twine) = @_;
	my $e = xml_element("foo", $twine);
	is ref($e), "XML::Easy::Element";
	is_deeply $e->content_twine, $twine;
	is_deeply $e->content_object->twine, $twine;
	is_deeply xml_element("foo", @$twine), $e;
};

test_error_content_item \&xml_content_object;
test_error_content_item \&xml_content_twine;
test_error_content_item sub {
	die "invalid XML data: invalid content item\n"
		if is_ref($_[0], "HASH");
	xml_element("foo", $_[0]);
};
test_error_content_item sub { xml_content_object("foo", $_[0]) };
test_error_content_item sub { xml_content_twine("foo", $_[0]) };
test_error_content_item sub {
	die "invalid XML data: invalid content item\n"
		if is_ref($_[0], "HASH");
	xml_element("foo", "foo", $_[0]);
};
test_error_content_item sub { xml_content_object($_[0], "foo") };
test_error_content_item sub { xml_content_twine($_[0], "foo") };
test_error_content_item sub {
	die "invalid XML data: invalid content item\n"
		if is_ref($_[0], "HASH");
	xml_element("foo", $_[0], "foo");
};

test_error_content_item sub { xml_content_object($_[0], []) };
test_error_content_item sub { xml_content_twine($_[0], []) };
test_error_content_item sub {
	die "invalid XML data: invalid content item\n"
		if is_ref($_[0], "HASH");
	xml_element("foo", $_[0], []);
};

foreach_yes_name sub { my($name) = @_;
	my $e = xml_element($name, {}, "bop");
	is ref($e), "XML::Easy::Element";
	is $e->type_name, $name;
};
foreach_yes_attributes sub { my($attr) = @_;
	my $e = xml_element("foo", $attr, "bop");
	is ref($e), "XML::Easy::Element";
	is_deeply $e->attributes, $attr;
	is $e->attribute("foo"), $attr->{foo};
	is $e->attribute("bar"), $attr->{bar};
};

test_error_type_name sub { xml_element($_[0], $c0) };
eval { xml_element("foo", { foo=>"bar", baz=>"quux" }, $c0, {foo=>"bar"}) };
is $@, "invalid XML data: duplicate attribute name\n";
test_error_attributes sub {
	die "invalid XML data: attribute hash isn't a hash\n"
		unless is_ref($_[0], "HASH");
	xml_element("foo", $_[0], $c0);
};
test_error_attributes sub {
	die "invalid XML data: attribute hash isn't a hash\n"
		unless is_ref($_[0], "HASH");
	xml_element("foo", { womble => "foo" }, $_[0], $c0);
};
test_error_attributes sub {
	die "invalid XML data: attribute hash isn't a hash\n"
		unless is_ref($_[0], "HASH");
	xml_element("foo", $_[0], { womble => "foo" }, $c0);
};

test_error_type_name sub { xml_element($_[0], undef) };
test_error_type_name sub { xml_element($_[0], { "foo\0bar" => {} }) };
test_error_type_name sub { xml_element($_[0], {foo=>"bar"}, {foo=>"bar"}) };
eval { xml_element("foo", {foo=>"bar"}, {foo=>"bar"}, undef) };
is $@, "invalid XML data: duplicate attribute name\n";
eval { xml_element("foo", {foo=>"bar"}, undef, {foo=>"bar"}) };
is $@, "invalid XML data: duplicate attribute name\n";
eval { xml_element("foo", undef, {foo=>"bar"}, {foo=>"bar"}) };
is $@, "invalid XML data: duplicate attribute name\n";
eval {
	xml_element("foo", undef, { "foo\0bar" => {} },
		{foo=>"bar"}, {foo=>"bar"});
};
is $@, "invalid XML data: duplicate attribute name\n";
test_error_attributes sub {
	die "invalid XML data: attribute hash isn't a hash\n"
		unless is_ref($_[0], "HASH");
	xml_element("foo", $_[0], undef);
};
test_error_attributes sub {
	die "invalid XML data: attribute hash isn't a hash\n"
		unless is_ref($_[0], "HASH");
	xml_element("foo", undef, $_[0]);
};

is_deeply xml_c_content_object($c0), $c0;
is_deeply xml_c_content_object(["bop"]), $c0;
is_deeply xml_c_content_object($c1), $c1;
is_deeply xml_c_content_object(["a", $e0, "b"]), $c1;

test_error_content \&xml_c_content_object;

is_deeply xml_c_content_twine($c0), ["bop"];
is_deeply xml_c_content_twine(["bop"]), ["bop"];
is_deeply xml_c_content_twine($c1), ["a", $e0, "b"];
is_deeply xml_c_content_twine(["a", $e0, "b"]), ["a", $e0, "b"];

test_error_content \&xml_c_content_twine;

is xml_e_type_name($e0), "foo";
is xml_e_type_name($e1), "bar";

test_error_element \&xml_e_type_name;

is_deeply xml_e_attributes($e0), { bar => "baz", quux => "wibble" };
is_deeply xml_e_attributes($e1), {};

test_error_element \&xml_e_attributes;

is xml_e_attribute($e0, "foo"), undef;
is xml_e_attribute($e0, "bar"), "baz";
is xml_e_attribute($e0, "quux"), "wibble";
is xml_e_attribute($e1, "foo"), undef;
is xml_e_attribute($e1, "bar"), undef;
is xml_e_attribute($e1, "quux"), undef;

test_error_element sub { xml_e_attribute($_[0], "foo") };
test_error_attribute_name sub { xml_e_attribute($e1, $_[0]) };

test_error_element sub { xml_e_attribute($_[0], undef) };

is_deeply xml_e_content_object($e0), $c0;
is_deeply xml_e_content_object($e1), $c1;

test_error_element \&xml_e_content_object;

is_deeply xml_e_content_twine($e0), ["bop"];
is_deeply xml_e_content_twine($e1), [ "a", $e0, "b" ];

test_error_element \&xml_e_content_twine;

foreach(
	[ $c0, $c0 ],
	[ $c0->twine, $c0->twine ],
	[ ["bop"], ["bop"] ],
	[ $c0, $c0->twine ],
	[ $c0, ["bop"] ],
	[ $c0->twine, ["bop"] ],
	[ $c1, $c1 ],
	[ $c1->twine, $c1->twine ],
	[ ["a",$e0,"b"], ["a",$e0,"b"] ],
	[ $c1, $c1->twine ],
	[ $c1, ["a",$e0,"b"] ],
	[ $c1->twine, ["a",$e0,"b"] ],
) {
	my($a, $b) = @$_;
	ok xml_c_equal($a, $b);
	ok xml_c_equal($b, $a);
	ok !xml_c_unequal($a, $b);
	ok !xml_c_unequal($b, $a);
}

foreach(
	[ ["xyz"], $c0 ],
	[ ["bop",$e0,""], $c0 ],
	[ ["xyz",$e0,"b"], $c1 ],
	[ ["a",$e1,"b"], $c1 ],
	[ ["a",$e0,"xyz"], $c1 ],
	[ ["a"], $c1 ],
	[ ["a",$e0,"b",$e0,"c"], $c1 ],
	[ $c0, $c1 ],
) {
	my($a, $b) = @$_;
	ok !xml_c_equal($a, $b);
	ok !xml_c_equal($b, $a);
	ok xml_c_unequal($a, $b);
	ok xml_c_unequal($b, $a);
}

foreach(
	[ $e0, $e0 ],
	[ $e0, xml_element("foo", $a0, "bop") ],
	[ $e1, $e1 ],
	[ $e1, xml_element("bar", "a", $e0, "b") ],
) {
	my($a, $b) = @$_;
	ok xml_e_equal($a, $b);
	ok xml_e_equal($b, $a);
	ok !xml_e_unequal($a, $b);
	ok !xml_e_unequal($b, $a);
}

foreach(
	[ $e0, xml_element("xyz", $a0, "bop") ],
	[ $e0, xml_element("foo", {}, "bop") ],
	[ $e0, xml_element("foo", { %$a0, bar=>"orinoco" }, "bop") ],
	[ $e0, xml_element("foo", { %$a0, quux=>"orinoco" }, "bop") ],
	[ $e0, xml_element("foo", { %$a0, womble=>"orinoco" }, "bop") ],
	[ $e0, xml_element("foo", { bar=>"baz" }, "bop") ],
	[ $e0, xml_element("foo", { quux=>"wibble" }, "bop") ],
	[ $e0, xml_element("foo", $a0, "xyz") ],
	[ $e1, xml_element("xyz", "a", $e0, "b") ],
	[ $e1, xml_element("bar", $a0, "a", $e0, "b") ],
	[ $e1, xml_element("bar", "xyz", $e0, "b") ],
	[ $e0, $e1 ],
) {
	my($a, $b) = @$_;
	ok !xml_e_equal($a, $b);
	ok !xml_e_equal($b, $a);
	ok xml_e_unequal($a, $b);
	ok xml_e_unequal($b, $a);
}

foreach_yes_content sub { my($c) = @_;
	eval { xml_c_equal($c, $c1) }; is $@, "";
	eval { xml_c_equal($c1, $c) }; is $@, "";
	eval { xml_c_unequal($c, $c1) }; is $@, "";
	eval { xml_c_unequal($c1, $c) }; is $@, "";
};
foreach_yes_element sub { my($e) = @_;
	eval { xml_e_equal($e, $e1) }; is $@, "";
	eval { xml_e_equal($e1, $e) }; is $@, "";
	eval { xml_e_unequal($e, $e1) }; is $@, "";
	eval { xml_e_unequal($e1, $e) }; is $@, "";
};

test_error_content sub { xml_c_equal($_[0], $c1) };
test_error_content sub { xml_c_equal($c1, $_[0]) };
test_error_content sub { xml_c_unequal($_[0], $c1) };
test_error_content sub { xml_c_unequal($c1, $_[0]) };
test_error_element sub { xml_e_equal($_[0], $e1) };
test_error_element sub { xml_e_equal($e1, $_[0]) };
test_error_element sub { xml_e_unequal($_[0], $e1) };
test_error_element sub { xml_e_unequal($e1, $_[0]) };

foreach(
	[qw(xml_content xml_content_twine)],
	[qw(xml_c_content xml_c_content_twine)],
	[qw(xml_e_content xml_e_content_twine)],
	[qw(xc xml_content_object)],
	[qw(xct xml_content_twine)],
	[qw(xe xml_element)],
	[qw(xc_cont xml_c_content_object)],
	[qw(xc_twine xml_c_content_twine)],
	[qw(xe_type xml_e_type_name)],
	[qw(xe_attrs xml_e_attributes)],
	[qw(xe_attr xml_e_attribute)],
	[qw(xe_cont xml_e_content_object)],
	[qw(xe_twine xml_e_content_twine)],
	[qw(xc_eq xml_c_equal)],
	[qw(xe_eq xml_e_equal)],
	[qw(xc_ne xml_c_unequal)],
	[qw(xe_ne xml_e_unequal)],
) {
	my($alias, $orig) = @$_;
	no strict "refs";
	ok defined(&{"XML::Easy::NodeBasics::$alias"});
	ok \&{"XML::Easy::NodeBasics::$alias"} == \&{"XML::Easy::NodeBasics::$orig"};
	use_ok "XML::Easy::NodeBasics", $alias;
}

1;