pegjs/test/helpers.js

73 lines
1.9 KiB
JavaScript
Raw Normal View History

2012-04-16 15:45:50 +02:00
(function(global) {
2012-04-16 15:45:50 +02:00
var extensions = {
parses: function(parser, input, expected) {
QUnit.deepEqual(parser.parse(input), expected);
},
2012-04-16 15:45:50 +02:00
parsesWithStartRule: function(parser, input, startRule, expected) {
QUnit.deepEqual(parser.parse(input, startRule), expected);
},
2012-04-16 15:45:50 +02:00
doesNotParse: function(parser, input) {
QUnit.raises(function() { parser.parse(input); }, parser.SyntaxError);
},
doesNotParseWithMessage: function(parser, input, message) {
QUnit.raises(
function() { parser.parse(input); },
function(e) {
return e instanceof parser.SyntaxError && e.message === message;
}
);
},
doesNotParseWithDetails: function(parser, input, expected, found, message) {
QUnit.raises(
function() { parser.parse(input); },
function(e) {
var i;
2012-04-16 15:45:50 +02:00
if (!(e instanceof parser.SyntaxError)) { return false; }
if (e.expected.length !== expected.length) { return false; }
for (i = 0; i < e.expected.length; i++) {
if (e.expected[i] !== expected[i]) { return false; }
}
if (e.found !== found) { return false; }
if (e.message !== message) { return false; }
2012-04-16 15:45:50 +02:00
return true;
}
2012-04-16 15:45:50 +02:00
);
},
2012-04-16 15:45:50 +02:00
doesNotParseWithPos: function(parser, input, offset, line, column) {
QUnit.raises(
function() { parser.parse(input); },
function(e) {
return e instanceof parser.SyntaxError
&& e.offset === offset
&& e.line === line
&& e.column === column;
}
);
},
2012-04-16 15:45:50 +02:00
parserParses: function(input, expected) {
QUnit.parses(PEG.parser, input, expected);
},
2012-04-16 15:45:50 +02:00
parserDoesNotParse: function(input) {
QUnit.doesNotParse(PEG.parser, input);
},
2012-04-16 15:45:50 +02:00
parserDoesNotParseWithMessage: function(input, message) {
QUnit.doesNotParseWithMessage(PEG.parser, input, message);
}
};
2012-04-16 15:45:50 +02:00
QUnit.extend(QUnit, extensions);
QUnit.extend(global, extensions);
})(this);