1#!/usr/bin/perl -w
2
3use strict;
4use Carp;
5
6use File::Spec;
7use lib File::Spec->catdir(File::Spec->curdir,"t");
8use tools;
9
10
11$|=1;
12
13use XML::Twig;
14
15# abort (before compiling so the 3 arg open doesn't cause a crash) unless perl 5.8+
16BEGIN
17  { if( $] < 5.008) { print "1..1\nok 1\n"; warn "skipping tests that require 3 args open\n"; exit 0; } }
18
19my $TMAX=4;
20print "1..$TMAX\n";
21
22{ my $out='';
23  open( my $fh, '>', \$out);
24  my $doc=q{<doc><elt att="a">foo</elt><elt att="b">bar</elt></doc>};
25  my $t= XML::Twig->new( twig_handlers => { elt => sub { $_->flush( $fh) } });
26  $t->parse( $doc);
27  is( $out, $doc, "flush to a scalar (with autoflush)");
28  $t->flush( $fh);
29  is( $out, $doc, "double flush");
30  $t->flush();
31  is( $out, $doc, "triple flush");
32}
33
34{
35my $out= '';
36my $twig = XML::Twig->new( output_encoding => 'utf-8',);
37$twig->parse( "<root/>");
38my $greet = $twig->root->insert_new_elt( last_child => 'g');
39$greet->set_text("Gr\x{00FC}\x{00DF}");
40open(my $fh, '>:utf8', \$out);
41$twig->print(\*$fh);
42print {*$fh} "<c>Copyright \x{00A9} 2008 Me</c>";
43close($fh);
44is( $out, qq{<?xml version="1.0" encoding="utf-8"?><root><g>Grüß</g></root><c>Copyright © 2008 Me</c>},
45          '$t->print and regular print mixed, with utf-8 encoding'
46  );
47}
48
49