forked from drager/xml-object-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.coffee
161 lines (127 loc) · 3.23 KB
/
test.coffee
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
assert = require 'assert'
{Stream} = require 'stream'
{parse} = require './index'
streamData = (xml) ->
stream = new Stream()
process.nextTick ->
stream.emit 'data', xml
stream.emit 'end'
return stream
describe "xml streamer thing", ->
it "should parse", (done) ->
foundBook = false
xml = """
<root>
<book asdf="asdf">
<author name="john"></author>
<author><name>will</name></author>
<title>Title</title>
<description>stuff</description>
</book>
</root>
"""
stream = streamData xml
parser = parse stream
parser.each 'book', (book) ->
foundBook = true
assert.ok book
assert.equal book.$.asdf, "asdf"
assert.equal book.title.$text, "Title"
assert.equal book.description.$text, "stuff"
authors = book.$children.filter (node) -> node.$name is "author"
assert.equal authors.length, 2
assert.equal authors[0].$.name, "john"
assert.equal authors[1].name.$text, "will"
parser.on 'end', ->
assert.ok foundBook
done()
it 'should find all the nodes', (done) ->
found = []
xml = """
<root>
<items>
<item name="1"/>
<item name="2"/>
<item name="3"/>
<item name="4"/>
<item name="5"/>
<item name="6"/>
<item name="7"/>
<item name="8"/>
</items>
</root>
"""
stream = streamData xml
parser = parse stream
parser.each 'item', (item) ->
found.push item
parser.on 'end', ->
assert.equal found.length, 8
done()
it 'should not ignore casing', (done) ->
found = []
xml = """
<root>
<Items>
<item name="1"/>
<Item name="2"/>
<ITEM name="3"/>
<itEM name="4"/>
</Items>
</root>
"""
stream = streamData xml
parser = parse stream
parser.each 'item', (item) ->
found.push item
parser.on 'end', ->
assert.equal found.length, 1
done()
it 'should make all names lowercase', (done) ->
found = []
xml = """
<root>
<Items>
<item name="1"/>
<Item name="2"/>
<ITEM name="3"/>
<itEM name="4"/>
</Items>
</root>
"""
stream = streamData xml
parser = parse stream, {ignoreNodeNameCasing: true}
parser.each 'item', (item) ->
found.push item
parser.on 'end', ->
assert.equal found.length, 4
done()
describe 'namespaces', ->
it 'should strip namespaces by default', (done) ->
stream = streamData """
<root>
<me:item>one</me:item>
</root>
"""
found = []
parser = parse stream
parser.each 'item', (item) ->
found.push item
assert.equal item.$text, "one"
parser.on 'end', ->
assert.equal found.length, 1
done()
it 'should preserve them if you turn it off', (done) ->
stream = streamData """
<root>
<me:item>one</me:item>
</root>
"""
found = []
parser = parse stream, {stripNamespaces: false}
parser.each 'me:item', (item) ->
found.push item
assert.equal item.$text, "one"
parser.on 'end', ->
assert.equal found.length, 1
done()