add test to ensure correct shared functions are being parsed

This commit is contained in:
undergroundwires
2021-02-08 07:10:41 +01:00
parent df273f7f63
commit d7de420d5c
5 changed files with 97 additions and 66 deletions

View File

@@ -10,6 +10,10 @@ import { mockEnumParser } from '../../stubs/EnumParserStub';
import { ProjectInformationStub } from '../../stubs/ProjectInformationStub';
import { getCategoryStub, CollectionDataStub } from '../../stubs/CollectionDataStub';
import { CategoryCollectionParseContextStub } from '../../stubs/CategoryCollectionParseContextStub';
import { CategoryDataStub } from '../../stubs/CategoryDataStub';
import { ScriptDataStub } from '../../stubs/ScriptDataStub';
import { FunctionDataStub } from '../../stubs/FunctionDataStub';
import { RecommendationLevel } from '../../../../src/domain/RecommendationLevel';
describe('CategoryCollectionParser', () => {
describe('parseCategoryCollection', () => {
@@ -93,5 +97,35 @@ describe('CategoryCollectionParser', () => {
expect(actual.os).to.equal(expectedOs);
});
});
describe('functions', () => {
it('compiles script call with given function', () => {
// arrange
const expectedCode = 'code-from-the-function';
const functionName = 'function-name';
const scriptName = 'script-name';
const script = ScriptDataStub.createWithCall({ function: functionName })
.withName(scriptName);
const func = new FunctionDataStub()
.withName(functionName)
.withCode(expectedCode);
const category = new CategoryDataStub()
.withChildren([ script,
ScriptDataStub.createWithCode().withName('2')
.withRecommendationLevel(RecommendationLevel.Standard),
ScriptDataStub.createWithCode()
.withName('3').withRecommendationLevel(RecommendationLevel.Strict),
]);
const collection = new CollectionDataStub()
.withActions([ category ])
.withFunctions([ func ]);
const info = new ProjectInformationStub();
// act
const actual = parseCategoryCollection(collection, info);
// assert
const actualScript = actual.findScript(scriptName);
const actualCode = actualScript.code.execute;
expect(actualCode).to.equal(expectedCode);
});
});
});
});

View File

@@ -1,13 +1,13 @@
import 'mocha';
import { expect } from 'chai';
import { parseCategory } from '@/application/Parser/CategoryParser';
import { CategoryData, CategoryOrScriptData } from 'js-yaml-loader!@/*';
import { parseScript } from '@/application/Parser/Script/ScriptParser';
import { parseDocUrls } from '@/application/Parser/DocumentationParser';
import { ScriptCompilerStub } from '../../stubs/ScriptCompilerStub';
import { ScriptDataStub } from '../../stubs/ScriptDataStub';
import { CategoryCollectionParseContextStub } from '../../stubs/CategoryCollectionParseContextStub';
import { LanguageSyntaxStub } from '../../stubs/LanguageSyntaxStub';
import { CategoryDataStub } from '../../stubs/CategoryDataStub';
describe('CategoryParser', () => {
describe('parseCategory', () => {
@@ -26,10 +26,9 @@ describe('CategoryParser', () => {
// arrange
const categoryName = 'test';
const expectedMessage = `category has no children: "${categoryName}"`;
const category: CategoryData = {
category: categoryName,
children: [],
};
const category = new CategoryDataStub()
.withName(categoryName)
.withChildren([]);
const context = new CategoryCollectionParseContextStub();
// act
const act = () => parseCategory(category, context);
@@ -40,10 +39,9 @@ describe('CategoryParser', () => {
// arrange
const categoryName = 'test';
const expectedMessage = `category has no children: "${categoryName}"`;
const category: CategoryData = {
category: categoryName,
children: undefined,
};
const category = new CategoryDataStub()
.withName(categoryName)
.withChildren(undefined);
const context = new CategoryCollectionParseContextStub();
// act
const act = () => parseCategory(category, context);
@@ -55,10 +53,8 @@ describe('CategoryParser', () => {
const expectedMessage = 'category has no name';
const invalidNames = ['', undefined];
invalidNames.forEach((invalidName) => {
const category: CategoryData = {
category: invalidName,
children: getTestChildren(),
};
const category = new CategoryDataStub()
.withName(invalidName);
const context = new CategoryCollectionParseContextStub();
// act
const act = () => parseCategory(category, context);
@@ -71,7 +67,7 @@ describe('CategoryParser', () => {
// arrange
const expectedError = 'undefined context';
const context = undefined;
const category = getValidCategory();
const category = new CategoryDataStub();
// act
const act = () => parseCategory(category, context);
// assert
@@ -81,11 +77,8 @@ describe('CategoryParser', () => {
// arrange
const url = 'https://privacy.sexy';
const expected = parseDocUrls({ docs: url });
const category: CategoryData = {
category: 'category name',
children: getTestChildren(),
docs: url,
};
const category = new CategoryDataStub()
.withDocs(url);
const context = new CategoryCollectionParseContextStub();
// act
const actual = parseCategory(category, context).documentationUrls;
@@ -98,10 +91,8 @@ describe('CategoryParser', () => {
const script = ScriptDataStub.createWithCode();
const context = new CategoryCollectionParseContextStub();
const expected = [ parseScript(script, context) ];
const category: CategoryData = {
category: 'category name',
children: [ script ],
};
const category = new CategoryDataStub()
.withChildren([ script ]);
// act
const actual = parseCategory(category, context).scripts;
// assert
@@ -115,10 +106,8 @@ describe('CategoryParser', () => {
const context = new CategoryCollectionParseContextStub()
.withCompiler(compiler);
const expected = [ parseScript(script, context) ];
const category: CategoryData = {
category: 'category name',
children: [ script ],
};
const category = new CategoryDataStub()
.withChildren([ script ]);
// act
const actual = parseCategory(category, context).scripts;
// assert
@@ -128,10 +117,8 @@ describe('CategoryParser', () => {
// arrange
const callableScript = ScriptDataStub.createWithCall();
const scripts = [ callableScript, ScriptDataStub.createWithCode() ];
const category: CategoryData = {
category: 'category name',
children: scripts,
};
const category = new CategoryDataStub()
.withChildren(scripts);
const compiler = new ScriptCompilerStub()
.withCompileAbility(callableScript);
const context = new CategoryCollectionParseContextStub()
@@ -148,19 +135,16 @@ describe('CategoryParser', () => {
const duplicatedCode = `${commentDelimiter} duplicate-line\n${commentDelimiter} duplicate-line`;
const parseContext = new CategoryCollectionParseContextStub()
.withSyntax(new LanguageSyntaxStub().withCommentDelimiters(commentDelimiter));
const category: CategoryData = {
category: 'category name',
children: [
{
category: 'sub-category',
children: [
const category = new CategoryDataStub()
.withChildren([
new CategoryDataStub()
.withName('sub-category')
.withChildren([
ScriptDataStub
.createWithoutCallOrCodes()
.withCode(duplicatedCode),
],
},
],
};
]),
]);
// act
const act = () => parseCategory(category, parseContext).scripts;
// assert
@@ -169,14 +153,13 @@ describe('CategoryParser', () => {
});
it('returns expected subcategories', () => {
// arrange
const expected: CategoryData[] = [ {
category: 'test category',
children: [ ScriptDataStub.createWithCode() ],
}];
const category: CategoryData = {
category: 'category name',
children: expected,
};
const expected = [ new CategoryDataStub()
.withName('test category')
.withChildren([ ScriptDataStub.createWithCode() ]),
];
const category = new CategoryDataStub()
.withName('category name')
.withChildren(expected);
const context = new CategoryCollectionParseContextStub();
// act
const actual = parseCategory(category, context).subCategories;
@@ -187,17 +170,3 @@ describe('CategoryParser', () => {
});
});
});
function getValidCategory(): CategoryData {
return {
category: 'category name',
children: getTestChildren(),
docs: undefined,
};
}
function getTestChildren(): ReadonlyArray<CategoryOrScriptData> {
return [
ScriptDataStub.createWithCode(),
];
}

View File

@@ -0,0 +1,21 @@
import { CategoryData, CategoryOrScriptData, DocumentationUrlsData } from 'js-yaml-loader!@/*';
import { ScriptDataStub } from './ScriptDataStub';
export class CategoryDataStub implements CategoryData {
public children: readonly CategoryOrScriptData[] = [ ScriptDataStub.createWithCode() ];
public category = 'category name';
public docs?: DocumentationUrlsData;
public withChildren(children: readonly CategoryOrScriptData[]) {
this.children = children;
return this;
}
public withName(name: string) {
this.category = name;
return this;
}
public withDocs(docs: DocumentationUrlsData) {
this.docs = docs;
return this;
}
}

View File

@@ -1,26 +1,29 @@
import { RecommendationLevel } from '@/domain/RecommendationLevel';
import { ScriptingLanguage } from '@/domain/ScriptingLanguage';
import { CategoryData, ScriptData, CollectionData, ScriptingDefinitionData } from 'js-yaml-loader!@/*';
import { CategoryData, ScriptData, CollectionData, ScriptingDefinitionData, FunctionData } from 'js-yaml-loader!@/*';
export class CollectionDataStub implements CollectionData {
public os = 'windows';
public actions: readonly CategoryData[] = [ getCategoryStub() ];
public scripting: ScriptingDefinitionData = getTestDefinitionStub();
public functions?: ReadonlyArray<FunctionData>;
public withActions(actions: readonly CategoryData[]): CollectionDataStub {
this.actions = actions;
return this;
}
public withOs(os: string): CollectionDataStub {
this.os = os;
return this;
}
public withScripting(scripting: ScriptingDefinitionData): CollectionDataStub {
this.scripting = scripting;
return this;
}
public withFunctions(functions: ReadonlyArray<FunctionData>) {
this.functions = functions;
return this;
}
}
export function getCategoryStub(scriptPrefix = 'testScript'): CategoryData {

View File

@@ -55,4 +55,8 @@ export class ScriptDataStub implements ScriptData {
this.recommend = recommend;
return this;
}
public withRecommendationLevel(level: RecommendationLevel): ScriptDataStub {
this.recommend = RecommendationLevel[level].toLowerCase();
return this;
}
}