UNPKG

3.23 kBJavaScriptView Raw
1'use strict'
2
3const fs = require('fs')
4const parse = require('./lib/parse')
5const path = require('path')
6const pickup = require('../')
7const { test } = require('tap')
8
9const dir = path.join(__dirname, 'data')
10const files = fs.readdirSync(dir)
11const groups = files.reduce((acc, file) => {
12 const ext = path.extname(file)
13
14 if (ext !== '.json') return acc
15
16 let charset
17 if (file === 'atom-latin1.json') charset = 'ISO-8859-1'
18
19 const jsonFile = path.join(dir, file)
20 const xmlFile = path.join(dir, file.split(ext)[0] + '.xml')
21
22 const xml = fs.readFileSync(xmlFile)
23 const json = JSON.parse(fs.readFileSync(jsonFile))
24
25 const group = { name: file, xml: xml, data: json, charset: charset }
26
27 acc.push(group)
28 return acc
29}, [])
30
31test('plain mode', (t) => {
32 let i = 0
33
34 function run (group) {
35 if (!group) {
36 t.is(i, groups.length)
37 return t.end()
38 }
39
40 const entries = group.data.entries
41 const feed = group.data.feed
42 const xml = group.xml
43 const wanted = entries.map((entry) => {
44 return ['data', entry]
45 })
46
47 wanted.push(['data', feed])
48 wanted.push(['finish'])
49 wanted.push(['end'])
50
51 parse({
52 xml: xml,
53 wanted: wanted,
54 eventMode: false,
55 objectMode: false,
56 encoding: 'utf8',
57 charset: group.charset,
58 t: t
59 }, (er) => {
60 t.ok(!er)
61 run(groups[++i])
62 })
63 }
64
65 run(groups[i])
66})
67
68test('object mode', (t) => {
69 let i = 0
70
71 function run (group) {
72 if (!group) {
73 t.is(i, groups.length)
74 return t.end()
75 }
76
77 const entries = group.data.entries
78 const feed = group.data.feed
79 const xml = group.xml
80 const wanted = entries.map((entry) => {
81 return ['data', pickup.entry(entry)]
82 })
83
84 wanted.push(['data', pickup.feed(feed)])
85 wanted.push(['finish'])
86 wanted.push(['end'])
87
88 parse({
89 xml: xml,
90 wanted: wanted,
91 eventMode: false,
92 objectMode: true,
93 charset: group.charset,
94 t: t
95 }, (er) => {
96 t.ok(!er)
97 run(groups[++i])
98 })
99 }
100
101 run(groups[i])
102})
103
104test('event mode', (t) => {
105 let i = 0
106
107 function run (group) {
108 if (!group) {
109 t.is(i, groups.length)
110 return t.end()
111 }
112
113 const feed = group.data.feed
114 const entries = group.data.entries
115 const xml = group.xml
116 const wanted = entries.map((entry) => {
117 return ['entry', entry]
118 })
119
120 wanted.push(['feed', feed])
121 wanted.push(['finish'])
122 wanted.push(['end'])
123
124 parse({
125 xml: xml,
126 wanted: wanted,
127 charset: group.charset,
128 t: t
129 }, (er) => {
130 t.ok(!er, 'should not error')
131 run(groups[++i])
132 })
133 }
134
135 run(groups[i])
136})
137
138test('event mode (concurrently)', (t) => {
139 let i = groups.length
140
141 function cb (er) {
142 t.ok(!er, 'should not error')
143 if (--i === 0) { t.end() }
144 }
145
146 groups.forEach((group) => {
147 const feed = group.data.feed
148 const entries = group.data.entries
149 const xml = group.xml
150 const wanted = entries.map((entry) => {
151 return ['entry', entry]
152 })
153
154 wanted.push(['feed', feed])
155 wanted.push(['finish'])
156 wanted.push(['end'])
157
158 parse({
159 xml: xml,
160 wanted: wanted,
161 charset: group.charset,
162 t: t
163 }, cb)
164 })
165})