Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

coverage-map.js 3.4KB

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