-
Notifications
You must be signed in to change notification settings - Fork 29.9k
/
xml-tests.ts
164 lines (142 loc) · 5.55 KB
/
xml-tests.ts
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
// Code get from the test file of http://github.com/dylang/node-xml
import * as xml from "xml";
interface Tester {
is(a: any, b: any): void;
same(a: any, b: any): void;
plan(id: number): void;
ok(a: any): void;
}
function test(c: string, f: (t: Tester) => void) {
return;
}
test('no elements', t => {
t.is(xml(), '');
t.is(xml([]), '');
t.is(xml('test'), 'test');
t.is(xml('scotch & whisky'), 'scotch & whisky');
t.is(xml('bob\'s escape character'), 'bob's escape character');
});
test('simple options', t => {
t.is(xml([{a: {}}]), '<a/>');
t.is(xml([{a: null}]), '<a/>');
t.is(xml([{a: []}]), '<a></a>');
t.is(xml([{a: -1}]), '<a>-1</a>');
t.is(xml([{a: false}]), '<a>false</a>');
t.is(xml([{a: 'test'}]), '<a>test</a>');
t.is(xml({a: {}}), '<a/>');
t.is(xml({a: null}), '<a/>');
t.is(xml({a: []}), '<a></a>');
t.is(xml({a: -1}), '<a>-1</a>');
t.is(xml({a: false}), '<a>false</a>');
t.is(xml({a: 'test'}), '<a>test</a>');
t.is(xml([{a: 'test'}, {b: 123}, {c: -0.5}]), '<a>test</a><b>123</b><c>-0.5</c>');
});
test('deeply nested objects', t => {
t.is(xml([{a: [{b: [{c: 1}, {c: 2}, {c: 3}]}]}]), '<a><b><c>1</c><c>2</c><c>3</c></b></a>');
});
test('indents property', t => {
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>');
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>');
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>');
t.is(xml({guid: [{_attr: {premalink: true}}, 'content']}, true), '<guid premalink="true">content</guid>');
});
test('supports xml attributes', t => {
t.is(xml([{b: {_attr: {}}}]), '<b/>');
t.is(xml([{
a: {
_attr: {
attribute1: 'some value',
attribute2: 12345
}
}
}]), '<a attribute1="some value" attribute2="12345"/>');
t.is(xml([{
a: [{
_attr: {
attribute1: 'some value',
attribute2: 12345
}
}]
}]), '<a attribute1="some value" attribute2="12345"></a>');
t.is(xml([{
a: [{
_attr: {
attribute1: 'some value',
attribute2: 12345
}
}, 'content']
}]), '<a attribute1="some value" attribute2="12345">content</a>');
});
test('supports cdata', t => {
t.is(xml([{a: {_cdata: 'This is some <strong>CDATA</strong>'}}]), '<a><![CDATA[This is some <strong>CDATA</strong>]]></a>');
t.is(xml([{
a: {
_attr: {attribute1: 'some value', attribute2: 12345},
_cdata: 'This is some <strong>CDATA</strong>'
}
}]), '<a attribute1="some value" attribute2="12345"><![CDATA[This is some <strong>CDATA</strong>]]></a>');
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>');
});
test('supports encoding', t => {
t.is(xml([{
a: [{
_attr: {
anglebrackets: 'this is <strong>strong</strong>',
url: 'http://google.com?s=opower&y=fun'
}
}, 'text']
}]), '<a anglebrackets="this is <strong>strong</strong>" url="http://google.com?s=opower&y=fun">text</a>');
});
test('supports stream interface', t => {
const elem = xml.element({_attr: {decade: '80s', locale: 'US'}});
const xmlStream = xml({toys: elem}, {stream: true});
const results = ['<toys decade="80s" locale="US">', '<toy>Transformers</toy>', '<toy><name>He-man</name></toy>', '<toy>GI Joe</toy>', '</toys>'];
elem.push({toy: 'Transformers'});
elem.push({toy: [{name: 'He-man'}]});
elem.push({toy: 'GI Joe'});
elem.close();
xmlStream.on('data', (stanza: any[]) => {
t.is(stanza, results.shift());
});
return new Promise( (resolve, reject) => {
xmlStream.on('close', () => {
t.same(results, []);
resolve();
});
xmlStream.on('error', reject);
});
});
test('streams end properly', t => {
const elem = xml.element({ _attr: { decade: '80s', locale: 'US'} });
const xmlStream = xml({ toys: elem }, { stream: true });
let gotData = false;
t.plan(7);
elem.push({ toy: 'Transformers' });
elem.push({ toy: 'GI Joe' });
elem.push({ toy: [{name: 'He-man'}] });
elem.close();
xmlStream.on('data', (data: any) => {
t.ok(data);
gotData = true;
});
xmlStream.on('end', () => {
t.ok(gotData);
});
return new Promise( (resolve, reject) => {
xmlStream.on('close', () => {
t.ok(gotData);
resolve();
});
xmlStream.on('error', reject);
});
});
test('xml declaration options', t => {
t.is(xml([{a: 'test'}], {declaration: true}), '<?xml version="1.0" encoding="UTF-8"?><a>test</a>');
t.is(xml([{a: 'test'}], {declaration: {encoding: 'foo'}}), '<?xml version="1.0" encoding="foo"?><a>test</a>');
t.is(xml([{a: 'test'}], {declaration: {standalone: 'yes'}}), '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><a>test</a>');
t.is(xml([{a: 'test'}], {declaration: false}), '<a>test</a>');
t.is(xml([{a: 'test'}], {declaration: true, indent: '\n'}), '<?xml version="1.0" encoding="UTF-8"?>\n<a>test</a>');
t.is(xml([{a: 'test'}], {}), '<a>test</a>');
});