Bug 1317019 - Fix object iteration and check if scriptName exists. r?jmaher draft
authorGreg Mierzwinski <gmierz2@outlook.com>
Sat, 21 Jan 2017 16:04:28 -0700
changeset 470039 a3ee4b7c1cac5c668df067354a029e259011916b
parent 465990 6dccae211ae5fec6a1c1244b878ce0b93860154f
child 544372 66bc5b18d12770d3cac6dbf8e829b21cda1a9ada
push id43916
push userbmo:gmierz2@outlook.com
push dateFri, 03 Feb 2017 04:30:12 +0000
reviewersjmaher
bugs1317019
milestone54.0a1
Bug 1317019 - Fix object iteration and check if scriptName exists. r?jmaher This patch first implements an iterator so that we can properly iterate over the elements of the object returned by the _getMethodNames function. Next, the recordTestCoverage function now checks to see if the file actually has methods before recording them. And finally, some formatting was done. MozReview-Commit-ID: EPOeYlMBc0w
testing/modules/CoverageUtils.jsm
--- a/testing/modules/CoverageUtils.jsm
+++ b/testing/modules/CoverageUtils.jsm
@@ -94,75 +94,87 @@ CoverageCollector.prototype._getLinesCov
 }
 
 CoverageCollector.prototype._getUncoveredLines = function() {
   let uncoveredLines = {};
   this._scripts.forEach(s => {
     let scriptName = s.url;
     let scriptOffsets = s.getAllOffsets();
 
-    if (!uncoveredLines[scriptName]){
+    if (!uncoveredLines[scriptName]) {
       uncoveredLines[scriptName] = new Set();
     }
 
     // Get all lines in the script
     scriptOffsets.forEach( function(element, index) {
-      if (!element){
+      if (!element) {
         return;
       }
       uncoveredLines[scriptName].add(index);
     });
   });
 
   // For all covered lines, delete their entry
-  for (let scriptName in this._allCoverage){
-    for (let key in this._allCoverage[scriptName]){
+  for (let scriptName in this._allCoverage) {
+    for (let key in this._allCoverage[scriptName]) {
       let [lineNumber, columnNumber, offset] = key.split('#');
       uncoveredLines[scriptName].delete(parseInt(lineNumber, 10));
     }
   }
 
   return uncoveredLines;
 }
 
 CoverageCollector.prototype._getMethodNames = function() {
   let methodNames = {};
   this._scripts.forEach(s => {
     let method = s.displayName;
     // If the method name is undefined, we return early
-    if (!method){
+    if (!method) {
       return;
     }
 
     let scriptName = s.url;
     let tempMethodCov = [];
     let scriptOffsets = s.getAllOffsets();
 
-    if (!methodNames[scriptName]){
+    if (!methodNames[scriptName]) {
       methodNames[scriptName] = {};
     }
 
     /**
     * Get all lines contained within the method and
     * push a record of the form:
     * <method name> : <lines covered>
     */
-    scriptOffsets.forEach(function (element, index){
-      if (!element){
+    scriptOffsets.forEach( function (element, index) {
+      if (!element) {
         return;
       }
       tempMethodCov.push(index);
     });
     methodNames[scriptName][method] = tempMethodCov;
   });
 
   return methodNames;
 }
 
 /**
+ * Implements an iterator for objects. It is
+ * used to iterate over the elements of the object obtained
+ * from the function _getMethodNames.
+ */
+Object.prototype[Symbol.iterator] = function * () {
+  for (var [key, value] of Object.entries(this)) {
+    yield [key, value];
+  }
+};
+
+
+/**
  * Records lines covered since the last time coverage was recorded,
  * associating them with the given test name. The result is written
  * to a json file in a specified directory.
  */
 CoverageCollector.prototype.recordTestCoverage = function (testName) {
   dump("Collecting coverage for: " + testName + "\n");
   let rawLines = this._getLinesCovered(testName);
   let methods = this._getMethodNames();
@@ -175,25 +187,27 @@ CoverageCollector.prototype.recordTestCo
     let rec = {
       testUrl: testName,
       sourceFile: scriptName,
       methods: {},
       covered: [],
       uncovered: []
     };
 
-    for (let methodName in methods[scriptName]){
-      rec.methods[methodName] = methods[scriptName][methodName];
+    if (typeof(methods[scriptName]) != 'undefined' && methods[scriptName] != null) {
+      for (let [methodName, methodLines] of methods[scriptName]) {
+        rec.methods[methodName] = methodLines;
+      }
     }
 
     for (let line of rawLines[scriptName]) {
       rec.covered.push(line);
     }
 
-    for (let line of uncoveredLines[scriptName]){
+    for (let line of uncoveredLines[scriptName]) {
       rec.uncovered.push(line);
     }
 
     result.push(rec);
   }
   let arr = this._encoder.encode(JSON.stringify(result, null, 2));
   let path = this._prefix + '/' + 'jscov_' + Date.now() + '.json';
   dump("Writing coverage to: " + path + "\n");