Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Commit

Permalink
LanguageManager unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonsanjose committed Feb 26, 2013
1 parent 4ce9438 commit 9e93e5a
Show file tree
Hide file tree
Showing 6 changed files with 322 additions and 53 deletions.
6 changes: 4 additions & 2 deletions Gruntfile.js
Expand Up @@ -20,7 +20,7 @@
* DEALINGS IN THE SOFTWARE.
*
*/
/*global module, require*/
/*global module, require*/
module.exports = function (grunt) {
'use strict';

Expand Down Expand Up @@ -48,6 +48,7 @@ module.exports = function (grunt) {
/* specs that can run in phantom.js */
specs : [
'test/spec/CommandManager-test.js',
'test/spec/LanguageManager-test.js',
'test/spec/PreferencesManager-test.js',
'test/spec/ViewUtils-test.js'
]
Expand All @@ -73,7 +74,8 @@ module.exports = function (grunt) {
'src/thirdparty/CodeMirror2/lib/codemirror.js',
'src/thirdparty/CodeMirror2/lib/util/dialog.js',
'src/thirdparty/CodeMirror2/lib/util/searchcursor.js',
'src/thirdparty/mustache/mustache.js'
'src/thirdparty/mustache/mustache.js',
'src/thirdparty/path-utils/path-utils.min'
],
template : require('grunt-template-jasmine-requirejs'),
templateOptions: {
Expand Down
42 changes: 20 additions & 22 deletions src/file/NativeFileSystem.js
Expand Up @@ -990,30 +990,28 @@ define(function (require, exports, module) {

// Add the callbacks to this top-level Promise, which wraps all the individual deferred objects
timeoutWrapper.done(function () { // success
// The entries array may have null values if stat returned things that were
// neither a file nor a dir. So, we need to clean those out.
var cleanedEntries = [], i;
for (i = 0; i < entries.length; i++) {
if (entries[i]) {
cleanedEntries.push(entries[i]);
}
}
successCallback(cleanedEntries);
})
.fail(function (err) { // error
if (err === Async.ERROR_TIMEOUT) {
// SECURITY_ERR is the HTML5 File catch-all error, and there isn't anything
// more fitting for a timeout.
err = new NativeFileError(NativeFileError.SECURITY_ERR);
} else {
err = lastError;
}

if (errorCallback) {
errorCallback(err);
// The entries array may have null values if stat returned things that were
// neither a file nor a dir. So, we need to clean those out.
var cleanedEntries = [], i;
for (i = 0; i < entries.length; i++) {
if (entries[i]) {
cleanedEntries.push(entries[i]);
}
}
);
successCallback(cleanedEntries);
}).fail(function (err) { // error
if (err === Async.ERROR_TIMEOUT) {
// SECURITY_ERR is the HTML5 File catch-all error, and there isn't anything
// more fitting for a timeout.
err = new NativeFileError(NativeFileError.SECURITY_ERR);
} else {
err = lastError;
}

if (errorCallback) {
errorCallback(err);
}
});

} else { // There was an error reading the initial directory.
errorCallback(new NativeFileError(NativeFileSystem._fsErrorToDOMErrorName(err)));
Expand Down
1 change: 1 addition & 0 deletions test/SpecRunner.html
Expand Up @@ -40,6 +40,7 @@
<script src="../src/thirdparty/CodeMirror2/addon/search/searchcursor.js"></script>
<script src="../src/thirdparty/CodeMirror2/addon/edit/closetag.js"></script>
<script src="../src/thirdparty/mustache/mustache.js"></script>
<script src="../src/thirdparty/path-utils/path-utils.js"></script>
<script src="thirdparty/bootstrap2/js/bootstrap.min.js"></script>

<!-- All other scripts are loaded through require. -->
Expand Down
1 change: 1 addition & 0 deletions test/UnitTestSuite.js
Expand Up @@ -41,6 +41,7 @@ define(function (require, exports, module) {
require("spec/FindReplace-test");
require("spec/InlineEditorProviders-test");
require("spec/KeyBindingManager-test");
require("spec/LanguageManager-test");
require("spec/LiveDevelopment-test");
require("spec/LowLevelFileIO-test");
require("spec/Menu-test");
Expand Down
74 changes: 45 additions & 29 deletions test/spec/Editor-test.js
Expand Up @@ -32,6 +32,25 @@ define(function (require, exports, module) {
EditorManager = require("editor/EditorManager"),
SpecRunnerUtils = require("spec/SpecRunnerUtils"),
LanguageManager = require("language/LanguageManager");

var langNames = {
css: {mode: "css", lang: "CSS"},
javascript: {mode: "javascript", lang: "JavaScript"},
html: {mode: "html", lang: "HTML"}
};

function compareMode(expected, actual) {
if (typeof actual === "string") {
return actual === expected;
}

return actual.name === expected;
}

function expectModeAndLang(editor, lang) {
expect(editor.getModeForSelection()).toSpecifyModeNamed(lang.mode);
expect(editor.getLanguageForSelection()).toBe(lang.lang);
}

describe("Editor", function () {
var defaultContent = 'Brackets is going to be awesome!\n';
Expand All @@ -43,6 +62,14 @@ define(function (require, exports, module) {
myDocument = mocks.doc;
myEditor = mocks.editor;
}

beforeEach(function () {
this.addMatchers({
toSpecifyModeNamed: function (expected) {
return compareMode(expected, this.actual);
}
});
});

afterEach(function () {
if (myEditor) {
Expand Down Expand Up @@ -84,17 +111,6 @@ define(function (require, exports, module) {
});

describe("File extension to mode mapping", function () {
beforeEach(function () {
this.addMatchers({
toSpecifyModeNamed: function (expected) {
if (typeof this.actual === "string") {
return this.actual === expected;
} else {
return this.actual.name === expected;
}
}
});
});

it("should switch to the HTML mode for files ending in .html", function () {
// verify editor content
Expand All @@ -105,13 +121,13 @@ define(function (require, exports, module) {
it("should switch modes even if the url has a query string", function () {
// verify editor content
var mode = LanguageManager.getLanguageForFileExtension("http://only.org/testing/the/path.css?v=2").mode;
expect(mode).toSpecifyModeNamed("css");
expect(mode).toSpecifyModeNamed(langNames.css.mode);
});

it("should accept just a file name too", function () {
// verify editor content
var mode = LanguageManager.getLanguageForFileExtension("path.js").mode;
expect(mode).toSpecifyModeNamed("javascript");
expect(mode).toSpecifyModeNamed(langNames.javascript.mode);
});

it("should default to plaintext for unknown file extensions", function () {
Expand Down Expand Up @@ -147,57 +163,57 @@ define(function (require, exports, module) {
"</body></html>";

it("should get mode in homogenous file", function () {
createTestEditor(jsContent, "javascript");
createTestEditor(jsContent, langNames.javascript.mode);

// Mode at point
myEditor.setCursorPos(0, 0); // first char in text
expect(myEditor.getModeForSelection()).toBe("javascript");
expectModeAndLang(myEditor, langNames.javascript);
myEditor.setCursorPos(0, 8); // last char in text
expect(myEditor.getModeForSelection()).toBe("javascript");
expectModeAndLang(myEditor, langNames.javascript);

myEditor.setCursorPos(0, 3); // middle of text
expect(myEditor.getModeForSelection()).toBe("javascript");
expectModeAndLang(myEditor, langNames.javascript);

// Mode for range
myEditor.setSelection({line: 0, ch: 4}, {line: 0, ch: 7});
expect(myEditor.getModeForSelection()).toBe("javascript");
expectModeAndLang(myEditor, langNames.javascript);
myEditor.setSelection({line: 0, ch: 0}, {line: 0, ch: 8}); // select all
expect(myEditor.getModeForSelection()).toBe("javascript");
expectModeAndLang(myEditor, langNames.javascript);
});

it("should get mode in HTML file", function () {
createTestEditor(htmlContent, "html");

// Mode at point
myEditor.setCursorPos(0, 0); // first char in text
expect(myEditor.getModeForSelection()).toBe("html");
expectModeAndLang(myEditor, langNames.html);
myEditor.setCursorPos(6, 14); // last char in text
expect(myEditor.getModeForSelection()).toBe("html");
expectModeAndLang(myEditor, langNames.html);

myEditor.setCursorPos(5, 7); // middle of text - html
expect(myEditor.getModeForSelection()).toBe("html");
expectModeAndLang(myEditor, langNames.html);
myEditor.setCursorPos(2, 7); // middle of text - js
expect(myEditor.getModeForSelection()).toBe("javascript");
expectModeAndLang(myEditor, langNames.javascript);

// Mode for range - homogenous mode
myEditor.setSelection({line: 5, ch: 2}, {line: 5, ch: 14});
expect(myEditor.getModeForSelection()).toBe("html");
expectModeAndLang(myEditor, langNames.html);
myEditor.setSelection({line: 5, ch: 0}, {line: 6, ch: 0}); // whole line
expect(myEditor.getModeForSelection()).toBe("html");
expectModeAndLang(myEditor, langNames.html);
myEditor.setSelection({line: 2, ch: 4}, {line: 2, ch: 12});
expect(myEditor.getModeForSelection()).toBe("javascript");
expectModeAndLang(myEditor, langNames.javascript);
myEditor.setSelection({line: 2, ch: 0}, {line: 3, ch: 0}); // whole line
expect(myEditor.getModeForSelection()).toBe("javascript");
expectModeAndLang(myEditor, langNames.javascript);

// Mode for range - mix of modes
myEditor.setSelection({line: 2, ch: 4}, {line: 3, ch: 7});
expect(myEditor.getModeForSelection()).toBeNull();
expectModeAndLang(myEditor, null);

// Mode for range - mix of modes where start & endpoints are same mode
// Known limitation of getModeForSelection() that it does not spot where the mode
// differs in mid-selection
myEditor.setSelection({line: 0, ch: 0}, {line: 6, ch: 14}); // select all
expect(myEditor.getModeForSelection()).toBe("html");
expectModeAndLang(myEditor, langNames.html);
});

});
Expand Down

0 comments on commit 9e93e5a

Please sign in to comment.