| 1 |
|
| 2 |
|
| 3 |
"use strict"; |
| 4 |
|
| 5 |
beforeEach(function() { |
| 6 |
this.addMatchers({ |
| 7 |
toChangeAST: function(grammar, details, options) { |
| 8 |
options = options !== undefined ? options : {}; |
| 9 |
|
| 10 |
function matchDetails(value, details) { |
| 11 |
function isArray(value) { |
| 12 |
return Object.prototype.toString.apply(value) === "[object Array]"; |
| 13 |
} |
| 14 |
|
| 15 |
function isObject(value) { |
| 16 |
return value !== null && typeof value === "object"; |
| 17 |
} |
| 18 |
|
| 19 |
var i, key; |
| 20 |
|
| 21 |
if (isArray(details)) { |
| 22 |
if (!isArray(value)) { return false; } |
| 23 |
|
| 24 |
if (value.length !== details.length) { return false; } |
| 25 |
for (i = 0; i < details.length; i++) { |
| 26 |
if (!matchDetails(value[i], details[i])) { return false; } |
| 27 |
} |
| 28 |
|
| 29 |
return true; |
| 30 |
} else if (isObject(details)) { |
| 31 |
if (!isObject(value)) { return false; } |
| 32 |
|
| 33 |
for (key in details) { |
| 34 |
if (details.hasOwnProperty(key)) { |
| 35 |
if (!(key in value)) { return false; } |
| 36 |
|
| 37 |
if (!matchDetails(value[key], details[key])) { return false; } |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
return true; |
| 42 |
} else { |
| 43 |
return value === details; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
var ast = peg.parser.parse(grammar); |
| 48 |
|
| 49 |
this.actual(ast, options); |
| 50 |
|
| 51 |
this.message = function() { |
| 52 |
return "Expected the pass " |
| 53 |
+ "with options " + jasmine.pp(options) + " " |
| 54 |
+ (this.isNot ? "not " : "") |
| 55 |
+ "to change the AST " + jasmine.pp(ast) + " " |
| 56 |
+ "to match " + jasmine.pp(details) + ", " |
| 57 |
+ "but it " + (this.isNot ? "did" : "didn't") + "."; |
| 58 |
}; |
| 59 |
|
| 60 |
return matchDetails(ast, details); |
| 61 |
}, |
| 62 |
|
| 63 |
toReportError: function(grammar, details) { |
| 64 |
var ast = peg.parser.parse(grammar), |
| 65 |
key; |
| 66 |
|
| 67 |
try { |
| 68 |
this.actual(ast); |
| 69 |
} catch (e) { |
| 70 |
if (this.isNot) { |
| 71 |
this.message = function() { |
| 72 |
return "Expected the pass not to report an error " |
| 73 |
+ "for grammar " + jasmine.pp(grammar) + ", " |
| 74 |
+ "but it did."; |
| 75 |
}; |
| 76 |
} else { |
| 77 |
if (details) { |
| 78 |
for (key in details) { |
| 79 |
if (details.hasOwnProperty(key)) { |
| 80 |
if (!this.env.equals_(e[key], details[key])) { |
| 81 |
this.message = function() { |
| 82 |
return "Expected the pass to report an error " |
| 83 |
+ "with details " + jasmine.pp(details) + " " |
| 84 |
+ "for grammar " + jasmine.pp(grammar) + ", " |
| 85 |
+ "but " + jasmine.pp(key) + " " |
| 86 |
+ "is " + jasmine.pp(e[key]) + "."; |
| 87 |
}; |
| 88 |
|
| 89 |
return false; |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return true; |
| 97 |
} |
| 98 |
|
| 99 |
this.message = function() { |
| 100 |
return "Expected the pass to report an error " |
| 101 |
+ (details ? "with details " + jasmine.pp(details) + " ": "") |
| 102 |
+ "for grammar " + jasmine.pp(grammar) + ", " |
| 103 |
+ "but it didn't."; |
| 104 |
}; |
| 105 |
|
| 106 |
return false; |
| 107 |
} |
| 108 |
}); |
| 109 |
}); |