coverage-map.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Copyright 2012-2015, Yahoo Inc.
  3. Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
  4. */
  5. "use strict";
  6. var FileCoverage = require('./file').FileCoverage,
  7. CoverageSummary = require('./file').CoverageSummary;
  8. function loadMap(source) {
  9. var data = {};
  10. Object.keys(source).forEach(function (k) {
  11. var cov = source[k];
  12. if (cov instanceof FileCoverage) {
  13. data[k] = cov;
  14. } else {
  15. data[k] = new FileCoverage(cov);
  16. }
  17. });
  18. return data;
  19. }
  20. /**
  21. * CoverageMap is a map of `FileCoverage` objects keyed by file paths.
  22. * @param {Object} [obj=undefined] obj A coverage map from which to initialize this
  23. * map's contents. This can be the raw global coverage object.
  24. * @constructor
  25. */
  26. function CoverageMap(obj) {
  27. if (!obj) {
  28. this.data = {};
  29. } else if (obj instanceof CoverageMap) {
  30. this.data = obj.data;
  31. } else {
  32. this.data = loadMap(obj);
  33. }
  34. }
  35. /**
  36. * merges a second coverage map into this one
  37. * @param {CoverageMap} obj - a CoverageMap or its raw data. Coverage is merged
  38. * correctly for the same files and additional file coverage keys are created
  39. * as needed.
  40. */
  41. CoverageMap.prototype.merge = function (obj) {
  42. var that = this,
  43. other;
  44. if (obj instanceof CoverageMap) {
  45. other = obj;
  46. } else {
  47. other = new CoverageMap(obj);
  48. }
  49. Object.keys(other.data).forEach(function (k) {
  50. var fc = other.data[k];
  51. if (that.data[k]) {
  52. that.data[k].merge(fc);
  53. } else {
  54. that.data[k] = fc;
  55. }
  56. });
  57. };
  58. /**
  59. * filter the coveragemap based on the callback provided
  60. * @param {Function (filename)} callback - Returns true if the path
  61. * should be included in the coveragemap. False if it should be
  62. * removed.
  63. */
  64. CoverageMap.prototype.filter = function (callback) {
  65. var that = this;
  66. Object.keys(that.data).forEach(function (k) {
  67. if (!callback(k)) {
  68. delete that.data[k];
  69. }
  70. });
  71. };
  72. /**
  73. * returns a JSON-serializable POJO for this coverage map
  74. * @returns {Object}
  75. */
  76. CoverageMap.prototype.toJSON = function () {
  77. return this.data;
  78. };
  79. /**
  80. * returns an array for file paths for which this map has coverage
  81. * @returns {Array{string}} - array of files
  82. */
  83. CoverageMap.prototype.files = function () {
  84. return Object.keys(this.data);
  85. };
  86. /**
  87. * returns the file coverage for the specified file.
  88. * @param {String} file
  89. * @returns {FileCoverage}
  90. */
  91. CoverageMap.prototype.fileCoverageFor = function (file) {
  92. var fc = this.data[file];
  93. if (!fc) {
  94. throw new Error('No file coverage available for: ' + file);
  95. }
  96. return fc;
  97. };
  98. /**
  99. * adds a file coverage object to this map. If the path for the object,
  100. * already exists in the map, it is merged with the existing coverage
  101. * otherwise a new key is added to the map.
  102. * @param {FileCoverage} fc the file coverage to add
  103. */
  104. CoverageMap.prototype.addFileCoverage = function (fc) {
  105. var cov = new FileCoverage(fc),
  106. path = cov.path;
  107. if (this.data[path]) {
  108. this.data[path].merge(cov);
  109. } else {
  110. this.data[path] = cov;
  111. }
  112. };
  113. /**
  114. * returns the coverage summary for all the file coverage objects in this map.
  115. * @returns {CoverageSummary}
  116. */
  117. CoverageMap.prototype.getCoverageSummary = function () {
  118. var that = this,
  119. ret = new CoverageSummary();
  120. this.files().forEach(function (key) {
  121. ret.merge(that.fileCoverageFor(key).toSummary());
  122. });
  123. return ret;
  124. };
  125. module.exports = {
  126. CoverageMap: CoverageMap
  127. };