You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.1 KiB
JavaScript
89 lines
2.1 KiB
JavaScript
10 years ago
|
"use strict";
|
||
|
|
||
8 years ago
|
let peg = require("../../../../lib/peg");
|
||
8 years ago
|
|
||
8 years ago
|
module.exports = function(chai, utils) {
|
||
|
let Assertion = chai.Assertion;
|
||
11 years ago
|
|
||
8 years ago
|
Assertion.addMethod("changeAST", function(grammar, props, options) {
|
||
|
options = options !== undefined ? options : {};
|
||
11 years ago
|
|
||
8 years ago
|
function matchProps(value, props) {
|
||
|
function isArray(value) {
|
||
|
return Object.prototype.toString.apply(value) === "[object Array]";
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
function isObject(value) {
|
||
|
return value !== null && typeof value === "object";
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
if (isArray(props)) {
|
||
|
if (!isArray(value)) { return false; }
|
||
11 years ago
|
|
||
8 years ago
|
if (value.length !== props.length) { return false; }
|
||
|
for (let i = 0; i < props.length; i++) {
|
||
|
if (!matchProps(value[i], props[i])) { return false; }
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
return true;
|
||
|
} else if (isObject(props)) {
|
||
|
if (!isObject(value)) { return false; }
|
||
8 years ago
|
|
||
8 years ago
|
let keys = Object.keys(props);
|
||
|
for (let i = 0; i < keys.length; i++) {
|
||
|
let key = keys[i];
|
||
11 years ago
|
|
||
8 years ago
|
if (!(key in value)) { return false; }
|
||
11 years ago
|
|
||
8 years ago
|
if (!matchProps(value[key], props[key])) { return false; }
|
||
11 years ago
|
}
|
||
|
|
||
|
return true;
|
||
8 years ago
|
} else {
|
||
|
return value === props;
|
||
11 years ago
|
}
|
||
8 years ago
|
}
|
||
|
|
||
|
let ast = peg.parser.parse(grammar);
|
||
|
|
||
|
utils.flag(this, "object")(ast, options);
|
||
|
|
||
|
this.assert(
|
||
|
matchProps(ast, props),
|
||
|
"expected #{this} to change the AST to match #{exp}",
|
||
|
"expected #{this} to not change the AST to match #{exp}",
|
||
|
props,
|
||
|
ast
|
||
|
);
|
||
|
});
|
||
|
|
||
|
Assertion.addMethod("reportError", function(grammar, props) {
|
||
|
let ast = peg.parser.parse(grammar);
|
||
8 years ago
|
|
||
8 years ago
|
let passed, result;
|
||
|
|
||
|
try {
|
||
|
utils.flag(this, "object")(ast);
|
||
|
passed = true;
|
||
|
} catch (e) {
|
||
|
result = e;
|
||
|
passed = false;
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
this.assert(
|
||
|
!passed,
|
||
|
"expected #{this} to report an error but it didn't",
|
||
|
"expected #{this} to not report an error but #{act} was reported",
|
||
|
null,
|
||
|
result
|
||
|
);
|
||
|
|
||
|
if (!passed && props !== undefined) {
|
||
|
Object.keys(props).forEach(key => {
|
||
|
new Assertion(result).to.have.property(key)
|
||
|
.that.is.deep.equal(props[key]);
|
||
|
});
|
||
11 years ago
|
}
|
||
|
});
|
||
8 years ago
|
};
|