pegjs/spec/unit/compiler/passes/helpers.js
David Majda 6fa8ad63f9 Replace some functions with arrow functions
Because arrow functions work rather differently than normal functions (a
bad design mistake if you ask me), I decided to be conservative with the
conversion.

I converted:

  * event handlers
  * callbacks
  * arguments to Array.prototype.map & co.
  * small standalone lambda functions

I didn't convert:

  * functions assigned to object literal properties (the new shorthand
    syntax would be better here)
  * functions passed to "describe", "it", etc. in specs (because Jasmine
    relies on dynamic "this")

See #442.
2016-09-12 16:07:43 +02:00

103 lines
2.9 KiB
JavaScript

"use strict";
let peg = require("../../../../lib/peg");
beforeEach(function() {
this.addMatchers({
toChangeAST: function(grammar, details, options) {
options = options !== undefined ? options : {};
function matchDetails(value, details) {
function isArray(value) {
return Object.prototype.toString.apply(value) === "[object Array]";
}
function isObject(value) {
return value !== null && typeof value === "object";
}
if (isArray(details)) {
if (!isArray(value)) { return false; }
if (value.length !== details.length) { return false; }
for (let i = 0; i < details.length; i++) {
if (!matchDetails(value[i], details[i])) { return false; }
}
return true;
} else if (isObject(details)) {
if (!isObject(value)) { return false; }
for (let key in details) {
if (details.hasOwnProperty(key)) {
if (!(key in value)) { return false; }
if (!matchDetails(value[key], details[key])) { return false; }
}
}
return true;
} else {
return value === details;
}
}
let ast = peg.parser.parse(grammar);
this.actual(ast, options);
this.message = () =>
"Expected the pass "
+ "with options " + jasmine.pp(options) + " "
+ (this.isNot ? "not " : "")
+ "to change the AST " + jasmine.pp(ast) + " "
+ "to match " + jasmine.pp(details) + ", "
+ "but it " + (this.isNot ? "did" : "didn't") + ".";
return matchDetails(ast, details);
},
toReportError: function(grammar, details) {
let ast = peg.parser.parse(grammar);
try {
this.actual(ast);
} catch (e) {
if (this.isNot) {
this.message = () =>
"Expected the pass not to report an error "
+ "for grammar " + jasmine.pp(grammar) + ", "
+ "but it did.";
} else {
if (details) {
for (let key in details) {
if (details.hasOwnProperty(key)) {
if (!this.env.equals_(e[key], details[key])) {
this.message = () =>
"Expected the pass to report an error "
+ "with details " + jasmine.pp(details) + " "
+ "for grammar " + jasmine.pp(grammar) + ", "
+ "but " + jasmine.pp(key) + " "
+ "is " + jasmine.pp(e[key]) + ".";
return false;
}
}
}
}
}
return true;
}
this.message = () =>
"Expected the pass to report an error "
+ (details ? "with details " + jasmine.pp(details) + " ": "")
+ "for grammar " + jasmine.pp(grammar) + ", "
+ "but it didn't.";
return false;
}
});
});