Skip to content

Commit

Permalink
test: integ tests for auto tab space detection
Browse files Browse the repository at this point in the history
  • Loading branch information
abose committed Aug 20, 2024
1 parent e63fbb3 commit da60c71
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/extensions/default/DebugCommands/MacroRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ define(function (require, exports, module) {
KeyEvent = brackets.getModule("utils/KeyEvent"),
Commands = brackets.getModule("command/Commands"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
Editor = brackets.getModule("editor/Editor"),
_ = brackets.getModule("thirdparty/lodash"),
ProjectManager = brackets.getModule("project/ProjectManager");

/**
Expand Down Expand Up @@ -304,6 +306,12 @@ define(function (require, exports, module) {
}
}

function validateEqual(obj1, obj2) {
if(!_.isEqual(obj1, obj2)){
throw new Error(`validateEqual: expected ${JSON.stringify(obj1)} to equal ${JSON.stringify(obj2)}`);
}
}

/**
* validates if the given mark type is present in the specified selections
* @param {string} markType
Expand Down Expand Up @@ -355,9 +363,29 @@ define(function (require, exports, module) {
return PreferencesManager.get(key);
}

const EDITING = {
setEditorSpacing: function (useTabs, spaceOrTabCount, isAutoMode) {
const activeEditor = EditorManager.getActiveEditor();
if(!activeEditor){
throw new Error(`No active editor found to setEditorSpacing`);
}
const fullPath = activeEditor.document.file.fullPath;
if(Editor.Editor.getAutoTabSpaces(fullPath) !== isAutoMode){
Editor.Editor.setAutoTabSpaces(isAutoMode, fullPath);
isAutoMode && Editor.Editor._autoDetectTabSpaces(activeEditor, true, true);
}
Editor.Editor.setUseTabChar(useTabs);
if(useTabs) {
Editor.Editor.setTabSize(spaceOrTabCount);
} else {
Editor.Editor.setSpaceUnits(spaceOrTabCount);
}
}
};

const __PR= {
openFile, setCursors, expectCursorsToBe, keydown, typeAtCursor, validateText, validateAllMarks, validateMarks,
closeFile, closeAll, undo, redo, setPreference, getPreference
closeFile, closeAll, undo, redo, setPreference, getPreference, validateEqual, EDITING
};

async function runMacro(macroText) {
Expand Down
1 change: 1 addition & 0 deletions test/UnitTestSuite.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ define(function (require, exports, module) {
require("spec/StateManager-test");
require("spec/TaskManager-integ-test");
require("spec/Generic-integ-test");
require("spec/spacing-auto-detect-integ-test");
// Integrated extension tests
require("spec/Extn-InAppNotifications-integ-test");
require("spec/Extn-RemoteFileAdapter-integ-test");
Expand Down
3 changes: 3 additions & 0 deletions test/spec/space-detect-test-files/space-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function f() {
console.log("hello");
}
3 changes: 3 additions & 0 deletions test/spec/space-detect-test-files/space-12.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function f() {
console.log("hello");
}
1 change: 1 addition & 0 deletions test/spec/space-detect-test-files/space-none.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function f() {console.log("hello");}
3 changes: 3 additions & 0 deletions test/spec/space-detect-test-files/tab-12.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function x(){
console.log();
}
3 changes: 3 additions & 0 deletions test/spec/space-detect-test-files/tab-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function x(){
console.log();
}
112 changes: 112 additions & 0 deletions test/spec/spacing-auto-detect-integ-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2013 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/

/*global describe, it, beforeAll, afterAll */

define(function (require, exports, module) {


var SpecRunnerUtils = require("spec/SpecRunnerUtils");

describe("integration:Auto space and tabs detect", function () {
const testRootSpec = "/spec/space-detect-test-files/";
let testProjectsFolder = SpecRunnerUtils.getTestPath(testRootSpec),
testWindow,
$,
__PR; // __PR can be debugged using debug menu> phoenix code diag tools> test builder

beforeAll(async function () {
testWindow = await SpecRunnerUtils.createTestWindowAndRun();
// Load module instances from brackets.test
await SpecRunnerUtils.loadProjectInTestWindow(testProjectsFolder);
__PR = testWindow.__PR;
$ = testWindow.$;
}, 30000);

afterAll(async function () {
await __PR.closeAll();
testWindow = null;
__PR = null;
$ = null;
await SpecRunnerUtils.closeTestWindow();
}, 30000);

function validateSpacing(type, value, autoMode) {
__PR.validateEqual($("#indent-type").text(), type);
__PR.validateEqual($("#indent-width-label").text(), value);
__PR.validateEqual($("#indent-auto").text(), autoMode);
}

it(`should detect 1 space auto`, async function () {
await __PR.openFile("space-1.js");
validateSpacing("Spaces:", "1", "Auto");
await __PR.closeFile();
});

it(`should detect 12 space as 10 spaces auto`, async function () {
await __PR.openFile("space-12.js");
validateSpacing("Spaces:", "10", "Auto");
await __PR.closeFile();
});

it(`should detect no space as default 4 spaces auto`, async function () {
await __PR.openFile("space-none.js");
validateSpacing("Spaces:", "4", "Auto");
await __PR.closeFile();
});

it(`should detect 2 tabs auto`, async function () {
await __PR.openFile("tab-2.js");
validateSpacing("Tab Size:", "2", "Auto");
await __PR.closeFile();
});

it(`should detect 12 tabs as 10 tabs auto`, async function () {
await __PR.openFile("tab-12.js");
validateSpacing("Tab Size:", "10", "Auto");
await __PR.closeFile();
});

it(`should be able to override individual file's tab/spacing in auto mode`, async function () {
await __PR.openFile("space-1.js");
validateSpacing("Spaces:", "1", "Auto");
$("#indent-type").click();
validateSpacing("Tab Size:", "4", "Auto");
// now switch to another file
await __PR.openFile("tab-2.js");
validateSpacing("Tab Size:", "2", "Auto");
// now switch back and it should remember the overridden settings
await __PR.openFile("space-1.js");
validateSpacing("Tab Size:", "4", "Auto");
// now change the tab units
__PR.EDITING.setEditorSpacing(true, 6, true);
validateSpacing("Tab Size:", "6", "Auto");
// now close the file and switch to another file
await __PR.closeFile();
await __PR.openFile("tab-2.js");
validateSpacing("Tab Size:", "2", "Auto");
// now switch back and it should remember the overridden settings
await __PR.openFile("space-1.js");
validateSpacing("Tab Size:", "6", "Auto");
await __PR.closeFile();
});
});
});

0 comments on commit da60c71

Please sign in to comment.