All files generator.js

66.67% Statements 72/108
50% Branches 21/42
100% Functions 8/8
69.07% Lines 67/97

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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223                2x 2x 2x 2x   2x 2x 2x 2x                     2x   2x 2x   2x   2x   2x     2x         2x   2x 2x 2x 2x     2x       2x   2x                     2x 1x 1x   1x 4x   4x 4x   4x   6x   6x 6x 18x 18x   4x 4x 4x   4x       6x 6x 3x 3x     4x 4x     2x                     2x 4x   4x                                                         4x                     2x 1x   1x 1x 2x 5x 3x         1x 3x         1x                       2x 1x                                                 2x 4x 5x   1x    
"use strict";
 
/**
 * Tests generator.
 *
 * @module
 */
 
require("colors");
var _ = require("lodash");
var prettyms = require("pretty-ms");
var U = require("glace-utils");
 
var CONF = require("./config");
var loadSteps = require("./loader").loadSteps;
var Test = require("./test");
var train = require("./train");
 
/**
 * Generates test cases.
 *
 * @function
 * @arg {object} opts - Options.
 * @arg {string} [opts.filter] - Chunk of step name to choose tests which
 *  contain this step.
 * @return {object[]} - List of generated tests and unused steps.
 */
exports.generate = opts => {
 
    opts = U.defVal(opts, {});
    var filter = U.defVal(opts.filter, CONF.gen.filter);
 
    var startTime = new Date();
 
    console.log("Generating tests from steps...".yellow);
 
    Iif (CONF.gen.pretrain) {
        Test.pretrain = U.loadJson(CONF.gen.pretrain);
    } else {
        Iif (CONF.gen.trainBefore) {
            Test.pretrain = train(CONF.gen.trainBefore);
        };
    }
 
    var steps = loadSteps();
 
    var tests = generateTests(steps);
    var unusedSteps = getUnusedSteps(steps, tests);
    tests = filterTests(tests, filter);
    Iif (CONF.gen.testsMax) {
        tests = tests.slice(0, CONF.gen.testsMax);
    }
    Iif (CONF.gen.testsReverse) {
        tests.reverse();
    }
 
    console.log(`${tests.length} tests are generated during ${prettyms(new Date() - startTime)}`.yellow);
 
    return [tests, unusedSteps];
};
 
/**
 * Generates tests.
 *
 * @ignore
 * @function
 * @arg {Step[]} steps
 * @return {Test[]}
 */
var generateTests = steps => {
    var tests = [new Test()];
    var isStarted = false;
 
    while (!isStarted || changes(tests)) {
        isStarted = true;
 
        var tmp = CONF.gen.testsShuffle ? _.shuffle(tests) : tests;
        tests = [];
 
        for (var t of tmp) {
 
            var sameTest = true;
 
            Eif (t.steps.length < CONF.gen.stepsLimit) {
                for (var s of steps) {
                    Iif (tests.length >= CONF.gen.testsLimit) break;
                    if (!t.mayAdd(s)) continue;
 
                    var clone = t.clone();
                    clone.add(s.clone());
                    tests.push(clone);
 
                    sameTest = false;
                };
            };
 
            Iif (tests.length >= CONF.gen.testsLimit) break;
            if (sameTest) {
                t.commit();
                tests.push(t);
            };
        };
        tests.sort((a, b) => b.weight - a.weight);
        tests = filterByUniqSteps(tests);
    };
 
    return tests.filter(t => !t.incomplete.length);
};
 
/**
 * Filters tests by unique steps sequence.
 *
 * @ignore
 * @function
 * @arg {Test[]} tests
 * @return {Test[]}
 */
var filterByUniqSteps = tests => {
    var stepsUniq = CONF.gen.stepsUniq;
 
    Iif (stepsUniq) {
        var stepNames = [];
        var tmp = [];
 
        for (var t of tests) {
 
            if (t.steps.length <= stepsUniq) {
                tmp.push(t);
                continue;
            };
 
            var isPresent = true;
            for (var i = 0; i < t.steps.length - stepsUniq + 1; i++) {
 
                var sName = "";
                for (var j = 0; j < stepsUniq; j++) {
                    sName += " " + t.steps[i+j].name;
                };
 
                if (!stepNames.includes(sName)) {
                    stepNames.push(sName);
                    isPresent = false;
                };
            };
            if (!isPresent) tmp.push(t);
        };
        tests = tmp;
    };
 
    return tests;
};
 
/**
 * Gets unused steps
 *
 * @ignore
 * @function
 * @arg {Step[]} steps
 * @return {Step[]}
 */
var getUnusedSteps = (steps, tests) => {
    var ss = [];
 
    var stepNames = [];
    for (var t of tests) {
        for (var s of t.steps) {
            if (!stepNames.includes(s.name)) {
                stepNames.push(s.name);
            };
        };
    };
 
    for (var step of steps) {
        Iif (!stepNames.includes(step.name)) {
            ss.push(step);
        };
    };
 
    return ss;
};
 
/**
 * Filters tests
 *
 * @ignore
 * @function
 * @arg {Test[]} tests
 * @arg {string} filter
 * @return {Test[]}
 */
var filterTests = (tests, filter) => {
    Eif (!filter) return tests;
    var filtered = [];
    for (var t of tests) {
        var isMatched = false;
 
        for (var s of t.steps) {
            if (s.name.includes(filter)) {
                isMatched = true;
                break;
            };
        };
 
        if (isMatched) filtered.push(t);
    };
    return filtered;
};
 
/**
 * Defines whether there are changes in tests or no.
 *
 * @ignore
 * @function
 * @arg {Test[]}
 * @return {boolean}
 */
var changes = tests => {
    for (var t of tests) {
        if (t.isChanged) return true;
    };
    return false;
};