Skip to content

Commit

Permalink
added viewer for results comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelangarano committed Sep 7, 2023
1 parent bfd11fe commit 675f830
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 8,013 deletions.
93 changes: 40 additions & 53 deletions e2e/cypress-12-demo/scripts/validate.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,53 @@
import fs from "fs";

function compareObjectsRecursively(objA: Record<string, any>, objB: Record<string, any>, path = '') {
let result: { isEqual: boolean, path: string, valueA: any, valueB: any }[] = [];

if (Array.isArray(objA) && Array.isArray(objB)) {
for (let i = 0; i < Math.max(objA.length, objB.length); i++) {
const newPath = `${path}[${i}]`;
if (i >= objA.length || i >= objB.length || typeof objA[i] !== typeof objB[i]) {
result.push({
path: newPath,
valueA: objA[i] || 'Does not exist',
valueB: objB[i] || 'Does not exist',
isEqual: false
});
} else {
result = result.concat(compareObjectsRecursively(objA[i], objB[i], newPath));
}
}
} else {
for (let key in objA) {
type ComparisonResult = {
path: string;
valueA: any;
valueB: any;
isEqual: boolean;
note?: string;
};

function compareObjectsRecursively(objA: any, objB: any, path: string = ''): ComparisonResult[] {
let results: ComparisonResult[] = [];

// Si ambos son objetos pero no arrays ni strings
if (typeof objA === 'object' && objA !== null && !(objA instanceof Array) && typeof objA !== 'string' &&
typeof objB === 'object' && objB !== null && !(objB instanceof Array) && typeof objB !== 'string') {

const keys = new Set([...Object.keys(objA), ...Object.keys(objB)]);

keys.forEach(key => {
const newPath = path ? `${path}.${key}` : key;

if (objB.hasOwnProperty(key) && typeof objA[key] === 'object' && objA[key] !== null && typeof objB[key] === 'object') {
result = result.concat(compareObjectsRecursively(objA[key], objB[key], newPath));
} else {
if (!objB.hasOwnProperty(key) || objA[key] !== objB[key]) {
result.push({
path: newPath,
valueA: objA[key],
valueB: objB.hasOwnProperty(key) ? objB[key] : 'Does not exist',
isEqual: false
});
} else {
result.push({
path: newPath,
valueA: objA[key],
valueB: objB[key],
isEqual: true
});
}
}
}

for (let key in objB) {
if (!objA.hasOwnProperty(key)) {
const newPath = path ? `${path}.${key}` : key;
result.push({
path: newPath,
valueA: 'Does not exist',
valueB: objB[key],
isEqual: false
});
}
results = results.concat(compareObjectsRecursively(objA[key], objB[key], newPath));
});
}
// Si ambos son arrays
else if (Array.isArray(objA) && Array.isArray(objB)) {
const maxLength = Math.max(objA.length, objB.length);
for (let i = 0; i < maxLength; i++) {
const newPath = `${path}[${i}]`;
results = results.concat(compareObjectsRecursively(objA[i], objB[i], newPath));
}
}
else {
const isEqual = objA === objB;
const note = objA === undefined ? "Does not exist in A" : objB === undefined ? "Does not exist in B" : undefined;

results.push({
path: path || 'root',
valueA: objA,
valueB: objB,
isEqual: isEqual,
note: note
});
}

return result;
return results;
}




(async function runValidation() {
try {
const originalCurrentApiFile = fs.readFileSync('data-references/original-cypress-12/currents-api-output-reference.json', 'utf8');
Expand Down
Loading

0 comments on commit 675f830

Please sign in to comment.