All files / test/Common TestHelper.js

81.16% Statements 125/154
81.81% Branches 27/33
80% Functions 12/15
81.16% Lines 125/154

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 15543x 43x 43x 43x 43x 43x 119x 119x 119x 119x 119x 16x 1x 1x           16x 14x 14x     15x 1x 1x 16x 119x 119x 118x 118x 119x 119x 105x 105x 119x 119x 118x 118x 119x 119x 119x 119x 119x 43x 43x           43x 43x           43x 43x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 43x 43x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 43x 43x 38x 38x 38x 38x 43x 43x 48x 48x 3x 47x 45x 45x 23x 45x 22x 22x 45x 48x 48x 48x 38x 38x 48x 48x 43x 43x 25x 25x 43x 43x         43x 43x 40x 40x 43x 43x 43x 43x 43x 43x 11x 11x 43x 43x 8x 8x 43x 43x 8x 8x 43x 43x 43x 12406x             12406x  
// Copyright (c) ZeroC, Inc.
 
import { Ice } from "@zeroc/ice";
 
export class TestHelper {
    getTestEndpoint(...args) {
        let properties;
        let num;
        let protocol = "";
 
        if (args.length > 0) {
            if (typeof args[0] == "object") {
                properties = args[0];
                if (args.length > 1) {
                    num = args[1];
                    if (args.length > 2) {
                        protocol = args[2];
                    }
                }
            } else if (typeof args[0] == "number") {
                num = args[0];
                if (args.length > 1) {
                    protocol = args[1];
                }
            } else {
                protocol = args[0];
            }
        }
 
        if (properties === undefined) {
            properties = this._communicator.getProperties();
        }
 
        if (num === undefined) {
            num = 0;
        }
 
        if (protocol == "") {
            protocol = properties.getIceProperty("Ice.Default.Protocol");
        }
 
        const port = properties.getPropertyAsIntWithDefault("Test.BasePort", 12010) + num;
 
        return `${protocol} -p ${port}`;
    }
 
    getTestHost(properties) {
        if (properties === undefined) {
            properties = this._communicator.getProperties();
        }
        return properties.getPropertyWithDefault("Ice.Default.Host", "127.0.0.1");
    }
 
    getTestProtocol(properties) {
        if (properties === undefined) {
            properties = this._communicator.getProperties();
        }
        return properties.getIceProperty("Ice.Default.Protocol");
    }
 
    getTestPort(...args) {
        let properties;
        let num;
        if (args.length > 1) {
            properties = args[0];
            num = args[1];
        } else {
            num = args[0];
        }
 
        if (properties === undefined) {
            properties = this._communicator.getProperties();
        }
        return properties.getPropertyAsIntWithDefault("Test.BasePort", 12010) + num;
    }
 
    updateLogFileProperty(properties, suffix) {
        const logFile = properties.getIceProperty("Ice.LogFile");
        if (!TestHelper.isBrowser() && logFile.length > 0) {
            const dotIndex = logFile.lastIndexOf(".");
            const newLogFile =
                dotIndex == -1
                    ? logFile + suffix
                    : logFile.substring(0, dotIndex) + suffix + logFile.substring(dotIndex);
            properties.setProperty("Ice.LogFile", newLogFile);
        }
    }
 
    createTestProperties(args = []) {
        const properties = new Ice.Properties(args);
        args = properties.parseCommandLineOptions("Test", args);
        return [properties, args];
    }
 
    initialize(...args) {
        let initData;
        if (args[0] instanceof Ice.InitializationData) {
            initData = args[0];
        } else {
            initData = new Ice.InitializationData();
            if (args[0] instanceof Ice.Properties) {
                initData.properties = args[0];
            } else {
                [initData.properties, args[0]] = this.createTestProperties(args[0]);
            }
        }
 
        const communicator = new Ice.Communicator(initData);
        if (this._communicator === undefined) {
            this._communicator = communicator;
        }
        return [communicator, args[0]];
    }
 
    communicator() {
        return this._communicator;
    }
 
    shutdown() {
        if (this._communicator !== undefined) {
            this._communicator.shutdown();
        }
    }
 
    getWriter() {
        return this.controllerHelper;
    }
 
    setControllerHelper(controllerHelper) {
        this.controllerHelper = controllerHelper;
    }
 
    serverReady() {
        this.controllerHelper.serverReady();
    }
 
    static isBrowser() {
        return typeof window !== "undefined" || TestHelper.isWorker();
    }
 
    static isWorker() {
        return typeof WorkerGlobalScope !== "undefined" && self instanceof WorkerGlobalScope;
    }
}
 
export function test(value, error) {
    if (!value) {
        let message = "test failed";
        if (error) {
            message += `\n${error}`;
        }
        throw new Error(message);
    }
}