Skip to content

Commit

Permalink
add test to ensure correct shared functions are being parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
undergroundwires committed Feb 8, 2021
1 parent df273f7 commit d7de420
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 66 deletions.
34 changes: 34 additions & 0 deletions tests/unit/application/Parser/CategoryCollectionParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
});
});
});
95 changes: 32 additions & 63 deletions tests/unit/application/Parser/CategoryParser.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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 changes: 21 additions & 0 deletions tests/unit/stubs/CategoryDataStub.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
9 changes: 6 additions & 3 deletions tests/unit/stubs/CollectionDataStub.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/stubs/ScriptDataStub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit d7de420

Please sign in to comment.