1 |
|
2 |
|
3 | var flow = require('../lib/xml-flow')
|
4 | , fs = require('fs')
|
5 | , should = require('chai').should()
|
6 | ;
|
7 |
|
8 | function getFlow(fileName, options) {
|
9 | return flow(fs.createReadStream(fileName), options);
|
10 | }
|
11 |
|
12 | describe('xml-flow', function(){
|
13 | describe('invoke', function(){
|
14 | it('should create an emitter when invoked with a stream and options', function(){
|
15 | var inStream = fs.createReadStream('./test/simple.xml'), simpleStream;
|
16 | inStream.pause();
|
17 | simpleStream = flow(inStream);
|
18 |
|
19 | simpleStream.on.should.be.a('function');
|
20 | simpleStream.pause();
|
21 | simpleStream.resume();
|
22 | });
|
23 | });
|
24 |
|
25 | describe(".on('end')", function(){
|
26 | it('should emit after the file has been read', function(done){
|
27 | var simpleStream = getFlow('./test/simple.xml');
|
28 | simpleStream.on('end', function(){
|
29 | done();
|
30 | });
|
31 | });
|
32 | });
|
33 |
|
34 | describe(".on('tag:...')", function(){
|
35 | it('should emit the right number of nodes', function(done){
|
36 | var simpleStream = getFlow('./test/simple.xml')
|
37 | , count = 0
|
38 | ;
|
39 |
|
40 | simpleStream.on('tag:item', function(){
|
41 | count++;
|
42 | });
|
43 |
|
44 | simpleStream.on('end', function(){
|
45 | count.should.equal(3);
|
46 | done();
|
47 | });
|
48 | });
|
49 |
|
50 | it('should make non-attributed data look really simple', function(done){
|
51 | var simpleStream = getFlow('./test/test.xml')
|
52 | , output = {
|
53 | $name: 'no-attrs',
|
54 | person: [
|
55 | {name: 'Bill', id: '1', age:'27'},
|
56 | {name: 'Joe', id: '2', age:'29'},
|
57 | {name: 'Smitty', id: '3', age:'37'}
|
58 | ]
|
59 | }
|
60 | ;
|
61 |
|
62 | simpleStream.on('tag:no-attrs', function(node){
|
63 | node.should.deep.equal(output);
|
64 | done();
|
65 | });
|
66 | });
|
67 |
|
68 | it('should make all-attributed data look really simple', function(done){
|
69 | var simpleStream = getFlow('./test/test.xml')
|
70 | , output = {
|
71 | $name: 'all-attrs',
|
72 | person: [
|
73 | {name: 'Bill', id: '1', age:'27'},
|
74 | {name: 'Joe', id: '2', age:'29'},
|
75 | {name: 'Smitty', id: '3', age:'37'}
|
76 | ]
|
77 | }
|
78 | ;
|
79 |
|
80 | simpleStream.on('tag:all-attrs', function(node){
|
81 | node.should.deep.equal(output);
|
82 | done();
|
83 | });
|
84 | });
|
85 |
|
86 | it('should handle tags with both attributes and other stuff', function(done){
|
87 | var simpleStream = getFlow('./test/test.xml')
|
88 | , output = {
|
89 | $name: 'mixed',
|
90 | person: [
|
91 | {$attrs:{name: 'Bill', id: '1', age:'27'}, $text: 'some text'},
|
92 | {$attrs: {name: 'Joe', id: '2', age:'29'}, p: 'some paragraph'},
|
93 | {$attrs: {name: 'Smitty', id: '3', age:'37'}, thing: {id:'999', ref:'blah'}}
|
94 | ]
|
95 | }
|
96 | ;
|
97 |
|
98 | simpleStream.on('tag:mixed', function(node){
|
99 | node.should.deep.equal(output);
|
100 | done();
|
101 | });
|
102 | });
|
103 |
|
104 | it('should preserve markup when things get more complex', function(done) {
|
105 | var simpleStream = getFlow('./test/test.xml')
|
106 | , output = {
|
107 | $name: 'markup',
|
108 | $markup: [
|
109 | 'Some unwrapped text',
|
110 | {$name: 'person', $attrs:{name: 'Bill', id: '1', age:'27'}, $text: 'some text'},
|
111 | 'Some more unwrapped text',
|
112 | {$name: 'person', $attrs: {name: 'Joe', id: '2', age:'29'}, p: 'some paragraph'},
|
113 | {$name: 'person', $attrs: {name: 'Smitty', id: '3', age:'37'}, thing: {id:'999', ref:'blah'}}
|
114 | ]
|
115 | }
|
116 | ;
|
117 |
|
118 | simpleStream.on('tag:markup', function(node){
|
119 | node.should.deep.equal(output);
|
120 | done();
|
121 | });
|
122 | });
|
123 |
|
124 | it('should handle scripts', function(done){
|
125 | var simpleStream = getFlow('./test/test.xml')
|
126 | , output = {
|
127 | $name: 'has-scripts',
|
128 | script: [
|
129 | 'var x = 3;',
|
130 | {$attrs: {type: 'text/javascript'}, $script: '//this is a comment'}
|
131 | ]
|
132 | }
|
133 | ;
|
134 |
|
135 | simpleStream.on('tag:has-scripts', function(node){
|
136 | node.should.deep.equal(output);
|
137 | done();
|
138 | });
|
139 | });
|
140 | });
|
141 |
|
142 | describe('options', function(){
|
143 | it('should normalize whitespace by default', function(done) {
|
144 | var simpleStream = getFlow('./test/test.xml')
|
145 | , output = 'This is some text with extra whitespace.'
|
146 | ;
|
147 |
|
148 | simpleStream.on('tag:extra-whitespace', function(node){
|
149 | node.$text.should.equal(output);
|
150 | done();
|
151 | });
|
152 | });
|
153 |
|
154 | it('should not normalize whitespace when asked not to', function(done) {
|
155 | var simpleStream = getFlow('./test/test.xml', {normalize: false})
|
156 | , output = 'This is some text with extra whitespace.'
|
157 | ;
|
158 |
|
159 | simpleStream.on('tag:extra-whitespace', function(node){
|
160 | node.$text.should.equal(output);
|
161 | done();
|
162 | });
|
163 | });
|
164 |
|
165 | it('should trim by default', function(done) {
|
166 | var simpleStream = getFlow('./test/test.xml')
|
167 | , output = 'This is some text with extra whitespace.'
|
168 | ;
|
169 |
|
170 | simpleStream.on('tag:extra-whitespace', function(node){
|
171 | node.$text.should.equal(output);
|
172 | done();
|
173 | });
|
174 | });
|
175 |
|
176 | it('should not trim when asked not to', function(done) {
|
177 | var simpleStream = getFlow('./test/test.xml', {trim: false})
|
178 | , output = 'This is some text with extra whitespace. '
|
179 | ;
|
180 |
|
181 | simpleStream.on('tag:extra-whitespace', function(node){
|
182 | node.$text.should.equal(output);
|
183 | done();
|
184 | });
|
185 | });
|
186 |
|
187 | it('should not preserve markup when told to never do so', function(done) {
|
188 | var simpleStream = getFlow('./test/test.xml', {preserveMarkup: flow.NEVER})
|
189 | , output = {
|
190 | $name: 'markup',
|
191 | $text: [
|
192 | 'Some unwrapped text',
|
193 | 'Some more unwrapped text'
|
194 | ],
|
195 | person: [
|
196 | {$attrs:{name: 'Bill', id: '1', age:'27'}, $text: 'some text'},
|
197 | {$attrs: {name: 'Joe', id: '2', age:'29'}, p: 'some paragraph'},
|
198 | {$attrs: {name: 'Smitty', id: '3', age:'37'}, thing: {id:'999', ref:'blah'}}
|
199 | ]
|
200 | }
|
201 | ;
|
202 |
|
203 | simpleStream.on('tag:markup', function(node){
|
204 | node.should.deep.equal(output);
|
205 | done();
|
206 | });
|
207 | });
|
208 |
|
209 | it('should always preserve markup when told to do so', function(done) {
|
210 | var simpleStream = getFlow('./test/test.xml', {preserveMarkup: flow.ALWAYS})
|
211 | , output = {
|
212 | $name: 'mixed',
|
213 | $markup: [
|
214 | {
|
215 | $name: 'person',
|
216 | $attrs:{name: 'Bill', id: '1', age:'27'},
|
217 | $markup: ['some text']
|
218 | },
|
219 | {
|
220 | $name: 'person',
|
221 | $attrs: {name: 'Joe', id: '2', age:'29'},
|
222 | $markup: [{$name:'p', $markup: ['some paragraph']}]
|
223 | },
|
224 | {
|
225 | $name: 'person',
|
226 | $attrs: {name: 'Smitty', id: '3', age:'37'},
|
227 | $markup: [{$name: 'thing', id:'999', ref:'blah'}]
|
228 | }
|
229 | ]
|
230 | }
|
231 | ;
|
232 |
|
233 | simpleStream.on('tag:mixed', function(node){
|
234 | node.should.deep.equal(output);
|
235 | done();
|
236 | });
|
237 | });
|
238 | });
|
239 |
|
240 | describe("toXml()", function(){
|
241 | it('should convert $attrs as expected', function(){
|
242 | var input = {$name: 'tag', $attrs:{id: 3}}
|
243 | , output = '<tag id="3"></tag>';
|
244 |
|
245 | flow.toXml(input).should.equal(output);
|
246 | });
|
247 |
|
248 | it('should convert $text as expected', function(){
|
249 | var input = {$name: 'tag', $attrs:{id: 3}, $text:'some text'}
|
250 | , output = '<tag id="3">some text</tag>';
|
251 |
|
252 | flow.toXml(input).should.equal(output);
|
253 | });
|
254 |
|
255 | it('should convert random fields as expected', function(){
|
256 | var input = {$name: 'tag', $attrs:{id: 3}, $text:'some text', p:'other text', j:['text1', 'text2']}
|
257 | , output = '<tag id="3">some text<p>other text</p><j>text1</j><j>text2</j></tag>';
|
258 |
|
259 | flow.toXml(input).should.equal(output);
|
260 | });
|
261 |
|
262 | it('should convert $markup', function(){
|
263 | var input = {$name: 'tag', $markup: ['text', {$name: 'j', $text: 'stuff'}]}
|
264 | , output = '<tag>text<j>stuff</j></tag>';
|
265 |
|
266 | flow.toXml(input).should.equal(output);
|
267 | });
|
268 |
|
269 | it('should convert $script', function(){
|
270 | var input = {$name: 'tag', $script:'console.log("stuff");'}
|
271 | , output = '<tag><script>console.log("stuff");</script></tag>';
|
272 |
|
273 | flow.toXml(input).should.equal(output);
|
274 | });
|
275 | });
|
276 | });
|