All files test.js

65.28% Statements 47/72
54% Branches 27/50
88.89% Functions 8/9
69.35% Lines 43/62

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                              1x   1x 1x   1x 14x 14x 14x 14x 14x                   1x     20x         20x           20x     11x     8x                           8x                 1x   6x 6x             6x   6x   6x 4x   2x     6x         6x       6x   6x               6x     1x               1x 5x 5x 5x 5x 5x 5x 5x               1x 4x                   1x 12x     1x  
"use strict";
 
/**
 * Creates instance of test.
 * 
 * @class
 * @classdesc Test case data structure which contains its current state and steps.
 * @name Test
 * @prop {boolean} [isChanged=false] - Flag whether test case is changed,
 *  for example, after step addition.
 * @prop {object} [state={}] - Current test case state.
 * @prop {Step[]} [steps=[]] - Test case steps.
 * @prop {string[]} [incomplete=[]] - Test case incompletenesses.
 */
 
var _ = require("lodash");
 
var isSub = require("./utils").isSub;
var CONF = require("./config");
 
var Test = function () {
    this.isChanged = false;
    this.state = {};
    this.steps = [];
    this.incomplete = [];
    this.weight = 0;
};
 
/**
 * Detects whether it's possible to add step to test case or no.
 *
 * @method
 * @arg {Step} step - Step to check.
 * @return {boolean} `true` if it's possible, `false` otherwise.
 */
Test.prototype.mayAdd = function (step) {
 
    /* add step if it should after last step in test */
    Iif (this.steps.length &&
            this.steps[this.steps.length - 1].name === step.after) {
        return true;
    };
 
    Iif (this.steps.length &&
            this.steps[this.steps.length - 1].before === step.name) {
        return true;
    };
 
    /* don't add step if test case is finished */
    if (this.state === null && this.steps.length) return false;
 
    /* don't add step if its limit is exhausted */
    if (this._amount(step) >= (CONF.gen.stepsUsage || step.usage)) return false;
 
    /* if step has completeness it should be matches test incompleteness */
    Iif (step.complete) {
        var isEqual = true;
        var i = 0;
        var incomplete = _.clone(this.incomplete).reverse();
        for (var cmpl of step.complete) {
            var testCmpl = incomplete[i++];
            if (cmpl !== testCmpl) {
                isEqual = false;
                break;
            };
        };
        if (!isEqual) return false;
    };
 
    return isSub(this.state, step.income || {});
};
 
/**
 * Adds step to test case.
 *
 * @method
 * @arg {Step} step - Added step.
 */
Test.prototype.add = function (step) {
 
    this.steps.push(step);
    this.isChanged = true;
 
    /**
     * Don't merge states, if there were steps before addition and state is
     * finished. It means, that this step was added explicitly via keywords
     * `before` or `after`.
     */
    Iif (this.state === null && this.steps.length > 1) return;
 
    var outcome = step.outcome === undefined ? {} : step.outcome;
 
    if (_.isObject(this.state) && _.isObject(outcome)) {
        _.merge(this.state, outcome);
    } else {
        this.state = outcome;
    };
 
    Iif (step.complete) {
        this.incomplete = this.incomplete.slice(-this.incomplete.length,
            this.incomplete.length - step.complete.length);
    };
 
    Iif (step.incomplete) {
        this.incomplete = this.incomplete.concat(step.incomplete);
    };
 
    var weight = step.weight;
 
    Iif (Test.pretrain) {
        var x = 0;
        var prevStep = _.nth(this.steps, -2);
        if (prevStep) x += (Test.pretrain[prevStep.name + " | " + step.name] || 0);
        var prevPrevStep = _.nth(this.steps, -3);
        if (prevPrevStep) x += (Test.pretrain[prevPrevStep.name + " | " + prevStep.name + " | " + step.name] || 0);
        if (x > 0) weight *= (1 + activate(x));
    };
    this.weight = _.round(this.weight + weight, 3);
};
 
var activate = x => (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x));
 
/**
 * Clones test case.
 *
 * @method
 * @return {Test} New instance of test with the same parameters.
 */
Test.prototype.clone = function () {
    var c = new this.constructor();
    c.isChanged = this.isChanged;
    c.state = _.cloneDeep(this.state);
    c.steps = this.steps.map(s => s.clone());
    c.incomplete = _.clone(this.incomplete);
    c.weight = this.weight;
    return c;
};
 
/**
 * Commits test case and flushes changes state.
 *
 * @method
 */
Test.prototype.commit = function () {
    this.isChanged = false;
};
 
/**
 * Calculates number of step usage in test case.
 *
 * @method
 * @arg {Step} step - Step which usage is calculated in test case.
 * @return {number} Number of step usage.
 */
Test.prototype._amount = function (step) {
    return this.steps.filter(s => s.name === step.name).length;
};
 
module.exports = Test;