test-openapi
Version:
Automated client requests
71 lines (50 loc) • 1.4 kB
JavaScript
const { getDirective } = require('./directive')
const { getErrorProps } = require('./error_props')
const { checkArgument } = require('./check')
// TAP assert
const assert = function(state, { ok, name = '', directive = {}, error }) {
const category = getCategory({ ok, directive })
const index = updateState({ state, category })
const okString = getOk({ ok })
const nameString = getName({ name })
const directiveString = getDirective({ directive })
const errorProps = getErrorProps({ ok, error })
return state.colors[category](
`${okString} ${index}${nameString}${directiveString}${errorProps}\n\n`,
)
}
const getCategory = function({ ok, directive: { skip } }) {
if (skip !== undefined && skip !== false) {
return 'skip'
}
if (ok) {
return 'pass'
}
return 'fail'
}
// Update index|tests|pass|skip|fail counters
const updateState = function({ state, category }) {
// eslint-disable-next-line fp/no-mutation, no-param-reassign
state[category] += 1
// eslint-disable-next-line fp/no-mutation, no-param-reassign
state.index += 1
return state.index
}
const getOk = function({ ok }) {
checkArgument(ok, 'boolean')
if (ok) {
return 'ok'
}
return 'not ok'
}
const getName = function({ name }) {
checkArgument(name, 'string')
if (name === '') {
return ''
}
return ` ${name}`
}
module.exports = {
assert,
}