How to use assert_throws_dom_impl method in wpt

Best JavaScript code snippet using wpt

testharness.js

Source:testharness.js Github

copy

Full Screen

...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",1403 {func:func, e:e, type:typeof e});1404 assert(e !== null,1405 assertion_type, description,1406 "${func} threw null, not an object",1407 {func:func});1408 // Basic sanity-check on the passed-in constructor1409 assert(typeof constructor == "function",1410 assertion_type, description,1411 "${constructor} is not a constructor",1412 {constructor:constructor});1413 var obj = constructor;1414 while (obj) {1415 if (typeof obj === "function" &&1416 obj.name === "Error") {1417 break;1418 }1419 obj = Object.getPrototypeOf(obj);1420 }1421 assert(obj != null,1422 assertion_type, description,1423 "${constructor} is not an Error subtype",1424 {constructor:constructor});1425 // And checking that our exception is reasonable1426 assert(e.constructor === constructor &&1427 e.name === constructor.name,1428 assertion_type, description,1429 "${func} threw ${actual} (${actual_name}) expected instance of ${expected} (${expected_name})",1430 {func:func, actual:e, actual_name:e.name,1431 expected:constructor,1432 expected_name:constructor.name});1433 }1434 }1435 /**1436 * Assert a DOMException with the expected type is thrown.1437 *1438 * @param {number|string} type The expected exception name or code. See the1439 * table of names and codes at1440 * https://heycam.github.io/webidl/#dfn-error-names-table1441 * If a number is passed it should be one of the numeric code values1442 * in that table (e.g. 3, 4, etc). If a string is passed it can1443 * either be an exception name (e.g. "HierarchyRequestError",1444 * "WrongDocumentError") or the name of the corresponding error code1445 * (e.g. "HIERARCHY_REQUEST_ERR", "WRONG_DOCUMENT_ERR").1446 *1447 * For the remaining arguments, there are two ways of calling1448 * promise_rejects_dom:1449 *1450 * 1) If the DOMException is expected to come from the current global, the1451 * second argument should be the function expected to throw and a third,1452 * optional, argument is the assertion description.1453 *1454 * 2) If the DOMException is expected to come from some other global, the1455 * second argument should be the DOMException constructor from that global,1456 * the third argument the function expected to throw, and the fourth, optional,1457 * argument the assertion description.1458 */1459 function assert_throws_dom(type, funcOrConstructor, descriptionOrFunc, maybeDescription)1460 {1461 let constructor, func, description;1462 if (funcOrConstructor.name === "DOMException") {1463 constructor = funcOrConstructor;1464 func = descriptionOrFunc;1465 description = maybeDescription;1466 } else {1467 constructor = self.DOMException;1468 func = funcOrConstructor;1469 description = descriptionOrFunc;1470 assert(maybeDescription === undefined,1471 "Too many args pased to no-constructor version of assert_throws_dom");1472 }1473 assert_throws_dom_impl(type, func, description, "assert_throws_dom", constructor)1474 }1475 expose(assert_throws_dom, "assert_throws_dom");1476 /**1477 * Similar to assert_throws_dom but allows specifying the assertion type1478 * (assert_throws_dom or promise_rejects_dom, in practice). The1479 * "constructor" argument must be the DOMException constructor from the1480 * global we expect the exception to come from.1481 */1482 function assert_throws_dom_impl(type, func, description, assertion_type, constructor)1483 {1484 try {1485 func.call(this);1486 assert(false, assertion_type, description,1487 "${func} did not throw", {func:func});1488 } catch (e) {1489 if (e instanceof AssertionError) {1490 throw e;1491 }1492 // Basic sanity-checks on the thrown exception.1493 assert(typeof e === "object",1494 assertion_type, description,1495 "${func} threw ${e} with type ${type}, not an object",1496 {func:func, e:e, type:typeof e});...

Full Screen

Full Screen

aflprep_testharness.js

Source:aflprep_testharness.js Github

copy

Full Screen

...564 }565 return bring_promise_to_current_realm(promise)566 .then(test.unreached_func("Should have rejected: " + description))567 .catch(function(e) {568 assert_throws_dom_impl(type, function() { throw e }, description,569 "promise_rejects_dom", constructor);570 });571 }572 function promise_rejects_exactly(test, exception, promise, description) {573 return bring_promise_to_current_realm(promise)574 .then(test.unreached_func("Should have rejected: " + description))575 .catch(function(e) {576 assert_throws_exactly_impl(exception, function() { throw e },577 description, "promise_rejects_exactly");578 });579 }580 * This constructor helper allows DOM events to be handled using Promises,581 * which can make it a lot easier to test a very specific series of events,582 * including ensuring that unexpected events are not fired at any point.583 function EventWatcher(test, watchedNode, eventTypes, timeoutPromise)584 {585 if (typeof eventTypes == 'string') {586 eventTypes = [eventTypes];587 }588 var waitingFor = null;589 var recordedEvents = null;590 var eventHandler = test.step_func(function(evt) {591 assert_true(!!waitingFor,592 'Not expecting event, but got ' + evt.type + ' event');593 assert_equals(evt.type, waitingFor.types[0],594 'Expected ' + waitingFor.types[0] + ' event, but got ' +595 evt.type + ' event instead');596 if (Array.isArray(recordedEvents)) {597 recordedEvents.push(evt);598 }599 if (waitingFor.types.length > 1) {600 waitingFor.types.shift();601 return;602 }603 var resolveFunc = waitingFor.resolve;604 waitingFor = null;605 var result = recordedEvents || evt;606 recordedEvents = null;607 resolveFunc(result);608 });609 for (var i = 0; i < eventTypes.length; i++) {610 watchedNode.addEventListener(eventTypes[i], eventHandler, false);611 }612 * Returns a Promise that will resolve after the specified event or613 * series of events has occurred.614 *615 * @param options An optional options object. If the 'record' property616 * on this object has the value 'all', when the Promise617 * returned by this function is resolved, *all* Event618 * objects that were waited for will be returned as an619 * array.620 *621 * For example,622 *623 * ```js624 * const watcher = new EventWatcher(t, div, [ 'animationstart',625 * 'animationiteration',626 * 'animationend' ]);627 * return watcher.wait_for([ 'animationstart', 'animationend' ],628 * { record: 'all' }).then(evts => {629 * assert_equals(evts[0].elapsedTime, 0.0);630 * assert_equals(evts[1].elapsedTime, 2.0);631 * });632 * ```633 this.wait_for = function(types, options) {634 if (waitingFor) {635 return Promise.reject('Already waiting for an event or events');636 }637 if (typeof types == 'string') {638 types = [types];639 }640 if (options && options.record && options.record === 'all') {641 recordedEvents = [];642 }643 return new Promise(function(resolve, reject) {644 var timeout = test.step_func(function() {645 if (!waitingFor || waitingFor.resolve !== resolve)646 return;647 assert_true(waitingFor.types.length == 0,648 'Timed out waiting for ' + waitingFor.types.join(', '));649 var result = recordedEvents;650 recordedEvents = null;651 var resolveFunc = waitingFor.resolve;652 waitingFor = null;653 resolveFunc(result);654 });655 if (timeoutPromise) {656 timeoutPromise().then(timeout);657 }658 waitingFor = {659 types: types,660 resolve: resolve,661 reject: reject662 };663 });664 };665 function stop_watching() {666 for (var i = 0; i < eventTypes.length; i++) {667 watchedNode.removeEventListener(eventTypes[i], eventHandler, false);668 }669 };670 test._add_cleanup(stop_watching);671 return this;672 }673 expose(EventWatcher, 'EventWatcher');674 function setup(func_or_properties, maybe_properties)675 {676 var func = null;677 var properties = {};678 if (arguments.length === 2) {679 func = func_or_properties;680 properties = maybe_properties;681 } else if (func_or_properties instanceof Function) {682 func = func_or_properties;683 } else {684 properties = func_or_properties;685 }686 tests.setup(func, properties);687 test_environment.on_new_harness_properties(properties);688 }689 function promise_setup(func, maybe_properties)690 {691 if (typeof func !== "function") {692 tests.set_status(tests.status.ERROR,693 "promise_test invoked without a function");694 tests.complete();695 return;696 }697 tests.promise_setup_called = true;698 if (!tests.promise_tests) {699 tests.promise_tests = Promise.resolve();700 }701 tests.promise_tests = tests.promise_tests702 .then(function()703 {704 var properties = maybe_properties || {};705 var result;706 tests.setup(null, properties);707 result = func();708 test_environment.on_new_harness_properties(properties);709 if (!result || typeof result.then !== "function") {710 throw "Non-thenable returned by function passed to `promise_setup`";711 }712 return result;713 })714 .catch(function(e)715 {716 tests.set_status(tests.status.ERROR,717 String(e),718 e && e.stack);719 tests.complete();720 });721 }722 function done() {723 if (tests.tests.length === 0) {724 if (tests.status.status === null) {725 tests.status.status = tests.status.ERROR;726 tests.status.message = "done() was called without first defining any tests";727 }728 tests.complete();729 return;730 }731 if (tests.file_is_test) {732 tests.tests[0].done();733 }734 tests.end_wait();735 }736 function generate_tests(func, args, properties) {737 forEach(args, function(x, i)738 {739 var name = x[0];740 test(function()741 {742 func.apply(this, x.slice(1));743 },744 name,745 Array.isArray(properties) ? properties[i] : properties);746 });747 }748 * Register a function as a DOM event listener to the given object for the749 * event bubbling phase.750 *751 * This function was deprecated in November of 2019.752 function on_event(object, event, callback)753 {754 object.addEventListener(event, callback, false);755 }756 function step_timeout(f, t) {757 var outer_this = this;758 var args = Array.prototype.slice.call(arguments, 2);759 return setTimeout(function() {760 f.apply(outer_this, args);761 }, t * tests.timeout_multiplier);762 }763 expose(test, 'test');764 expose(async_test, 'async_test');765 expose(promise_test, 'promise_test');766 expose(promise_rejects_js, 'promise_rejects_js');767 expose(promise_rejects_dom, 'promise_rejects_dom');768 expose(promise_rejects_exactly, 'promise_rejects_exactly');769 expose(generate_tests, 'generate_tests');770 expose(setup, 'setup');771 expose(promise_setup, 'promise_setup');772 expose(done, 'done');773 expose(on_event, 'on_event');774 expose(step_timeout, 'step_timeout');775 * Return a string truncated to the given length, with ... added at the end776 * if it was longer.777 function truncate(s, len)778 {779 if (s.length > len) {780 return s.substring(0, len - 3) + "...";781 }782 return s;783 }784 * Return true if object is probably a Node object.785 function is_node(object)786 {787 try {788 var has_node_properties = ("nodeType" in object &&789 "nodeName" in object &&790 "nodeValue" in object &&791 "childNodes" in object);792 } catch (e) {793 return false;794 }795 if (has_node_properties) {796 try {797 object.nodeType;798 } catch (e) {799 return false;800 }801 return true;802 }803 return false;804 }805 var replacements = {806 "0": "0",807 "1": "x01",808 "2": "x02",809 "3": "x03",810 "4": "x04",811 "5": "x05",812 "6": "x06",813 "7": "x07",814 "8": "b",815 "9": "t",816 "10": "n",817 "11": "v",818 "12": "f",819 "13": "r",820 "14": "x0e",821 "15": "x0f",822 "16": "x10",823 "17": "x11",824 "18": "x12",825 "19": "x13",826 "20": "x14",827 "21": "x15",828 "22": "x16",829 "23": "x17",830 "24": "x18",831 "25": "x19",832 "26": "x1a",833 "27": "x1b",834 "28": "x1c",835 "29": "x1d",836 "30": "x1e",837 "31": "x1f",838 "0xfffd": "ufffd",839 "0xfffe": "ufffe",840 "0xffff": "uffff",841 };842 * Convert a value to a nice, human-readable string843 function format_value(val, seen)844 {845 if (!seen) {846 seen = [];847 }848 if (typeof val === "object" && val !== null) {849 if (seen.indexOf(val) >= 0) {850 return "[...]";851 }852 seen.push(val);853 }854 if (Array.isArray(val)) {855 let output = "[";856 if (val.beginEllipsis !== undefined) {857 output += "…, ";858 }859 output += val.map(function(x) {return format_value(x, seen);}).join(", ");860 if (val.endEllipsis !== undefined) {861 output += ", …";862 }863 return output + "]";864 }865 switch (typeof val) {866 case "string":867 for (var p in replacements) {868 var replace = "\\" + replacements[p];869 val = val.replace(RegExp(String.fromCharCode(p), "g"), replace);870 }871 case "boolean":872 case "undefined":873 return String(val);874 case "number":875 return "-0";876 }877 return String(val);878 case "object":879 if (val === null) {880 return "null";881 }882 if (is_node(val)) {883 switch (val.nodeType) {884 case Node.ELEMENT_NODE:885 var ret = "<" + val.localName;886 for (var i = 0; i < val.attributes.length; i++) {887 ret += " " + val.attributes[i].name + '="' + val.attributes[i].value + '"';888 }889 return "Element node " + truncate(ret, 60);890 case Node.TEXT_NODE:891 return 'Text node "' + truncate(val.data, 60) + '"';892 case Node.PROCESSING_INSTRUCTION_NODE:893 return "ProcessingInstruction node with target " + format_value(truncate(val.target, 60)) + " and data " + format_value(truncate(val.data, 60));894 case Node.COMMENT_NODE:895 return "Comment node <!--" + truncate(val.data, 60) + "-->";896 case Node.DOCUMENT_NODE:897 return "Document node with " + val.childNodes.length + (val.childNodes.length == 1 ? " child" : " children");898 case Node.DOCUMENT_TYPE_NODE:899 return "DocumentType node";900 case Node.DOCUMENT_FRAGMENT_NODE:901 return "DocumentFragment node with " + val.childNodes.length + (val.childNodes.length == 1 ? " child" : " children");902 default:903 return "Node object of unknown type";904 }905 }906 default:907 try {908 return typeof val + ' "' + truncate(String(val), 1000) + '"';909 } catch(e) {910 return ("[stringifying object threw " + String(e) +911 " with type " + String(typeof e) + "]");912 }913 }914 }915 expose(format_value, "format_value");916 * Assertions917 function expose_assert(f, name) {918 function assert_wrapper(...args) {919 let status = Test.statuses.TIMEOUT;920 let stack = null;921 try {922 if (settings.debug) {923 console.debug("ASSERT", name, tests.current_test && tests.current_test.name, args);924 }925 if (tests.output) {926 tests.set_assert(name, args);927 }928 const rv = f.apply(undefined, args);929 status = Test.statuses.PASS;930 return rv;931 } catch(e) {932 if (e instanceof AssertionError) {933 status = Test.statuses.FAIL;934 stack = e.stack;935 } else {936 status = Test.statuses.ERROR;937 }938 throw e;939 } finally {940 if (tests.output && !stack) {941 stack = get_stack();942 }943 if (tests.output) {944 tests.set_assert_status(status, stack);945 }946 }947 }948 expose(assert_wrapper, name);949 }950 function assert_true(actual, description)951 {952 assert(actual === true, "assert_true", description,953 "expected true got ${actual}", {actual:actual});954 }955 expose_assert(assert_true, "assert_true");956 function assert_false(actual, description)957 {958 assert(actual === false, "assert_false", description,959 "expected false got ${actual}", {actual:actual});960 }961 expose_assert(assert_false, "assert_false");962 function same_value(x, y) {963 if (y !== y) {964 return x !== x;965 }966 if (x === 0 && y === 0) {967 }968 return x === y;969 }970 function assert_equals(actual, expected, description)971 {972 * Test if two primitives are equal or two objects973 * are the same object974 if (typeof actual != typeof expected) {975 assert(false, "assert_equals", description,976 "expected (" + typeof expected + ") ${expected} but got (" + typeof actual + ") ${actual}",977 {expected:expected, actual:actual});978 return;979 }980 assert(same_value(actual, expected), "assert_equals", description,981 "expected ${expected} but got ${actual}",982 {expected:expected, actual:actual});983 }984 expose_assert(assert_equals, "assert_equals");985 function assert_not_equals(actual, expected, description)986 {987 * Test if two primitives are unequal or two objects988 * are different objects989 assert(!same_value(actual, expected), "assert_not_equals", description,990 "got disallowed value ${actual}",991 {actual:actual});992 }993 expose_assert(assert_not_equals, "assert_not_equals");994 function assert_in_array(actual, expected, description)995 {996 assert(expected.indexOf(actual) != -1, "assert_in_array", description,997 "value ${actual} not in array ${expected}",998 {actual:actual, expected:expected});999 }1000 expose_assert(assert_in_array, "assert_in_array");1001 function assert_object_equals(actual, expected, description)1002 {1003 assert(typeof actual === "object" && actual !== null, "assert_object_equals", description,1004 "value is ${actual}, expected object",1005 {actual: actual});1006 function check_equal(actual, expected, stack)1007 {1008 stack.push(actual);1009 var p;1010 for (p in actual) {1011 assert(expected.hasOwnProperty(p), "assert_object_equals", description,1012 "unexpected property ${p}", {p:p});1013 if (typeof actual[p] === "object" && actual[p] !== null) {1014 if (stack.indexOf(actual[p]) === -1) {1015 check_equal(actual[p], expected[p], stack);1016 }1017 } else {1018 assert(same_value(actual[p], expected[p]), "assert_object_equals", description,1019 "property ${p} expected ${expected} got ${actual}",1020 {p:p, expected:expected[p], actual:actual[p]});1021 }1022 }1023 for (p in expected) {1024 assert(actual.hasOwnProperty(p),1025 "assert_object_equals", description,1026 "expected property ${p} missing", {p:p});1027 }1028 stack.pop();1029 }1030 check_equal(actual, expected, []);1031 }1032 expose_assert(assert_object_equals, "assert_object_equals");1033 function assert_array_equals(actual, expected, description)1034 {1035 const max_array_length = 20;1036 function shorten_array(arr, offset = 0) {1037 if (arr.length < max_array_length + 2) {1038 return arr;1039 }1040 let upper_bound = Math.min(length_after_offset + offset, arr.length);1041 const lower_bound = Math.max(upper_bound - max_array_length, 0);1042 if (lower_bound === 0) {1043 upper_bound = max_array_length;1044 }1045 const output = arr.slice(lower_bound, upper_bound);1046 if (lower_bound > 0) {1047 output.beginEllipsis = true;1048 }1049 if (upper_bound < arr.length) {1050 output.endEllipsis = true;1051 }1052 return output;1053 }1054 assert(typeof actual === "object" && actual !== null && "length" in actual,1055 "assert_array_equals", description,1056 "value is ${actual}, expected array",1057 {actual:actual});1058 assert(actual.length === expected.length,1059 "assert_array_equals", description,1060 "lengths differ, expected array ${expected} length ${expectedLength}, got ${actual} length ${actualLength}",1061 {expected:shorten_array(expected, expected.length - 1), expectedLength:expected.length,1062 actual:shorten_array(actual, actual.length - 1), actualLength:actual.length1063 });1064 for (var i = 0; i < actual.length; i++) {1065 assert(actual.hasOwnProperty(i) === expected.hasOwnProperty(i),1066 "assert_array_equals", description,1067 "expected property ${i} to be ${expected} but was ${actual} (expected array ${arrayExpected} got ${arrayActual})",1068 {i:i, expected:expected.hasOwnProperty(i) ? "present" : "missing",1069 actual:actual.hasOwnProperty(i) ? "present" : "missing",1070 arrayExpected:shorten_array(expected, i), arrayActual:shorten_array(actual, i)});1071 assert(same_value(expected[i], actual[i]),1072 "assert_array_equals", description,1073 "expected property ${i} to be ${expected} but got ${actual} (expected array ${arrayExpected} got ${arrayActual})",1074 {i:i, expected:expected[i], actual:actual[i],1075 arrayExpected:shorten_array(expected, i), arrayActual:shorten_array(actual, i)});1076 }1077 }1078 expose_assert(assert_array_equals, "assert_array_equals");1079 function assert_array_approx_equals(actual, expected, epsilon, description)1080 {1081 assert(actual.length === expected.length,1082 "assert_array_approx_equals", description,1083 "lengths differ, expected ${expected} got ${actual}",1084 {expected:expected.length, actual:actual.length});1085 for (var i = 0; i < actual.length; i++) {1086 assert(actual.hasOwnProperty(i) === expected.hasOwnProperty(i),1087 "assert_array_approx_equals", description,1088 "property ${i}, property expected to be ${expected} but was ${actual}",1089 {i:i, expected:expected.hasOwnProperty(i) ? "present" : "missing",1090 actual:actual.hasOwnProperty(i) ? "present" : "missing"});1091 assert(typeof actual[i] === "number",1092 "assert_array_approx_equals", description,1093 "property ${i}, expected a number but got a ${type_actual}",1094 {i:i, type_actual:typeof actual[i]});1095 assert(Math.abs(actual[i] - expected[i]) <= epsilon,1096 "assert_array_approx_equals", description,1097 {i:i, expected:expected[i], actual:actual[i], epsilon:epsilon});1098 }1099 }1100 expose_assert(assert_array_approx_equals, "assert_array_approx_equals");1101 function assert_approx_equals(actual, expected, epsilon, description)1102 {1103 assert(typeof actual === "number",1104 "assert_approx_equals", description,1105 "expected a number but got a ${type_actual}",1106 {type_actual:typeof actual});1107 if (isFinite(actual) || isFinite(expected)) {1108 assert(Math.abs(actual - expected) <= epsilon,1109 "assert_approx_equals", description,1110 {expected:expected, actual:actual, epsilon:epsilon});1111 } else {1112 assert_equals(actual, expected);1113 }1114 }1115 expose_assert(assert_approx_equals, "assert_approx_equals");1116 function assert_less_than(actual, expected, description)1117 {1118 * Test if a primitive number is less than another1119 assert(typeof actual === "number",1120 "assert_less_than", description,1121 "expected a number but got a ${type_actual}",1122 {type_actual:typeof actual});1123 assert(actual < expected,1124 "assert_less_than", description,1125 "expected a number less than ${expected} but got ${actual}",1126 {expected:expected, actual:actual});1127 }1128 expose_assert(assert_less_than, "assert_less_than");1129 function assert_greater_than(actual, expected, description)1130 {1131 * Test if a primitive number is greater than another1132 assert(typeof actual === "number",1133 "assert_greater_than", description,1134 "expected a number but got a ${type_actual}",1135 {type_actual:typeof actual});1136 assert(actual > expected,1137 "assert_greater_than", description,1138 "expected a number greater than ${expected} but got ${actual}",1139 {expected:expected, actual:actual});1140 }1141 expose_assert(assert_greater_than, "assert_greater_than");1142 function assert_between_exclusive(actual, lower, upper, description)1143 {1144 * Test if a primitive number is between two others1145 assert(typeof actual === "number",1146 "assert_between_exclusive", description,1147 "expected a number but got a ${type_actual}",1148 {type_actual:typeof actual});1149 assert(actual > lower && actual < upper,1150 "assert_between_exclusive", description,1151 "expected a number greater than ${lower} " +1152 "and less than ${upper} but got ${actual}",1153 {lower:lower, upper:upper, actual:actual});1154 }1155 expose_assert(assert_between_exclusive, "assert_between_exclusive");1156 function assert_less_than_equal(actual, expected, description)1157 {1158 * Test if a primitive number is less than or equal to another1159 assert(typeof actual === "number",1160 "assert_less_than_equal", description,1161 "expected a number but got a ${type_actual}",1162 {type_actual:typeof actual});1163 assert(actual <= expected,1164 "assert_less_than_equal", description,1165 "expected a number less than or equal to ${expected} but got ${actual}",1166 {expected:expected, actual:actual});1167 }1168 expose_assert(assert_less_than_equal, "assert_less_than_equal");1169 function assert_greater_than_equal(actual, expected, description)1170 {1171 * Test if a primitive number is greater than or equal to another1172 assert(typeof actual === "number",1173 "assert_greater_than_equal", description,1174 "expected a number but got a ${type_actual}",1175 {type_actual:typeof actual});1176 assert(actual >= expected,1177 "assert_greater_than_equal", description,1178 "expected a number greater than or equal to ${expected} but got ${actual}",1179 {expected:expected, actual:actual});1180 }1181 expose_assert(assert_greater_than_equal, "assert_greater_than_equal");1182 function assert_between_inclusive(actual, lower, upper, description)1183 {1184 * Test if a primitive number is between to two others or equal to either of them1185 assert(typeof actual === "number",1186 "assert_between_inclusive", description,1187 "expected a number but got a ${type_actual}",1188 {type_actual:typeof actual});1189 assert(actual >= lower && actual <= upper,1190 "assert_between_inclusive", description,1191 "expected a number greater than or equal to ${lower} " +1192 "and less than or equal to ${upper} but got ${actual}",1193 {lower:lower, upper:upper, actual:actual});1194 }1195 expose_assert(assert_between_inclusive, "assert_between_inclusive");1196 function assert_regexp_match(actual, expected, description) {1197 * Test if a string (actual) matches a regexp (expected)1198 assert(expected.test(actual),1199 "assert_regexp_match", description,1200 "expected ${expected} but got ${actual}",1201 {expected:expected, actual:actual});1202 }1203 expose_assert(assert_regexp_match, "assert_regexp_match");1204 function assert_class_string(object, class_string, description) {1205 var actual = {}.toString.call(object);1206 var expected = "[object " + class_string + "]";1207 assert(same_value(actual, expected), "assert_class_string", description,1208 "expected ${expected} but got ${actual}",1209 {expected:expected, actual:actual});1210 }1211 expose_assert(assert_class_string, "assert_class_string");1212 function assert_own_property(object, property_name, description) {1213 assert(object.hasOwnProperty(property_name),1214 "assert_own_property", description,1215 "expected property ${p} missing", {p:property_name});1216 }1217 expose_assert(assert_own_property, "assert_own_property");1218 function assert_not_own_property(object, property_name, description) {1219 assert(!object.hasOwnProperty(property_name),1220 "assert_not_own_property", description,1221 "unexpected property ${p} is found on object", {p:property_name});1222 }1223 expose_assert(assert_not_own_property, "assert_not_own_property");1224 function _assert_inherits(name) {1225 return function (object, property_name, description)1226 {1227 assert((typeof object === "object" && object !== null) ||1228 typeof object === "function" ||1229 String(object) === "[object HTMLAllCollection]",1230 name, description,1231 "provided value is not an object");1232 assert("hasOwnProperty" in object,1233 name, description,1234 "provided value is an object but has no hasOwnProperty method");1235 assert(!object.hasOwnProperty(property_name),1236 name, description,1237 "property ${p} found on object expected in prototype chain",1238 {p:property_name});1239 assert(property_name in object,1240 name, description,1241 "property ${p} not found in prototype chain",1242 {p:property_name});1243 };1244 }1245 expose_assert(_assert_inherits("assert_inherits"), "assert_inherits");1246 expose_assert(_assert_inherits("assert_idl_attribute"), "assert_idl_attribute");1247 function assert_readonly(object, property_name, description)1248 {1249 var initial_value = object[property_name];1250 try {1251 assert(same_value(object[property_name], initial_value),1252 "assert_readonly", description,1253 "changing property ${p} succeeded",1254 {p:property_name});1255 } finally {1256 object[property_name] = initial_value;1257 }1258 }1259 expose_assert(assert_readonly, "assert_readonly");1260 * Assert a JS Error with the expected constructor is thrown.1261 *1262 * @param {object} constructor The expected exception constructor.1263 * @param {Function} func Function which should throw.1264 * @param {string} description Error description for the case that the error is not thrown.1265 function assert_throws_js(constructor, func, description)1266 {1267 assert_throws_js_impl(constructor, func, description,1268 "assert_throws_js");1269 }1270 expose_assert(assert_throws_js, "assert_throws_js");1271 * Like assert_throws_js but allows specifying the assertion type1272 * (assert_throws_js or promise_rejects_js, in practice).1273 function assert_throws_js_impl(constructor, func, description,1274 assertion_type)1275 {1276 try {1277 func.call(this);1278 assert(false, assertion_type, description,1279 "${func} did not throw", {func:func});1280 } catch (e) {1281 if (e instanceof AssertionError) {1282 throw e;1283 }1284 assert(typeof e === "object",1285 assertion_type, description,1286 "${func} threw ${e} with type ${type}, not an object",1287 {func:func, e:e, type:typeof e});1288 assert(e !== null,1289 assertion_type, description,1290 "${func} threw null, not an object",1291 {func:func});1292 assert(typeof constructor == "function",1293 assertion_type, description,1294 "${constructor} is not a constructor",1295 {constructor:constructor});1296 var obj = constructor;1297 while (obj) {1298 if (typeof obj === "function" &&1299 obj.name === "Error") {1300 break;1301 }1302 obj = Object.getPrototypeOf(obj);1303 }1304 assert(obj != null,1305 assertion_type, description,1306 "${constructor} is not an Error subtype",1307 {constructor:constructor});1308 assert(e.constructor === constructor &&1309 e.name === constructor.name,1310 assertion_type, description,1311 "${func} threw ${actual} (${actual_name}) expected instance of ${expected} (${expected_name})",1312 {func:func, actual:e, actual_name:e.name,1313 expected:constructor,1314 expected_name:constructor.name});1315 }1316 }1317 * Assert a DOMException with the expected type is thrown.1318 *1319 * @param {number|string} type The expected exception name or code. See the1320 * table of names and codes at1321 * If a number is passed it should be one of the numeric code values1322 * in that table (e.g. 3, 4, etc). If a string is passed it can1323 * either be an exception name (e.g. "HierarchyRequestError",1324 * "WrongDocumentError") or the name of the corresponding error code1325 * (e.g. "HIERARCHY_REQUEST_ERR", "WRONG_DOCUMENT_ERR").1326 *1327 * For the remaining arguments, there are two ways of calling1328 * promise_rejects_dom:1329 *1330 * 1) If the DOMException is expected to come from the current global, the1331 * second argument should be the function expected to throw and a third,1332 * optional, argument is the assertion description.1333 *1334 * 2) If the DOMException is expected to come from some other global, the1335 * second argument should be the DOMException constructor from that global,1336 * the third argument the function expected to throw, and the fourth, optional,1337 * argument the assertion description.1338 function assert_throws_dom(type, funcOrConstructor, descriptionOrFunc, maybeDescription)1339 {1340 let constructor, func, description;1341 if (funcOrConstructor.name === "DOMException") {1342 constructor = funcOrConstructor;1343 func = descriptionOrFunc;1344 description = maybeDescription;1345 } else {1346 constructor = self.DOMException;1347 func = funcOrConstructor;1348 description = descriptionOrFunc;1349 assert(maybeDescription === undefined,1350 "Too many args pased to no-constructor version of assert_throws_dom");1351 }1352 assert_throws_dom_impl(type, func, description, "assert_throws_dom", constructor)1353 }1354 expose_assert(assert_throws_dom, "assert_throws_dom");1355 * Similar to assert_throws_dom but allows specifying the assertion type1356 * (assert_throws_dom or promise_rejects_dom, in practice). The1357 * "constructor" argument must be the DOMException constructor from the1358 * global we expect the exception to come from.1359 function assert_throws_dom_impl(type, func, description, assertion_type, constructor)1360 {1361 try {1362 func.call(this);1363 assert(false, assertion_type, description,1364 "${func} did not throw", {func:func});1365 } catch (e) {1366 if (e instanceof AssertionError) {1367 throw e;1368 }1369 assert(typeof e === "object",1370 assertion_type, description,1371 "${func} threw ${e} with type ${type}, not an object",1372 {func:func, e:e, type:typeof e});1373 assert(e !== null,...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { assert_throws_dom_impl } from './assert_throws_dom_impl.js';2import { assert_throws_dom } from './assert_throws_dom.js';3import { assert_throws_js } from './assert_throws_js.js';4import { assert_throws_js_impl } from './assert_throws_js_impl.js';5import { assert_throws } from './assert_throws.js';6import { assert_throws_impl } from './assert_throws_impl.js';7import { assert_unreached } from './assert_unreached.js';8import { assert_unreached_impl } from './assert_unreached_impl.js';9import { assert_true } from './assert_true.js';10import { assert_true_impl } from './assert_true_impl.js';11import { assert_equals } from './assert_equals.js';12import { assert_equals_impl } from './assert_equals_impl.js';13import { assert_array_equals } from './assert_array_equals.js';14import { assert_array_equals_impl } from './assert_array_equals_impl.js';15import { assert_approx_equals } from './assert_approx_equals.js';16import { assert_approx_equals_impl } from './assert_approx_equals_impl.js';17import { assert_in_array } from './assert_in_array.js';18import { assert_in_array_impl } from './assert_in_array_impl.js';19import { assert_not_equals } from './assert_not_equals.js';

Full Screen

Using AI Code Generation

copy

Full Screen

1test(function() {2 assert_throws_dom_impl("InvalidStateError", function() {3 });4});5test(function() {6 assert_throws_dom_impl("InvalidStateError", function() {7 });8}, "Test description");9test(function() {10 assert_throws_dom_impl("InvalidStateError", function() {11 });12}, "Test description", { allow_uncaught_exception: true });13test(function() {14 assert_throws_dom_impl("InvalidStateError", function() {15 });16}, "Test description", { allow_uncaught_exception: true });

Full Screen

Using AI Code Generation

copy

Full Screen

1var assert_throws_dom_impl = function (code, func, msg) {2 var error = null;3 try {4 func();5 } catch (e) {6 error = e;7 }8 assert_equals(error.name, code, msg);9};10var assert_throws_dom = function (code, func, msg) {11 assert_throws_dom_impl(code, func, msg);12};13var assert_throws_dom = function (code, func, msg) {14 assert_throws_dom_impl(code, func, msg);15};16var assert_throws_dom = function (code, func, msg) {17 assert_throws_dom_impl(code, func, msg);18};19var assert_throws_dom = function (code, func, msg) {20 assert_throws_dom_impl(code, func, msg);21};22var assert_throws_dom = function (code, func, msg) {23 assert_throws_dom_impl(code, func, msg);24};25var assert_throws_dom = function (code, func, msg) {26 assert_throws_dom_impl(code, func, msg);27};28var assert_throws_dom = function (code, func, msg) {29 assert_throws_dom_impl(code, func, msg);30};31var assert_throws_dom = function (code, func, msg) {32 assert_throws_dom_impl(code, func, msg);33};34var assert_throws_dom = function (code, func, msg) {35 assert_throws_dom_impl(code, func, msg);36};37var assert_throws_dom = function (code, func, msg) {38 assert_throws_dom_impl(code, func, msg);39};40var assert_throws_dom = function (code, func, msg) {41 assert_throws_dom_impl(code

Full Screen

Using AI Code Generation

copy

Full Screen

1test(t => {2 assert_throws_dom_impl('InvalidStateError', () => {3 throw new DOMException('InvalidStateError');4 });5}, 'test assert_throws_dom_impl');6assert_throws_dom_impl = function(expected, func, description) {7 var exception;8 var result;9 try {10 result = func();11 } catch(e) {12 exception = e;13 }14 assert_true(exception instanceof DOMException, 'exception is a DOMException');15 assert_equals(exception.name, expected, 'exception name');16 assert_equals(exception.code, DOMException[expected], 'exception code');17}

Full Screen

Using AI Code Generation

copy

Full Screen

1var test = async_test("test");2test.step(function() {3 assert_throws_dom_impl("TypeError", function() {4 var x = document.createElement("div");5 x.appendChild(document.createTextNode("Hello"));6 x.appendChild(document.createTextNode("World"));7 }, "Passing two text nodes to appendChild should throw TypeError");8 test.done();9});10var test = async_test("test");11test.step(function() {12 assert_throws_dom("HierarchyRequestError", function() {13 var x = document.createElement("div");14 x.appendChild(document.createTextNode("Hello"));15 x.appendChild(document.createTextNode("World"));16 }, "Passing two text nodes to appendChild should throw HierarchyRequestError");17 test.done();18});19var test = async_test("test");20test.step(function() {21 assert_throws_dom("HierarchyRequestError", function() {22 var x = document.createElement("div");23 x.appendChild(document.createTextNode("Hello"));24 x.appendChild(document.createTextNode("World"));25 }, "Passing two text nodes to appendChild should throw HierarchyRequestError");26 test.done();27});28var test = async_test("test");29test.step(function() {30 assert_throws_dom("HierarchyRequestError", function() {31 var x = document.createElement("div");32 x.appendChild(document.createTextNode("Hello"));33 x.appendChild(document.createTextNode("World"));34 }, "Passing two text nodes to appendChild should throw HierarchyRequestError");35 test.done();36});37var test = async_test("test");38test.step(function() {39 assert_throws_dom("HierarchyRequestError", function() {40 var x = document.createElement("div");41 x.appendChild(document.createTextNode("Hello"));42 x.appendChild(document.createTextNode("World"));43 }, "Passing two text nodes to appendChild should throw HierarchyRequestError");44 test.done();45});46var test = async_test("test");47test.step(function() {48 assert_throws_dom("HierarchyRequestError", function() {49 var x = document.createElement("div");

Full Screen

Using AI Code Generation

copy

Full Screen

1import { assert_throws_dom_impl } from '/resources/testharness.js';2let assert_throws_dom = assert_throws_dom_impl;3let test = async () => {4 try {5 assert_throws_dom("TypeError", () => {6 });7 } catch (e) {8 console.log(e);9 }10}11test();12import { assert_throws_dom_impl } from '/resources/testharness.js';13let assert_throws_dom = assert_throws_dom_impl;14let test = async () => {15 try {16 assert_throws_dom("TypeError", () => {17 });18 } catch (e) {19 console.log(e);20 }21}22test();

Full Screen

Using AI Code Generation

copy

Full Screen

1test(function() {2 assert_throws_dom_impl("SyntaxError", function() {3 document.getElementById("test").setRangeText("foo", 0, 0, "invalid");4 });5}, "setRangeText with invalid enum value");6test(function() {7 assert_throws_dom_impl("SyntaxError", function() {8 document.getElementById("test").setRangeText("foo", 0, 0, "end");9 });10}, "setRangeText with invalid enum value");11test(function() {12 assert_throws_dom_impl("SyntaxError", function() {13 document.getElementById("test").setRangeText("foo", 0, 0, "start");14 });15}, "setRangeText with invalid enum value");16test(function() {17 assert_throws_dom_impl("SyntaxError", function() {18 document.getElementById("test").setRangeText("foo", 0, 0, "preserve");19 });20}, "setRangeText with invalid enum value");21test(function() {22 assert_throws_dom_impl("SyntaxError", function() {23 document.getElementById("test").setRangeText("foo", 0, 0, "select");24 });25}, "setRangeText with invalid enum value");26test(function() {27 assert_throws_dom_impl("SyntaxError", function() {28 document.getElementById("test").setRangeText("foo", 0, 0, "selectstart");29 });30}, "setRangeText with invalid enum value");31test(function() {32 assert_throws_dom_impl("SyntaxError", function() {33 document.getElementById("test").setRangeText("foo", 0, 0, "selectend");34 });35}, "setRangeText with invalid enum value");

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