all files / dist/ TestFunctionBodyWriter.js

79.8% Statements 79/99
71.43% Branches 20/28
70.27% Functions 26/37
79.38% Lines 77/97
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155    10× 10× 10× 10×   10× 10×   10×         10×     10× 10×           10×   10× 10× 10×                   10×     10×   10×   10× 10× 10×         10× 10×                 10×   16× 16×   16× 16×                   16× 16× 16× 16×           15×       14×     15× 15× 15× 15× 15×           12×                      
"use strict";
var TestFunctionBodyWriter = (function () {
    /* istanbul ignore next */ function TestFunctionBodyWriter() {
    }
    TestFunctionBodyWriter.prototype.writeForStructure = function (structure, writer) {
        var _this = this;
        writer.write("this.assertions.describe(\"" + structure.getName() + "\", () => ").inlineBlock(function () {
            _this.writeNullCheck(writer);
            structure.getValidExtendsStructures().forEach(function (extendsStructure) {
                _this.writeExtends(extendsStructure, writer);
            });
            structure.getProperties().forEach(function (prop) {
                _this.writeTestForProperty(structure, prop, writer);
            });
            structure.getCustomTestTransforms().forEach(function (transform) {
                transform.testWrite(writer, structure.getDefinition());
            });
        }).write(");").newLine();
    };
    TestFunctionBodyWriter.prototype.writeNullCheck = function (writer) {
        writer.writeLine("if (this.assertions.isNull(actual, expected)) return;");
    };
    TestFunctionBodyWriter.prototype.writeExtends = function (extendsStructure, writer) {
        writer.writeLine("this." + extendsStructure.getName() + "TestRunner.runTest(actual, expected);");
    };
    TestFunctionBodyWriter.prototype.writeTestForProperty = function (structure, prop, writer) {
        var _this = this;
        Iif (prop.hasMatchedOptInTransforms()) {
            writer.write("if (typeof expected." + prop.getName() + " !== \"undefined\")").block(function () {
                _this.writeTestForPropertyInternal(structure, prop, writer);
            });
        }
        else
            this.writeTestForPropertyInternal(structure, prop, writer);
    };
    TestFunctionBodyWriter.prototype.writeTestForPropertyInternal = function (structure, prop, writer) {
        var _this = this;
        writer.write("this.assertions.describe(\"" + prop.getName() + "\", () => ").inlineBlock(function () {
            Iif (prop.shouldWriteOptionalAnyCheck()) {
                writer.write("this.assertions.assertAny(() => ").inlineBlock(function () {
                    writer.write("this.assertions.it(\"should be undefined\", () => ").inlineBlock(function () {
                        writer.writeLine("this.assertions.strictEqual(actual." + prop.getName() + ", undefined);");
                    }).write(");");
                }).write(", () => ").inlineBlock(function () {
                    _this.writePropertyTypeTest(structure, prop, prop.getType(), writer);
                }).write(");");
            }
            else
                _this.writePropertyTypeTest(structure, prop, prop.getType(), writer);
        }).write(");").newLine();
    };
    TestFunctionBodyWriter.prototype.writePropertyTypeTest = function (structure, prop, structureType, writer) {
        Iif (structureType.shouldIgnoreType())
            return;
        var matchedDefaultValueTransforms = prop.getMatchedDefaultTransforms();
        // todo: use the transformed properties name
        writer.writeLine("let actualValue = actual." + prop.getName() + ";");
        writer.writeLine("let expectedValue = expected." + prop.getName() + ";");
        Iif (matchedDefaultValueTransforms.length > 0) {
            writer.write("if (typeof expectedValue === \"undefined\")").block(function () {
                writer.writeLine("expectedValue = " + matchedDefaultValueTransforms[0].value + ";");
            });
        }
        var matchedPropertyTransforms = prop.getMatchedPropertyTransforms();
        Iif (matchedPropertyTransforms.length > 0) {
            // todo: use the transformed properties name
            writer.write("this.assertions.it(\"should have the same value\", () => ").inlineBlock(function () {
                matchedPropertyTransforms.forEach(function (transform) {
                    transform.testWrite(writer);
                });
            }).write(");");
            return;
        }
        this.writeTypeTest(structure, structureType, writer, "actualValue", "expectedValue");
    };
    TestFunctionBodyWriter.prototype.writeTypeTest = function (structure, structureType, writer, actualName, expectedName) {
        var _this = this;
        Iif (structureType.shouldIgnoreType())
            return;
        var matchedTypeTransforms = structureType.getMatchedTypeTransforms();
        Iif (matchedTypeTransforms.length > 0) {
            writer.write("((actualValue, expectedValue) => ").inlineBlock(function () {
                writer.write("this.assertions.it(\"should have the same value\", () => ").inlineBlock(function () {
                    matchedTypeTransforms.forEach(function (typeTransform) {
                        typeTransform.testWrite(writer);
                    });
                }).write(");");
            }).write(")(" + actualName + ", " + expectedName + ");").newLine();
            return;
        }
        var unionTypes = structureType.getUnionTypes().filter(function (t) { return !t.shouldIgnoreType(); });
        var intersectionTypes = structureType.getIntersectionTypes().filter(function (t) { return !t.shouldIgnoreType(); });
        if (unionTypes.length === 1)
            this.writeNonUnionAndIntersectionTypeTest(structure, unionTypes[0], writer, actualName, expectedName);
        if (unionTypes.length > 1) {
            writer.write("this.assertions.it(\"should equal one of the union types\", () => ").inlineBlock(function () {
                writer.write("this.assertions.assertAny(");
                unionTypes.forEach(function (subType, i) {
                    writer.conditionalWrite(i !== 0, ", ");
                    writer.write("() => ").inlineBlock(function () {
                        writer.write("((actualValue, expectedValue) => ").inlineBlock(function () {
                            _this.writeTypeTest(structure, subType, writer, actualName, expectedName);
                        }).write(")(" + actualName + " as " + subType.getText() + ", " + expectedName + " as " + subType.getExpectedText() + ");").newLine();
                    });
                });
                writer.write(");").newLine();
            }).write(");").newLine();
        }
        else if (intersectionTypes.length > 0) {
            intersectionTypes.forEach(function (subType) {
                _this.writeTypeTest(structure, subType, writer, actualName, expectedName);
            });
        }
        else {
            this.writeNonUnionAndIntersectionTypeTest(structure, structureType, writer, actualName, expectedName);
        }
    };
    TestFunctionBodyWriter.prototype.writeNonUnionAndIntersectionTypeTest = function (structure, structureType, writer, actualName, expectedName) {
        var _this = this;
        var validDefinitions = structureType.getImmediateValidDefinitions();
        var hasValidDefinition = validDefinitions.length > 0;
        var arrayType = structureType.getArrayType();
        if (arrayType != null) {
            writer.write("this.assertions.it(\"should have the same length\", () => ").inlineBlock(function () {
                writer.writeLine("this.assertions.strictEqual(" + actualName + "!.length, " + expectedName + "!.length);");
            }).write(");").newLine();
            writer.write("for (let i = 0; i < (" + expectedName + " || []).length; i++)").block(function () {
                writer.write("((actualValue, expectedValue, i) => ").inlineBlock(function () {
                    writer.write("this.assertions.describe(`index ${i}`, () => ").inlineBlock(function () {
                        _this.writeTypeTest(structure, arrayType, writer, "actualValue", "expectedValue");
                    }).write(");");
                }).write(")(" + actualName + "[i], " + expectedName + "![i], i);");
            });
        }
        else if (structureType.isTypeParameterType())
            writer.writeLine("this." + structureType.getText() + "TestRunner.runTest(" + actualName + ", " + expectedName + ");");
        else if (hasValidDefinition) {
            var isSameClass = validDefinitions[0].getName() === structure.getName();
            var runTestMethodName = isSameClass ? "runTest" : validDefinitions[0].getName() + "TestRunner.runTest";
            writer.writeLine("this." + runTestMethodName + "(" +
                (actualName + " as any as " + validDefinitions[0].getName() + ", ") +
                (expectedName + " as any as " + validDefinitions[0].getTestStructureName() + ");"));
        }
        else {
            writer.write("this.assertions.it(\"should have the same value\", () => ").inlineBlock(function () {
                writer.writeLine("this.assertions.strictEqual(" + actualName + ", " + expectedName + ");");
            }).write(");");
        }
    };
    return TestFunctionBodyWriter;
}());
exports.TestFunctionBodyWriter = TestFunctionBodyWriter;
 
//# sourceMappingURL=TestFunctionBodyWriter.js.map