brackets/test/spec/ProjectManager-test.js

174 строки
6.9 KiB
JavaScript
Исходник Обычный вид История

2012-04-25 22:55:41 +04:00
/*
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true, indent: 4, maxerr: 50 */
/*global define: false, require: false, describe: false, it: false, expect: false, beforeEach: false, afterEach: false, waitsFor: false, runs: false */
define(function (require, exports, module) {
'use strict';
2012-01-05 05:27:47 +04:00
// Load dependent modules
var ProjectManager, // Load from brackets.test
2012-07-14 01:27:30 +04:00
SpecRunnerUtils = require("spec/SpecRunnerUtils");
describe("ProjectManager", function () {
2012-01-05 05:27:47 +04:00
var testPath = SpecRunnerUtils.getTestPath("/spec/ProjectManager-test-files"),
brackets;
beforeEach(function () {
SpecRunnerUtils.createTestWindowAndRun(this, function (testWindow) {
// Load module instances from brackets.test
brackets = testWindow.brackets;
ProjectManager = testWindow.brackets.test.ProjectManager;
2012-01-05 05:27:47 +04:00
});
});
afterEach(function () {
SpecRunnerUtils.closeTestWindow();
});
describe("createNewItem", function () {
it("should create a new file with a given name", function () {
2012-01-05 05:27:47 +04:00
var didCreate = false, gotError = false;
SpecRunnerUtils.loadProjectInTestWindow(testPath);
runs(function () {
2012-01-05 05:27:47 +04:00
// skip rename
ProjectManager.createNewItem(testPath, "Untitled.js", true)
.done(function () { didCreate = true; })
.fail(function () { gotError = true; });
});
waitsFor(function () { return didCreate && !gotError; }, "ProjectManager.createNewItem() timeout", 1000);
2012-01-05 05:27:47 +04:00
var error, stat, complete = false;
var filePath = testPath + "/Untitled.js";
runs(function () {
brackets.fs.stat(filePath, function (err, _stat) {
2012-01-05 05:27:47 +04:00
error = err;
stat = _stat;
complete = true;
});
});
waitsFor(function () { return complete; }, 1000);
2012-01-05 05:27:47 +04:00
var unlinkError = brackets.fs.NO_ERROR;
runs(function () {
2012-01-05 05:27:47 +04:00
expect(error).toBeFalsy();
expect(stat.isFile()).toBe(true);
// delete the new file
complete = false;
brackets.fs.unlink(filePath, function (err) {
2012-01-05 05:27:47 +04:00
unlinkError = err;
complete = true;
});
});
waitsFor(
function () {
return complete && (unlinkError === brackets.fs.NO_ERROR);
},
"unlink() failed to cleanup Untitled.js, err=" + unlinkError,
1000
2012-01-05 05:27:47 +04:00
);
});
it("should fail when a file already exists", function () {
2012-01-05 05:27:47 +04:00
var didCreate = false, gotError = false;
SpecRunnerUtils.loadProjectInTestWindow(testPath);
runs(function () {
2012-01-05 05:27:47 +04:00
// skip rename
ProjectManager.createNewItem(testPath, "file.js", true)
.done(function () { didCreate = true; })
.fail(function () { gotError = true; });
2012-01-05 05:27:47 +04:00
});
waitsFor(function () { return !didCreate && gotError; }, "ProjectManager.createNewItem() timeout", 1000);
runs(function () {
2012-01-05 05:27:47 +04:00
expect(gotError).toBeTruthy();
expect(didCreate).toBeFalsy();
});
});
it("should fail when a file name matches a directory that already exists", function () {
2012-01-05 05:27:47 +04:00
var didCreate = false, gotError = false;
SpecRunnerUtils.loadProjectInTestWindow(testPath);
runs(function () {
// skip rename
ProjectManager.createNewItem(testPath, "directory", true)
.done(function () { didCreate = true; })
.fail(function () { gotError = true; });
});
waitsFor(function () { return !didCreate && gotError; }, "ProjectManager.createNewItem() timeout", 1000);
runs(function () {
expect(gotError).toBeTruthy();
expect(didCreate).toBeFalsy();
});
2012-01-05 05:27:47 +04:00
});
it("should fail when file name contains special characters", function () {
2012-01-05 05:27:47 +04:00
var chars = "/?*:;{}<>\\";
var i = 0;
var len = chars.length;
var charAt, didCreate, gotError;
SpecRunnerUtils.loadProjectInTestWindow(testPath);
2012-01-05 05:27:47 +04:00
function createFile() {
// skip rename
ProjectManager.createNewItem(testPath, "file" + charAt + ".js", true)
.done(function () { didCreate = true; })
.fail(function () { gotError = true; });
}
function waitForFileCreate() {
return !didCreate && gotError;
}
function assertFile() {
expect(gotError).toBeTruthy();
expect(didCreate).toBeFalsy();
}
2012-01-05 05:27:47 +04:00
for (i = 0; i < len; i++) {
didCreate = false;
gotError = false;
charAt = chars.charAt(i);
runs(createFile);
waitsFor(waitForFileCreate, "ProjectManager.createNewItem() timeout", 1000);
runs(assertFile);
2012-01-05 05:27:47 +04:00
}
});
});
2012-01-05 05:27:47 +04:00
});
});