Bug 873126: integrate Assert.jsm into Mochitest. r=ted
authorMike de Boer <mdeboer@mozilla.com>
Thu, 21 Nov 2013 11:26:33 +0100
changeset 171361 e7f2806161650209c6bb39783adffd71d8273ed5
parent 171360 182650a21d4b2a960111d9de5d5664e177142390
child 171362 cf3ed86dfe975832131df57dc4dff9e9f41e20ae
push id3224
push userlsblakk@mozilla.com
push dateTue, 04 Feb 2014 01:06:49 +0000
treeherdermozilla-beta@60c04d0987f1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersted
bugs873126
milestone28.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 873126: integrate Assert.jsm into Mochitest. r=ted
testing/mochitest/browser-test.js
testing/modules/moz.build
testing/modules/tests/browser/browser.ini
testing/modules/tests/browser/browser_test_assert.js
--- a/testing/mochitest/browser-test.js
+++ b/testing/mochitest/browser-test.js
@@ -70,22 +70,24 @@ function Tester(aTests, aDumper, aCallba
   this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/specialpowersAPI.js", simpleTestScope);
   this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/SpecialPowersObserverAPI.js", simpleTestScope);
   this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/ChromePowers.js", simpleTestScope);
   this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/SimpleTest.js", simpleTestScope);
   this._scriptLoader.loadSubScript("chrome://mochikit/content/chrome-harness.js", simpleTestScope);
   this.SimpleTest = simpleTestScope.SimpleTest;
   this.Task = Components.utils.import("resource://gre/modules/Task.jsm", null).Task;
   this.Promise = Components.utils.import("resource://gre/modules/commonjs/sdk/core/promise.js", null).Promise;
+  this.Assert = Components.utils.import("resource://testing-common/Assert.jsm", null).Assert;
 }
 Tester.prototype = {
   EventUtils: {},
   SimpleTest: {},
   Task: null,
   Promise: null,
+  Assert: null,
 
   repeat: 0,
   runUntilFailure: false,
   checker: null,
   currentTestIndex: -1,
   lastStartTime: null,
   openedWindows: null,
   lastAssertionCount: 0,
@@ -432,24 +434,42 @@ Tester.prototype = {
   },
 
   execTest: function Tester_execTest() {
     this.dumper.dump("TEST-START | " + this.currentTest.path + "\n");
 
     this.SimpleTest.reset();
 
     // Load the tests into a testscope
-    this.currentTest.scope = new testScope(this, this.currentTest);
+    let currentScope = this.currentTest.scope = new testScope(this, this.currentTest);
+    let currentTest = this.currentTest;
 
     // Import utils in the test scope.
     this.currentTest.scope.EventUtils = this.EventUtils;
     this.currentTest.scope.SimpleTest = this.SimpleTest;
     this.currentTest.scope.gTestPath = this.currentTest.path;
     this.currentTest.scope.Task = this.Task;
     this.currentTest.scope.Promise = this.Promise;
+    // Pass a custom report function for mochitest style reporting.
+    this.currentTest.scope.Assert = new this.Assert(function(err, message, stack) {
+      let res;
+      if (err) {
+        res = new testResult(false, err.message, err.stack, false, err.stack);
+      } else {
+        res = new testResult(true, message, "", false, stack);
+      }
+      currentTest.addResult(res);
+    });
+
+    // Allow Assert.jsm methods to be tacked to the current scope.
+    this.currentTest.scope.export_assertions = function() {
+      for (let func in this.Assert) {
+        this[func] = this.Assert[func].bind(this.Assert);
+      }
+    };
 
     // Override SimpleTest methods with ours.
     ["ok", "is", "isnot", "ise", "todo", "todo_is", "todo_isnot", "info", "expectAssertions"].forEach(function(m) {
       this.SimpleTest[m] = this[m];
     }, this.currentTest.scope);
 
     //load the tools to work with chrome .jar and remote
     try {
@@ -478,35 +498,33 @@ Tester.prototype = {
 
       // Run the test
       this.lastStartTime = Date.now();
       if (this.currentTest.scope.__tasks) {
         // This test consists of tasks, added via the `add_task()` API.
         if ("test" in this.currentTest.scope) {
           throw "Cannot run both a add_task test and a normal test at the same time.";
         }
-        let testScope = this.currentTest.scope;
-        let currentTest = this.currentTest;
         this.Task.spawn(function() {
           let task;
           while ((task = this.__tasks.shift())) {
             this.SimpleTest.info("Entering test " + task.name);
             try {
               yield task();
             } catch (ex) {
               let isExpected = !!this.SimpleTest.isExpectingUncaughtException();
               let stack = (typeof ex == "object" && "stack" in ex)?ex.stack:null;
               let name = "Uncaught exception";
               let result = new testResult(isExpected, name, ex, false, stack);
               currentTest.addResult(result);
             }
             this.SimpleTest.info("Leaving test " + task.name);
           }
           this.finish();
-        }.bind(testScope));
+        }.bind(currentScope));
       } else if ("generatorTest" in this.currentTest.scope) {
         if ("test" in this.currentTest.scope) {
           throw "Cannot run both a generator test and a normal test at the same time.";
         }
 
         // This test is a generator. It will not finish immediately.
         this.currentTest.scope.waitForExplicitFinish();
         var result = this.currentTest.scope.generatorTest();
@@ -766,16 +784,17 @@ testScope.prototype = {
   __timeoutFactor: 1,
   __expectedMinAsserts: 0,
   __expectedMaxAsserts: 0,
 
   EventUtils: {},
   SimpleTest: {},
   Task: null,
   Promise: null,
+  Assert: null,
 
   /**
    * Add a test function which is a Task function.
    *
    * Task functions are functions fed into Task.jsm's Task.spawn(). They are
    * generators that emit promises.
    *
    * If an exception is thrown, an assertion fails, or if a rejected
--- a/testing/modules/moz.build
+++ b/testing/modules/moz.build
@@ -1,7 +1,8 @@
 # -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
 # vim: set filetype=python:
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 XPCSHELL_TESTS_MANIFESTS += ['tests/xpcshell/xpcshell.ini']
+BROWSER_CHROME_MANIFESTS += ['tests/browser/browser.ini']
new file mode 100644
--- /dev/null
+++ b/testing/modules/tests/browser/browser.ini
@@ -0,0 +1,1 @@
+[browser_test_assert.js]
new file mode 100644
--- /dev/null
+++ b/testing/modules/tests/browser/browser_test_assert.js
@@ -0,0 +1,18 @@
+/* Any copyright is dedicated to the Public Domain.
+http://creativecommons.org/publicdomain/zero/1.0/ */
+
+function test() {
+  export_assertions();
+
+  ok(true, "pass ok");
+  is(true, true, "pass is");
+  isnot(false, true, "pass isnot");
+  ise(true, true, "pass ise");
+  todo(false, "pass todo");
+  todo_is(false, true, "pass todo_is");
+  todo_isnot(true, true, "pass todo_isnot");
+  info("info message");
+
+  var func = is;
+  func(true, 1, "pass indirect is");
+}