Skip to content

Commit

Permalink
refactor: Rename createParser to configureTestParser
Browse files Browse the repository at this point in the history
- module name kebab case
- document it's usefulness and parameters
  • Loading branch information
karfau committed Sep 3, 2020
1 parent da98f4c commit 70d9ef3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
38 changes: 38 additions & 0 deletions test/configure-test-parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var {DOMParser} = require('../lib/dom-parser')

/**
* @typedef ErrorLevel {'warn' | 'error' | 'fatal'}
*/
/**
* Helper method for configuring an instance of DOMParser.
* Calling it without any arguments allows to assert on `errors` after using the parser.
* every field of the first argument is options and allows to specify test specific behavior.
* - `errorHandler`: The `locator` to pass to DOMParser constructor options,
* default stores a list of all entries per `key` in `errors` and does not log or throw.
* - `errors`: the object for the `errorHandler` to use,
* is also returned with the same name for later assertions,
* default is an empty object
* - `locator`: The `locator` to pass to DOMParser constructor options,
* default is an empty object
*
* @param options {{
* errorHandler?: function (key: ErrorLevel, msg: string),
* errors?: Partial<Record<ErrorLevel, string[]>>,
* locator?: Object
* }}
* @returns {{parser: DOMParser, errors: Partial<Record<ErrorLevel, string[]>>}}
*/
function configureTestParser ({errorHandler, errors = {}, locator = {}} = {}) {
errorHandler = errorHandler || ((key, msg) => {
if (!errors[key]) errors[key] = []
errors[key].push(msg)
})
return {
errors,
parser: new DOMParser({errorHandler, locator})
}
}

module.exports = {
configureTestParser
}
16 changes: 0 additions & 16 deletions test/createParser.js

This file was deleted.

4 changes: 2 additions & 2 deletions test/xmltest/not-wf.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const xmltest = require('xmltest')
const {createParser} = require('../createParser')
const {configureTestParser} = require('../configure-test-parser')

describe('xmltest/not-wellformed', () => {
describe('standalone', () => {
Expand All @@ -19,7 +19,7 @@ describe('xmltest/not-wellformed', () => {
// TODO: The DOCTYPE totally confuses xmldom :sic:
// for now we remove it and any newlines after it so we have reasonable tests
.replace(/^<!DOCTYPE doc \[[^\]]+]>[\r\n]*/m, '')
const {errors, parser} = createParser();
const {errors, parser} = configureTestParser();

// for 050.xml the result is undefined so `.toString()` needs to be careful
const actual = parser.parseFromString(input)?.toString()
Expand Down
4 changes: 2 additions & 2 deletions test/xmltest/valid.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const xmltest = require('xmltest')
const {createParser} = require('../createParser')
const {configureTestParser} = require('../configure-test-parser')


describe('xmltest/valid', () => {
Expand All @@ -16,7 +16,7 @@ describe('xmltest/valid', () => {
// for now we remove it and any newlines after it so we have reasonable tests
.replace(/^<!DOCTYPE doc \[[^\]]+]>[\r\n]*/m, '')
const expected = await xmltest.getContent(xmltest.RELATED.out(pathInZip))
const {errors, parser} = createParser();
const {errors, parser} = configureTestParser();

const actual = parser.parseFromString(input).toString()

Expand Down

0 comments on commit 70d9ef3

Please sign in to comment.