mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-10 03:32:52 +00:00
feat: tests
This commit is contained in:
@@ -16,18 +16,17 @@ import {
|
||||
isLightweightClass,
|
||||
isLightweightEnum,
|
||||
isUnionType,
|
||||
} from '@openstapps/core-tools/lib/easy-ast/ast-util';
|
||||
import {LightweightAliasDefinition} from '@openstapps/core-tools/lib/easy-ast/types/lightweight-alias-definition';
|
||||
import {LightweightProjectWithIndex} from '@openstapps/core-tools/lib/easy-ast/types/lightweight-project';
|
||||
import {LightweightType} from '@openstapps/core-tools/lib/easy-ast/types/lightweight-type';
|
||||
import {LightweightClassDefinition} from '@openstapps/core-tools/src/easy-ast/types/lightweight-class-definition';
|
||||
import {LightweightDefinition} from '@openstapps/core-tools/src/easy-ast/types/lightweight-definition';
|
||||
import {LightweightProperty} from '@openstapps/core-tools/src/easy-ast/types/lightweight-property';
|
||||
LightweightAliasDefinition,
|
||||
LightweightProjectWithIndex,
|
||||
LightweightType,
|
||||
LightweightClassDefinition,
|
||||
LightweightDefinition,
|
||||
LightweightProperty,
|
||||
} from '@openstapps/easy-ast';
|
||||
import {expect} from 'chai';
|
||||
import {assign, chain, clone, flatMap, isNil, reduce, reject, some} from 'lodash';
|
||||
|
||||
process.on('unhandledRejection', err => {
|
||||
throw err;
|
||||
process.on('unhandledRejection', error => {
|
||||
throw error;
|
||||
});
|
||||
|
||||
describe('Features', () => {
|
||||
@@ -37,8 +36,8 @@ describe('Features', () => {
|
||||
let thingsWithoutReferences: LightweightClassDefinition[];
|
||||
|
||||
before(function () {
|
||||
this.timeout(15000);
|
||||
this.slow(10000);
|
||||
this.timeout(15_000);
|
||||
this.slow(10_000);
|
||||
|
||||
project = new LightweightProjectWithIndex('src');
|
||||
|
||||
@@ -51,9 +50,7 @@ describe('Features', () => {
|
||||
referenceName: 'SCDiff',
|
||||
});
|
||||
|
||||
expect(
|
||||
thingsReflection.type?.specificationTypes?.every(it => typeof it.referenceName !== 'undefined'),
|
||||
).to.be.true;
|
||||
expect(thingsReflection.type?.specificationTypes?.map(it => it.referenceName)).not.to.include(undefined);
|
||||
thingNames = thingsReflection.type?.specificationTypes?.map(type => type.referenceName!) ?? [];
|
||||
things = thingNames.map(it => project.definitions[it]).filter(isLightweightClass);
|
||||
thingsWithoutReferences = thingNames
|
||||
@@ -64,15 +61,22 @@ describe('Features', () => {
|
||||
const inheritedProperties = function (
|
||||
classLike: LightweightClassDefinition,
|
||||
): Record<string, LightweightProperty> | undefined {
|
||||
return reduce(
|
||||
[...(classLike.implementedDefinitions ?? []), ...(classLike.extendedDefinitions ?? [])],
|
||||
(obj, extension) => {
|
||||
const object = project.definitions[extension.referenceName ?? ''];
|
||||
const extendClause = [
|
||||
...(classLike.implementedDefinitions ?? []),
|
||||
...(classLike.extendedDefinitions ?? []),
|
||||
];
|
||||
const properties = {...classLike.properties};
|
||||
|
||||
return assign(obj, isLightweightClass(object) ? inheritedProperties(object) : obj);
|
||||
},
|
||||
clone(classLike.properties),
|
||||
);
|
||||
for (const definition of extendClause) {
|
||||
const object = project.definitions[definition.referenceName!];
|
||||
if (isLightweightClass(object)) {
|
||||
Object.assign(properties, inheritedProperties(object));
|
||||
} else {
|
||||
Object.assign(properties, object);
|
||||
}
|
||||
}
|
||||
|
||||
return properties;
|
||||
};
|
||||
|
||||
it('should have an origin', () => {
|
||||
@@ -82,42 +86,36 @@ describe('Features', () => {
|
||||
});
|
||||
|
||||
it('should not have duplicate names', () => {
|
||||
reduce(
|
||||
project.files,
|
||||
(fileResult, file) =>
|
||||
reduce(
|
||||
file,
|
||||
(definitionResult, definition: LightweightDefinition) => {
|
||||
expect(definitionResult[definition.name]).to.be.undefined;
|
||||
definitionResult[definition.name] = true; // something that's not undefined
|
||||
const names = new Set<string>();
|
||||
|
||||
return definitionResult;
|
||||
},
|
||||
fileResult,
|
||||
),
|
||||
{} as Record<string, true>,
|
||||
);
|
||||
for (const fileName in project.files) {
|
||||
const file = project.files[fileName];
|
||||
for (const definition in file) {
|
||||
const definitionName = file[definition].name;
|
||||
expect(names).not.to.include(definitionName);
|
||||
names.add(definitionName);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('should not have properties referencing SCThing', () => {
|
||||
const allPropertyReferenceNames: (property: LightweightProperty) => string[] = property =>
|
||||
reject(
|
||||
[property.type.referenceName!, ...flatMap(property.properties, allPropertyReferenceNames)],
|
||||
isNil,
|
||||
);
|
||||
[
|
||||
property.type.referenceName!,
|
||||
...Object.values(property.properties ?? []).flatMap(allPropertyReferenceNames),
|
||||
].filter(it => !!it);
|
||||
|
||||
const typeHasSCThingReferences: (type?: LightweightType) => boolean = type =>
|
||||
type?.referenceName
|
||||
? hasSCThingReferences(project.definitions[type.referenceName])
|
||||
: some(type?.specificationTypes, typeHasSCThingReferences);
|
||||
: type?.specificationTypes?.some(typeHasSCThingReferences) === true;
|
||||
|
||||
const hasSCThingReferences: (definition?: LightweightDefinition) => boolean = definition =>
|
||||
isLightweightClass(definition)
|
||||
? chain(inheritedProperties(definition))
|
||||
.flatMap(it => flatMap(it.properties, allPropertyReferenceNames))
|
||||
? Object.values(inheritedProperties(definition) ?? [])
|
||||
.flatMap(it => Object.values(it.properties ?? []).flatMap(allPropertyReferenceNames))
|
||||
.map(it => project.definitions[it] as LightweightDefinition)
|
||||
.some(it => it.name === 'SCThing' || hasSCThingReferences(it))
|
||||
.value()
|
||||
: definition
|
||||
? typeHasSCThingReferences(definition.type)
|
||||
: false;
|
||||
@@ -127,16 +125,18 @@ describe('Features', () => {
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Checks if a definition is an SCThing
|
||||
*/
|
||||
function extendsSCThing(definition?: LightweightDefinition): boolean {
|
||||
return isLightweightClass(definition)
|
||||
? chain([
|
||||
? [
|
||||
...((definition as LightweightClassDefinition).extendedDefinitions ?? []),
|
||||
...((definition as LightweightClassDefinition).implementedDefinitions ?? []),
|
||||
])
|
||||
]
|
||||
.map(it => it.referenceName)
|
||||
.reject(isNil)
|
||||
.filter(it => !!it)
|
||||
.some(it => it === 'SCThing' || extendsSCThing(project.definitions[it!]))
|
||||
.value()
|
||||
: false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user