add test to ensure correct shared functions are being parsed
This commit is contained in:
@@ -10,6 +10,10 @@ import { mockEnumParser } from '../../stubs/EnumParserStub';
|
|||||||
import { ProjectInformationStub } from '../../stubs/ProjectInformationStub';
|
import { ProjectInformationStub } from '../../stubs/ProjectInformationStub';
|
||||||
import { getCategoryStub, CollectionDataStub } from '../../stubs/CollectionDataStub';
|
import { getCategoryStub, CollectionDataStub } from '../../stubs/CollectionDataStub';
|
||||||
import { CategoryCollectionParseContextStub } from '../../stubs/CategoryCollectionParseContextStub';
|
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('CategoryCollectionParser', () => {
|
||||||
describe('parseCategoryCollection', () => {
|
describe('parseCategoryCollection', () => {
|
||||||
@@ -93,5 +97,35 @@ describe('CategoryCollectionParser', () => {
|
|||||||
expect(actual.os).to.equal(expectedOs);
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
import 'mocha';
|
import 'mocha';
|
||||||
import { expect } from 'chai';
|
import { expect } from 'chai';
|
||||||
import { parseCategory } from '@/application/Parser/CategoryParser';
|
import { parseCategory } from '@/application/Parser/CategoryParser';
|
||||||
import { CategoryData, CategoryOrScriptData } from 'js-yaml-loader!@/*';
|
|
||||||
import { parseScript } from '@/application/Parser/Script/ScriptParser';
|
import { parseScript } from '@/application/Parser/Script/ScriptParser';
|
||||||
import { parseDocUrls } from '@/application/Parser/DocumentationParser';
|
import { parseDocUrls } from '@/application/Parser/DocumentationParser';
|
||||||
import { ScriptCompilerStub } from '../../stubs/ScriptCompilerStub';
|
import { ScriptCompilerStub } from '../../stubs/ScriptCompilerStub';
|
||||||
import { ScriptDataStub } from '../../stubs/ScriptDataStub';
|
import { ScriptDataStub } from '../../stubs/ScriptDataStub';
|
||||||
import { CategoryCollectionParseContextStub } from '../../stubs/CategoryCollectionParseContextStub';
|
import { CategoryCollectionParseContextStub } from '../../stubs/CategoryCollectionParseContextStub';
|
||||||
import { LanguageSyntaxStub } from '../../stubs/LanguageSyntaxStub';
|
import { LanguageSyntaxStub } from '../../stubs/LanguageSyntaxStub';
|
||||||
|
import { CategoryDataStub } from '../../stubs/CategoryDataStub';
|
||||||
|
|
||||||
describe('CategoryParser', () => {
|
describe('CategoryParser', () => {
|
||||||
describe('parseCategory', () => {
|
describe('parseCategory', () => {
|
||||||
@@ -26,10 +26,9 @@ describe('CategoryParser', () => {
|
|||||||
// arrange
|
// arrange
|
||||||
const categoryName = 'test';
|
const categoryName = 'test';
|
||||||
const expectedMessage = `category has no children: "${categoryName}"`;
|
const expectedMessage = `category has no children: "${categoryName}"`;
|
||||||
const category: CategoryData = {
|
const category = new CategoryDataStub()
|
||||||
category: categoryName,
|
.withName(categoryName)
|
||||||
children: [],
|
.withChildren([]);
|
||||||
};
|
|
||||||
const context = new CategoryCollectionParseContextStub();
|
const context = new CategoryCollectionParseContextStub();
|
||||||
// act
|
// act
|
||||||
const act = () => parseCategory(category, context);
|
const act = () => parseCategory(category, context);
|
||||||
@@ -40,10 +39,9 @@ describe('CategoryParser', () => {
|
|||||||
// arrange
|
// arrange
|
||||||
const categoryName = 'test';
|
const categoryName = 'test';
|
||||||
const expectedMessage = `category has no children: "${categoryName}"`;
|
const expectedMessage = `category has no children: "${categoryName}"`;
|
||||||
const category: CategoryData = {
|
const category = new CategoryDataStub()
|
||||||
category: categoryName,
|
.withName(categoryName)
|
||||||
children: undefined,
|
.withChildren(undefined);
|
||||||
};
|
|
||||||
const context = new CategoryCollectionParseContextStub();
|
const context = new CategoryCollectionParseContextStub();
|
||||||
// act
|
// act
|
||||||
const act = () => parseCategory(category, context);
|
const act = () => parseCategory(category, context);
|
||||||
@@ -55,10 +53,8 @@ describe('CategoryParser', () => {
|
|||||||
const expectedMessage = 'category has no name';
|
const expectedMessage = 'category has no name';
|
||||||
const invalidNames = ['', undefined];
|
const invalidNames = ['', undefined];
|
||||||
invalidNames.forEach((invalidName) => {
|
invalidNames.forEach((invalidName) => {
|
||||||
const category: CategoryData = {
|
const category = new CategoryDataStub()
|
||||||
category: invalidName,
|
.withName(invalidName);
|
||||||
children: getTestChildren(),
|
|
||||||
};
|
|
||||||
const context = new CategoryCollectionParseContextStub();
|
const context = new CategoryCollectionParseContextStub();
|
||||||
// act
|
// act
|
||||||
const act = () => parseCategory(category, context);
|
const act = () => parseCategory(category, context);
|
||||||
@@ -71,7 +67,7 @@ describe('CategoryParser', () => {
|
|||||||
// arrange
|
// arrange
|
||||||
const expectedError = 'undefined context';
|
const expectedError = 'undefined context';
|
||||||
const context = undefined;
|
const context = undefined;
|
||||||
const category = getValidCategory();
|
const category = new CategoryDataStub();
|
||||||
// act
|
// act
|
||||||
const act = () => parseCategory(category, context);
|
const act = () => parseCategory(category, context);
|
||||||
// assert
|
// assert
|
||||||
@@ -81,11 +77,8 @@ describe('CategoryParser', () => {
|
|||||||
// arrange
|
// arrange
|
||||||
const url = 'https://privacy.sexy';
|
const url = 'https://privacy.sexy';
|
||||||
const expected = parseDocUrls({ docs: url });
|
const expected = parseDocUrls({ docs: url });
|
||||||
const category: CategoryData = {
|
const category = new CategoryDataStub()
|
||||||
category: 'category name',
|
.withDocs(url);
|
||||||
children: getTestChildren(),
|
|
||||||
docs: url,
|
|
||||||
};
|
|
||||||
const context = new CategoryCollectionParseContextStub();
|
const context = new CategoryCollectionParseContextStub();
|
||||||
// act
|
// act
|
||||||
const actual = parseCategory(category, context).documentationUrls;
|
const actual = parseCategory(category, context).documentationUrls;
|
||||||
@@ -98,10 +91,8 @@ describe('CategoryParser', () => {
|
|||||||
const script = ScriptDataStub.createWithCode();
|
const script = ScriptDataStub.createWithCode();
|
||||||
const context = new CategoryCollectionParseContextStub();
|
const context = new CategoryCollectionParseContextStub();
|
||||||
const expected = [ parseScript(script, context) ];
|
const expected = [ parseScript(script, context) ];
|
||||||
const category: CategoryData = {
|
const category = new CategoryDataStub()
|
||||||
category: 'category name',
|
.withChildren([ script ]);
|
||||||
children: [ script ],
|
|
||||||
};
|
|
||||||
// act
|
// act
|
||||||
const actual = parseCategory(category, context).scripts;
|
const actual = parseCategory(category, context).scripts;
|
||||||
// assert
|
// assert
|
||||||
@@ -115,10 +106,8 @@ describe('CategoryParser', () => {
|
|||||||
const context = new CategoryCollectionParseContextStub()
|
const context = new CategoryCollectionParseContextStub()
|
||||||
.withCompiler(compiler);
|
.withCompiler(compiler);
|
||||||
const expected = [ parseScript(script, context) ];
|
const expected = [ parseScript(script, context) ];
|
||||||
const category: CategoryData = {
|
const category = new CategoryDataStub()
|
||||||
category: 'category name',
|
.withChildren([ script ]);
|
||||||
children: [ script ],
|
|
||||||
};
|
|
||||||
// act
|
// act
|
||||||
const actual = parseCategory(category, context).scripts;
|
const actual = parseCategory(category, context).scripts;
|
||||||
// assert
|
// assert
|
||||||
@@ -128,10 +117,8 @@ describe('CategoryParser', () => {
|
|||||||
// arrange
|
// arrange
|
||||||
const callableScript = ScriptDataStub.createWithCall();
|
const callableScript = ScriptDataStub.createWithCall();
|
||||||
const scripts = [ callableScript, ScriptDataStub.createWithCode() ];
|
const scripts = [ callableScript, ScriptDataStub.createWithCode() ];
|
||||||
const category: CategoryData = {
|
const category = new CategoryDataStub()
|
||||||
category: 'category name',
|
.withChildren(scripts);
|
||||||
children: scripts,
|
|
||||||
};
|
|
||||||
const compiler = new ScriptCompilerStub()
|
const compiler = new ScriptCompilerStub()
|
||||||
.withCompileAbility(callableScript);
|
.withCompileAbility(callableScript);
|
||||||
const context = new CategoryCollectionParseContextStub()
|
const context = new CategoryCollectionParseContextStub()
|
||||||
@@ -148,19 +135,16 @@ describe('CategoryParser', () => {
|
|||||||
const duplicatedCode = `${commentDelimiter} duplicate-line\n${commentDelimiter} duplicate-line`;
|
const duplicatedCode = `${commentDelimiter} duplicate-line\n${commentDelimiter} duplicate-line`;
|
||||||
const parseContext = new CategoryCollectionParseContextStub()
|
const parseContext = new CategoryCollectionParseContextStub()
|
||||||
.withSyntax(new LanguageSyntaxStub().withCommentDelimiters(commentDelimiter));
|
.withSyntax(new LanguageSyntaxStub().withCommentDelimiters(commentDelimiter));
|
||||||
const category: CategoryData = {
|
const category = new CategoryDataStub()
|
||||||
category: 'category name',
|
.withChildren([
|
||||||
children: [
|
new CategoryDataStub()
|
||||||
{
|
.withName('sub-category')
|
||||||
category: 'sub-category',
|
.withChildren([
|
||||||
children: [
|
|
||||||
ScriptDataStub
|
ScriptDataStub
|
||||||
.createWithoutCallOrCodes()
|
.createWithoutCallOrCodes()
|
||||||
.withCode(duplicatedCode),
|
.withCode(duplicatedCode),
|
||||||
],
|
]),
|
||||||
},
|
]);
|
||||||
],
|
|
||||||
};
|
|
||||||
// act
|
// act
|
||||||
const act = () => parseCategory(category, parseContext).scripts;
|
const act = () => parseCategory(category, parseContext).scripts;
|
||||||
// assert
|
// assert
|
||||||
@@ -169,14 +153,13 @@ describe('CategoryParser', () => {
|
|||||||
});
|
});
|
||||||
it('returns expected subcategories', () => {
|
it('returns expected subcategories', () => {
|
||||||
// arrange
|
// arrange
|
||||||
const expected: CategoryData[] = [ {
|
const expected = [ new CategoryDataStub()
|
||||||
category: 'test category',
|
.withName('test category')
|
||||||
children: [ ScriptDataStub.createWithCode() ],
|
.withChildren([ ScriptDataStub.createWithCode() ]),
|
||||||
}];
|
];
|
||||||
const category: CategoryData = {
|
const category = new CategoryDataStub()
|
||||||
category: 'category name',
|
.withName('category name')
|
||||||
children: expected,
|
.withChildren(expected);
|
||||||
};
|
|
||||||
const context = new CategoryCollectionParseContextStub();
|
const context = new CategoryCollectionParseContextStub();
|
||||||
// act
|
// act
|
||||||
const actual = parseCategory(category, context).subCategories;
|
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(),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|||||||
21
tests/unit/stubs/CategoryDataStub.ts
Normal file
21
tests/unit/stubs/CategoryDataStub.ts
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,26 +1,29 @@
|
|||||||
import { RecommendationLevel } from '@/domain/RecommendationLevel';
|
import { RecommendationLevel } from '@/domain/RecommendationLevel';
|
||||||
import { ScriptingLanguage } from '@/domain/ScriptingLanguage';
|
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 {
|
export class CollectionDataStub implements CollectionData {
|
||||||
public os = 'windows';
|
public os = 'windows';
|
||||||
public actions: readonly CategoryData[] = [ getCategoryStub() ];
|
public actions: readonly CategoryData[] = [ getCategoryStub() ];
|
||||||
public scripting: ScriptingDefinitionData = getTestDefinitionStub();
|
public scripting: ScriptingDefinitionData = getTestDefinitionStub();
|
||||||
|
public functions?: ReadonlyArray<FunctionData>;
|
||||||
|
|
||||||
public withActions(actions: readonly CategoryData[]): CollectionDataStub {
|
public withActions(actions: readonly CategoryData[]): CollectionDataStub {
|
||||||
this.actions = actions;
|
this.actions = actions;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public withOs(os: string): CollectionDataStub {
|
public withOs(os: string): CollectionDataStub {
|
||||||
this.os = os;
|
this.os = os;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public withScripting(scripting: ScriptingDefinitionData): CollectionDataStub {
|
public withScripting(scripting: ScriptingDefinitionData): CollectionDataStub {
|
||||||
this.scripting = scripting;
|
this.scripting = scripting;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
public withFunctions(functions: ReadonlyArray<FunctionData>) {
|
||||||
|
this.functions = functions;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getCategoryStub(scriptPrefix = 'testScript'): CategoryData {
|
export function getCategoryStub(scriptPrefix = 'testScript'): CategoryData {
|
||||||
|
|||||||
@@ -55,4 +55,8 @@ export class ScriptDataStub implements ScriptData {
|
|||||||
this.recommend = recommend;
|
this.recommend = recommend;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
public withRecommendationLevel(level: RecommendationLevel): ScriptDataStub {
|
||||||
|
this.recommend = RecommendationLevel[level].toLowerCase();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user