From 322fd4ea6b2cdc69d8fbd567f8de644b93068369 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Fri, 26 Dec 2014 18:34:12 +0100 Subject: [PATCH 1/3] Ensure Test.set_status is not called with a non-string message argument. This would cause an error in escape_html later, when it tries to call .replace() on a non-string value. --- testharness.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testharness.js b/testharness.js index fe144390328a22..aef5a8d3a2d290 100644 --- a/testharness.js +++ b/testharness.js @@ -1178,8 +1178,8 @@ policies and contribution forms [3]. if (this.phase >= this.phases.HAS_RESULT) { return; } - var message = (typeof e === "object" && e !== null) ? e.message : e; - if (typeof e.stack != "undefined" && typeof e.message == "string") { + var message = String((typeof e === "object" && e !== null) ? e.message : e); + if (typeof e.stack != "undefined") { //Try to make it more informative for some exceptions, at least //in Gecko and WebKit. This results in a stack dump instead of //just errors like "Cannot read property 'parentNode' of null" From 91db5ba4d4c096e23d9053e43be48f00f0cc4578 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Fri, 26 Dec 2014 18:42:34 +0100 Subject: [PATCH 2/3] Don't assume the exception is an object in assert_throws. --- testharness.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/testharness.js b/testharness.js index aef5a8d3a2d290..c8ee5aa9d4effa 100644 --- a/testharness.js +++ b/testharness.js @@ -1036,7 +1036,10 @@ policies and contribution forms [3]. var required_props = { code: name_code_map[name] }; if (required_props.code === 0 || - ("name" in e && e.name !== e.name.toUpperCase() && e.name !== "DOMException")) { + (typeof e == "object" && + "name" in e && + e.name !== e.name.toUpperCase() && + e.name !== "DOMException")) { // New style exception: also test the name property. required_props.name = name; } From 83ab6e530c9b61cbce63797cc6d8c436b31512fd Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Fri, 26 Dec 2014 18:48:33 +0100 Subject: [PATCH 3/3] Implement a promise_rejects function to match assert_throws. --- docs/api.md | 21 +++++++++++---------- testharness.js | 7 +++++++ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/docs/api.md b/docs/api.md index 0e39154268a891..788b2e018f57ea 100644 --- a/docs/api.md +++ b/docs/api.md @@ -129,21 +129,22 @@ a resolve reaction that verifies the returned value. Note that in the promise chain constructed in `test_function` assertions don't need to wrapped in `step` or `step_func` calls. -Here's another example where the `test_function` uses the provided `test` -parameter to test a Promise that is expected to reject. Note that it's important -to handle all expected rejections since an unhandled rejection causes the test -to fail. +`promise_rejects` can be used to test Promises that need to reject: + + promise_rejects(test_object, code, promise) + +The `code` argument is equivalent to the same argument to the `assert_throws` +function. + +Here's an example where the `bar()` function returns a Promise that rejects +with a TypeError: function bar() { - return Promise.reject("bar"); + return Promise.reject(new TypeError()); } promise_test(function(t) { - return bar() - .then(t.unreached_func("bar() should not accept"), - function(result) { - assert_equals(result, "bar", "bar should return 'bar'"); - }); + return promise_rejects(t, new TypeError(), bar); }, "Another example"); ## Single Page Tests ## diff --git a/testharness.js b/testharness.js index c8ee5aa9d4effa..dbdad32007eeb8 100644 --- a/testharness.js +++ b/testharness.js @@ -464,6 +464,12 @@ policies and contribution forms [3]. })); } + function promise_rejects(test, expected, promise) { + return promise.then(test.unreached_func("Should have rejected.")).catch(function(e) { + assert_throws(expected, function() { throw e }); + }); + } + function setup(func_or_properties, maybe_properties) { var func = null; @@ -511,6 +517,7 @@ policies and contribution forms [3]. expose(test, 'test'); expose(async_test, 'async_test'); expose(promise_test, 'promise_test'); + expose(promise_rejects, 'promise_rejects'); expose(generate_tests, 'generate_tests'); expose(setup, 'setup'); expose(done, 'done');