Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Added a match function for tests #2541

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 66 additions & 1 deletion test/lib/custom-assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,74 @@ function compareSegments(parent, segments) {
})
}

/**
* Like `tap.prototype.match`. Verifies that `actual` satisfies the shape
* provided by `expected`.
*
* This may eventually make its way into `node:assert`. See
* https://github.com/fastify/fastify/discussions/5628#discussioncomment-10392942
*
* @example
* const input = {
* foo: /^foo.+bar$/,
* bar: [1, 2, '3']
* }
* // true
* match(input, {
* foo: 'foo is bar',
* bar: [1, 2, '3']
* })
* // false
* match(input, {
* foo: 'foo is bar',
* bar: [1, 2, '3', 4]
* })
*
* @param {string|object} actual The entity to verify.
* @param {string|object} expected What the entity should match against.
*
* @returns {boolean} `true` if `actual` satisfies `expected`. `false`
* otherwise.
*/
function match(actual, expected) {
if (typeof actual === 'string' && typeof expected === 'string') {
const patterns = expected
.trim()
.split(/\r?\n/)
.map((s) => s.trim())

let lastIndex = -1
for (const pattern of patterns) {
const index = actual.indexOf(pattern)
if (index === -1 || index < lastIndex) {
return false
}
lastIndex = index
}
return true
}

for (const key in expected) {
if (key in actual) {
if (typeof expected[key] === 'object' && expected[key] !== null) {
/* c8 ignore next 3 */
if (!match(actual[key], expected[key])) {
return false
}
} else if (actual[key] !== expected[key]) {
return false
}
} else {
return false
}
}
return true
}

module.exports = {
assertCLMAttrs,
assertExactClmAttrs,
compareSegments,
isNonWritable
isNonWritable,
match
}
Loading