all files / dist/ TestRunnerGenerator.js

96% Statements 48/50
70% Branches 7/10
100% Functions 12/12
96% Lines 48/50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109    10× 10×                               10×             10×                   10× 10×   10×           10×         10× 10× 10× 13×   10× 10× 13× 10×     10×             10× 10×                    
"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