Debian Codegraph @

t/08-x2h_trim.t

raw
1 package main;
2 use strict;
3 use warnings;
4
5 use Test::More tests => 5;
6 use Data::Dumper;
7 $Data::Dumper::Indent = 0;
8 $Data::Dumper::Sortkeys = 1;
9
10 use XML::Hash::XS 'xml2hash';
11 $XML::Hash::XS::keep_root = 0;
12
13 our $xml_decl_utf8 = qq{<?xml version="1.0" encoding="utf-8"?>};
14
15 {
16 is
17 xml2hash(" \r\n\t<root>boom</root>"),
18 'boom',
19 'ignore leading white spaces',
20 ;
21 }
22
23 {
24 is
25 Dumper(xml2hash(<<"XML", keep_root => 1)),
26 $xml_decl_utf8
27 <root>
28 <cdata1> <![CDATA[\n\t cdata1\n\t ]]> </cdata1>
29 <cdata2> <![CDATA[ ]]> </cdata2>
30 <text> text\n \n</text>
31 </root>
32 XML
33 Dumper({
34 root => {
35 cdata1 => "\n\t cdata1\n\t ",
36 cdata2 => " ",
37 text => " text\n \n",
38 },
39 }),
40 'if the trim is off',
41 ;
42 }
43
44 {
45 is
46 Dumper(xml2hash(<<"XML", keep_root => 1, trim => 1)),
47 $xml_decl_utf8
48 <root>
49 <cdata1> <![CDATA[\n\t cdata1\n\t ]]> </cdata1>
50 <cdata2> <![CDATA[ ]]> </cdata2>
51 <text> text\n \n</text>
52 </root>\r\n
53 XML
54 Dumper({
55 root => {
56 cdata1 => 'cdata1',
57 cdata2 => '',
58 text => 'text',
59 },
60 }),
61 'if the trim is on',
62 ;
63 }
64
65 {
66 is
67 Dumper(xml2hash(<<"XML", trim => 1)),
68 $xml_decl_utf8
69 <root>
70 <text> </text>
71 </root>\r\n
72 XML
73 Dumper({ text => '' }),
74 'when element contains only spaces and trim is on',
75 ;
76 }
77
78 {
79 is
80 Dumper(xml2hash(<<"XML", trim => 0)),
81 $xml_decl_utf8
82 <root>
83 <text> </text>
84 </root>\r\n
85 XML
86 Dumper({ text => ' ' }),
87 'when element contains only spaces and trim is off',
88 ;
89 }