All files combinatorial_generated_expression_factory.js

95% Statements 19/20
83.33% Branches 5/6
100% Functions 3/3
94.74% Lines 18/19

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 481x     1x       18x 18x       18x 18x 18x       413x       413x 286x     286x     127x   402x 7x     395x 395x 395x                 1x  
const GeneratedExpression = require('./generated_expression')
 
// 256 generated expressions ought to be enough for anybody
const MAX_EXPRESSIONS = 256
 
class CombinatorialGeneratedExpressionFactory {
  constructor(expressionTemplate, parameterTypeCombinations) {
    this._expressionTemplate = expressionTemplate
    this._parameterTypeCombinations = parameterTypeCombinations
  }
 
  generateExpressions() {
    const generatedExpressions = []
    this._generatePermutations(generatedExpressions, 0, [])
    return generatedExpressions
  }
 
  _generatePermutations(generatedExpressions, depth, currentParameterTypes) {
    Iif (generatedExpressions.length >= MAX_EXPRESSIONS) {
      return
    }
 
    if (depth === this._parameterTypeCombinations.length) {
      generatedExpressions.push(
        new GeneratedExpression(this._expressionTemplate, currentParameterTypes)
      )
      return
    }
 
    for (let i = 0; i < this._parameterTypeCombinations[depth].length; ++i) {
      // Avoid recursion if no elements can be added.
      if (generatedExpressions.length >= MAX_EXPRESSIONS) {
        return
      }
 
      const newCurrentParameterTypes = currentParameterTypes.slice() // clone
      newCurrentParameterTypes.push(this._parameterTypeCombinations[depth][i])
      this._generatePermutations(
        generatedExpressions,
        depth + 1,
        newCurrentParameterTypes
      )
    }
  }
}
 
module.exports = CombinatorialGeneratedExpressionFactory