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

feat: implement oneOf with ability of handling pactum-matchers like… #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
31 changes: 26 additions & 5 deletions src/compare.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { isPrimitive, getType } = require('./helpers');
const { isPrimitive, getType, isPactumMatchersLikeObj } = require('./helpers');
const patterns = require('./patterns');
const { setRules } = require('./rules');

function compare(actual, expected, rules, path) {
const regex_rules = getRegExRules(rules);
Expand Down Expand Up @@ -83,7 +84,7 @@ function compareWithRule(actual, expected, rules, regex_rules, path, rule) {
compareWithRuleRegex(actual, rule, path);
break;
case 'oneOf':
compareWithRuleOneOf(actual, rule, path);
compareWithRuleOneOf(...arguments);
break;
case 'expr':
compareWithRuleExpr(actual, rule, path);
Expand Down Expand Up @@ -162,15 +163,35 @@ function compareWithRuleRegex(actual, rule, path) {
}
}

function compareWithRuleOneOf(actual, rule, path) {
function compareWithRuleOneOf(actual, expected, rules, regex_rules, path, rule) {
const values = rule.value;
let found = false;
const errorMsg = [];
for (let i = 0; i < values.length; i++) {
found = actual === values[i];
if (isPactumMatchersLikeObj(values[i])) {
const nxtRules = {};
setRules(nxtRules, values[i], path);
/**
* `_compare` func will throw an error if `actual` doesn't match `nxtRules`
* so if there is no error, `found` should be true
* otherwise we should continue the loop and handle the rest of `values`
*/
try {
_compare(actual, expected, nxtRules, regex_rules, path);
found = true;
} catch (error) {
errorMsg.push(error);
continue;
}
} else {
found = actual === values[i];
}
if (found) break;
}
if (!found) {
throw `Json doesn't have one of the expected values at "${path}" but found "${actual}"`;
errorMsg.push(`Json doesn't have one of the expected values at '${path}' but found '${actual}'`)
const str = errorMsg.join('\n')
throw str;
}
}

Expand Down
13 changes: 12 additions & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ function isPrimitive(value) {
return typeof value !== 'object';
}

function isPactumMatchersLikeObj(data) {
if (data === null) {
return false;
}
return (
Object.prototype.hasOwnProperty.call(data, 'pactum_type')
&& Object.prototype.hasOwnProperty.call(data, 'value')
);
}

function getType(value) {
const type = typeof value;
if (type === 'object') {
Expand All @@ -23,5 +33,6 @@ module.exports = {
isPureObject,
isObject,
getType,
isPrimitive
isPrimitive,
isPactumMatchersLikeObj
};