Bug 1150822 - Need ability to skip each test on a given conditions. r=ted

This commit is contained in:
Hiroyuki Ikezoe 2015-04-27 20:39:00 +02:00
parent 35891662e5
commit 3f33560589
2 changed files with 148 additions and 9 deletions

View File

@ -1302,6 +1302,14 @@ function do_send_remote_message(name) {
/**
* Add a test function to the list of tests that are to be run asynchronously.
*
* @param funcOrProperties
* A function to be run or an object represents test properties.
* Supported properties:
* skip_if : An arrow function which has an expression to be
* evaluated whether the test is skipped or not.
* @param func
* A function to be run only if the funcOrProperies is not a function.
*
* Each test function must call run_next_test() when it's done. Test files
* should call run_next_test() in their run_test function to execute all
* async tests.
@ -1309,14 +1317,31 @@ function do_send_remote_message(name) {
* @return the test function that was passed in.
*/
let _gTests = [];
function add_test(func) {
_gTests.push([false, func]);
function add_test(funcOrProperties, func) {
if (typeof funcOrProperties == "function") {
_gTests.push([{ _isTask: false }, funcOrProperties]);
} else if (typeof funcOrProperties == "object") {
funcOrProperties._isTask = false;
_gTests.push([funcOrProperties, func]);
} else {
do_throw("add_test() should take a function or an object and a function");
}
return func;
}
/**
* Add a test function which is a Task function.
*
* @param funcOrProperties
* A generator function to be run or an object represents test
* properties.
* Supported properties:
* skip_if : An arrow function which has an expression to be
* evaluated whether the test is skipped or not.
* @param func
* A generator function to be run only if the funcOrProperies is not a
* function.
*
* Task functions are functions fed into Task.jsm's Task.spawn(). They are
* generators that emit promises.
*
@ -1331,7 +1356,7 @@ function add_test(func) {
*
* Example usage:
*
* add_task(function test() {
* add_task(function* test() {
* let result = yield Promise.resolve(true);
*
* do_check_true(result);
@ -1340,7 +1365,7 @@ function add_test(func) {
* do_check_eq(secondary, "expected value");
* });
*
* add_task(function test_early_return() {
* add_task(function* test_early_return() {
* let result = yield somethingThatReturnsAPromise();
*
* if (!result) {
@ -1350,9 +1375,24 @@ function add_test(func) {
*
* do_check_eq(result, "foo");
* });
*
* add_task({
* skip_if: () => !("@mozilla.org/telephony/volume-service;1" in Components.classes),
* }, function* test_volume_service() {
* let volumeService = Cc["@mozilla.org/telephony/volume-service;1"]
* .getService(Ci.nsIVolumeService);
* ...
* });
*/
function add_task(func) {
_gTests.push([true, func]);
function add_task(funcOrProperties, func) {
if (typeof funcOrProperties == "function") {
_gTests.push([{ _isTask: true }, funcOrProperties]);
} else if (typeof funcOrProperties == "object") {
funcOrProperties._isTask = true;
_gTests.push([funcOrProperties, func]);
} else {
do_throw("add_task() should take a function or an object and a function");
}
}
let _Task = Components.utils.import("resource://gre/modules/Task.jsm", {}).Task;
_Task.Debugging.maintainStack = true;
@ -1377,12 +1417,25 @@ function run_next_test()
if (_gTestIndex < _gTests.length) {
// Flush uncaught errors as early and often as possible.
_Promise.Debugging.flushUncaughtErrors();
let _isTask;
[_isTask, _gRunningTest] = _gTests[_gTestIndex++];
let _properties;
[_properties, _gRunningTest,] = _gTests[_gTestIndex++];
if (typeof(_properties.skip_if) == "function" && _properties.skip_if()) {
let _condition = _properties.skip_if.toSource().replace(/\(\)\s*=>\s*/, "");
let _message = _gRunningTest.name
+ " skipped because the following conditions were"
+ " met: (" + _condition + ")";
_testLogger.testStatus(_TEST_NAME,
_gRunningTest.name,
"SKIP",
"SKIP",
_message);
do_execute_soon(run_next_test);
return;
}
_testLogger.info(_TEST_NAME + " | Starting " + _gRunningTest.name);
do_test_pending(_gRunningTest.name);
if (_isTask) {
if (_properties._isTask) {
_gTaskRunning = true;
_Task.spawn(_gRunningTest).then(
() => { _gTaskRunning = false; run_next_test(); },

View File

@ -547,6 +547,92 @@ tail =
self.assertInLog(TEST_PASS_STRING)
self.assertNotInLog(TEST_FAIL_STRING)
def testSkipForAddTest(self):
"""
Check that add_test is skipped if |skip_if| condition is true
"""
self.writeFile("test_skip.js", """
add_test({
skip_if: () => true,
}, function test_should_be_skipped() {
do_check_true(false);
run_next_test();
});
""")
self.writeManifest(["test_skip.js"])
self.assertTestResult(True, verbose=True)
self.assertEquals(1, self.x.testCount)
self.assertEquals(1, self.x.passCount)
self.assertEquals(0, self.x.failCount)
self.assertEquals(0, self.x.todoCount)
self.assertInLog(TEST_PASS_STRING)
self.assertInLog("TEST-SKIP")
self.assertNotInLog(TEST_FAIL_STRING)
def testNotSkipForAddTask(self):
"""
Check that add_task is not skipped if |skip_if| condition is false
"""
self.writeFile("test_not_skip.js", """
add_task({
skip_if: () => false,
}, function test_should_not_be_skipped() {
do_check_true(true);
});
""")
self.writeManifest(["test_not_skip.js"])
self.assertTestResult(True, verbose=True)
self.assertEquals(1, self.x.testCount)
self.assertEquals(1, self.x.passCount)
self.assertEquals(0, self.x.failCount)
self.assertEquals(0, self.x.todoCount)
self.assertInLog(TEST_PASS_STRING)
self.assertNotInLog("TEST-SKIP")
self.assertNotInLog(TEST_FAIL_STRING)
def testSkipForAddTask(self):
"""
Check that add_task is skipped if |skip_if| condition is true
"""
self.writeFile("test_skip.js", """
add_task({
skip_if: () => true,
}, function test_should_be_skipped() {
do_check_true(false);
});
""")
self.writeManifest(["test_skip.js"])
self.assertTestResult(True, verbose=True)
self.assertEquals(1, self.x.testCount)
self.assertEquals(1, self.x.passCount)
self.assertEquals(0, self.x.failCount)
self.assertEquals(0, self.x.todoCount)
self.assertInLog(TEST_PASS_STRING)
self.assertInLog("TEST-SKIP")
self.assertNotInLog(TEST_FAIL_STRING)
def testNotSkipForAddTest(self):
"""
Check that add_test is not skipped if |skip_if| condition is false
"""
self.writeFile("test_not_skip.js", """
add_test({
skip_if: () => false,
}, function test_should_not_be_skipped() {
do_check_true(true);
run_next_test();
});
""")
self.writeManifest(["test_not_skip.js"])
self.assertTestResult(True, verbose=True)
self.assertEquals(1, self.x.testCount)
self.assertEquals(1, self.x.passCount)
self.assertEquals(0, self.x.failCount)
self.assertEquals(0, self.x.todoCount)
self.assertInLog(TEST_PASS_STRING)
self.assertNotInLog("TEST-SKIP")
self.assertNotInLog(TEST_FAIL_STRING)
def testSyntaxError(self):
"""
Check that running a test file containing a syntax error produces