"use strict";
var typeInfo = require("ts-type-info");
var code_block_writer_1 = require("code-block-writer");
var TestFunctionBodyWriter_1 = require("./TestFunctionBodyWriter");
var TestStructureGenerator_1 = require("./TestStructureGenerator");
var TypeTransformer_1 = require("./TypeTransformer");
var wrappers_1 = require("./wrappers");
var TestRunnerGenerator = (function () {
function TestRunnerGenerator() {
this.testStructureGenerator = new TestStructureGenerator_1.TestStructureGenerator(new TypeTransformer_1.TypeTransformer());
this.testFunctionBodyWriter = new TestFunctionBodyWriter_1.TestFunctionBodyWriter();
}
TestRunnerGenerator.prototype.fillTestFile = function (testFile, structures) {
var _this = this;
var _loop_1 = function (structure) {
this_1.testStructureGenerator.fillTestFileFromDefinition(testFile, structure);
var testRunnerClass = testFile.addClass({
name: structure.getName() + "TestRunner",
isExported: true,
constructorDef: {
parameters: [{
name: "assertions",
type: "WrapperAssertions",
isReadonly: true,
scope: typeInfo.ClassConstructorParameterScope.Private
}]
},
methods: [{
name: "initialize"
}]
});
// add initialize dependencies
structure.getInitializeDependencies().forEach(function (dep) {
if (dep instanceof wrappers_1.StructureTypeParameterWrapper) {
_this.addDependency(testRunnerClass, dep.getName() + "TestRunner", "TestRunner<" + dep.getName() + ", " + dep.getTestStructureName() + ">");
}
else Eif (dep instanceof wrappers_1.StructureTypeWrapper) {
_this.addDependency(testRunnerClass, dep.getImmediateValidDefinitions()[0].getName() + "TestRunner", "TestRunner<" + dep.getName() + ", " + dep.getTestStructureNameForTestRunner() + ">");
}
else if (dep instanceof wrappers_1.StructureWrapper) {
_this.addDependency(testRunnerClass, dep.getName() + "TestRunner", "TestRunner<" + dep.getNameWithTypeParameters() + ", " + dep.getTestStructureNameWithTypeParameters() + ">");
}
});
var testMethod = testRunnerClass.addMethod({
name: "runTest",
parameters: [{
name: "actual",
type: structure.getNameWithTypeParameters()
}, {
name: "expected",
type: structure.getTestStructureNameWithTypeParameters()
}]
});
testRunnerClass.addImplements("TestRunner<" + structure.getNameWithTypeParameters() + ", " + structure.getTestStructureNameWithTypeParameters() + ">");
var typeParameters = structure.getTypeParameters();
// add type parameters for actual
typeParameters.forEach(function (typeParam) {
var constraintType = typeParam.getConstraintType();
testRunnerClass.addTypeParameter({
name: typeParam.getName(),
constraintType: constraintType == null ? undefined : constraintType.getText()
});
});
// add type parameters for expected
typeParameters.forEach(function (typeParam) {
var constraintType = typeParam.getConstraintType();
testRunnerClass.addTypeParameter({
name: typeParam.getTestStructureName(),
constraintType: constraintType == null ? undefined : constraintType.getTestStructureName()
});
});
var writer = new code_block_writer_1.default();
this_1.testFunctionBodyWriter.writeForStructure(structure, writer);
testMethod.onWriteFunctionBody = function (methodBodyWriter) {
methodBodyWriter.write(writer.toString());
};
var initializeMethod = testRunnerClass.getMethod("initialize");
initializeMethod.onWriteFunctionBody = function (methodWriter) {
initializeMethod.parameters.forEach(function (p) {
methodWriter.writeLine("this." + p.name + " = " + p.name + ";");
});
};
initializeMethod.parameters.forEach(function (p) {
testRunnerClass.addProperty({
name: p.name,
type: p.type.text,
scope: typeInfo.Scope.Private
});
});
};
var this_1 = this;
for (var _i = 0, structures_1 = structures; _i < structures_1.length; _i++) {
var structure = structures_1[_i];
_loop_1(structure);
}
};
TestRunnerGenerator.prototype.addDependency = function (testRunnerClass, name, type) {
var initializeMethod = testRunnerClass.getMethod("initialize");
initializeMethod.addParameter({
name: name,
type: type
});
};
return TestRunnerGenerator;
}());
exports.TestRunnerGenerator = TestRunnerGenerator;
//# sourceMappingURL=TestRunnerGenerator.js.map
|