all files / lib/ coberturaReportParser.js

100% Statements 20/20
91.67% Branches 11/12
100% Functions 6/6
100% Lines 20/20
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                        10×                             12×   16×                               24×   21×   20× 96×   96×            
"use strict";
 
module.exports = processReport;
 
/**
 * Processes the XML report based on the package.
 *
 * <package>...</package>
 *
 * @param {Object} xml is the parsed report.
 * @param {Object} computation is result of computation.
 */
function processReport(xml, computation) {
  if (xml.packages.package instanceof Array) {
    // Process all the packages
    xml.packages.package.forEach(function(packageObject) {
      processPackage(packageObject, computation);
    });
 
  } else {
    processPackage(xml.packages.package, computation);
  }
}
 
/**
 * Processes the individual package entry.
 *
 * <package><class></package>
 *
 * @param {Object} packageObject is the package object from the report.
 * @param {Object} computation is the result of the computation.
 */
function processPackage(packageObject, computation) {
  if (packageObject.classes.class instanceof Array) {
    // Process each individual class
    packageObject.classes.class.forEach(function(clazz) {
      processClass(clazz, computation);
    });
 
  } else {
    // Single class to be processed
    processClass(packageObject.classes.class, computation);
  }
}
 
/**
 * Processes the individual Class and compute totals.
 *
 * <class>...</class>
 *
 * @param {Object} clazz is the class object from the report.
 * @param {Object} computation is the result of the computation.
 */
function processClass(clazz, computation) {
  if (!clazz.methods.method) {
    return;
  }
  if (clazz.methods.method instanceof Array) {
 
    clazz.methods.method.forEach(function(method) {
      ++computation.total;
      // Incremente the total number of methods if there were hits. Don't increment for no hit
      computation.passed = parseInt(method.hits) > 0 ? ++computation.passed : computation.passed;
    });
 
  } else { // That's the method object itself.
    ++computation.total;
    computation.passed = parseInt(clazz.methods.method.hits) > 0 ? ++computation.passed : computation.passed;
  }
}