From d7de420d5c91bd9ce64880cd4a4391ad3a0a5401 Mon Sep 17 00:00:00 2001 From: undergroundwires Date: Mon, 8 Feb 2021 07:10:41 +0100 Subject: [PATCH] add test to ensure correct shared functions are being parsed --- .../Parser/CategoryCollectionParser.spec.ts | 34 +++++++ .../application/Parser/CategoryParser.spec.ts | 95 +++++++------------ tests/unit/stubs/CategoryDataStub.ts | 21 ++++ tests/unit/stubs/CollectionDataStub.ts | 9 +- tests/unit/stubs/ScriptDataStub.ts | 4 + 5 files changed, 97 insertions(+), 66 deletions(-) create mode 100644 tests/unit/stubs/CategoryDataStub.ts diff --git a/tests/unit/application/Parser/CategoryCollectionParser.spec.ts b/tests/unit/application/Parser/CategoryCollectionParser.spec.ts index e2c8b555..bfceeca7 100644 --- a/tests/unit/application/Parser/CategoryCollectionParser.spec.ts +++ b/tests/unit/application/Parser/CategoryCollectionParser.spec.ts @@ -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); + }); + }); }); }); diff --git a/tests/unit/application/Parser/CategoryParser.spec.ts b/tests/unit/application/Parser/CategoryParser.spec.ts index b60a336d..cc292de0 100644 --- a/tests/unit/application/Parser/CategoryParser.spec.ts +++ b/tests/unit/application/Parser/CategoryParser.spec.ts @@ -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 { - return [ - ScriptDataStub.createWithCode(), - ]; -} diff --git a/tests/unit/stubs/CategoryDataStub.ts b/tests/unit/stubs/CategoryDataStub.ts new file mode 100644 index 00000000..55e176ed --- /dev/null +++ b/tests/unit/stubs/CategoryDataStub.ts @@ -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; + } +} diff --git a/tests/unit/stubs/CollectionDataStub.ts b/tests/unit/stubs/CollectionDataStub.ts index 13d1137a..2c4662b5 100644 --- a/tests/unit/stubs/CollectionDataStub.ts +++ b/tests/unit/stubs/CollectionDataStub.ts @@ -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; 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) { + this.functions = functions; + return this; + } } export function getCategoryStub(scriptPrefix = 'testScript'): CategoryData { diff --git a/tests/unit/stubs/ScriptDataStub.ts b/tests/unit/stubs/ScriptDataStub.ts index 34cf95e7..0d0f93de 100644 --- a/tests/unit/stubs/ScriptDataStub.ts +++ b/tests/unit/stubs/ScriptDataStub.ts @@ -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; + } }