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

@@ -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);
});
});
});