meld-spec
Version:
Specification for the Meld scripting language
78 lines (77 loc) • 2.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.compareNodes = compareNodes;
/**
* Compare two AST nodes for structural equality
*/
function compareNodes(actual, expected) {
if (actual.type !== expected.type) {
return false;
}
switch (actual.type) {
case 'Directive':
return compareDirectiveNodes(actual, expected);
case 'Error':
return compareErrorNodes(actual, expected);
// Add other node type comparisons as they are implemented
default:
return false;
}
}
/**
* Compare two directive nodes for structural equality
*/
function compareDirectiveNodes(actual, expected) {
if (actual.directive.kind !== expected.directive.kind) {
return false;
}
// Compare all fields in expected against actual
for (const key in expected.directive) {
const expectedValue = expected.directive[key];
const actualValue = actual.directive[key];
if (typeof expectedValue === 'object' && expectedValue !== null) {
if (!compareObjects(actualValue, expectedValue)) {
return false;
}
}
else if (actualValue !== expectedValue) {
return false;
}
}
return true;
}
/**
* Compare two error nodes for structural equality
* Ignores debugDetails since that's implementation-specific
*/
function compareErrorNodes(actual, expected) {
// For errors, we only care that the error message matches
// The exact wording isn't important as long as it conveys the same meaning
// We ignore debugDetails since that's implementation-specific
return actual.error.toLowerCase().includes(expected.error.toLowerCase()) ||
expected.error.toLowerCase().includes(actual.error.toLowerCase());
}
/**
* Deep compare two objects for structural equality
*/
function compareObjects(actual, expected) {
if (actual === expected)
return true;
if (actual == null || expected == null)
return false;
if (typeof actual !== typeof expected)
return false;
if (Array.isArray(actual) && Array.isArray(expected)) {
if (actual.length !== expected.length)
return false;
return actual.every((item, index) => compareObjects(item, expected[index]));
}
if (typeof actual === 'object') {
const actualKeys = Object.keys(actual);
const expectedKeys = Object.keys(expected);
if (actualKeys.length !== expectedKeys.length)
return false;
return expectedKeys.every(key => actual.hasOwnProperty(key) && compareObjects(actual[key], expected[key]));
}
return actual === expected;
}