How to use assert_throws_js_impl method in wpt

Best JavaScript code snippet using wpt

testharness.js

Source:testharness.js Github

copy

Full Screen

...586 function promise_rejects_js(test, constructor, promise, description) {587 return bring_promise_to_current_realm(promise)588 .then(test.unreached_func("Should have rejected: " + description))589 .catch(function(e) {590 assert_throws_js_impl(constructor, function() { throw e },591 description, "promise_rejects_js");592 });593 }594 /**595 * Assert that a Promise is rejected with the right DOMException.596 *597 * @param test the test argument passed to promise_test598 * @param {number|string} type. See documentation for assert_throws_dom.599 *600 * For the remaining arguments, there are two ways of calling601 * promise_rejects_dom:602 *603 * 1) If the DOMException is expected to come from the current global, the604 * third argument should be the promise expected to reject, and a fourth,605 * optional, argument is the assertion description.606 *607 * 2) If the DOMException is expected to come from some other global, the608 * third argument should be the DOMException constructor from that global,609 * the fourth argument the promise expected to reject, and the fifth,610 * optional, argument the assertion description.611 */612 function promise_rejects_dom(test, type, promiseOrConstructor, descriptionOrPromise, maybeDescription) {613 let constructor, promise, description;614 if (typeof promiseOrConstructor === "function" &&615 promiseOrConstructor.name === "DOMException") {616 constructor = promiseOrConstructor;617 promise = descriptionOrPromise;618 description = maybeDescription;619 } else {620 constructor = self.DOMException;621 promise = promiseOrConstructor;622 description = descriptionOrPromise;623 assert(maybeDescription === undefined,624 "Too many args pased to no-constructor version of promise_rejects_dom");625 }626 return bring_promise_to_current_realm(promise)627 .then(test.unreached_func("Should have rejected: " + description))628 .catch(function(e) {629 assert_throws_dom_impl(type, function() { throw e }, description,630 "promise_rejects_dom", constructor);631 });632 }633 function promise_rejects_exactly(test, exception, promise, description) {634 return bring_promise_to_current_realm(promise)635 .then(test.unreached_func("Should have rejected: " + description))636 .catch(function(e) {637 assert_throws_exactly_impl(exception, function() { throw e },638 description, "promise_rejects_exactly");639 });640 }641 /**642 * This constructor helper allows DOM events to be handled using Promises,643 * which can make it a lot easier to test a very specific series of events,644 * including ensuring that unexpected events are not fired at any point.645 */646 function EventWatcher(test, watchedNode, eventTypes, timeoutPromise)647 {648 if (typeof eventTypes == 'string') {649 eventTypes = [eventTypes];650 }651 var waitingFor = null;652 // This is null unless we are recording all events, in which case it653 // will be an Array object.654 var recordedEvents = null;655 var eventHandler = test.step_func(function(evt) {656 assert_true(!!waitingFor,657 'Not expecting event, but got ' + evt.type + ' event');658 assert_equals(evt.type, waitingFor.types[0],659 'Expected ' + waitingFor.types[0] + ' event, but got ' +660 evt.type + ' event instead');661 if (Array.isArray(recordedEvents)) {662 recordedEvents.push(evt);663 }664 if (waitingFor.types.length > 1) {665 // Pop first event from array666 waitingFor.types.shift();667 return;668 }669 // We need to null out waitingFor before calling the resolve function670 // since the Promise's resolve handlers may call wait_for() which will671 // need to set waitingFor.672 var resolveFunc = waitingFor.resolve;673 waitingFor = null;674 // Likewise, we should reset the state of recordedEvents.675 var result = recordedEvents || evt;676 recordedEvents = null;677 resolveFunc(result);678 });679 for (var i = 0; i < eventTypes.length; i++) {680 watchedNode.addEventListener(eventTypes[i], eventHandler, false);681 }682 /**683 * Returns a Promise that will resolve after the specified event or684 * series of events has occurred.685 *686 * @param options An optional options object. If the 'record' property687 * on this object has the value 'all', when the Promise688 * returned by this function is resolved, *all* Event689 * objects that were waited for will be returned as an690 * array.691 *692 * For example,693 *694 * ```js695 * const watcher = new EventWatcher(t, div, [ 'animationstart',696 * 'animationiteration',697 * 'animationend' ]);698 * return watcher.wait_for([ 'animationstart', 'animationend' ],699 * { record: 'all' }).then(evts => {700 * assert_equals(evts[0].elapsedTime, 0.0);701 * assert_equals(evts[1].elapsedTime, 2.0);702 * });703 * ```704 */705 this.wait_for = function(types, options) {706 if (waitingFor) {707 return Promise.reject('Already waiting for an event or events');708 }709 if (typeof types == 'string') {710 types = [types];711 }712 if (options && options.record && options.record === 'all') {713 recordedEvents = [];714 }715 return new Promise(function(resolve, reject) {716 var timeout = test.step_func(function() {717 // If the timeout fires after the events have been received718 // or during a subsequent call to wait_for, ignore it.719 if (!waitingFor || waitingFor.resolve !== resolve)720 return;721 // This should always fail, otherwise we should have722 // resolved the promise.723 assert_true(waitingFor.types.length == 0,724 'Timed out waiting for ' + waitingFor.types.join(', '));725 var result = recordedEvents;726 recordedEvents = null;727 var resolveFunc = waitingFor.resolve;728 waitingFor = null;729 resolveFunc(result);730 });731 if (timeoutPromise) {732 timeoutPromise().then(timeout);733 }734 waitingFor = {735 types: types,736 resolve: resolve,737 reject: reject738 };739 });740 };741 function stop_watching() {742 for (var i = 0; i < eventTypes.length; i++) {743 watchedNode.removeEventListener(eventTypes[i], eventHandler, false);744 }745 };746 test._add_cleanup(stop_watching);747 return this;748 }749 expose(EventWatcher, 'EventWatcher');750 function setup(func_or_properties, maybe_properties)751 {752 var func = null;753 var properties = {};754 if (arguments.length === 2) {755 func = func_or_properties;756 properties = maybe_properties;757 } else if (func_or_properties instanceof Function) {758 func = func_or_properties;759 } else {760 properties = func_or_properties;761 }762 tests.setup(func, properties);763 test_environment.on_new_harness_properties(properties);764 }765 function promise_setup(func, maybe_properties)766 {767 if (typeof func !== "function") {768 tests.set_status(tests.status.ERROR,769 "promise_test invoked without a function");770 tests.complete();771 return;772 }773 tests.promise_setup_called = true;774 if (!tests.promise_tests) {775 tests.promise_tests = Promise.resolve();776 }777 tests.promise_tests = tests.promise_tests778 .then(function()779 {780 var properties = maybe_properties || {};781 var result;782 tests.setup(null, properties);783 result = func();784 test_environment.on_new_harness_properties(properties);785 if (!result || typeof result.then !== "function") {786 throw "Non-thenable returned by function passed to `promise_setup`";787 }788 return result;789 })790 .catch(function(e)791 {792 tests.set_status(tests.status.ERROR,793 String(e),794 e && e.stack);795 tests.complete();796 });797 }798 function done() {799 if (tests.tests.length === 0) {800 // `done` is invoked after handling uncaught exceptions, so if the801 // harness status is already set, the corresponding message is more802 // descriptive than the generic message defined here.803 if (tests.status.status === null) {804 tests.status.status = tests.status.ERROR;805 tests.status.message = "done() was called without first defining any tests";806 }807 tests.complete();808 return;809 }810 if (tests.file_is_test) {811 // file is test files never have asynchronous cleanup logic,812 // meaning the fully-synchronous `done` function can be used here.813 tests.tests[0].done();814 }815 tests.end_wait();816 }817 function generate_tests(func, args, properties) {818 forEach(args, function(x, i)819 {820 var name = x[0];821 test(function()822 {823 func.apply(this, x.slice(1));824 },825 name,826 Array.isArray(properties) ? properties[i] : properties);827 });828 }829 /*830 * Register a function as a DOM event listener to the given object for the831 * event bubbling phase.832 *833 * This function was deprecated in November of 2019.834 */835 function on_event(object, event, callback)836 {837 object.addEventListener(event, callback, false);838 }839 function step_timeout(f, t) {840 var outer_this = this;841 var args = Array.prototype.slice.call(arguments, 2);842 return setTimeout(function() {843 f.apply(outer_this, args);844 }, t * tests.timeout_multiplier);845 }846 expose(test, 'test');847 expose(async_test, 'async_test');848 expose(promise_test, 'promise_test');849 expose(promise_rejects_js, 'promise_rejects_js');850 expose(promise_rejects_dom, 'promise_rejects_dom');851 expose(promise_rejects_exactly, 'promise_rejects_exactly');852 expose(generate_tests, 'generate_tests');853 expose(setup, 'setup');854 expose(promise_setup, 'promise_setup');855 expose(done, 'done');856 expose(on_event, 'on_event');857 expose(step_timeout, 'step_timeout');858 /*859 * Return a string truncated to the given length, with ... added at the end860 * if it was longer.861 */862 function truncate(s, len)863 {864 if (s.length > len) {865 return s.substring(0, len - 3) + "...";866 }867 return s;868 }869 /*870 * Return true if object is probably a Node object.871 */872 function is_node(object)873 {874 // I use duck-typing instead of instanceof, because875 // instanceof doesn't work if the node is from another window (like an876 // iframe's contentWindow):877 // http://www.w3.org/Bugs/Public/show_bug.cgi?id=12295878 try {879 var has_node_properties = ("nodeType" in object &&880 "nodeName" in object &&881 "nodeValue" in object &&882 "childNodes" in object);883 } catch (e) {884 // We're probably cross-origin, which means we aren't a node885 return false;886 }887 if (has_node_properties) {888 try {889 object.nodeType;890 } catch (e) {891 // The object is probably Node.prototype or another prototype892 // object that inherits from it, and not a Node instance.893 return false;894 }895 return true;896 }897 return false;898 }899 var replacements = {900 "0": "0",901 "1": "x01",902 "2": "x02",903 "3": "x03",904 "4": "x04",905 "5": "x05",906 "6": "x06",907 "7": "x07",908 "8": "b",909 "9": "t",910 "10": "n",911 "11": "v",912 "12": "f",913 "13": "r",914 "14": "x0e",915 "15": "x0f",916 "16": "x10",917 "17": "x11",918 "18": "x12",919 "19": "x13",920 "20": "x14",921 "21": "x15",922 "22": "x16",923 "23": "x17",924 "24": "x18",925 "25": "x19",926 "26": "x1a",927 "27": "x1b",928 "28": "x1c",929 "29": "x1d",930 "30": "x1e",931 "31": "x1f",932 "0xfffd": "ufffd",933 "0xfffe": "ufffe",934 "0xffff": "uffff",935 };936 /*937 * Convert a value to a nice, human-readable string938 */939 function format_value(val, seen)940 {941 if (!seen) {942 seen = [];943 }944 if (typeof val === "object" && val !== null) {945 if (seen.indexOf(val) >= 0) {946 return "[...]";947 }948 seen.push(val);949 }950 if (Array.isArray(val)) {951 let output = "[";952 if (val.beginEllipsis !== undefined) {953 output += "…, ";954 }955 output += val.map(function(x) {return format_value(x, seen);}).join(", ");956 if (val.endEllipsis !== undefined) {957 output += ", …";958 }959 return output + "]";960 }961 switch (typeof val) {962 case "string":963 val = val.replace(/\\/g, "\\\\");964 for (var p in replacements) {965 var replace = "\\" + replacements[p];966 val = val.replace(RegExp(String.fromCharCode(p), "g"), replace);967 }968 return '"' + val.replace(/"/g, '\\"') + '"';969 case "boolean":970 case "undefined":971 return String(val);972 case "number":973 // In JavaScript, -0 === 0 and String(-0) == "0", so we have to974 // special-case.975 if (val === -0 && 1/val === -Infinity) {976 return "-0";977 }978 return String(val);979 case "object":980 if (val === null) {981 return "null";982 }983 // Special-case Node objects, since those come up a lot in my tests. I984 // ignore namespaces.985 if (is_node(val)) {986 switch (val.nodeType) {987 case Node.ELEMENT_NODE:988 var ret = "<" + val.localName;989 for (var i = 0; i < val.attributes.length; i++) {990 ret += " " + val.attributes[i].name + '="' + val.attributes[i].value + '"';991 }992 ret += ">" + val.innerHTML + "</" + val.localName + ">";993 return "Element node " + truncate(ret, 60);994 case Node.TEXT_NODE:995 return 'Text node "' + truncate(val.data, 60) + '"';996 case Node.PROCESSING_INSTRUCTION_NODE:997 return "ProcessingInstruction node with target " + format_value(truncate(val.target, 60)) + " and data " + format_value(truncate(val.data, 60));998 case Node.COMMENT_NODE:999 return "Comment node <!--" + truncate(val.data, 60) + "-->";1000 case Node.DOCUMENT_NODE:1001 return "Document node with " + val.childNodes.length + (val.childNodes.length == 1 ? " child" : " children");1002 case Node.DOCUMENT_TYPE_NODE:1003 return "DocumentType node";1004 case Node.DOCUMENT_FRAGMENT_NODE:1005 return "DocumentFragment node with " + val.childNodes.length + (val.childNodes.length == 1 ? " child" : " children");1006 default:1007 return "Node object of unknown type";1008 }1009 }1010 /* falls through */1011 default:1012 try {1013 return typeof val + ' "' + truncate(String(val), 1000) + '"';1014 } catch(e) {1015 return ("[stringifying object threw " + String(e) +1016 " with type " + String(typeof e) + "]");1017 }1018 }1019 }1020 expose(format_value, "format_value");1021 /*1022 * Assertions1023 */1024 function assert_true(actual, description)1025 {1026 assert(actual === true, "assert_true", description,1027 "expected true got ${actual}", {actual:actual});1028 }1029 expose(assert_true, "assert_true");1030 function assert_false(actual, description)1031 {1032 assert(actual === false, "assert_false", description,1033 "expected false got ${actual}", {actual:actual});1034 }1035 expose(assert_false, "assert_false");1036 function same_value(x, y) {1037 if (y !== y) {1038 //NaN case1039 return x !== x;1040 }1041 if (x === 0 && y === 0) {1042 //Distinguish +0 and -01043 return 1/x === 1/y;1044 }1045 return x === y;1046 }1047 function assert_equals(actual, expected, description)1048 {1049 /*1050 * Test if two primitives are equal or two objects1051 * are the same object1052 */1053 if (typeof actual != typeof expected) {1054 assert(false, "assert_equals", description,1055 "expected (" + typeof expected + ") ${expected} but got (" + typeof actual + ") ${actual}",1056 {expected:expected, actual:actual});1057 return;1058 }1059 assert(same_value(actual, expected), "assert_equals", description,1060 "expected ${expected} but got ${actual}",1061 {expected:expected, actual:actual});1062 }1063 expose(assert_equals, "assert_equals");1064 function assert_not_equals(actual, expected, description)1065 {1066 /*1067 * Test if two primitives are unequal or two objects1068 * are different objects1069 */1070 assert(!same_value(actual, expected), "assert_not_equals", description,1071 "got disallowed value ${actual}",1072 {actual:actual});1073 }1074 expose(assert_not_equals, "assert_not_equals");1075 function assert_in_array(actual, expected, description)1076 {1077 assert(expected.indexOf(actual) != -1, "assert_in_array", description,1078 "value ${actual} not in array ${expected}",1079 {actual:actual, expected:expected});1080 }1081 expose(assert_in_array, "assert_in_array");1082 // This function was deprecated in July of 2015.1083 // See https://github.com/web-platform-tests/wpt/issues/20331084 function assert_object_equals(actual, expected, description)1085 {1086 assert(typeof actual === "object" && actual !== null, "assert_object_equals", description,1087 "value is ${actual}, expected object",1088 {actual: actual});1089 //This needs to be improved a great deal1090 function check_equal(actual, expected, stack)1091 {1092 stack.push(actual);1093 var p;1094 for (p in actual) {1095 assert(expected.hasOwnProperty(p), "assert_object_equals", description,1096 "unexpected property ${p}", {p:p});1097 if (typeof actual[p] === "object" && actual[p] !== null) {1098 if (stack.indexOf(actual[p]) === -1) {1099 check_equal(actual[p], expected[p], stack);1100 }1101 } else {1102 assert(same_value(actual[p], expected[p]), "assert_object_equals", description,1103 "property ${p} expected ${expected} got ${actual}",1104 {p:p, expected:expected[p], actual:actual[p]});1105 }1106 }1107 for (p in expected) {1108 assert(actual.hasOwnProperty(p),1109 "assert_object_equals", description,1110 "expected property ${p} missing", {p:p});1111 }1112 stack.pop();1113 }1114 check_equal(actual, expected, []);1115 }1116 expose(assert_object_equals, "assert_object_equals");1117 function assert_array_equals(actual, expected, description)1118 {1119 const max_array_length = 20;1120 function shorten_array(arr, offset = 0) {1121 // Make ", …" only show up when it would likely reduce the length, not accounting for1122 // fonts.1123 if (arr.length < max_array_length + 2) {1124 return arr;1125 }1126 // By default we want half the elements after the offset and half before1127 // But if that takes us past the end of the array, we have more before, and1128 // if it takes us before the start we have more after.1129 const length_after_offset = Math.floor(max_array_length / 2);1130 let upper_bound = Math.min(length_after_offset + offset, arr.length);1131 const lower_bound = Math.max(upper_bound - max_array_length, 0);1132 if (lower_bound === 0) {1133 upper_bound = max_array_length;1134 }1135 const output = arr.slice(lower_bound, upper_bound);1136 if (lower_bound > 0) {1137 output.beginEllipsis = true;1138 }1139 if (upper_bound < arr.length) {1140 output.endEllipsis = true;1141 }1142 return output;1143 }1144 assert(typeof actual === "object" && actual !== null && "length" in actual,1145 "assert_array_equals", description,1146 "value is ${actual}, expected array",1147 {actual:actual});1148 assert(actual.length === expected.length,1149 "assert_array_equals", description,1150 "lengths differ, expected array ${expected} length ${expectedLength}, got ${actual} length ${actualLength}",1151 {expected:shorten_array(expected, expected.length - 1), expectedLength:expected.length,1152 actual:shorten_array(actual, actual.length - 1), actualLength:actual.length1153 });1154 for (var i = 0; i < actual.length; i++) {1155 assert(actual.hasOwnProperty(i) === expected.hasOwnProperty(i),1156 "assert_array_equals", description,1157 "expected property ${i} to be ${expected} but was ${actual} (expected array ${arrayExpected} got ${arrayActual})",1158 {i:i, expected:expected.hasOwnProperty(i) ? "present" : "missing",1159 actual:actual.hasOwnProperty(i) ? "present" : "missing",1160 arrayExpected:shorten_array(expected, i), arrayActual:shorten_array(actual, i)});1161 assert(same_value(expected[i], actual[i]),1162 "assert_array_equals", description,1163 "expected property ${i} to be ${expected} but got ${actual} (expected array ${arrayExpected} got ${arrayActual})",1164 {i:i, expected:expected[i], actual:actual[i],1165 arrayExpected:shorten_array(expected, i), arrayActual:shorten_array(actual, i)});1166 }1167 }1168 expose(assert_array_equals, "assert_array_equals");1169 function assert_array_approx_equals(actual, expected, epsilon, description)1170 {1171 /*1172 * Test if two primitive arrays are equal within +/- epsilon1173 */1174 assert(actual.length === expected.length,1175 "assert_array_approx_equals", description,1176 "lengths differ, expected ${expected} got ${actual}",1177 {expected:expected.length, actual:actual.length});1178 for (var i = 0; i < actual.length; i++) {1179 assert(actual.hasOwnProperty(i) === expected.hasOwnProperty(i),1180 "assert_array_approx_equals", description,1181 "property ${i}, property expected to be ${expected} but was ${actual}",1182 {i:i, expected:expected.hasOwnProperty(i) ? "present" : "missing",1183 actual:actual.hasOwnProperty(i) ? "present" : "missing"});1184 assert(typeof actual[i] === "number",1185 "assert_array_approx_equals", description,1186 "property ${i}, expected a number but got a ${type_actual}",1187 {i:i, type_actual:typeof actual[i]});1188 assert(Math.abs(actual[i] - expected[i]) <= epsilon,1189 "assert_array_approx_equals", description,1190 "property ${i}, expected ${expected} +/- ${epsilon}, expected ${expected} but got ${actual}",1191 {i:i, expected:expected[i], actual:actual[i], epsilon:epsilon});1192 }1193 }1194 expose(assert_array_approx_equals, "assert_array_approx_equals");1195 function assert_approx_equals(actual, expected, epsilon, description)1196 {1197 /*1198 * Test if two primitive numbers are equal within +/- epsilon1199 */1200 assert(typeof actual === "number",1201 "assert_approx_equals", description,1202 "expected a number but got a ${type_actual}",1203 {type_actual:typeof actual});1204 assert(Math.abs(actual - expected) <= epsilon,1205 "assert_approx_equals", description,1206 "expected ${expected} +/- ${epsilon} but got ${actual}",1207 {expected:expected, actual:actual, epsilon:epsilon});1208 }1209 expose(assert_approx_equals, "assert_approx_equals");1210 function assert_less_than(actual, expected, description)1211 {1212 /*1213 * Test if a primitive number is less than another1214 */1215 assert(typeof actual === "number",1216 "assert_less_than", description,1217 "expected a number but got a ${type_actual}",1218 {type_actual:typeof actual});1219 assert(actual < expected,1220 "assert_less_than", description,1221 "expected a number less than ${expected} but got ${actual}",1222 {expected:expected, actual:actual});1223 }1224 expose(assert_less_than, "assert_less_than");1225 function assert_greater_than(actual, expected, description)1226 {1227 /*1228 * Test if a primitive number is greater than another1229 */1230 assert(typeof actual === "number",1231 "assert_greater_than", description,1232 "expected a number but got a ${type_actual}",1233 {type_actual:typeof actual});1234 assert(actual > expected,1235 "assert_greater_than", description,1236 "expected a number greater than ${expected} but got ${actual}",1237 {expected:expected, actual:actual});1238 }1239 expose(assert_greater_than, "assert_greater_than");1240 function assert_between_exclusive(actual, lower, upper, description)1241 {1242 /*1243 * Test if a primitive number is between two others1244 */1245 assert(typeof actual === "number",1246 "assert_between_exclusive", description,1247 "expected a number but got a ${type_actual}",1248 {type_actual:typeof actual});1249 assert(actual > lower && actual < upper,1250 "assert_between_exclusive", description,1251 "expected a number greater than ${lower} " +1252 "and less than ${upper} but got ${actual}",1253 {lower:lower, upper:upper, actual:actual});1254 }1255 expose(assert_between_exclusive, "assert_between_exclusive");1256 function assert_less_than_equal(actual, expected, description)1257 {1258 /*1259 * Test if a primitive number is less than or equal to another1260 */1261 assert(typeof actual === "number",1262 "assert_less_than_equal", description,1263 "expected a number but got a ${type_actual}",1264 {type_actual:typeof actual});1265 assert(actual <= expected,1266 "assert_less_than_equal", description,1267 "expected a number less than or equal to ${expected} but got ${actual}",1268 {expected:expected, actual:actual});1269 }1270 expose(assert_less_than_equal, "assert_less_than_equal");1271 function assert_greater_than_equal(actual, expected, description)1272 {1273 /*1274 * Test if a primitive number is greater than or equal to another1275 */1276 assert(typeof actual === "number",1277 "assert_greater_than_equal", description,1278 "expected a number but got a ${type_actual}",1279 {type_actual:typeof actual});1280 assert(actual >= expected,1281 "assert_greater_than_equal", description,1282 "expected a number greater than or equal to ${expected} but got ${actual}",1283 {expected:expected, actual:actual});1284 }1285 expose(assert_greater_than_equal, "assert_greater_than_equal");1286 function assert_between_inclusive(actual, lower, upper, description)1287 {1288 /*1289 * Test if a primitive number is between to two others or equal to either of them1290 */1291 assert(typeof actual === "number",1292 "assert_between_inclusive", description,1293 "expected a number but got a ${type_actual}",1294 {type_actual:typeof actual});1295 assert(actual >= lower && actual <= upper,1296 "assert_between_inclusive", description,1297 "expected a number greater than or equal to ${lower} " +1298 "and less than or equal to ${upper} but got ${actual}",1299 {lower:lower, upper:upper, actual:actual});1300 }1301 expose(assert_between_inclusive, "assert_between_inclusive");1302 function assert_regexp_match(actual, expected, description) {1303 /*1304 * Test if a string (actual) matches a regexp (expected)1305 */1306 assert(expected.test(actual),1307 "assert_regexp_match", description,1308 "expected ${expected} but got ${actual}",1309 {expected:expected, actual:actual});1310 }1311 expose(assert_regexp_match, "assert_regexp_match");1312 function assert_class_string(object, class_string, description) {1313 var actual = {}.toString.call(object);1314 var expected = "[object " + class_string + "]";1315 assert(same_value(actual, expected), "assert_class_string", description,1316 "expected ${expected} but got ${actual}",1317 {expected:expected, actual:actual});1318 }1319 expose(assert_class_string, "assert_class_string");1320 function assert_own_property(object, property_name, description) {1321 assert(object.hasOwnProperty(property_name),1322 "assert_own_property", description,1323 "expected property ${p} missing", {p:property_name});1324 }1325 expose(assert_own_property, "assert_own_property");1326 function assert_not_own_property(object, property_name, description) {1327 assert(!object.hasOwnProperty(property_name),1328 "assert_not_own_property", description,1329 "unexpected property ${p} is found on object", {p:property_name});1330 }1331 expose(assert_not_own_property, "assert_not_own_property");1332 function _assert_inherits(name) {1333 return function (object, property_name, description)1334 {1335 assert(typeof object === "object" || typeof object === "function" ||1336 // Or has [[IsHTMLDDA]] slot1337 String(object) === "[object HTMLAllCollection]",1338 name, description,1339 "provided value is not an object");1340 assert("hasOwnProperty" in object,1341 name, description,1342 "provided value is an object but has no hasOwnProperty method");1343 assert(!object.hasOwnProperty(property_name),1344 name, description,1345 "property ${p} found on object expected in prototype chain",1346 {p:property_name});1347 assert(property_name in object,1348 name, description,1349 "property ${p} not found in prototype chain",1350 {p:property_name});1351 };1352 }1353 expose(_assert_inherits("assert_inherits"), "assert_inherits");1354 expose(_assert_inherits("assert_idl_attribute"), "assert_idl_attribute");1355 function assert_readonly(object, property_name, description)1356 {1357 var initial_value = object[property_name];1358 try {1359 //Note that this can have side effects in the case where1360 //the property has PutForwards1361 object[property_name] = initial_value + "a"; //XXX use some other value here?1362 assert(same_value(object[property_name], initial_value),1363 "assert_readonly", description,1364 "changing property ${p} succeeded",1365 {p:property_name});1366 } finally {1367 object[property_name] = initial_value;1368 }1369 }1370 expose(assert_readonly, "assert_readonly");1371 /**1372 * Assert a JS Error with the expected constructor is thrown.1373 *1374 * @param {object} constructor The expected exception constructor.1375 * @param {Function} func Function which should throw.1376 * @param {string} description Error description for the case that the error is not thrown.1377 */1378 function assert_throws_js(constructor, func, description)1379 {1380 assert_throws_js_impl(constructor, func, description,1381 "assert_throws_js");1382 }1383 expose(assert_throws_js, "assert_throws_js");1384 /**1385 * Like assert_throws_js but allows specifying the assertion type1386 * (assert_throws_js or promise_rejects_js, in practice).1387 */1388 function assert_throws_js_impl(constructor, func, description,1389 assertion_type)1390 {1391 try {1392 func.call(this);1393 assert(false, assertion_type, description,1394 "${func} did not throw", {func:func});1395 } catch (e) {1396 if (e instanceof AssertionError) {1397 throw e;1398 }1399 // Basic sanity-checks on the thrown exception.1400 assert(typeof e === "object",1401 assertion_type, description,1402 "${func} threw ${e} with type ${type}, not an object",...

Full Screen

Full Screen

assert.js

Source:assert.js Github

copy

Full Screen

...138 substitute_children(template.slice(2), rv);139 }140 return rv;141}142export function assert_throws_js_impl(constructor, func, description, assertion_type) {143 try {144 func.call(this);145 assert.fail(make_message(assertion_type, description, '${func} did not throw', { func: func }));146 } catch (e) {147 if (e instanceof AssertionError) {148 throw e;149 }150 // Basic sanity-checks on the thrown exception.151 assert(typeof e === 'object', make_message(152 assertion_type,153 description,154 '${func} threw ${e} with type ${type}, not an object',155 { func: func, e: e, type: typeof e }156 ));157 assert(e !== null, make_message(assertion_type, description, '${func} threw null, not an object', { func: func }));158 // Basic sanity-check on the passed-in constructor159 assert(typeof constructor === 'function', make_message(160 assertion_type,161 description,162 '${constructor} is not a constructor',163 { constructor: constructor }164 ));165 let obj = constructor;166 while (obj) {167 if (typeof obj === 'function' && obj.name === 'Error') {168 break;169 }170 obj = Object.getPrototypeOf(obj);171 }172 assert(obj != null, make_message(173 assertion_type,174 description,175 '${constructor} is not an Error subtype',176 { constructor: constructor }));177 // And checking that our exception is reasonable178 assert(e.constructor === constructor && e.name === constructor.name, make_message(179 assertion_type, description,180 '${func} threw ${actual} (${actual_name}) expected instance of ${expected} (${expected_name})',181 {182 func: func,183 actual: e,184 actual_name: e.name,185 expected: constructor,186 expected_name: constructor.name187 }188 ));189 }190}191export function assert_throws_js(constructor, func, description) {192 assert_throws_js_impl(constructor, func, description, 'assert_throws_js');193}194export function promise_rejects_js(test, constructor, promise, description) {195 return bring_promise_to_current_realm(promise)196 .then(unreached_func('Should have rejected: ' + description))197 .catch(function (e) {198 assert_throws_js_impl(constructor, function () { throw e; },199 description, 'promise_rejects_js');200 });201}202export function step_timeout(f, t) {203 const outer_this = this;204 const args = Array.prototype.slice.call(arguments, 2);205 return setTimeout(function () {206 f.apply(outer_this, args);207 }, t);208}209export function promise_rejects_exactly(test, exception, promise, description) {210 return bring_promise_to_current_realm(promise)211 .then(unreached_func('Should have rejected: ' + description))212 .catch(function (e) {...

Full Screen

Full Screen

wpthelp.mjs

Source:wpthelp.mjs Github

copy

Full Screen

...90 }91};92global.done = function () {};93global.assert_throws_js = function (constructor, func, description) {94 // assert_throws_js_impl(constructor, func, description, "assert_throws_js");95 (current_t || tap).throws(96 func,97 constructor,98 description99 // [100 // constructor,101 // "assert_throws_js",102 // ]103 );104};105global.async_test = function (func, name, properties) {106 return {107 done: function () {},108 };...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const assert_throws_js_impl = (expected, func, message) => {2 let caught = false;3 try {4 func();5 } catch (e) {6 caught = true;7 assert_equals(e.constructor.name, expected.name, message);8 }9 assert_true(caught, message);10};11const assert_throws_dom = (expected, func, message) => {12 let caught = false;13 try {14 func();15 } catch (e) {16 caught = true;17 assert_equals(e.name, expected, message);18 }19 assert_true(caught, message);20};21test(() => {22 assert_throws_js_impl(TypeError, () => {23 new EventTarget();24 }, "EventTarget constructor should throw TypeError");25}, "EventTarget constructor should throw TypeError");26test(() => {27 assert_throws_dom("TypeError", () => {28 new EventTarget();29 }, "EventTarget constructor should throw TypeError");30}, "EventTarget constructor should throw TypeError");31test(() => {32 assert_throws_js_impl(TypeError, () => {33 new EventTarget("test");34 }, "EventTarget constructor should throw TypeError");35}, "EventTarget constructor should throw TypeError");36test(() => {37 assert_throws_dom("TypeError", () => {38 new EventTarget("test");39 }, "EventTarget constructor should throw TypeError");40}, "EventTarget constructor should throw TypeError");41test(() => {42 assert_throws_js_impl(TypeError, () => {43 new EventTarget(null);44 }, "EventTarget constructor should throw TypeError");45}, "EventTarget constructor should throw TypeError");46test(() => {47 assert_throws_dom("TypeError", () => {48 new EventTarget(null);49 }, "EventTarget constructor should throw TypeError");50}, "EventTarget constructor should throw TypeError");51test(() => {52 assert_throws_js_impl(TypeError, () => {53 new EventTarget(undefined);54 }, "EventTarget constructor should throw TypeError");55}, "EventTarget constructor should throw TypeError");56test(() => {57 assert_throws_dom("TypeError", () => {58 new EventTarget(undefined);59 }, "EventTarget constructor should throw TypeError");60}, "EventTarget constructor should throw TypeError");61test(() => {62 assert_throws_js_impl(TypeError, () => {63 new EventTarget(1);64 }, "EventTarget constructor should throw TypeError");65}, "

Full Screen

Using AI Code Generation

copy

Full Screen

1test(function() {2 assert_throws_js_impl(3 function() {4 throw new Error("Error message");5 },6 );7}, "Test assert_throws_js_impl method");

Full Screen

Using AI Code Generation

copy

Full Screen

1test(()=> {2 assert_throws_js_impl('TypeError', () => {3 throw new TypeError();4 });5});6function assert_throws_js_impl(errorType, func) {7 let error;8 try {9 func();10 } catch (e) {11 error = e;12 }13 if (error) {14 assert_true(error instanceof errorType, 'Error thrown is not of type ' + errorType.name);15 } else {16 assert_unreached('No error was thrown');17 }18}

Full Screen

Using AI Code Generation

copy

Full Screen

1function test() {2 assert_throws_js_impl(3 () => {4 throw new Error("test");5 },6 );7}8function test() {9 assert_throws_dom_impl(10 () => {11 throw new DOMException("test", "test");12 },13 );14}15function test() {16 assert_throws_dom_impl(17 () => {18 throw new DOMException("test", "test");19 },20 );21}22function test() {23 assert_throws_dom_impl(24 () => {25 throw new DOMException("test", "test");26 },27 );28}29function test() {30 assert_throws_dom_impl(31 () => {32 throw new DOMException("test", "test");33 },34 );35}36function test() {37 assert_throws_dom_impl(38 () => {39 throw new DOMException("test", "test");40 },41 );42}43function test() {44 assert_throws_dom_impl(45 () => {46 throw new DOMException("test", "test");47 },48 );49}50function test() {51 assert_throws_dom_impl(52 () => {53 throw new DOMException("test", "test");54 },

Full Screen

Using AI Code Generation

copy

Full Screen

1assert_throws_js_impl("TypeError", Math.max, [], "This should throw a TypeError");2assert_throws_dom_impl("InvalidStateError", function() {3 throw new DOMException("This should throw a InvalidStateError", "InvalidStateError");4});5assert_throws_impl("Error", function() {6 throw new Error("This should throw a Error");7});8assert_throws_impl("Error", function() {9 throw new Error("This should throw a Error");10});11assert_throws_impl("Error", function() {12 throw new Error("This should throw a Error");13});14assert_throws_impl("Error", function() {15 throw new Error("This should throw a Error");16});17assert_throws_impl("Error", function() {18 throw new Error("This should throw a Error");19});20assert_throws_impl("Error", function() {21 throw new Error("This should throw a Error");22});23assert_throws_impl("Error", function() {24 throw new Error("This should throw a Error");25});

Full Screen

Using AI Code Generation

copy

Full Screen

1function test() {2 var x = 10;3 var y = 0;4 assert_throws_js_impl(function() {x/y;}, TypeError);5}6test();

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful