source: node_modules/xml/test/xml.test.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 5.3 KB
Line 
1import test from 'ava';
2import xml from '../lib/xml';
3
4test('no elements', t => {
5 t.is(xml(), '');
6 t.is(xml([]), '');
7 t.is(xml('test'), 'test');
8 t.is(xml('scotch & whisky'), 'scotch &amp; whisky');
9 t.is(xml('bob\'s escape character'), 'bob&apos;s escape character');
10});
11
12test('simple options', t => {
13 t.is(xml([{a: {}}]), '<a/>');
14 t.is(xml([{a: null}]), '<a/>');
15 t.is(xml([{a: []}]), '<a></a>');
16 t.is(xml([{a: -1}]), '<a>-1</a>');
17 t.is(xml([{a: false}]), '<a>false</a>');
18 t.is(xml([{a: 'test'}]), '<a>test</a>');
19 t.is(xml({a: {}}), '<a/>');
20 t.is(xml({a: null}), '<a/>');
21 t.is(xml({a: []}), '<a></a>');
22 t.is(xml({a: -1}), '<a>-1</a>');
23 t.is(xml({a: false}), '<a>false</a>');
24 t.is(xml({a: 'test'}), '<a>test</a>');
25 t.is(xml([{a: 'test'}, {b: 123}, {c: -0.5}]), '<a>test</a><b>123</b><c>-0.5</c>');
26});
27
28test('deeply nested objects', t => {
29 t.is(xml([{a: [{b: [{c: 1}, {c: 2}, {c: 3}]}]}]), '<a><b><c>1</c><c>2</c><c>3</c></b></a>');
30});
31
32test('indents property', t => {
33 t.is(xml([{a: [{b: [{c: 1}, {c: 2}, {c: 3}]}]}], true), '<a>\n <b>\n <c>1</c>\n <c>2</c>\n <c>3</c>\n </b>\n</a>');
34 t.is(xml([{a: [{b: [{c: 1}, {c: 2}, {c: 3}]}]}], ' '), '<a>\n <b>\n <c>1</c>\n <c>2</c>\n <c>3</c>\n </b>\n</a>');
35 t.is(xml([{a: [{b: [{c: 1}, {c: 2}, {c: 3}]}]}], '\t'), '<a>\n\t<b>\n\t\t<c>1</c>\n\t\t<c>2</c>\n\t\t<c>3</c>\n\t</b>\n</a>');
36 t.is(xml({guid: [{_attr: {premalink: true}}, 'content']}, true), '<guid premalink="true">content</guid>');
37});
38
39test('supports xml attributes', t => {
40 t.is(xml([{b: {_attr: {}}}]), '<b/>');
41 t.is(xml([{
42 a: {
43 _attr: {
44 attribute1: 'some value',
45 attribute2: 12345
46 }
47 }
48 }]), '<a attribute1="some value" attribute2="12345"/>');
49 t.is(xml([{
50 a: [{
51 _attr: {
52 attribute1: 'some value',
53 attribute2: 12345
54 }
55 }]
56 }]), '<a attribute1="some value" attribute2="12345"></a>');
57 t.is(xml([{
58 a: [{
59 _attr: {
60 attribute1: 'some value',
61 attribute2: 12345
62 }
63 }, 'content']
64 }]), '<a attribute1="some value" attribute2="12345">content</a>');
65});
66
67test('supports cdata', t => {
68 t.is(xml([{a: {_cdata: 'This is some <strong>CDATA</strong>'}}]), '<a><![CDATA[This is some <strong>CDATA</strong>]]></a>');
69 t.is(xml([{
70 a: {
71 _attr: {attribute1: 'some value', attribute2: 12345},
72 _cdata: 'This is some <strong>CDATA</strong>'
73 }
74 }]), '<a attribute1="some value" attribute2="12345"><![CDATA[This is some <strong>CDATA</strong>]]></a>');
75 t.is(xml([{a: {_cdata: 'This is some <strong>CDATA</strong> with ]]> and then again ]]>'}}]), '<a><![CDATA[This is some <strong>CDATA</strong> with ]]]]><![CDATA[> and then again ]]]]><![CDATA[>]]></a>');
76});
77
78test('supports encoding', t => {
79 t.is(xml([{
80 a: [{
81 _attr: {
82 anglebrackets: 'this is <strong>strong</strong>',
83 url: 'http://google.com?s=opower&y=fun'
84 }
85 }, 'text']
86 }]), '<a anglebrackets="this is &lt;strong&gt;strong&lt;/strong&gt;" url="http://google.com?s=opower&amp;y=fun">text</a>');
87});
88
89test('supports stream interface', t => {
90 const elem = xml.element({_attr: {decade: '80s', locale: 'US'}});
91 const xmlStream = xml({toys: elem}, {stream: true});
92 const results = ['<toys decade="80s" locale="US">', '<toy>Transformers</toy>', '<toy><name>He-man</name></toy>', '<toy>GI Joe</toy>', '</toys>'];
93
94 elem.push({toy: 'Transformers'});
95 elem.push({toy: [{name: 'He-man'}]});
96 elem.push({toy: 'GI Joe'});
97 elem.close();
98
99 xmlStream.on('data', stanza => {
100 t.is(stanza, results.shift());
101 });
102
103 return new Promise( (resolve, reject) => {
104 xmlStream.on('close', () => {
105 t.same(results, []);
106 resolve();
107 });
108 xmlStream.on('error', reject);
109 });
110});
111
112test('streams end properly', t => {
113 const elem = xml.element({ _attr: { decade: '80s', locale: 'US'} });
114 const xmlStream = xml({ toys: elem }, { stream: true });
115
116 let gotData;
117
118 t.plan(7);
119
120 elem.push({ toy: 'Transformers' });
121 elem.push({ toy: 'GI Joe' });
122 elem.push({ toy: [{name:'He-man'}] });
123 elem.close();
124
125 xmlStream.on('data', data => {
126 t.ok(data);
127 gotData = true;
128 });
129
130 xmlStream.on('end', () => {
131 t.ok(gotData);
132 });
133
134 return new Promise( (resolve, reject) => {
135 xmlStream.on('close', () => {
136 t.ok(gotData);
137 resolve();
138 });
139 xmlStream.on('error', reject);
140 });
141});
142
143test('xml declaration options', t => {
144 t.is(xml([{a: 'test'}], {declaration: true}), '<?xml version="1.0" encoding="UTF-8"?><a>test</a>');
145 t.is(xml([{a: 'test'}], {declaration: {encoding: 'foo'}}), '<?xml version="1.0" encoding="foo"?><a>test</a>');
146 t.is(xml([{a: 'test'}], {declaration: {standalone: 'yes'}}), '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><a>test</a>');
147 t.is(xml([{a: 'test'}], {declaration: false}), '<a>test</a>');
148 t.is(xml([{a: 'test'}], {declaration: true, indent: '\n'}), '<?xml version="1.0" encoding="UTF-8"?>\n<a>test</a>');
149 t.is(xml([{a: 'test'}], {}), '<a>test</a>');
150});
Note: See TracBrowser for help on using the repository browser.