add auto-highlighting of selected/updated code

This commit is contained in:
undergroundwires
2020-08-25 16:52:38 +01:00
parent 5df458739d
commit b789250cb8
39 changed files with 1163 additions and 175 deletions

View File

@@ -1,11 +1,15 @@
import 'mocha';
import { expect } from 'chai';
import { CategoryStub } from './../../../stubs/CategoryStub';
import { ScriptStub } from './../../../stubs/ScriptStub';
import { ApplicationStub } from './../../../stubs/ApplicationStub';
import { UserSelection } from '@/application/State/Selection/UserSelection';
import { ApplicationCode } from '@/application/State/Code/ApplicationCode';
import 'mocha';
import { expect } from 'chai';
import { SelectedScript } from '@/application/State/Selection/SelectedScript';
import { ICodeChangedEvent } from '@/application/State/Code/Event/ICodeChangedEvent';
import { IUserScriptGenerator } from '@/application/State/Code/Generation/IUserScriptGenerator';
import { CodePosition } from '@/application/State/Code/Position/CodePosition';
import { ICodePosition } from '@/application/State/Code/Position/ICodePosition';
describe('ApplicationCode', () => {
describe('ctor', () => {
@@ -31,35 +35,76 @@ describe('ApplicationCode', () => {
expect(actual).to.have.length.greaterThan(0).and.include(version);
});
});
describe('user selection changes', () => {
it('empty when selection is empty', () => {
// arrange
let signaled: string;
const scripts = [new ScriptStub('first'), new ScriptStub('second')];
const app = new ApplicationStub().withAction(new CategoryStub(1).withScripts(...scripts));
const selection = new UserSelection(app, scripts);
const sut = new ApplicationCode(selection, 'version');
sut.changed.on((code) => signaled = code);
// act
selection.changed.notify([]);
// assert
expect(signaled).to.have.lengthOf(0);
expect(signaled).to.equal(sut.current);
describe('changed event', () => {
describe('code', () => {
it('empty when nothing is selected', () => {
// arrange
let signaled: ICodeChangedEvent;
const scripts = [new ScriptStub('first'), new ScriptStub('second')];
const app = new ApplicationStub().withAction(new CategoryStub(1).withScripts(...scripts));
const selection = new UserSelection(app, scripts);
const sut = new ApplicationCode(selection, 'version');
sut.changed.on((code) => signaled = code);
// act
selection.changed.notify([]);
// assert
expect(signaled.code).to.have.lengthOf(0);
expect(signaled.code).to.equal(sut.current);
});
it('has code when some are selected', () => {
// arrange
let signaled: ICodeChangedEvent;
const scripts = [new ScriptStub('first'), new ScriptStub('second')];
const app = new ApplicationStub().withAction(new CategoryStub(1).withScripts(...scripts));
const selection = new UserSelection(app, scripts);
const version = 'version-string';
const sut = new ApplicationCode(selection, version);
sut.changed.on((code) => signaled = code);
// act
selection.changed.notify(scripts.map((s) => new SelectedScript(s, false)));
// assert
expect(signaled.code).to.have.length.greaterThan(0).and.include(version);
expect(signaled.code).to.equal(sut.current);
});
});
it('has code when selection is not empty', () => {
it('sets positions from the generator', () => {
// arrange
let signaled: string;
let signaled: ICodeChangedEvent;
const scripts = [new ScriptStub('first'), new ScriptStub('second')];
const app = new ApplicationStub().withAction(new CategoryStub(1).withScripts(...scripts));
const selection = new UserSelection(app, scripts);
const version = 'version-string';
const sut = new ApplicationCode(selection, version);
const expectedVersion = 'version-string';
const scriptsToSelect = scripts.map((s) => new SelectedScript(s, false));
const totalLines = 20;
const expected = new Map<SelectedScript, ICodePosition>(
[
[ scriptsToSelect[0], new CodePosition(0, totalLines / 2)],
[ scriptsToSelect[1], new CodePosition(totalLines / 2, totalLines)],
],
);
const generatorMock: IUserScriptGenerator = {
buildCode: (selectedScripts, version) => {
if (version !== expectedVersion) {
throw new Error('Unexpected version');
}
if (JSON.stringify(selectedScripts) !== JSON.stringify(scriptsToSelect)) {
throw new Error('Unexpected scripts');
}
return {
code: '\nREM LINE'.repeat(totalLines),
scriptPositions: expected,
};
},
};
const sut = new ApplicationCode(selection, expectedVersion, generatorMock);
sut.changed.on((code) => signaled = code);
// act
selection.changed.notify(scripts.map((s) => new SelectedScript(s, false)));
selection.changed.notify(scriptsToSelect);
// assert
expect(signaled).to.have.length.greaterThan(0).and.include(version);
expect(signaled).to.equal(sut.current);
expect(signaled.getScriptPositionInCode(scripts[0]))
.to.deep.equal(expected.get(scriptsToSelect[0]));
expect(signaled.getScriptPositionInCode(scripts[1]))
.to.deep.equal(expected.get(scriptsToSelect[1]));
});
});
});

View File

@@ -0,0 +1,147 @@
import 'mocha';
import { expect } from 'chai';
import { CodeChangedEvent } from '@/application/State/Code/Event/CodeChangedEvent';
import { SelectedScript } from '@/application/State/Selection/SelectedScript';
import { ICodePosition } from '@/application/State/Code/Position/ICodePosition';
import { CodePosition } from '@/application/State/Code/Position/CodePosition';
import { SelectedScriptStub } from '../../../../stubs/SelectedScriptStub';
import { ScriptStub } from '../../../../stubs/ScriptStub';
describe('CodeChangedEvent', () => {
describe('ctor', () => {
describe('position validation', () => {
it('throws when code position is out of range', () => {
const act = () => new CodeChangedEvent(
'singleline code', [], new Map<SelectedScript, ICodePosition>([
[ new SelectedScriptStub('1'), new CodePosition(0, 2) ],
]),
);
expect(act).to.throw();
});
it('does not throw with valid code position', () => {
const act = () => new CodeChangedEvent(
'singleline code', [], new Map<SelectedScript, ICodePosition>([
[ new SelectedScriptStub('1'), new CodePosition(0, 1) ],
]),
);
expect(act).to.not.throw();
});
});
});
it('code returns expected', () => {
// arrange
const expected = 'code';
// act
const sut = new CodeChangedEvent(
expected, [], new Map<SelectedScript, ICodePosition>(),
);
const actual = sut.code;
// assert
expect(actual).to.equal(expected);
});
describe('addedScripts', () => {
it('returns new scripts when scripts are added', () => {
// arrange
const expected = [ new ScriptStub('3'), new ScriptStub('4') ];
const initialScripts = [ new SelectedScriptStub('1'), new SelectedScriptStub('2') ];
const newScripts = new Map<SelectedScript, ICodePosition>([
[initialScripts[0], new CodePosition(0, 1) ],
[initialScripts[1], new CodePosition(0, 1) ],
[new SelectedScript(expected[0], false), new CodePosition(0, 1) ],
[new SelectedScript(expected[1], false), new CodePosition(0, 1) ],
]);
// act
const sut = new CodeChangedEvent(
'code', initialScripts, newScripts,
);
const actual = sut.addedScripts;
// assert
expect(actual).to.have.lengthOf(2);
expect(actual[0]).to.deep.equal(expected[0]);
expect(actual[1]).to.deep.equal(expected[1]);
});
});
describe('removedScripts', () => {
it('returns removed scripts when script are removed', () => {
// arrange
const existingScripts = [ new SelectedScriptStub('0'), new SelectedScriptStub('1') ];
const removedScripts = [ new SelectedScriptStub('2') ];
const initialScripts = [ ...existingScripts, ...removedScripts ];
const newScripts = new Map<SelectedScript, ICodePosition>([
[initialScripts[0], new CodePosition(0, 1) ],
[initialScripts[1], new CodePosition(0, 1) ],
]);
// act
const sut = new CodeChangedEvent(
'code', initialScripts, newScripts,
);
const actual = sut.removedScripts;
// assert
expect(actual).to.have.lengthOf(removedScripts.length);
expect(actual[0]).to.deep.equal(removedScripts[0].script);
});
});
describe('changedScripts', () => {
it('returns changed scripts when scripts are changed', () => {
// arrange
const initialScripts = [ new SelectedScriptStub('1', false), new SelectedScriptStub('2', false) ];
const newScripts = new Map<SelectedScript, ICodePosition>([
[new SelectedScriptStub('1', true), new CodePosition(0, 1) ],
[new SelectedScriptStub('2', false), new CodePosition(0, 1) ],
]);
// act
const sut = new CodeChangedEvent(
'code', initialScripts, newScripts,
);
const actual = sut.changedScripts;
// assert
expect(actual).to.have.lengthOf(1);
expect(actual[0]).to.deep.equal(initialScripts[0].script);
});
});
describe('isEmpty', () => {
it('returns true when empty', () => {
// arrange
const newScripts = new Map<SelectedScript, ICodePosition>();
const oldScripts = [ new SelectedScriptStub('1', false) ];
const sut = new CodeChangedEvent(
'code', oldScripts, newScripts,
);
// act
const actual = sut.isEmpty();
// assert
expect(actual).to.equal(true);
});
it('returns false when not empty', () => {
// arrange
const oldScripts = [ new SelectedScriptStub('1') ];
const newScripts = new Map<SelectedScript, ICodePosition>( [
[oldScripts[0], new CodePosition(0, 1) ],
]);
const sut = new CodeChangedEvent(
'code', oldScripts, newScripts,
);
// act
const actual = sut.isEmpty();
// assert
expect(actual).to.equal(false);
});
});
describe('getScriptPositionInCode', () => {
it('returns expected position for existing script', () => {
// arrange
const script = new ScriptStub('1');
const expected = new CodePosition(0, 1);
const newScripts = new Map<SelectedScript, ICodePosition>( [
[new SelectedScript(script, false), expected ],
]);
const sut = new CodeChangedEvent(
'code', [], newScripts,
);
// act
const actual = sut.getScriptPositionInCode(script);
// assert
expect(actual).to.deep.equal(expected);
});
});
});

View File

@@ -0,0 +1,112 @@
import 'mocha';
import { expect } from 'chai';
import { CodeBuilder } from '@/application/State/Code/Generation/CodeBuilder';
describe('CodeBuilder', () => {
describe('appendLine', () => {
it('when empty appends empty line', () => {
// arrange
const sut = new CodeBuilder();
// act
sut.appendLine().appendLine().appendLine();
// assert
expect(sut.toString()).to.equal('\n\n');
});
it('when not empty append string in new line', () => {
// arrange
const sut = new CodeBuilder();
const expected = 'str';
// act
sut.appendLine()
.appendLine(expected);
// assert
const result = sut.toString();
const lines = getLines(result);
expect(lines[1]).to.equal('str');
});
});
it('appendFunction', () => {
// arrange
const sut = new CodeBuilder();
const functionName = 'function';
const code = 'code';
// act
sut.appendFunction(functionName, code);
// assert
const result = sut.toString();
expect(result).to.include(functionName);
expect(result).to.include(code);
});
it('appendTrailingHyphensCommentLine', () => {
// arrange
const sut = new CodeBuilder();
const totalHypens = 5;
const expected = `:: ${'-'.repeat(totalHypens)}`;
// act
sut.appendTrailingHyphensCommentLine(totalHypens);
// assert
const result = sut.toString();
const lines = getLines(result);
expect(lines[0]).to.equal(expected);
});
it('appendCommentLine', () => {
// arrange
const sut = new CodeBuilder();
const comment = 'comment';
const expected = ':: comment';
// act
sut.appendCommentLine(comment);
// assert
const result = sut.toString();
const lines = getLines(result);
expect(lines[0]).to.equal(expected);
});
it('appendCommentLineWithHyphensAround', () => {
// arrange
const sut = new CodeBuilder();
const sectionName = 'section';
const totalHypens = sectionName.length + 3 * 2;
const expected = ':: ---section---';
sut.appendCommentLineWithHyphensAround(sectionName, totalHypens);
// assert
const result = sut.toString();
const lines = getLines(result);
expect(lines[1]).to.equal(expected);
});
describe('currentLine', () => {
it('no lines returns zero', () => {
// arrange & act
const sut = new CodeBuilder();
// assert
expect(sut.currentLine).to.equal(0);
});
it('single line returns one', () => {
// arrange
const sut = new CodeBuilder();
// act
sut.appendLine();
// assert
expect(sut.currentLine).to.equal(1);
});
it('multiple lines returns as expected', () => {
// arrange
const sut = new CodeBuilder();
// act
sut.appendLine('1').appendCommentLine('2').appendLine();
// assert
expect(sut.currentLine).to.equal(3);
});
it('multiple lines in code', () => {
// arrange
const sut = new CodeBuilder();
// act
sut.appendLine('hello\ncode-here\nwith-3-lines');
// assert
expect(sut.currentLine).to.equal(3);
});
});
});
function getLines(text: string): string[] {
return text.split(/\r\n|\r|\n/);
}

View File

@@ -1,33 +1,34 @@
import { ScriptStub } from './../../../stubs/ScriptStub';
import { UserScriptGenerator, adminRightsScript } from '@/application/State/Code/UserScriptGenerator';
import { ScriptStub } from '../../../../stubs/ScriptStub';
import { UserScriptGenerator, adminRightsScript } from '@/application/State/Code/Generation/UserScriptGenerator';
import 'mocha';
import { expect } from 'chai';
import { SelectedScript } from '@/application/State/Selection/SelectedScript';
import { SelectedScriptStub } from '../../../../stubs/SelectedScriptStub';
describe('UserScriptGenerator', () => {
it('adds version', () => {
const sut = new UserScriptGenerator();
// arrange
const sut = new UserScriptGenerator();
const version = '1.5.0';
const selectedScripts = [ new SelectedScript(new ScriptStub('id'), false)];
// act
const actual = sut.buildCode(selectedScripts, version);
// assert
expect(actual).to.include(version);
expect(actual.code).to.include(version);
});
it('adds admin rights function', () => {
const sut = new UserScriptGenerator();
// arrange
const sut = new UserScriptGenerator();
const selectedScripts = [ new SelectedScript(new ScriptStub('id'), false)];
// act
const actual = sut.buildCode(selectedScripts, 'non-important-version');
// assert
expect(actual).to.include(adminRightsScript.code);
expect(actual).to.include(adminRightsScript.name);
expect(actual.code).to.include(adminRightsScript.code);
expect(actual.code).to.include(adminRightsScript.name);
});
it('appends revert script', () => {
const sut = new UserScriptGenerator();
// arrange
const sut = new UserScriptGenerator();
const scriptName = 'test non-revert script';
const scriptCode = 'REM nop';
const script = new ScriptStub('id').withName(scriptName).withRevertCode(scriptCode);
@@ -35,8 +36,8 @@ describe('UserScriptGenerator', () => {
// act
const actual = sut.buildCode(selectedScripts, 'non-important-version');
// assert
expect(actual).to.include(`${scriptName} (revert)`);
expect(actual).to.include(scriptCode);
expect(actual.code).to.include(`${scriptName} (revert)`);
expect(actual.code).to.include(scriptCode);
});
it('appends non-revert script', () => {
const sut = new UserScriptGenerator();
@@ -48,7 +49,46 @@ describe('UserScriptGenerator', () => {
// act
const actual = sut.buildCode(selectedScripts, 'non-important-version');
// assert
expect(actual).to.include(scriptName);
expect(actual).to.include(scriptCode);
expect(actual.code).to.include(scriptName);
expect(actual.code).to.include(scriptCode);
});
describe('scriptPositions', () => {
it('single script', () => {
// arrange
const sut = new UserScriptGenerator();
const scriptName = 'test non-revert script';
const scriptCode = 'REM nop\nREM nop2';
const script = new ScriptStub('id').withName(scriptName).withCode(scriptCode);
const selectedScripts = [ new SelectedScript(script, false)];
// act
const actual = sut.buildCode(selectedScripts, 'non-important-version');
// assert
expect(actual.scriptPositions.size).to.equal(1);
const position = actual.scriptPositions.get(selectedScripts[0]);
expect(position.endLine).to.be.greaterThan(position.startLine + 2);
});
it('multiple scripts', () => {
// arrange
const sut = new UserScriptGenerator();
const selectedScripts = [ new SelectedScriptStub('1'), new SelectedScriptStub('2') ];
// act
const actual = sut.buildCode(selectedScripts, 'non-important-version');
// assert
const firstPosition = actual.scriptPositions.get(selectedScripts[0]);
const secondPosition = actual.scriptPositions.get(selectedScripts[1]);
expect(actual.scriptPositions.size).to.equal(2);
expect(firstPosition.endLine).to.be.greaterThan(firstPosition.startLine + 1);
expect(secondPosition.startLine).to.be.greaterThan(firstPosition.endLine);
expect(secondPosition.endLine).to.be.greaterThan(secondPosition.startLine + 1);
});
it('no script', () => {
// arrange
const sut = new UserScriptGenerator();
const selectedScripts = [ ];
// act
const actual = sut.buildCode(selectedScripts, 'non-important-version');
// assert
expect(actual.scriptPositions.size).to.equal(0);
});
});
});

View File

@@ -0,0 +1,54 @@
import { CodePosition } from '@/application/State/Code/Position/CodePosition';
import 'mocha';
import { expect } from 'chai';
describe('CodePosition', () => {
describe('ctor', () => {
it('creates with valid parameters', () => {
// arrange
const startPosition = 0;
const endPosition = 5;
// act
const sut = new CodePosition(startPosition, endPosition);
// assert
expect(sut.startLine).to.equal(startPosition);
expect(sut.endLine).to.equal(endPosition);
});
it('throws with negative start position', () => {
// arrange
const startPosition = -1;
const endPosition = 5;
// act
const getSut = () => new CodePosition(startPosition, endPosition);
// assert
expect(getSut).to.throw('Code cannot start in a negative line');
});
it('throws with negative end position', () => {
// arrange
const startPosition = 1;
const endPosition = -5;
// act
const getSut = () => new CodePosition(startPosition, endPosition);
// assert
expect(getSut).to.throw('Code cannot end in a negative line');
});
it('throws when start and end position is same', () => {
// arrange
const startPosition = 0;
const endPosition = 0;
// act
const getSut = () => new CodePosition(startPosition, endPosition);
// assert
expect(getSut).to.throw('Empty code');
});
it('throws when ends before start', () => {
// arrange
const startPosition = 3;
const endPosition = 2;
// act
const getSut = () => new CodePosition(startPosition, endPosition);
// assert
expect(getSut).to.throw('End line cannot be less than start line');
});
});
});

View File

@@ -93,4 +93,127 @@ describe('UserSelection', () => {
expect(events[0]).to.deep.equal(expected);
});
});
describe('removeAllInCategory', () => {
it('does nothing when nothing exists', () => {
// arrange
const events: Array<readonly SelectedScript[]> = [];
const categoryId = 1;
const app = new ApplicationStub()
.withAction(new CategoryStub(categoryId)
.withScripts(new ScriptStub('s1'), new ScriptStub('s2')));
const sut = new UserSelection(app, []);
sut.changed.on((s) => events.push(s));
// act
sut.removeAllInCategory(categoryId);
// assert
expect(events).to.have.lengthOf(0);
expect(sut.selectedScripts).to.have.lengthOf(0);
});
it('removes all when all exists', () => {
// arrange
const categoryId = 1;
const scripts = [new ScriptStub('s1'), new ScriptStub('s2')];
const app = new ApplicationStub()
.withAction(new CategoryStub(categoryId)
.withScripts(...scripts));
const sut = new UserSelection(app, scripts);
// act
sut.removeAllInCategory(categoryId);
// assert
expect(sut.totalSelected).to.equal(0);
expect(sut.selectedScripts.length).to.equal(0);
});
it('removes existing some exists', () => {
// arrange
const categoryId = 1;
const existing = [new ScriptStub('s1'), new ScriptStub('s2')];
const notExisting = [new ScriptStub('s3'), new ScriptStub('s4')];
const app = new ApplicationStub()
.withAction(new CategoryStub(categoryId)
.withScripts(...existing, ...notExisting));
const sut = new UserSelection(app, existing);
// act
sut.removeAllInCategory(categoryId);
// assert
expect(sut.totalSelected).to.equal(0);
expect(sut.selectedScripts.length).to.equal(0);
});
});
describe('addAllInCategory', () => {
it('does nothing when all already exists', () => {
// arrange
const events: Array<readonly SelectedScript[]> = [];
const categoryId = 1;
const scripts = [new ScriptStub('s1'), new ScriptStub('s2')];
const app = new ApplicationStub()
.withAction(new CategoryStub(categoryId)
.withScripts(...scripts));
const sut = new UserSelection(app, scripts);
sut.changed.on((s) => events.push(s));
// act
sut.addAllInCategory(categoryId);
// assert
expect(events).to.have.lengthOf(0);
expect(sut.selectedScripts.map((script) => script.id))
.to.have.deep.members(scripts.map((script) => script.id));
});
it('adds all when nothing exists', () => {
// arrange
const categoryId = 1;
const expected = [new ScriptStub('s1'), new ScriptStub('s2')];
const app = new ApplicationStub()
.withAction(new CategoryStub(categoryId)
.withScripts(...expected));
const sut = new UserSelection(app, []);
// act
sut.addAllInCategory(categoryId);
// assert
expect(sut.selectedScripts.map((script) => script.id))
.to.have.deep.members(expected.map((script) => script.id));
});
it('adds not existing some exists', () => {
// arrange
const categoryId = 1;
const notExisting = [ new ScriptStub('notExisting1'), new ScriptStub('notExisting2') ];
const existing = [ new ScriptStub('existing1'), new ScriptStub('existing2') ];
const allScripts = [ ...existing, ...notExisting ];
const app = new ApplicationStub()
.withAction(new CategoryStub(categoryId)
.withScripts(...allScripts));
const sut = new UserSelection(app, existing);
// act
sut.addAllInCategory(categoryId);
// assert
expect(sut.selectedScripts.map((script) => script.id))
.to.have.deep.members(allScripts.map((script) => script.id));
});
});
describe('isSelected', () => {
it('returns false when not selected', () => {
// arrange
const selectedScript = new ScriptStub('selected');
const notSelectedScript = new ScriptStub('not selected');
const app = new ApplicationStub()
.withAction(new CategoryStub(1)
.withScripts(selectedScript, notSelectedScript));
const sut = new UserSelection(app, [ selectedScript ]);
// act
const actual = sut.isSelected(notSelectedScript.id);
// assert
expect(actual).to.equal(false);
});
it('returns true when selected', () => {
// arrange
const selectedScript = new ScriptStub('selected');
const notSelectedScript = new ScriptStub('not selected');
const app = new ApplicationStub()
.withAction(new CategoryStub(1)
.withScripts(selectedScript, notSelectedScript));
const sut = new UserSelection(app, [ selectedScript ]);
// act
const actual = sut.isSelected(selectedScript.id);
// assert
expect(actual).to.equal(true);
});
});
});