-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathxml.test.js
More file actions
107 lines (93 loc) · 6.3 KB
/
xml.test.js
File metadata and controls
107 lines (93 loc) · 6.3 KB
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
/*
Run `npm test` to run tests
*/
var xml = require('../lib/xml');
var expect = require('chai').expect;
describe('xml module', function(done) {
it('can be have no elements', function(done) {
expect(xml()).to.be.empty;
expect(xml([])).to.be.empty;
expect(xml('test')).to.equal('test');
expect(xml('test')).to.equal('test');
expect(xml('scotch & whisky')).to.equal('scotch & whisky');
expect(xml('bob\'s escape character')).to.equal('bob's escape character');
done();
});
it('works with simple options', function(done) {
expect(xml([ { a: {} }])).to.equal('<a/>');
expect(xml([ { a: [] }])).to.equal('<a></a>');
expect(xml([ { a: -1 }])).to.equal('<a>-1</a>');
expect(xml([ { a: false }])).to.equal('<a>false</a>');
expect(xml([ { a: 'test' }])).to.equal('<a>test</a>');
expect(xml( { a: {} })).to.equal('<a/>');
expect(xml( { a: [] })).to.equal('<a></a>');
expect(xml( { a: -1 })).to.equal('<a>-1</a>');
expect(xml( { a: false })).to.equal('<a>false</a>');
expect(xml( { a: 'test' })).to.equal('<a>test</a>');
expect(xml([ { a: 'test' }, { b: 123 }, { c: -0.5 } ])).to.equal('<a>test</a><b>123</b><c>-0.5</c>');
done();
});
it('works with deeply nested objects', function(done) {
expect(xml([ { a: [ { b: [ { c: 1 }, { c: 2 }, { c: 3 } ] } ] }]), '<a><b><c>1</c><c>2</c><c>3</c></b></a>');
done();
});
it('supports mixed xml and text nodes with whitespace', function(done) {
expect(xml([ { a: [ { foo1: '' }, { bar1: '' }, 'textNode', { foo2: '' }, { bar2: '' } ] } ])).to.equal('<a><foo1></foo1><bar1></bar1>textNode<foo2></foo2><bar2></bar2></a>');
expect(xml([ { a: [ { foo1: ' ' }, { bar1: '' }, ' textNode ', { foo2: '' }, ' ', { bar2: '' } ] } ])).to.equal('<a><foo1> </foo1><bar1></bar1> textNode <foo2></foo2> <bar2></bar2></a>');
done();
});
it('indents property', function(done) {
expect(xml([ { a: [ { b: [ { c: 1 }, { c: 2 }, { c: 3 } ] } ] }], true)).to.equal('<a>\n <b>\n <c>1</c>\n <c>2</c>\n <c>3</c>\n </b>\n</a>');
expect(xml([ { a: [ { b: [ { c: 1 }, { c: 2 }, { c: 3 } ] } ] }], ' ')).to.equal('<a>\n <b>\n <c>1</c>\n <c>2</c>\n <c>3</c>\n </b>\n</a>');
expect(xml([ { a: [ { b: [ { c: 1 }, { c: 2 }, { c: 3 } ] } ] }], '\t')).to.equal('<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>');
expect(xml([ { a: [ { b: [ { c: 1 }, { c: 2 }, 'textNode', { c: 3 } ] } ] }], true)).to.equal('<a>\n <b><c>1</c><c>2</c>textNode<c>3</c></b>\n</a>');
expect(xml([ { a: [ { b: [ ' textNode ', { c: 1 }, { c: 2 }, ' textNode ', { c: 3 }, ' textNode ' ] } ] }], ' ')).to.equal('<a>\n <b> textNode <c>1</c><c>2</c> textNode <c>3</c> textNode </b>\n</a>');
expect(xml([ { a: [ { b: [ { c: 1 }, { c: 2 }, 'textNode ', { c: 3 } ] } ] }], '\t')).to.equal('<a>\n\t<b><c>1</c><c>2</c>textNode <c>3</c></b>\n</a>');
expect(xml({guid:[{_attr:{premalink:true}},'content']},true)).to.equal('<guid premalink="true">content</guid>');
done();
});
it('supports xml attributes', function(done) {
expect(xml([ { b: { _attr: {} } } ]), '<b/>');
expect(xml([ { a: { _attr: { attribute1: 'some value', attribute2: 12345 } } } ])).to.equal('<a attribute1="some value" attribute2="12345"/>');
expect(xml([ { a: [{ _attr: { attribute1: 'some value', attribute2: 12345 } }] } ])).to.equal('<a attribute1="some value" attribute2="12345"></a>');
expect(xml([ { a: [{ _attr: { attribute1: 'some value', attribute2: 12345 } }, 'content'] } ])).to.equal('<a attribute1="some value" attribute2="12345">content</a>');
done();
});
it('supports cdata', function(done) {
expect(xml([ { a: { _cdata: 'This is some <strong>CDATA</strong>' } } ])).to.equal('<a><![CDATA[This is some <strong>CDATA</strong>]]></a>');
expect(xml([ { a: { _attr: { attribute1: 'some value', attribute2: 12345 }, _cdata: 'This is some <strong>CDATA</strong>' } } ])).to.equal('<a attribute1="some value" attribute2="12345"><![CDATA[This is some <strong>CDATA</strong>]]></a>');
expect(xml([ { a: { _cdata: 'This is some <strong>CDATA</strong> with ]]> and then again ]]>' } } ])).to.equal('<a><![CDATA[This is some <strong>CDATA</strong> with ]]]]><![CDATA[> and then again ]]]]><![CDATA[>]]></a>');
done();
});
it('supports encoding', function(done) {
expect(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>');
done();
});
it('supports stream interface', function (done) {
var elem = xml.element({ _attr: { decade: '80s', locale: 'US'} });
var xmlStream = xml({ toys: elem }, { stream: true });
var results = ['<toys decade="80s" locale="US">','<toy>Transformers</toy>','<toy><name>He-man</name></toy>','<toy>GI Joe</toy>','</toys>'];
xmlStream.on('data', function (stanza) {
expect(stanza).to.equal(results.shift());
});
xmlStream.on('close', function () {
expect(results).to.be.be.empty;
done();
});
elem.push({ toy: 'Transformers' });
elem.push({ toy: [ { name: 'He-man' } ] });
setTimeout(function () {
elem.push({ toy: 'GI Joe' });
elem.close();
}, 10);
});
it('xml declaration options', function(done) {
expect(xml([ { a: 'test' }], { declaration: true })).to.equal('<?xml version="1.0" encoding="UTF-8"?><a>test</a>');
expect(xml([ { a: 'test' }], { declaration: {encoding: 'foo' }})).to.equal('<?xml version="1.0" encoding="foo"?><a>test</a>');
expect(xml([ { a: 'test' }], { declaration: {standalone: 'yes' }})).to.equal('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><a>test</a>');
expect(xml([ { a: 'test' }], { declaration: false })).to.equal('<a>test</a>');
expect(xml([ { a: 'test' }], { declaration: true, indent: '\n' })).to.equal('<?xml version="1.0" encoding="UTF-8"?>\n<a>test</a>');
expect(xml([ { a: 'test' }], {})).to.equal('<a>test</a>');
done();
});
});