add auto-highlighting of selected/updated code
This commit is contained in:
@@ -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]));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user