Fix start position for HTML comments and add tests.

This commit is contained in:
Max Schaefer
2014-09-03 18:20:39 +01:00
committed by Marijn Haverbeke
parent 6c854ad221
commit 7da3b6f1fd
3 changed files with 69 additions and 16 deletions

View File

@@ -2,8 +2,8 @@
var tests = [];
var acorn = typeof require == "undefined" ? window.acorn : require("../acorn.js");
exports.test = function(code, ast, options) {
tests.push({code: code, ast: ast, options: options});
exports.test = function(code, ast, options, comments) {
tests.push({code: code, ast: ast, options: options, comments: comments});
};
exports.testFail = function(code, message, options) {
tests.push({code: code, error: message, options: options});
@@ -13,10 +13,26 @@
};
exports.runTests = function(callback) {
var opts = {locations: true};
var comments;
function onComment(block, text, start, end, startLoc, endLoc) {
comments.push({
block: block,
text: text,
start: start,
end: end,
startLoc: { line: startLoc.line, column: startLoc.column },
endLoc: { line: endLoc.line, column: endLoc.column }
});
}
var opts = {locations: true, onComment: onComment};
for (var i = 0; i < tests.length; ++i) {
var test = tests[i];
try {
comments = [];
if (test.options && !test.options.onComment) test.options.onComment = onComment;
var ast = acorn.parse(test.code, test.options || opts);
if (test.error) callback("fail", test.code,
"Expected error message: " + test.error + "\nBut parsing succeeded.");
@@ -27,6 +43,8 @@
else callback("ok", test.code);
} else {
var mis = misMatch(test.ast, ast);
if (mis) callback("fail", test.code, mis);
if (test.comments) mis = misMatch(test.comments, comments);
if (!mis) callback("ok", test.code);
else callback("fail", test.code, mis);
}

View File

@@ -28645,11 +28645,48 @@ testFail("for(const x = 0;;);", "Unexpected token (1:4)", {ecmaVersion: 6});
);
})();
test("<!--\n;", {
type: "Program",
body: [
{
type: "EmptyStatement"
}
]
}
);
(function() {
var comments = 0;
testAssert("\nfunction plop() {\n'use strict';\n/* Comment */\n}", function() {
if (comments != 1) return "Comment after strict counted twice.";
}, {onComment: function() {++comments;}});
test("\nfunction plop() {\n'use strict';\n/* Comment */\n}", {}, {locations: true},
[{
block: true,
text: " Comment ",
startLoc: { line: 4, column: 0 },
endLoc: { line: 4, column: 13 }
}]);
test("// line comment", {}, {locations: true},
[{
block: false,
text: " line comment",
startLoc: { line: 1, column: 0 },
endLoc: { line: 1, column: 15 }
}]);
test("<!-- HTML comment", {}, {locations: true},
[{
block: false,
text: " HTML comment",
startLoc: { line: 1, column: 0 },
endLoc: { line: 1, column: 17 }
}]);
test(";\n--> HTML comment", {}, {locations: true},
[{
block: false,
text: " HTML comment",
startLoc: { line: 2, column: 0 },
endLoc: { line: 2, column: 16 }
}]);
})();
(function() {
@@ -28747,4 +28784,4 @@ testFail("for(const x = 0;;);", "Unexpected token (1:4)", {ecmaVersion: 6});
actualTokens.push(token);
}
});
})();
})();