1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/packages/fork-sax/test/index.js
2020-11-05 16:58:23 +00:00

58 lines
1.3 KiB
JavaScript

var sax = require('../lib/sax')
var t = require('tap')
exports.sax = sax
// handy way to do simple unit tests
// if the options contains an xml string, it'll be written and the parser closed.
// otherwise, it's assumed that the test will write and close.
exports.test = function test (options) {
var xml = options.xml
var parser = sax.parser(options.strict, options.opt)
var expect = options.expect
var e = 0
sax.EVENTS.forEach(function (ev) {
parser['on' + ev] = function (n) {
if (process.env.DEBUG) {
console.error({
expect: expect[e],
actual: [ev, n]
})
}
if (e >= expect.length && (ev === 'end' || ev === 'ready')) {
return
}
t.ok(e < expect.length, 'no unexpected events')
if (!expect[e]) {
t.fail('did not expect this event', {
event: ev,
expect: expect,
data: n
})
return
}
t.equal(ev, expect[e][0])
if (ev === 'error') {
t.equal(n.message, expect[e][1])
} else {
t.deepEqual(n, expect[e][1])
}
e++
if (ev === 'error') {
parser.resume()
}
}
})
if (xml) {
parser.write(xml).close()
}
return parser
}
if (module === require.main) {
t.pass('common test file')
}