Add tests for jscodehints in an html file.

Also fix some bugs with jump-to-def and function type hints uncovered by the tests.
This commit is contained in:
Erik Tierney committed 2013-05-02 16:28:40 -04:00
1 parent 0755c30d58
commit 9416e75828
3 files changed
+69 -10

No files matched your search

@@ -152,7 +152,7 @@ define(function (require, exports, module) {
dir = split.dir,
file = split.file;
var ternPromise = getJumptoDef(dir, file, offset, document.getText());
var ternPromise = getJumptoDef(dir, file, offset, session.getJavascriptText());
return {promise: ternPromise};
}
@@ -254,14 +254,14 @@ define(function (require, exports, module) {
* function being called
*/
Session.prototype.getType = function () {
function getLexicalInfo(token) {
function getLexicalState(token) {
if (token.state.lexical) {
// in a javascript file this is just in the state field
return token.state.lexical.info;
return token.state.lexical;
} else if (token.state.localState && token.state.localState.lexical) {
// inline javascript in an html file will have this in
// the localState field
return token.state.localState.lexical.info;
return token.state.localState.lexical;
}
}
var propertyLookup = false,
@@ -270,12 +270,14 @@ define(function (require, exports, module) {
context = null,
cursor = this.getCursor(),
functionCallPos,
token = this.getToken(cursor);
token = this.getToken(cursor),
lexical;
if (token) {
// if this token is part of a function call, then the tokens lexical info
// will be annotated with "call"
if (getLexicalInfo(token) === "call") {
lexical = getLexicalState(token);
if (lexical.info === "call") {
inFunctionCall = true;
if (this.getQuery().length > 0) {
inFunctionCall = false;
@@ -292,7 +294,7 @@ define(function (require, exports, module) {
// and it will prevent us from walking back thousands of lines if something went wrong.
// there is nothing magical about 9 lines, and it can be adjusted if it doesn't seem to be
// working well
var col = token.state.lexical.column,
var col = lexical.column,
line,
e,
found;
@@ -37,8 +37,11 @@ define(function (require, exports, module) {
var extensionPath = FileUtils.getNativeModuleDirectoryPath(module),
testPath = extensionPath + "/test/file1.js",
testHtmlPath = extensionPath + "/test/index.html",
testDoc = null,
testEditor;
testHtmlDoc = null,
testEditor,
testHtmlEditor;
/**
* Returns an Editor suitable for use in isolation, given a Document. (Unlike
@@ -76,7 +79,7 @@ define(function (require, exports, module) {
if (key === undefined) {
key = null;
}
expect(provider.hasHints(testEditor, key)).toBe(true);
return provider.getHints(null);
}
@@ -317,6 +320,19 @@ define(function (require, exports, module) {
}
/**
* Should be called at the beginning of any tests for hints in an html file.
* Start a test int he index.html test file, instead
* of the file1.js file.
* This switches the document and editor to point to the html file
* and re-inits the hinting session with the html editor
*/
function startHtmlTest() {
testEditor = testHtmlEditor;
testDoc = testHtmlDoc;
JSCodeHints.initializeSession(testEditor);
}
describe("JavaScript Code Hinting", function () {
beforeEach(function () {
@@ -324,14 +340,18 @@ define(function (require, exports, module) {
DocumentManager.getDocumentForPath(testPath).done(function (doc) {
testDoc = doc;
});
DocumentManager.getDocumentForPath(testHtmlPath).done(function (doc) {
testHtmlDoc = doc;
});
waitsFor(function () {
return testDoc !== null;
return testDoc !== null && testHtmlDoc !== null;
}, "Unable to open test document", 10000);
// create Editor instance (containing a CodeMirror instance)
runs(function () {
testEditor = createMockEditor(testDoc);
testHtmlEditor = createMockEditor(testHtmlDoc);
JSCodeHints.initializeSession(testEditor);
});
});
@@ -731,6 +751,43 @@ define(function (require, exports, module) {
});
});
it("basic codehints in html file", function () {
var start = { line: 30, ch: 9 },
end = { line: 30, ch: 11 };
startHtmlTest();
testDoc.replaceRange("x.", start, start);
testEditor.setCursorPos(end);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
runs(function () {
hintsPresentOrdered(hintObj, ["charAt", "charCodeAt", "concat", "indexOf"]);
});
});
it("function type hint in html file", function () {
var start = { line: 29, ch: 12 };
startHtmlTest();
testEditor.setCursorPos(start);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
runs(function () {
hintsPresentExact(hintObj, ["foo(a: number) -> string"]);
});
});
it("jump-to-def in html file", function () {
var start = { line: 29, ch: 10 };
startHtmlTest();
testEditor.setCursorPos(start);
runs(function () {
editorJumped({line: 18, ch: 20});
});
});
});
});