Code coverage report for lib/checker.js

Statements: 100% (32 / 32)      Branches: 100% (12 / 12)      Functions: 100% (7 / 7)      Lines: 100% (32 / 32)      Ignored: none     

All files » lib/ » checker.js
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 951                 1   9 9 9   9 1     9 1     9                   1 9                   1 9                   1 9     1   27 27 27   27 2     27                   1   9 9   9 36 36 3       9     1
var util = require('util');
 
/**
 * Check result exit code against expectated exit code.
 *
 * @param {Object} result: command execution result
 * @param {Object} test: test expectation
 * @return {Object} error if actual exit code doesn't equal expected exit code
 */
function _checkExitCode(result, test) {
 
  var actual = result.exitcode;
  var expect = test.exitcode;
  var error;
 
  if (typeof expect === 'string') {
    expect = parseInt(expect, 10);
  }
 
  if (expect !== undefined && expect !== actual) {
    error = util.format('Exit code %d does not match expected %d', actual, expect);
  }
 
  return error;
}
 
/**
 * Check result output against expectated output.
 *
 * @param {Object} result: command execution result
 * @param {Object} test: test expectation
 * @return {Object} error if actual output doesn't match expected regexp pattern
 */
function _checkOutput(result, test) {
  return __checkRegExp(result, test, 'output', 'Output');
}
 
/**
 * Check result stdout against expectated stdout.
 *
 * @param {Object} result: command execution result
 * @param {Object} test: test expectation
 * @return {Object} error if actual stdout doesn't match expected regexp pattern
 */
function _checkStdout(result, test) {
  return __checkRegExp(result, test, 'stdout', 'Stdout');
}
 
/**
 * Check result stderr against expectated stderr.
 *
 * @param {Object} result: command execution result
 * @param {Object} test: test expectation
 * @return {Object} error if actual stderr doesn't match expected regexp pattern
 */
function _checkStderr(result, test) {
  return __checkRegExp(result, test, 'stderr', 'Stderr');
}
 
function __checkRegExp(result, test, field, label) {
 
  var actual = result[field];
  var expect = test[field];
  var error;
 
  if (expect !== undefined && !actual.match(new RegExp(expect))) {
    error = util.format('%s does not match expected regexp \'%s\'', label, expect);
  }
 
  return error;
}
 
/**
 * Check command execution result with test expectation against a set of check functions.
 *
 * @param {Object} result: command execution result
 * @param {Object} test: test expectation
 * @return {Array} check errors
 */
function check(result, test) {
 
  const CHECKERS = [ _checkExitCode, _checkOutput, _checkStdout, _checkStderr ];
  var errors = [];
 
  CHECKERS.forEach(function (checker) {
    var error = checker(result, test);
    if (error) {
      errors.push(error);
    }
  });
  
  return errors;
}
 
exports.check = check;