| OLD | NEW |
| 1 /* | 1 /* |
| 2 * testharness-helpers contains various useful extensions to testharness.js to | 2 * testharness-helpers contains various useful extensions to testharness.js to |
| 3 * allow them to be used across multiple tests before they have been | 3 * allow them to be used across multiple tests before they have been |
| 4 * upstreamed. This file is intended to be usable from both document and worker | 4 * upstreamed. This file is intended to be usable from both document and worker |
| 5 * environments, so code should for example not rely on the DOM. | 5 * environments, so code should for example not rely on the DOM. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 // Returns a promise that fulfills after the provided |promise| is fulfilled. | 8 // Returns a promise that fulfills after the provided |promise| is fulfilled. |
| 9 // The |test| succeeds only if |promise| rejects with an exception matching | 9 // The |test| succeeds only if |promise| rejects with an exception matching |
| 10 // |code|. Accepted values for |code| follow those accepted for assert_throws(). | 10 // |code|. Accepted values for |code| follow those accepted for assert_throws(). |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 function() { | 25 function() { |
| 26 throw 'assert_promise_rejects: ' + description + ' Promise did not reject.
'; | 26 throw 'assert_promise_rejects: ' + description + ' Promise did not reject.
'; |
| 27 }, | 27 }, |
| 28 function(e) { | 28 function(e) { |
| 29 if (code !== undefined) { | 29 if (code !== undefined) { |
| 30 assert_throws(code, function() { throw e; }, description); | 30 assert_throws(code, function() { throw e; }, description); |
| 31 } | 31 } |
| 32 }); | 32 }); |
| 33 } | 33 } |
| 34 | 34 |
| 35 // Equivalent to testharness.js's assert_object_equals(), but correctly | |
| 36 // tests that property ownership is the same. | |
| 37 // TODO(jsbell): Upstream this to assert_object_equals and remove. | |
| 38 function assert_object_equals_fixed(actual, expected, description) | |
| 39 { | |
| 40 function check_equal(actual, expected, stack) | |
| 41 { | |
| 42 stack.push(actual); | |
| 43 var p; | |
| 44 for (p in actual) { | |
| 45 assert_equals(expected.hasOwnProperty(p), actual.hasOwnProperty(p), | |
| 46 "different property ownership: " + p + " - " + descrip
tion); | |
| 47 | |
| 48 if (typeof actual[p] === "object" && actual[p] !== null) { | |
| 49 if (stack.indexOf(actual[p]) === -1) { | |
| 50 check_equal(actual[p], expected[p], stack); | |
| 51 } | |
| 52 } else { | |
| 53 assert_equals(actual[p], expected[p], description); | |
| 54 } | |
| 55 } | |
| 56 for (p in expected) { | |
| 57 assert_equals(expected.hasOwnProperty(p), actual.hasOwnProperty(p), | |
| 58 "different property ownership: " + p + " - " + descrip
tion); | |
| 59 } | |
| 60 stack.pop(); | |
| 61 } | |
| 62 check_equal(actual, expected, []); | |
| 63 } | |
| 64 | |
| 65 // Equivalent to assert_in_array, but uses the object-aware equivalence relation | |
| 66 // provided by assert_object_equals_fixed(). | |
| 67 function assert_object_in_array(actual, expected_array, description) { | |
| 68 assert_true(expected_array.some(function(element) { | |
| 69 try { | |
| 70 assert_object_equals_fixed(actual, element); | |
| 71 return true; | |
| 72 } catch (e) { | |
| 73 return false; | |
| 74 } | |
| 75 }), description); | |
| 76 } | |
| 77 | |
| 78 // Assert that the two arrays |actual| and |expected| contain the same set of | |
| 79 // elements as determined by assert_object_equals_fixed. The order is not signif
icant. | |
| 80 // | |
| 81 // |expected| is assumed to not contain any duplicates as determined by | |
| 82 // assert_object_equals_fixed(). | |
| 83 function assert_array_equivalent(actual, expected, description) { | |
| 84 assert_true(Array.isArray(actual), description); | |
| 85 assert_equals(actual.length, expected.length, description); | |
| 86 expected.forEach(function(expected_element) { | |
| 87 // assert_in_array treats the first argument as being 'actual', and the | |
| 88 // second as being 'expected array'. We are switching them around because | |
| 89 // we want to be resilient against the |actual| array containing | |
| 90 // duplicates. | |
| 91 assert_object_in_array(expected_element, actual, description); | |
| 92 }); | |
| 93 } | |
| 94 | |
| 95 // Asserts that two arrays |actual| and |expected| contain the same set of | |
| 96 // elements as determined by assert_object_equals(). The corresponding elements | |
| 97 // must occupy corresponding indices in their respective arrays. | |
| 98 function assert_array_objects_equals(actual, expected, description) { | |
| 99 assert_true(Array.isArray(actual), description); | |
| 100 assert_equals(actual.length, expected.length, description); | |
| 101 actual.forEach(function(value, index) { | |
| 102 assert_object_equals(value, expected[index], | |
| 103 description + ' : object[' + index + ']'); | |
| 104 }); | |
| 105 } | |
| 106 | |
| 107 // Asserts that |object| that is an instance of some interface has the attribute | 35 // Asserts that |object| that is an instance of some interface has the attribute |
| 108 // |attribute_name| following the conditions specified by WebIDL, but it's | 36 // |attribute_name| following the conditions specified by WebIDL, but it's |
| 109 // acceptable that the attribute |attribute_name| is an own property of the | 37 // acceptable that the attribute |attribute_name| is an own property of the |
| 110 // object because we're in the middle of moving the attribute to a prototype | 38 // object because we're in the middle of moving the attribute to a prototype |
| 111 // chain. Once we complete the transition to prototype chains, | 39 // chain. Once we complete the transition to prototype chains, |
| 112 // assert_will_be_idl_attribute must be replaced with assert_idl_attribute | 40 // assert_will_be_idl_attribute must be replaced with assert_idl_attribute |
| 113 // defined in testharness.js. | 41 // defined in testharness.js. |
| 114 // | 42 // |
| 115 // FIXME: Remove assert_will_be_idl_attribute once we complete the transition | 43 // FIXME: Remove assert_will_be_idl_attribute once we complete the transition |
| 116 // of moving the DOM attributes to prototype chains. (http://crbug.com/43394) | 44 // of moving the DOM attributes to prototype chains. (http://crbug.com/43394) |
| (...skipping 18 matching lines...) Expand all Loading... |
| 135 if (typeof src != "object") | 63 if (typeof src != "object") |
| 136 return src; | 64 return src; |
| 137 var dst = Array.isArray(src) ? [] : {}; | 65 var dst = Array.isArray(src) ? [] : {}; |
| 138 for (var property in src) { | 66 for (var property in src) { |
| 139 dst[property] = deepCopy(src[property]); | 67 dst[property] = deepCopy(src[property]); |
| 140 } | 68 } |
| 141 return dst; | 69 return dst; |
| 142 } | 70 } |
| 143 return JSON.stringify(deepCopy(object)); | 71 return JSON.stringify(deepCopy(object)); |
| 144 } | 72 } |
| OLD | NEW |