forked from danielwhelansb/xml-object-stream
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.coffee
111 lines (89 loc) · 2.37 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
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()
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()