refactor to allow switching ICategoryCollection context #40
This commit is contained in:
108
tests/unit/application/Parser/ApplicationParser.spec.ts
Normal file
108
tests/unit/application/Parser/ApplicationParser.spec.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
import { parseProjectInformation } from '@/application/Parser/ProjectInformationParser';
|
||||
import { CategoryCollectionParserType, parseApplication } from '@/application/Parser/ApplicationParser';
|
||||
import applicationFile, { YamlApplication } from 'js-yaml-loader!@/application/application.yaml';
|
||||
import { IProjectInformation } from '@/domain/IProjectInformation';
|
||||
import { ProjectInformation } from '@/domain/ProjectInformation';
|
||||
import { ICategoryCollection } from '@/domain/ICategoryCollection';
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
import { CategoryCollectionStub } from '../../stubs/CategoryCollectionStub';
|
||||
import { getProcessEnvironmentStub } from '../../stubs/ProcessEnvironmentStub';
|
||||
import { YamlApplicationStub } from '../../stubs/YamlApplicationStub';
|
||||
|
||||
describe('ApplicationParser', () => {
|
||||
describe('parseApplication', () => {
|
||||
it('can parse current application', () => { // Integration test
|
||||
// act
|
||||
const act = () => parseApplication();
|
||||
// assert
|
||||
expect(act).to.not.throw();
|
||||
});
|
||||
describe('parser', () => {
|
||||
it('returns result from the parser', () => {
|
||||
// arrange
|
||||
const os = OperatingSystem.macOS;
|
||||
const expected = new CategoryCollectionStub()
|
||||
.withOs(os);
|
||||
const parser = new CategoryCollectionParserSpy()
|
||||
.setResult(expected)
|
||||
.mockParser();
|
||||
// act
|
||||
const context = parseApplication(parser);
|
||||
// assert
|
||||
const actual = context.getCollection(os);
|
||||
expect(expected).to.equal(actual);
|
||||
});
|
||||
});
|
||||
describe('processEnv', () => {
|
||||
it('used to parse expected project information', () => {
|
||||
// arrange
|
||||
const env = getProcessEnvironmentStub();
|
||||
const expected = parseProjectInformation(env);
|
||||
const parserSpy = new CategoryCollectionParserSpy();
|
||||
const parserMock = parserSpy.mockParser();
|
||||
// act
|
||||
const context = parseApplication(parserMock, env);
|
||||
// assert
|
||||
expect(expected).to.deep.equal(context.info);
|
||||
expect(expected).to.deep.equal(parserSpy.lastArguments.info);
|
||||
});
|
||||
it('defaults to process.env', () => {
|
||||
// arrange
|
||||
const env = process.env;
|
||||
const expected = parseProjectInformation(env);
|
||||
const parserSpy = new CategoryCollectionParserSpy();
|
||||
const parserMock = parserSpy.mockParser();
|
||||
// act
|
||||
const context = parseApplication(parserMock);
|
||||
// assert
|
||||
expect(expected).to.deep.equal(context.info);
|
||||
expect(expected).to.deep.equal(parserSpy.lastArguments.info);
|
||||
});
|
||||
});
|
||||
describe('collectionData', () => {
|
||||
it('parsed with expected data', () => {
|
||||
// arrange
|
||||
const expected = new YamlApplicationStub();
|
||||
const env = getProcessEnvironmentStub();
|
||||
const parserSpy = new CategoryCollectionParserSpy();
|
||||
const parserMock = parserSpy.mockParser();
|
||||
// act
|
||||
parseApplication(parserMock, env, expected);
|
||||
// assert
|
||||
expect(expected).to.equal(parserSpy.lastArguments.file);
|
||||
});
|
||||
it('defaults to applicationFile', () => {
|
||||
// arrange
|
||||
const expected = applicationFile;
|
||||
const parserSpy = new CategoryCollectionParserSpy();
|
||||
const parserMock = parserSpy.mockParser();
|
||||
// act
|
||||
parseApplication(parserMock);
|
||||
// assert
|
||||
expect(expected).to.equal(parserSpy.lastArguments.file);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
class CategoryCollectionParserSpy {
|
||||
public lastArguments: {
|
||||
file: YamlApplication;
|
||||
info: ProjectInformation;
|
||||
} = { file: undefined, info: undefined };
|
||||
private result: ICategoryCollection = new CategoryCollectionStub();
|
||||
|
||||
public setResult(collection: ICategoryCollection): CategoryCollectionParserSpy {
|
||||
this.result = collection;
|
||||
return this;
|
||||
}
|
||||
public mockParser(): CategoryCollectionParserType {
|
||||
return (file: YamlApplication, info: IProjectInformation) => {
|
||||
this.lastArguments.file = file;
|
||||
this.lastArguments.info = info;
|
||||
return this.result;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,30 +1,24 @@
|
||||
import { IEntity } from '@/infrastructure/Entity/IEntity';
|
||||
import applicationFile, { YamlCategory, YamlScript, YamlApplication, YamlScriptingDefinition } from 'js-yaml-loader!@/application/application.yaml';
|
||||
import { parseCategoryCollection } from '@/application/Parser/CategoryCollectionParser';
|
||||
import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
import { IEntity } from '@/infrastructure/Entity/IEntity';
|
||||
import { parseCategoryCollection } from '@/application/Parser/CategoryCollectionParser';
|
||||
import { parseCategory } from '@/application/Parser/CategoryParser';
|
||||
import { RecommendationLevel } from '@/domain/RecommendationLevel';
|
||||
import { ScriptCompilerStub } from '../../stubs/ScriptCompilerStub';
|
||||
import { parseProjectInformation } from '@/application/Parser/ProjectInformationParser';
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
import { ScriptingLanguage } from '@/domain/ScriptingLanguage';
|
||||
import { parseScriptingDefinition } from '@/application/Parser/ScriptingDefinitionParser';
|
||||
import { mockEnumParser } from '../../stubs/EnumParserStub';
|
||||
import { ProjectInformationStub } from '../../stubs/ProjectInformationStub';
|
||||
import { ScriptCompilerStub } from '../../stubs/ScriptCompilerStub';
|
||||
import { getCategoryStub, YamlApplicationStub } from '../../stubs/YamlApplicationStub';
|
||||
|
||||
describe('CategoryCollectionParser', () => {
|
||||
describe('parseCategoryCollection', () => {
|
||||
it('can parse current application file', () => {
|
||||
// act
|
||||
const act = () => parseCategoryCollection(applicationFile);
|
||||
// assert
|
||||
expect(act).to.not.throw();
|
||||
});
|
||||
it('throws when undefined', () => {
|
||||
// arrange
|
||||
const expectedError = 'content is null or undefined';
|
||||
const info = new ProjectInformationStub();
|
||||
// act
|
||||
const act = () => parseCategoryCollection(undefined);
|
||||
const act = () => parseCategoryCollection(undefined, info);
|
||||
// assert
|
||||
expect(act).to.throw(expectedError);
|
||||
});
|
||||
@@ -32,29 +26,35 @@ describe('CategoryCollectionParser', () => {
|
||||
it('throws when undefined actions', () => {
|
||||
// arrange
|
||||
const expectedError = 'content does not define any action';
|
||||
const collection = new YamlApplicationBuilder().withActions(undefined).build();
|
||||
const collection = new YamlApplicationStub()
|
||||
.withActions(undefined);
|
||||
const info = new ProjectInformationStub();
|
||||
// act
|
||||
const act = () => parseCategoryCollection(collection);
|
||||
const act = () => parseCategoryCollection(collection, info);
|
||||
// assert
|
||||
expect(act).to.throw(expectedError);
|
||||
});
|
||||
it('throws when has no actions', () => {
|
||||
// arrange
|
||||
const expectedError = 'content does not define any action';
|
||||
const collection = new YamlApplicationBuilder().withActions([]).build();
|
||||
const collection = new YamlApplicationStub()
|
||||
.withActions([]);
|
||||
const info = new ProjectInformationStub();
|
||||
// act
|
||||
const act = () => parseCategoryCollection(collection);
|
||||
const act = () => parseCategoryCollection(collection, info);
|
||||
// assert
|
||||
expect(act).to.throw(expectedError);
|
||||
});
|
||||
it('parses actions', () => {
|
||||
// arrange
|
||||
const actions = [ getTestCategory('test1'), getTestCategory('test2') ];
|
||||
const actions = [ getCategoryStub('test1'), getCategoryStub('test2') ];
|
||||
const compiler = new ScriptCompilerStub();
|
||||
const expected = [ parseCategory(actions[0], compiler), parseCategory(actions[1], compiler) ];
|
||||
const collection = new YamlApplicationBuilder().withActions(actions).build();
|
||||
const collection = new YamlApplicationStub()
|
||||
.withActions(actions);
|
||||
const info = new ProjectInformationStub();
|
||||
// act
|
||||
const actual = parseCategoryCollection(collection).actions;
|
||||
const actual = parseCategoryCollection(collection, info).actions;
|
||||
// assert
|
||||
expect(excludingId(actual)).to.be.deep.equal(excludingId(expected));
|
||||
function excludingId<TId>(array: ReadonlyArray<IEntity<TId>>) {
|
||||
@@ -65,60 +65,14 @@ describe('CategoryCollectionParser', () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe('info', () => {
|
||||
it('returns expected repository version', () => {
|
||||
// arrange
|
||||
const expected = 'expected-version';
|
||||
const env = getProcessEnvironmentStub();
|
||||
env.VUE_APP_VERSION = expected;
|
||||
const collection = new YamlApplicationBuilder().build();
|
||||
// act
|
||||
const actual = parseCategoryCollection(collection, env).info.version;
|
||||
// assert
|
||||
expect(actual).to.be.equal(expected);
|
||||
});
|
||||
it('returns expected repository url', () => {
|
||||
// arrange
|
||||
const expected = 'https://expected-repository.url';
|
||||
const env = getProcessEnvironmentStub();
|
||||
env.VUE_APP_REPOSITORY_URL = expected;
|
||||
const collection = new YamlApplicationBuilder().build();
|
||||
// act
|
||||
const actual = parseCategoryCollection(collection, env).info.repositoryUrl;
|
||||
// assert
|
||||
expect(actual).to.be.equal(expected);
|
||||
});
|
||||
it('returns expected name', () => {
|
||||
// arrange
|
||||
const expected = 'expected-app-name';
|
||||
const env = getProcessEnvironmentStub();
|
||||
env.VUE_APP_NAME = expected;
|
||||
const collection = new YamlApplicationBuilder().build();
|
||||
// act
|
||||
const actual = parseCategoryCollection(collection, env).info.name;
|
||||
// assert
|
||||
expect(actual).to.be.equal(expected);
|
||||
});
|
||||
it('returns expected homepage url', () => {
|
||||
// arrange
|
||||
const expected = 'https://expected.sexy';
|
||||
const env = getProcessEnvironmentStub();
|
||||
env.VUE_APP_HOMEPAGE_URL = expected;
|
||||
const collection = new YamlApplicationBuilder().build();
|
||||
// act
|
||||
const actual = parseCategoryCollection(collection, env).info.homepage;
|
||||
// assert
|
||||
expect(actual).to.be.equal(expected);
|
||||
});
|
||||
});
|
||||
describe('scripting definition', () => {
|
||||
it('parses scripting definition as expected', () => {
|
||||
// arrange
|
||||
const collection = new YamlApplicationBuilder().build();
|
||||
const collection = new YamlApplicationStub();
|
||||
const information = parseProjectInformation(process.env);
|
||||
const expected = parseScriptingDefinition(collection.scripting, information);
|
||||
// act
|
||||
const actual = parseCategoryCollection(collection).scripting;
|
||||
const actual = parseCategoryCollection(collection, information).scripting;
|
||||
// assert
|
||||
expect(expected).to.deep.equal(actual);
|
||||
});
|
||||
@@ -129,79 +83,15 @@ describe('CategoryCollectionParser', () => {
|
||||
const expectedOs = OperatingSystem.macOS;
|
||||
const osText = 'macos';
|
||||
const expectedName = 'os';
|
||||
const collection = new YamlApplicationBuilder()
|
||||
.withOs(osText)
|
||||
.build();
|
||||
const collection = new YamlApplicationStub()
|
||||
.withOs(osText);
|
||||
const parserMock = mockEnumParser(expectedName, osText, expectedOs);
|
||||
const env = getProcessEnvironmentStub();
|
||||
const info = new ProjectInformationStub();
|
||||
// act
|
||||
const actual = parseCategoryCollection(collection, env, parserMock);
|
||||
const actual = parseCategoryCollection(collection, info, parserMock);
|
||||
// assert
|
||||
expect(actual.os).to.equal(expectedOs);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
class YamlApplicationBuilder {
|
||||
private os = 'windows';
|
||||
private actions: readonly YamlCategory[] = [ getTestCategory() ];
|
||||
private scripting: YamlScriptingDefinition = getTestDefinition();
|
||||
|
||||
public withActions(actions: readonly YamlCategory[]): YamlApplicationBuilder {
|
||||
this.actions = actions;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withOs(os: string): YamlApplicationBuilder {
|
||||
this.os = os;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withScripting(scripting: YamlScriptingDefinition): YamlApplicationBuilder {
|
||||
this.scripting = scripting;
|
||||
return this;
|
||||
}
|
||||
|
||||
public build(): YamlApplication {
|
||||
return { os: this.os, scripting: this.scripting, actions: this.actions };
|
||||
}
|
||||
}
|
||||
|
||||
function getTestDefinition(): YamlScriptingDefinition {
|
||||
return {
|
||||
fileExtension: '.bat',
|
||||
language: ScriptingLanguage[ScriptingLanguage.batchfile],
|
||||
startCode: 'start',
|
||||
endCode: 'end',
|
||||
};
|
||||
}
|
||||
|
||||
function getTestCategory(scriptPrefix = 'testScript'): YamlCategory {
|
||||
return {
|
||||
category: 'category name',
|
||||
children: [
|
||||
getTestScript(`${scriptPrefix}-standard`, RecommendationLevel.Standard),
|
||||
getTestScript(`${scriptPrefix}-strict`, RecommendationLevel.Strict),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
function getTestScript(scriptName: string, level: RecommendationLevel = RecommendationLevel.Standard): YamlScript {
|
||||
return {
|
||||
name: scriptName,
|
||||
code: 'script code',
|
||||
revertCode: 'revert code',
|
||||
recommend: RecommendationLevel[level].toLowerCase(),
|
||||
call: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
function getProcessEnvironmentStub(): NodeJS.ProcessEnv {
|
||||
return {
|
||||
VUE_APP_VERSION: 'stub-version',
|
||||
VUE_APP_NAME: 'stub-name',
|
||||
VUE_APP_REPOSITORY_URL: 'stub-repository-url',
|
||||
VUE_APP_HOMEPAGE_URL: 'stub-homepage-url',
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
import { parseProjectInformation } from '@/application/Parser/ProjectInformationParser';
|
||||
import { getProcessEnvironmentStub } from '../../stubs/ProcessEnvironmentStub';
|
||||
|
||||
describe('ProjectInformationParser', () => {
|
||||
describe('parseProjectInformation', () => {
|
||||
@@ -46,13 +47,3 @@ describe('ProjectInformationParser', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
function getProcessEnvironmentStub(): NodeJS.ProcessEnv {
|
||||
return {
|
||||
VUE_APP_VERSION: 'stub-version',
|
||||
VUE_APP_NAME: 'stub-name',
|
||||
VUE_APP_REPOSITORY_URL: 'stub-repository-url',
|
||||
VUE_APP_HOMEPAGE_URL: 'stub-homepage-url',
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user