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