Extend QUnit a bit more cleanly

This commit is contained in:
David Majda 2012-04-16 15:45:50 +02:00
parent abf33d3feb
commit f3970bfa5c

View file

@ -1,26 +1,29 @@
parses = function(parser, input, expected) {
deepEqual(parser.parse(input), expected);
};
(function(global) {
parsesWithStartRule = function(parser, input, startRule, expected) {
deepEqual(parser.parse(input, startRule), expected);
};
var extensions = {
parses: function(parser, input, expected) {
QUnit.deepEqual(parser.parse(input), expected);
},
doesNotParse = function(parser, input) {
raises(function() { parser.parse(input); }, parser.SyntaxError);
};
parsesWithStartRule: function(parser, input, startRule, expected) {
QUnit.deepEqual(parser.parse(input, startRule), expected);
},
doesNotParseWithMessage = function(parser, input, message) {
raises(
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) {
raises(
doesNotParseWithDetails: function(parser, input, expected, found, message) {
QUnit.raises(
function() { parser.parse(input); },
function(e) {
var i;
@ -36,10 +39,10 @@ doesNotParseWithDetails = function(parser, input, expected, found, message) {
return true;
}
);
};
},
doesNotParseWithPos = function(parser, input, offset, line, column) {
raises(
doesNotParseWithPos: function(parser, input, offset, line, column) {
QUnit.raises(
function() { parser.parse(input); },
function(e) {
return e instanceof parser.SyntaxError
@ -48,16 +51,22 @@ doesNotParseWithPos = function(parser, input, offset, line, column) {
&& e.column === column;
}
);
},
parserParses: function(input, expected) {
QUnit.parses(PEG.parser, input, expected);
},
parserDoesNotParse: function(input) {
QUnit.doesNotParse(PEG.parser, input);
},
parserDoesNotParseWithMessage: function(input, message) {
QUnit.doesNotParseWithMessage(PEG.parser, input, message);
}
};
parserParses = function(input, expected) {
parses(PEG.parser, input, expected);
};
QUnit.extend(QUnit, extensions);
QUnit.extend(global, extensions);
parserDoesNotParse = function(input) {
doesNotParse(PEG.parser, input);
};
parserDoesNotParseWithMessage = function(input, message) {
doesNotParseWithMessage(PEG.parser, input, message);
};
})(this);