-
Notifications
You must be signed in to change notification settings - Fork 38
/
test.js
105 lines (94 loc) · 2.75 KB
/
test.js
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
//copyright Ryan Day 2010 <http://ryanday.org> [MIT Licensed]
var test = require('tape')
, jsonxml = require("./jsontoxml.js")
;
var date = (new Date());
var input = {
node:'text content',
parent:[
{name:'taco',text:'beef taco',children:{salsa:'hot!'}},
{name:'xml',text:'tag'},
{name:'taco',text:'fish taco',attrs:{mood:'sad'},children:[
{name:'salsa',text:'mild'},
'hi',
{name:'salsa',text:'weak',attrs:{type:2}}
]},
{name:'taco',attrs:{mood:"party!"}},
{name:'andrew', text:"<wuz>here</wuz>", noescape:true}
],
parent2:{
hi:'this & this is a nice thing to say',
node:'i am another not special child node',
date:date+'',
date2:date
}
};
var expected_no_element_substitution =
'<node>text content</node>'
+'<parent>'
+'<taco>'
+'beef taco'
+'<salsa>hot!</salsa>'
+'</taco>'
+'<xml>tag</xml>'
+'<taco mood="sad">'
+'fish taco'
+'<salsa>mild</salsa>'
+'hi'
+'<salsa type="2">weak</salsa>'
+'</taco>'
+"<taco mood=\"party!\"/>"
+"<andrew><wuz>here</wuz></andrew>"
+'</parent>'
+'<parent2>'
+'<hi>this & this is a nice thing to say</hi>'
+'<node>i am another not special child node</node>'
+'<date>'+date+'</date>'
+'<date2>'+date.toJSON()+'</date2>'
+'</parent2>';
var expected_with_element_substitution =
'<node>text content</node>'
+'<parent>'
+'<taco>'
+'beef taco'
+'<salsa>hot!</salsa>'
+'</taco>'
+'<_>tag</_>'
+'<taco mood="sad">'
+'fish taco'
+'<salsa>mild</salsa>'
+'hi'
+'<salsa type="2">weak</salsa>'
+'</taco>'
+"<taco mood=\"party!\"/>"
+"<andrew><wuz>here</wuz></andrew>"
+'</parent>'
+'<parent2>'
+'<hi>this & this is a nice thing to say</hi>'
+'<node>i am another not special child node</node>'
+'<date>'+date+'</date>'
+'<date2>'+date.toJSON()+'</date2>'
+'</parent2>';
var expected = expected_no_element_substitution;
var buffer = Buffer.from?Buffer.from(JSON.stringify(input)):new Buffer(JSON.stringify(input));
test("creates correct object from buffer",function(t){
var result = jsonxml(buffer,{escape:true});
t.equals(result,expected,' should have generated correct xml');
t.end()
});
test("creates correct object from string",function(t){
var result = jsonxml(input,{escape:true});
t.equals(result,expected,' test should have generated correct xml');
t.end()
});
test("creates correct object with element fixup",function(t){
var result = jsonxml(input,{escape:true, removeIllegalNameCharacters:true});
t.equals(result,expected_with_element_substitution,' test should have generated correct xml');
t.end()
});
test("creates open and close tag on empty body",function(t){
var o = [{name:'salsa',text:''}]
var result = jsonxml(o,{html:true});
t.equals(result,'<salsa></salsa>')
t.end()
});