Debian Codegraph @

t/22-h2x-output-cb.t

raw
1 package main;
2 use strict;
3 use warnings;
4
5 use Test::More 0.98;
6 use Scalar::Util qw(weaken);
7
8 use XML::Hash::XS qw(hash2xml);
9
10 my %data = (
11 alpha => 'one',
12 beta => [ map { "value-$_" } 1 .. 20 ],
13 );
14
15 my $expected = hash2xml(\%data, canonical => 1, buf_size => 32);
16
17 {
18 my @chunks;
19 my @argc;
20 my $result = hash2xml(
21 \%data,
22 canonical => 1,
23 buf_size => 32,
24 output_cb => sub {
25 push @argc, scalar @_;
26 push @chunks, $_[0];
27 },
28 );
29
30 is($result, undef, 'callback output returns undef');
31 cmp_ok(scalar @chunks, '>', 1, 'small buffer produces multiple chunks');
32 is(join('', @chunks), $expected, 'chunks contain the complete XML document');
33 is_deeply(\@argc, [ (1) x @argc ], 'callback receives exactly one argument');
34 }
35
36 {
37 my @weak_chunks;
38 hash2xml(
39 \%data,
40 canonical => 1,
41 buf_size => 8,
42 output_cb => sub {
43 push @weak_chunks, \$_[0];
44 weaken($weak_chunks[-1]);
45 },
46 );
47
48 ok(!grep(defined, @weak_chunks), 'unretained callback chunks are released immediately');
49 }
50
51 {
52 my @chunks;
53 hash2xml(
54 { value => 'abcdef' },
55 xml_decl => 0,
56 buf_size => 4,
57 output_cb => sub {
58 push @chunks, $_[0];
59 $_[0] = 'changed';
60 },
61 );
62
63 isnt(join('', @chunks), 'changed', 'callback receives stable independent chunks');
64 is(join('', @chunks), '<root><value>abcdef</value></root>', 'mutating callback argument does not alter output');
65 }
66
67 {
68 my @chunks;
69 my $converter;
70 my $weak;
71 {
72 my $callback = sub { push @chunks, $_[0] };
73 $weak = $callback;
74 weaken($weak);
75 $converter = XML::Hash::XS->new(
76 xml_decl => 0,
77 output_cb => $callback,
78 );
79 }
80
81 ok(defined $weak, 'object retains output callback');
82 is($converter->hash2xml({ item => 'value' }), undef, 'OOP callback output returns undef');
83 is(join('', @chunks), '<root><item>value</item></root>', 'OOP callback receives XML');
84 undef $converter;
85 ok(!defined $weak, 'object releases output callback');
86 }
87
88 {
89 my $calls = 0;
90 my $error;
91 eval {
92 hash2xml(
93 \%data,
94 canonical => 1,
95 buf_size => 8,
96 output_cb => sub {
97 ++$calls;
98 die "output callback failed\n";
99 },
100 );
101 };
102 $error = $@;
103
104 like($error, qr/^output callback failed/, 'callback exception is propagated');
105 is($calls, 1, 'failing callback is not called again during cleanup');
106 }
107
108 {
109 my $error;
110 eval { hash2xml({}, output_cb => 'not a callback') };
111 $error = $@;
112 like($error, qr/Parameter 'output_cb' is not CODE reference/, 'output_cb must be a code reference');
113 }
114
115 {
116 open(my $fh, '>', \my $output) or die "Can't open scalar filehandle: $!";
117 my $error;
118 eval { hash2xml({}, output => $fh, output_cb => sub {}) };
119 $error = $@;
120 like($error, qr/mutually exclusive/, 'output and output_cb cannot be combined');
121 }
122
123 {
124 my @utf8_chunks;
125 hash2xml(
126 { text => "\x{410}\x{411}" },
127 xml_decl => 0,
128 utf8 => 1,
129 buf_size => 4,
130 output_cb => sub { push @utf8_chunks, $_[0] },
131 );
132 ok(!grep(!utf8::is_utf8($_), @utf8_chunks), 'callback chunks carry the UTF-8 flag');
133
134 my @byte_chunks;
135 hash2xml(
136 { text => "\x{410}\x{411}" },
137 xml_decl => 0,
138 utf8 => 0,
139 buf_size => 4,
140 output_cb => sub { push @byte_chunks, $_[0] },
141 );
142 ok(!grep(utf8::is_utf8($_), @byte_chunks), 'utf8 => 0 returns byte chunks');
143 }
144
145 SKIP: {
146 my $encoded = eval {
147 hash2xml(
148 { text => "\x{410}\x{411}" },
149 canonical => 1,
150 encoding => 'cp1251',
151 utf8 => 0,
152 buf_size => 4096,
153 );
154 };
155 skip 'external encoding support is not available', 3 if $@;
156
157 my @chunks;
158 hash2xml(
159 { text => "\x{410}\x{411}" },
160 canonical => 1,
161 encoding => 'cp1251',
162 utf8 => 0,
163 buf_size => 4,
164 output_cb => sub { push @chunks, $_[0] },
165 );
166 is(join('', @chunks), $encoded, 'callback receives externally encoded chunks');
167
168 my $calls = 0;
169 my $error;
170 eval {
171 hash2xml(
172 \%data,
173 canonical => 1,
174 encoding => 'cp1251',
175 utf8 => 0,
176 buf_size => 4,
177 output_cb => sub {
178 ++$calls;
179 die "encoded callback failed\n";
180 },
181 );
182 };
183 $error = $@;
184 like($error, qr/^encoded callback failed/, 'encoded callback exception is propagated');
185 is($calls, 1, 'failing encoded callback is not retried during cleanup');
186 }
187
188 {
189 my @chunks;
190 local $XML::Hash::XS::output_cb = sub { push @chunks, $_[0] };
191 my $result = hash2xml({ item => 'global' }, xml_decl => 0);
192
193 is($result, undef, 'global output_cb returns undef');
194 is(join('', @chunks), '<root><item>global</item></root>', 'global output_cb receives XML');
195 }
196
197 done_testing;