UNPKG

org.eclipse.n4js.mangelhaft

Version:

An xUnit Testing Framework for N4JS.

189 lines (186 loc) 6.91 kB
// Generated by N4JS transpiler; for copyright see original N4JS source file. import 'n4js-runtime' import {IInstrumentedTest} from './types/IInstrumentedTest.js' import {TestFunctionType} from './types/TestFunctionType.js' import {TestMethodDescriptor} from './types/TestMethodDescriptor.js' export class InstrumentedTest extends N4Object { filterTests(testNames) { this.tests = this.tests.filter(function(test) { return testNames.indexOf(test.name) !== -1; }); } constructor(testClass, info, testObject, parameterizedName, parameterizedTests) { super(); this.tests = []; this.beforeAlls = []; this.afterAlls = []; this.befores = []; this.afters = []; this.filePath = undefined; this.fqn = ""; this.child = null; this.error = undefined; this.classIgnoreAnnotation = undefined; $initFieldsFromInterfaces(this, InstrumentedTest, undefined, { n4type: true }); if (testClass) { this.load(testClass, info); this.parameterizedName = parameterizedName; this.setTestObject(testObject); if (info && info.testMethods) { this.filterTests(info.testMethods); } } if (parameterizedTests) { this.hasParameterizedTests = true; this.parameterizedTests = parameterizedTests; } } static getParameterizedFields(testClass) { let parameterizedFields = new Map(); for(let field of testClass.n4type.dataFieldsWithAnnotation("Parameter", true, true, false)) { let [ indexStr ] = field.anyAnnotation("Parameter").details || [ "" ]; let argNum = Number.parseInt(indexStr) || 0; parameterizedFields.set(argNum, field.name); } return parameterizedFields; } static getParameterizedInstrumentedTests(testClass, info, testInjector, parameterGroups, nameTemplate) { nameTemplate = nameTemplate || "{index}"; let tests = []; let parameterizedFields = this.getParameterizedFields(testClass); if (parameterGroups && parameterGroups.length) { let ii = 0; for(let pGroup of parameterGroups) { let testObject = testInjector.create(testClass); let jj = 0; for(let parm of pGroup) { if (parameterizedFields.has(jj)) { (testObject)[parameterizedFields.get(jj)] = parm; } ++jj; } let parameterizedName = nameTemplate.replace(/{(index|[0-9]*)}/g, (match, item)=>{ if (item === "index") { return ii; } else { const paramNumber = Number.parseInt(item); return String(pGroup[paramNumber] ?? "").replace(/[^\w]/mg, "").slice(0, 100); } }); tests.push(new InstrumentedTest(testClass, info, testObject, parameterizedName)); ++ii; } } else { tests.push(new InstrumentedTest(testClass, info, testInjector.create(testClass))); } return tests; } static getInstrumentedTest(testClass, info, testInjector, controller) { let parameters = null; let nameTemplate = null; let pMeth = testClass.n4type.methodsWithAnnotation("Parameters", true, true, true).pop(); if (pMeth) { let anno = pMeth.anyAnnotation("Parameters"); [ nameTemplate ] = anno.details || [ "" ]; parameters = pMeth.jsFunction.call(testClass); } let parameterizedTests; if (parameters) { parameterizedTests = this.getParameterizedInstrumentedTests(testClass, info, testInjector, parameters, nameTemplate); } const instance = testInjector.internalCreate(testClass, testInjector, new Map([ [ `${'org.eclipse.n4js.mangelhaft'}${'org/eclipse/n4js/mangelhaft/TestController/TestController'}`, controller ] ])); return new InstrumentedTest(testClass, info, instance, null, parameterizedTests); } getFixmeScope(fixme) { let scopes; if (fixme) { let scope; if (fixme.details.length > 1) { scope = fixme.details[1]; } if (scope) { scopes = new Set(scope.split(",").map((scope)=>scope.trim())); } } return scopes; } getTestMethodDescriptors(meths, tftype) { return meths.map((methodDescriptor)=>{ const desc = methodDescriptor.anyAnnotation("Description"); const details = desc ? desc.details : []; const fixmeAnnotation = methodDescriptor.anyAnnotation("Fixme"); const ignoreAnnotation = this.classIgnoreAnnotation ? this.classIgnoreAnnotation : methodDescriptor.anyAnnotation("Ignore"); const timeoutAnnotation = methodDescriptor.anyAnnotation("Timeout"); return new TestMethodDescriptor({ timeout: timeoutAnnotation && timeoutAnnotation.details ? parseInt(timeoutAnnotation.details.pop()) : 60 * 1000, description: details.length ? details.join(" ") : "", ignore: !!ignoreAnnotation, ignoreReason: ignoreAnnotation ? ignoreAnnotation.details.join(" ") : "", fixme: !!fixmeAnnotation, fixmeReason: fixmeAnnotation ? fixmeAnnotation.details.join(" ") : "", fixmeScopes: this.getFixmeScope(fixmeAnnotation), name: methodDescriptor.name, value: methodDescriptor.jsFunction, type: tftype }); }); } setTestObject(test) { this.testObject = test; return this; } setError(error) { this.error = error; return this; } load(testClass, info) { this.classIgnoreAnnotation = testClass.n4type.allAnnotations("Ignore")[0]; this.beforeAlls = this.getTestMethodDescriptors(testClass.n4type.methodsWithAnnotation("BeforeAll", true, false, false), TestFunctionType.BEFORE_ALL); this.afterAlls = this.getTestMethodDescriptors(testClass.n4type.methodsWithAnnotation("AfterAll", true, false, false), TestFunctionType.AFTER_ALL); this.befores = this.getTestMethodDescriptors(testClass.n4type.methodsWithAnnotation("Before", true, false, false), TestFunctionType.BEFORE_TEST); this.afters = this.getTestMethodDescriptors(testClass.n4type.methodsWithAnnotation("After", true, false, false), TestFunctionType.AFTER_TEST); this.tests = this.getTestMethodDescriptors(testClass.n4type.methodsWithAnnotation("Test", true, true, false), TestFunctionType.TEST); if (info) { this.fqn = info.fqn; this.filePath = info.filePath; } this.fqn = this.fqn || testClass.n4type.fqn; this.name = this.fqn; if (info && info.testMethods && info.testMethods.length) { this.tests = this.tests.filter(function(test) { return info.testMethods.indexOf(test.name) !== -1; }); } let parentClass = Object.getPrototypeOf(testClass); let parentClassFn = parentClass; if (parentClassFn !== Object) { this.parent = new InstrumentedTest().load(parentClass); this.parent.child = this; } return this; } static get n4type() { return $getReflectionForClass(this, '["InstrumentedTest","org/eclipse/n4js/mangelhaft/InstrumentedTest","org.eclipse.n4js.mangelhaft",["f.tests","f.beforeAlls","f.afterAlls","f.befores","f.afters","f.filePath","f.fqn","f.child","f.error","f.classIgnoreAnnotation","f:parent","f:hasParameterizedTests","f:parameterizedTests","f:testObject","f:name","f:parameterizedName"]]'); } static get $implements() { return [ IInstrumentedTest ]; } } //# sourceMappingURL=InstrumentedTest.map