Best JavaScript code snippet using wpt
testharness.js
Source:testharness.js  
...1058    expose(format_value, "format_value");1059    /*1060     * Assertions1061     */1062    function expose_assert(f, name) {1063        function assert_wrapper(...args) {1064            let status = Test.statuses.TIMEOUT;1065            let stack = null;1066            try {1067                if (settings.debug) {1068                    console.debug("ASSERT", name, tests.current_test && tests.current_test.name, args);1069                }1070                if (tests.output) {1071                    tests.set_assert(name, args);1072                }1073                const rv = f.apply(undefined, args);1074                status = Test.statuses.PASS;1075                return rv;1076            } catch(e) {1077                if (e instanceof AssertionError) {1078                    status = Test.statuses.FAIL;1079                    stack = e.stack;1080                 } else {1081                    status = Test.statuses.ERROR;1082                 }1083                throw e;1084            } finally {1085                if (tests.output && !stack) {1086                    stack = get_stack();1087                }1088                if (tests.output) {1089                    tests.set_assert_status(status, stack);1090                }1091            }1092        }1093        expose(assert_wrapper, name);1094    }1095    function assert_true(actual, description)1096    {1097        assert(actual === true, "assert_true", description,1098                                "expected true got ${actual}", {actual:actual});1099    }1100    expose_assert(assert_true, "assert_true");1101    function assert_false(actual, description)1102    {1103        assert(actual === false, "assert_false", description,1104                                 "expected false got ${actual}", {actual:actual});1105    }1106    expose_assert(assert_false, "assert_false");1107    function same_value(x, y) {1108        if (y !== y) {1109            //NaN case1110            return x !== x;1111        }1112        if (x === 0 && y === 0) {1113            //Distinguish +0 and -01114            return 1/x === 1/y;1115        }1116        return x === y;1117    }1118    function assert_equals(actual, expected, description)1119    {1120         /*1121          * Test if two primitives are equal or two objects1122          * are the same object1123          */1124        if (typeof actual != typeof expected) {1125            assert(false, "assert_equals", description,1126                          "expected (" + typeof expected + ") ${expected} but got (" + typeof actual + ") ${actual}",1127                          {expected:expected, actual:actual});1128            return;1129        }1130        assert(same_value(actual, expected), "assert_equals", description,1131                                             "expected ${expected} but got ${actual}",1132                                             {expected:expected, actual:actual});1133    }1134    expose_assert(assert_equals, "assert_equals");1135    function assert_not_equals(actual, expected, description)1136    {1137         /*1138          * Test if two primitives are unequal or two objects1139          * are different objects1140          */1141        assert(!same_value(actual, expected), "assert_not_equals", description,1142                                              "got disallowed value ${actual}",1143                                              {actual:actual});1144    }1145    expose_assert(assert_not_equals, "assert_not_equals");1146    function assert_in_array(actual, expected, description)1147    {1148        assert(expected.indexOf(actual) != -1, "assert_in_array", description,1149                                               "value ${actual} not in array ${expected}",1150                                               {actual:actual, expected:expected});1151    }1152    expose_assert(assert_in_array, "assert_in_array");1153    // This function was deprecated in July of 2015.1154    // See https://github.com/web-platform-tests/wpt/issues/20331155    function assert_object_equals(actual, expected, description)1156    {1157         assert(typeof actual === "object" && actual !== null, "assert_object_equals", description,1158                                                               "value is ${actual}, expected object",1159                                                               {actual: actual});1160         //This needs to be improved a great deal1161         function check_equal(actual, expected, stack)1162         {1163             stack.push(actual);1164             var p;1165             for (p in actual) {1166                 assert(expected.hasOwnProperty(p), "assert_object_equals", description,1167                                                    "unexpected property ${p}", {p:p});1168                 if (typeof actual[p] === "object" && actual[p] !== null) {1169                     if (stack.indexOf(actual[p]) === -1) {1170                         check_equal(actual[p], expected[p], stack);1171                     }1172                 } else {1173                     assert(same_value(actual[p], expected[p]), "assert_object_equals", description,1174                                                       "property ${p} expected ${expected} got ${actual}",1175                                                       {p:p, expected:expected[p], actual:actual[p]});1176                 }1177             }1178             for (p in expected) {1179                 assert(actual.hasOwnProperty(p),1180                        "assert_object_equals", description,1181                        "expected property ${p} missing", {p:p});1182             }1183             stack.pop();1184         }1185         check_equal(actual, expected, []);1186    }1187    expose_assert(assert_object_equals, "assert_object_equals");1188    function assert_array_equals(actual, expected, description)1189    {1190        const max_array_length = 20;1191        function shorten_array(arr, offset = 0) {1192            // Make ", â¦" only show up when it would likely reduce the length, not accounting for1193            // fonts.1194            if (arr.length < max_array_length + 2) {1195                return arr;1196            }1197            // By default we want half the elements after the offset and half before1198            // But if that takes us past the end of the array, we have more before, and1199            // if it takes us before the start we have more after.1200            const length_after_offset = Math.floor(max_array_length / 2);1201            let upper_bound = Math.min(length_after_offset + offset, arr.length);1202            const lower_bound = Math.max(upper_bound - max_array_length, 0);1203            if (lower_bound === 0) {1204                upper_bound = max_array_length;1205            }1206            const output = arr.slice(lower_bound, upper_bound);1207            if (lower_bound > 0) {1208                output.beginEllipsis = true;1209            }1210            if (upper_bound < arr.length) {1211                output.endEllipsis = true;1212            }1213            return output;1214        }1215        assert(typeof actual === "object" && actual !== null && "length" in actual,1216               "assert_array_equals", description,1217               "value is ${actual}, expected array",1218               {actual:actual});1219        assert(actual.length === expected.length,1220               "assert_array_equals", description,1221               "lengths differ, expected array ${expected} length ${expectedLength}, got ${actual} length ${actualLength}",1222               {expected:shorten_array(expected, expected.length - 1), expectedLength:expected.length,1223                actual:shorten_array(actual, actual.length - 1), actualLength:actual.length1224               });1225        for (var i = 0; i < actual.length; i++) {1226            assert(actual.hasOwnProperty(i) === expected.hasOwnProperty(i),1227                   "assert_array_equals", description,1228                   "expected property ${i} to be ${expected} but was ${actual} (expected array ${arrayExpected} got ${arrayActual})",1229                   {i:i, expected:expected.hasOwnProperty(i) ? "present" : "missing",1230                    actual:actual.hasOwnProperty(i) ? "present" : "missing",1231                    arrayExpected:shorten_array(expected, i), arrayActual:shorten_array(actual, i)});1232            assert(same_value(expected[i], actual[i]),1233                   "assert_array_equals", description,1234                   "expected property ${i} to be ${expected} but got ${actual} (expected array ${arrayExpected} got ${arrayActual})",1235                   {i:i, expected:expected[i], actual:actual[i],1236                    arrayExpected:shorten_array(expected, i), arrayActual:shorten_array(actual, i)});1237        }1238    }1239    expose_assert(assert_array_equals, "assert_array_equals");1240    function assert_array_approx_equals(actual, expected, epsilon, description)1241    {1242        /*1243         * Test if two primitive arrays are equal within +/- epsilon1244         */1245        assert(actual.length === expected.length,1246               "assert_array_approx_equals", description,1247               "lengths differ, expected ${expected} got ${actual}",1248               {expected:expected.length, actual:actual.length});1249        for (var i = 0; i < actual.length; i++) {1250            assert(actual.hasOwnProperty(i) === expected.hasOwnProperty(i),1251                   "assert_array_approx_equals", description,1252                   "property ${i}, property expected to be ${expected} but was ${actual}",1253                   {i:i, expected:expected.hasOwnProperty(i) ? "present" : "missing",1254                    actual:actual.hasOwnProperty(i) ? "present" : "missing"});1255            assert(typeof actual[i] === "number",1256                   "assert_array_approx_equals", description,1257                   "property ${i}, expected a number but got a ${type_actual}",1258                   {i:i, type_actual:typeof actual[i]});1259            assert(Math.abs(actual[i] - expected[i]) <= epsilon,1260                   "assert_array_approx_equals", description,1261                   "property ${i}, expected ${expected} +/- ${epsilon}, expected ${expected} but got ${actual}",1262                   {i:i, expected:expected[i], actual:actual[i], epsilon:epsilon});1263        }1264    }1265    expose_assert(assert_array_approx_equals, "assert_array_approx_equals");1266    function assert_approx_equals(actual, expected, epsilon, description)1267    {1268        /*1269         * Test if two primitive numbers are equal within +/- epsilon1270         */1271        assert(typeof actual === "number",1272               "assert_approx_equals", description,1273               "expected a number but got a ${type_actual}",1274               {type_actual:typeof actual});1275        // The epsilon math below does not place nice with NaN and Infinity1276        // But in this case Infinity = Infinity and NaN = NaN1277        if (isFinite(actual) || isFinite(expected)) {1278            assert(Math.abs(actual - expected) <= epsilon,1279                   "assert_approx_equals", description,1280                   "expected ${expected} +/- ${epsilon} but got ${actual}",1281                   {expected:expected, actual:actual, epsilon:epsilon});1282        } else {1283            assert_equals(actual, expected);1284        }1285    }1286    expose_assert(assert_approx_equals, "assert_approx_equals");1287    function assert_less_than(actual, expected, description)1288    {1289        /*1290         * Test if a primitive number is less than another1291         */1292        assert(typeof actual === "number",1293               "assert_less_than", description,1294               "expected a number but got a ${type_actual}",1295               {type_actual:typeof actual});1296        assert(actual < expected,1297               "assert_less_than", description,1298               "expected a number less than ${expected} but got ${actual}",1299               {expected:expected, actual:actual});1300    }1301    expose_assert(assert_less_than, "assert_less_than");1302    function assert_greater_than(actual, expected, description)1303    {1304        /*1305         * Test if a primitive number is greater than another1306         */1307        assert(typeof actual === "number",1308               "assert_greater_than", description,1309               "expected a number but got a ${type_actual}",1310               {type_actual:typeof actual});1311        assert(actual > expected,1312               "assert_greater_than", description,1313               "expected a number greater than ${expected} but got ${actual}",1314               {expected:expected, actual:actual});1315    }1316    expose_assert(assert_greater_than, "assert_greater_than");1317    function assert_between_exclusive(actual, lower, upper, description)1318    {1319        /*1320         * Test if a primitive number is between two others1321         */1322        assert(typeof actual === "number",1323               "assert_between_exclusive", description,1324               "expected a number but got a ${type_actual}",1325               {type_actual:typeof actual});1326        assert(actual > lower && actual < upper,1327               "assert_between_exclusive", description,1328               "expected a number greater than ${lower} " +1329               "and less than ${upper} but got ${actual}",1330               {lower:lower, upper:upper, actual:actual});1331    }1332    expose_assert(assert_between_exclusive, "assert_between_exclusive");1333    function assert_less_than_equal(actual, expected, description)1334    {1335        /*1336         * Test if a primitive number is less than or equal to another1337         */1338        assert(typeof actual === "number",1339               "assert_less_than_equal", description,1340               "expected a number but got a ${type_actual}",1341               {type_actual:typeof actual});1342        assert(actual <= expected,1343               "assert_less_than_equal", description,1344               "expected a number less than or equal to ${expected} but got ${actual}",1345               {expected:expected, actual:actual});1346    }1347    expose_assert(assert_less_than_equal, "assert_less_than_equal");1348    function assert_greater_than_equal(actual, expected, description)1349    {1350        /*1351         * Test if a primitive number is greater than or equal to another1352         */1353        assert(typeof actual === "number",1354               "assert_greater_than_equal", description,1355               "expected a number but got a ${type_actual}",1356               {type_actual:typeof actual});1357        assert(actual >= expected,1358               "assert_greater_than_equal", description,1359               "expected a number greater than or equal to ${expected} but got ${actual}",1360               {expected:expected, actual:actual});1361    }1362    expose_assert(assert_greater_than_equal, "assert_greater_than_equal");1363    function assert_between_inclusive(actual, lower, upper, description)1364    {1365        /*1366         * Test if a primitive number is between to two others or equal to either of them1367         */1368        assert(typeof actual === "number",1369               "assert_between_inclusive", description,1370               "expected a number but got a ${type_actual}",1371               {type_actual:typeof actual});1372        assert(actual >= lower && actual <= upper,1373               "assert_between_inclusive", description,1374               "expected a number greater than or equal to ${lower} " +1375               "and less than or equal to ${upper} but got ${actual}",1376               {lower:lower, upper:upper, actual:actual});1377    }1378    expose_assert(assert_between_inclusive, "assert_between_inclusive");1379    function assert_regexp_match(actual, expected, description) {1380        /*1381         * Test if a string (actual) matches a regexp (expected)1382         */1383        assert(expected.test(actual),1384               "assert_regexp_match", description,1385               "expected ${expected} but got ${actual}",1386               {expected:expected, actual:actual});1387    }1388    expose_assert(assert_regexp_match, "assert_regexp_match");1389    function assert_class_string(object, class_string, description) {1390        var actual = {}.toString.call(object);1391        var expected = "[object " + class_string + "]";1392        assert(same_value(actual, expected), "assert_class_string", description,1393                                             "expected ${expected} but got ${actual}",1394                                             {expected:expected, actual:actual});1395    }1396    expose_assert(assert_class_string, "assert_class_string");1397    function assert_own_property(object, property_name, description) {1398        assert(object.hasOwnProperty(property_name),1399               "assert_own_property", description,1400               "expected property ${p} missing", {p:property_name});1401    }1402    expose_assert(assert_own_property, "assert_own_property");1403    function assert_not_own_property(object, property_name, description) {1404        assert(!object.hasOwnProperty(property_name),1405               "assert_not_own_property", description,1406               "unexpected property ${p} is found on object", {p:property_name});1407    }1408    expose_assert(assert_not_own_property, "assert_not_own_property");1409    function _assert_inherits(name) {1410        return function (object, property_name, description)1411        {1412            assert((typeof object === "object" && object !== null) ||1413                   typeof object === "function" ||1414                   // Or has [[IsHTMLDDA]] slot1415                   String(object) === "[object HTMLAllCollection]",1416                   name, description,1417                   "provided value is not an object");1418            assert("hasOwnProperty" in object,1419                   name, description,1420                   "provided value is an object but has no hasOwnProperty method");1421            assert(!object.hasOwnProperty(property_name),1422                   name, description,1423                   "property ${p} found on object expected in prototype chain",1424                   {p:property_name});1425            assert(property_name in object,1426                   name, description,1427                   "property ${p} not found in prototype chain",1428                   {p:property_name});1429        };1430    }1431    expose_assert(_assert_inherits("assert_inherits"), "assert_inherits");1432    expose_assert(_assert_inherits("assert_idl_attribute"), "assert_idl_attribute");1433    function assert_readonly(object, property_name, description)1434    {1435         var initial_value = object[property_name];1436         try {1437             //Note that this can have side effects in the case where1438             //the property has PutForwards1439             object[property_name] = initial_value + "a"; //XXX use some other value here?1440             assert(same_value(object[property_name], initial_value),1441                    "assert_readonly", description,1442                    "changing property ${p} succeeded",1443                    {p:property_name});1444         } finally {1445             object[property_name] = initial_value;1446         }1447    }1448    expose_assert(assert_readonly, "assert_readonly");1449    /**1450     * Assert a JS Error with the expected constructor is thrown.1451     *1452     * @param {object} constructor The expected exception constructor.1453     * @param {Function} func Function which should throw.1454     * @param {string} description Error description for the case that the error is not thrown.1455     */1456    function assert_throws_js(constructor, func, description)1457    {1458        assert_throws_js_impl(constructor, func, description,1459                              "assert_throws_js");1460    }1461    expose_assert(assert_throws_js, "assert_throws_js");1462    /**1463     * Like assert_throws_js but allows specifying the assertion type1464     * (assert_throws_js or promise_rejects_js, in practice).1465     */1466    function assert_throws_js_impl(constructor, func, description,1467                                   assertion_type)1468    {1469        try {1470            func.call(this);1471            assert(false, assertion_type, description,1472                   "${func} did not throw", {func:func});1473        } catch (e) {1474            if (e instanceof AssertionError) {1475                throw e;1476            }1477            // Basic sanity-checks on the thrown exception.1478            assert(typeof e === "object",1479                   assertion_type, description,1480                   "${func} threw ${e} with type ${type}, not an object",1481                   {func:func, e:e, type:typeof e});1482            assert(e !== null,1483                   assertion_type, description,1484                   "${func} threw null, not an object",1485                   {func:func});1486            // Basic sanity-check on the passed-in constructor1487            assert(typeof constructor == "function",1488                   assertion_type, description,1489                   "${constructor} is not a constructor",1490                   {constructor:constructor});1491            var obj = constructor;1492            while (obj) {1493                if (typeof obj === "function" &&1494                    obj.name === "Error") {1495                    break;1496                }1497                obj = Object.getPrototypeOf(obj);1498            }1499            assert(obj != null,1500                   assertion_type, description,1501                   "${constructor} is not an Error subtype",1502                   {constructor:constructor});1503            // And checking that our exception is reasonable1504            assert(e.constructor === constructor &&1505                   e.name === constructor.name,1506                   assertion_type, description,1507                   "${func} threw ${actual} (${actual_name}) expected instance of ${expected} (${expected_name})",1508                   {func:func, actual:e, actual_name:e.name,1509                    expected:constructor,1510                    expected_name:constructor.name});1511        }1512    }1513    /**1514     * Assert a DOMException with the expected type is thrown.1515     *1516     * @param {number|string} type The expected exception name or code.  See the1517     *        table of names and codes at1518     *        https://webidl.spec.whatwg.org/#dfn-error-names-table1519     *        If a number is passed it should be one of the numeric code values1520     *        in that table (e.g. 3, 4, etc).  If a string is passed it can1521     *        either be an exception name (e.g. "HierarchyRequestError",1522     *        "WrongDocumentError") or the name of the corresponding error code1523     *        (e.g. "HIERARCHY_REQUEST_ERR", "WRONG_DOCUMENT_ERR").1524     *1525     * For the remaining arguments, there are two ways of calling1526     * promise_rejects_dom:1527     *1528     * 1) If the DOMException is expected to come from the current global, the1529     * second argument should be the function expected to throw and a third,1530     * optional, argument is the assertion description.1531     *1532     * 2) If the DOMException is expected to come from some other global, the1533     * second argument should be the DOMException constructor from that global,1534     * the third argument the function expected to throw, and the fourth, optional,1535     * argument the assertion description.1536     */1537    function assert_throws_dom(type, funcOrConstructor, descriptionOrFunc, maybeDescription)1538    {1539        let constructor, func, description;1540        if (funcOrConstructor.name === "DOMException") {1541            constructor = funcOrConstructor;1542            func = descriptionOrFunc;1543            description = maybeDescription;1544        } else {1545            constructor = self.DOMException;1546            func = funcOrConstructor;1547            description = descriptionOrFunc;1548            assert(maybeDescription === undefined,1549                   "Too many args pased to no-constructor version of assert_throws_dom");1550        }1551        assert_throws_dom_impl(type, func, description, "assert_throws_dom", constructor)1552    }1553    expose_assert(assert_throws_dom, "assert_throws_dom");1554    /**1555     * Similar to assert_throws_dom but allows specifying the assertion type1556     * (assert_throws_dom or promise_rejects_dom, in practice).  The1557     * "constructor" argument must be the DOMException constructor from the1558     * global we expect the exception to come from.1559     */1560    function assert_throws_dom_impl(type, func, description, assertion_type, constructor)1561    {1562        try {1563            func.call(this);1564            assert(false, assertion_type, description,1565                   "${func} did not throw", {func:func});1566        } catch (e) {1567            if (e instanceof AssertionError) {1568                throw e;1569            }1570            // Basic sanity-checks on the thrown exception.1571            assert(typeof e === "object",1572                   assertion_type, description,1573                   "${func} threw ${e} with type ${type}, not an object",1574                   {func:func, e:e, type:typeof e});1575            assert(e !== null,1576                   assertion_type, description,1577                   "${func} threw null, not an object",1578                   {func:func});1579            // Sanity-check our type1580            assert(typeof type == "number" ||1581                   typeof type == "string",1582                   assertion_type, description,1583                   "${type} is not a number or string",1584                   {type:type});1585            var codename_name_map = {1586                INDEX_SIZE_ERR: 'IndexSizeError',1587                HIERARCHY_REQUEST_ERR: 'HierarchyRequestError',1588                WRONG_DOCUMENT_ERR: 'WrongDocumentError',1589                INVALID_CHARACTER_ERR: 'InvalidCharacterError',1590                NO_MODIFICATION_ALLOWED_ERR: 'NoModificationAllowedError',1591                NOT_FOUND_ERR: 'NotFoundError',1592                NOT_SUPPORTED_ERR: 'NotSupportedError',1593                INUSE_ATTRIBUTE_ERR: 'InUseAttributeError',1594                INVALID_STATE_ERR: 'InvalidStateError',1595                SYNTAX_ERR: 'SyntaxError',1596                INVALID_MODIFICATION_ERR: 'InvalidModificationError',1597                NAMESPACE_ERR: 'NamespaceError',1598                INVALID_ACCESS_ERR: 'InvalidAccessError',1599                TYPE_MISMATCH_ERR: 'TypeMismatchError',1600                SECURITY_ERR: 'SecurityError',1601                NETWORK_ERR: 'NetworkError',1602                ABORT_ERR: 'AbortError',1603                URL_MISMATCH_ERR: 'URLMismatchError',1604                QUOTA_EXCEEDED_ERR: 'QuotaExceededError',1605                TIMEOUT_ERR: 'TimeoutError',1606                INVALID_NODE_TYPE_ERR: 'InvalidNodeTypeError',1607                DATA_CLONE_ERR: 'DataCloneError'1608            };1609            var name_code_map = {1610                IndexSizeError: 1,1611                HierarchyRequestError: 3,1612                WrongDocumentError: 4,1613                InvalidCharacterError: 5,1614                NoModificationAllowedError: 7,1615                NotFoundError: 8,1616                NotSupportedError: 9,1617                InUseAttributeError: 10,1618                InvalidStateError: 11,1619                SyntaxError: 12,1620                InvalidModificationError: 13,1621                NamespaceError: 14,1622                InvalidAccessError: 15,1623                TypeMismatchError: 17,1624                SecurityError: 18,1625                NetworkError: 19,1626                AbortError: 20,1627                URLMismatchError: 21,1628                QuotaExceededError: 22,1629                TimeoutError: 23,1630                InvalidNodeTypeError: 24,1631                DataCloneError: 25,1632                EncodingError: 0,1633                NotReadableError: 0,1634                UnknownError: 0,1635                ConstraintError: 0,1636                DataError: 0,1637                TransactionInactiveError: 0,1638                ReadOnlyError: 0,1639                VersionError: 0,1640                OperationError: 0,1641                NotAllowedError: 01642            };1643            var code_name_map = {};1644            for (var key in name_code_map) {1645                if (name_code_map[key] > 0) {1646                    code_name_map[name_code_map[key]] = key;1647                }1648            }1649            var required_props = {};1650            var name;1651            if (typeof type === "number") {1652                if (type === 0) {1653                    throw new AssertionError('Test bug: ambiguous DOMException code 0 passed to assert_throws_dom()');1654                } else if (!(type in code_name_map)) {1655                    throw new AssertionError('Test bug: unrecognized DOMException code "' + type + '" passed to assert_throws_dom()');1656                }1657                name = code_name_map[type];1658                required_props.code = type;1659            } else if (typeof type === "string") {1660                name = type in codename_name_map ? codename_name_map[type] : type;1661                if (!(name in name_code_map)) {1662                    throw new AssertionError('Test bug: unrecognized DOMException code name or name "' + type + '" passed to assert_throws_dom()');1663                }1664                required_props.code = name_code_map[name];1665            }1666            if (required_props.code === 0 ||1667               ("name" in e &&1668                e.name !== e.name.toUpperCase() &&1669                e.name !== "DOMException")) {1670                // New style exception: also test the name property.1671                required_props.name = name;1672            }1673            for (var prop in required_props) {1674                assert(prop in e && e[prop] == required_props[prop],1675                       assertion_type, description,1676                       "${func} threw ${e} that is not a DOMException " + type + ": property ${prop} is equal to ${actual}, expected ${expected}",1677                       {func:func, e:e, prop:prop, actual:e[prop], expected:required_props[prop]});1678            }1679            // Check that the exception is from the right global.  This check is last1680            // so more specific, and more informative, checks on the properties can1681            // happen in case a totally incorrect exception is thrown.1682            assert(e.constructor === constructor,1683                   assertion_type, description,1684                   "${func} threw an exception from the wrong global",1685                   {func});1686        }1687    }1688    /**1689     * Assert the provided value is thrown.1690     *1691     * @param {value} exception The expected exception.1692     * @param {Function} func Function which should throw.1693     * @param {string} description Error description for the case that the error is not thrown.1694     */1695    function assert_throws_exactly(exception, func, description)1696    {1697        assert_throws_exactly_impl(exception, func, description,1698                                   "assert_throws_exactly");1699    }1700    expose_assert(assert_throws_exactly, "assert_throws_exactly");1701    /**1702     * Like assert_throws_exactly but allows specifying the assertion type1703     * (assert_throws_exactly or promise_rejects_exactly, in practice).1704     */1705    function assert_throws_exactly_impl(exception, func, description,1706                                        assertion_type)1707    {1708        try {1709            func.call(this);1710            assert(false, assertion_type, description,1711                   "${func} did not throw", {func:func});1712        } catch (e) {1713            if (e instanceof AssertionError) {1714                throw e;1715            }1716            assert(same_value(e, exception), assertion_type, description,1717                   "${func} threw ${e} but we expected it to throw ${exception}",1718                   {func:func, e:e, exception:exception});1719        }1720    }1721    function assert_unreached(description) {1722         assert(false, "assert_unreached", description,1723                "Reached unreachable code");1724    }1725    expose_assert(assert_unreached, "assert_unreached");1726    function assert_any(assert_func, actual, expected_array)1727    {1728        var args = [].slice.call(arguments, 3);1729        var errors = [];1730        var passed = false;1731        forEach(expected_array,1732                function(expected)1733                {1734                    try {1735                        assert_func.apply(this, [actual, expected].concat(args));1736                        passed = true;1737                    } catch (e) {1738                        errors.push(e.message);1739                    }1740                });1741        if (!passed) {1742            throw new AssertionError(errors.join("\n\n"));1743        }1744    }1745    // FIXME: assert_any cannot use expose_assert, because assert_wrapper does1746    // not support nested assert calls (e.g. to assert_func). We need to1747    // support bypassing assert_wrapper for the inner asserts here.1748    expose(assert_any, "assert_any");1749    /**1750     * Assert that a feature is implemented, based on a 'truthy' condition.1751     *1752     * This function should be used to early-exit from tests in which there is1753     * no point continuing without support for a non-optional spec or spec1754     * feature. For example:1755     *1756     *     assert_implements(window.Foo, 'Foo is not supported');1757     *1758     * @param {object} condition The truthy value to test1759     * @param {string} description Error description for the case that the condition is not truthy.1760     */1761    function assert_implements(condition, description) {1762        assert(!!condition, "assert_implements", description);1763    }1764    expose_assert(assert_implements, "assert_implements")1765    /**1766     * Assert that an optional feature is implemented, based on a 'truthy' condition.1767     *1768     * This function should be used to early-exit from tests in which there is1769     * no point continuing without support for an explicitly optional spec or1770     * spec feature. For example:1771     *1772     *     assert_implements_optional(video.canPlayType("video/webm"),1773     *                                "webm video playback not supported");1774     *1775     * @param {object} condition The truthy value to test1776     * @param {string} description Error description for the case that the condition is not truthy.1777     */1778    function assert_implements_optional(condition, description) {1779        if (!condition) {1780            throw new OptionalFeatureUnsupportedError(description);1781        }1782    }1783    expose_assert(assert_implements_optional, "assert_implements_optional")1784    function Test(name, properties)1785    {1786        if (tests.file_is_test && tests.tests.length) {1787            throw new Error("Tried to create a test with file_is_test");1788        }1789        this.name = name;1790        this.phase = (tests.is_aborted || tests.phase === tests.phases.COMPLETE) ?1791            this.phases.COMPLETE : this.phases.INITIAL;1792        this.status = this.NOTRUN;1793        this.timeout_id = null;1794        this.index = null;1795        this.properties = properties || {};1796        this.timeout_length = settings.test_timeout;1797        if (this.timeout_length !== null) {...aflprep_testharness.js
Source:aflprep_testharness.js  
...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,1374                   assertion_type, description,1375                   "${func} threw null, not an object",1376                   {func:func});1377            assert(typeof type == "number" ||1378                   typeof type == "string",1379                   assertion_type, description,1380                   "${type} is not a number or string",1381                   {type:type});1382            var codename_name_map = {1383                INDEX_SIZE_ERR: 'IndexSizeError',1384                HIERARCHY_REQUEST_ERR: 'HierarchyRequestError',1385                WRONG_DOCUMENT_ERR: 'WrongDocumentError',1386                INVALID_CHARACTER_ERR: 'InvalidCharacterError',1387                NO_MODIFICATION_ALLOWED_ERR: 'NoModificationAllowedError',1388                NOT_FOUND_ERR: 'NotFoundError',1389                NOT_SUPPORTED_ERR: 'NotSupportedError',1390                INUSE_ATTRIBUTE_ERR: 'InUseAttributeError',1391                INVALID_STATE_ERR: 'InvalidStateError',1392                SYNTAX_ERR: 'SyntaxError',1393                INVALID_MODIFICATION_ERR: 'InvalidModificationError',1394                NAMESPACE_ERR: 'NamespaceError',1395                INVALID_ACCESS_ERR: 'InvalidAccessError',1396                TYPE_MISMATCH_ERR: 'TypeMismatchError',1397                SECURITY_ERR: 'SecurityError',1398                NETWORK_ERR: 'NetworkError',1399                ABORT_ERR: 'AbortError',1400                URL_MISMATCH_ERR: 'URLMismatchError',1401                QUOTA_EXCEEDED_ERR: 'QuotaExceededError',1402                TIMEOUT_ERR: 'TimeoutError',1403                INVALID_NODE_TYPE_ERR: 'InvalidNodeTypeError',1404                DATA_CLONE_ERR: 'DataCloneError'1405            };1406            var name_code_map = {1407                IndexSizeError: 1,1408                HierarchyRequestError: 3,1409                WrongDocumentError: 4,1410                InvalidCharacterError: 5,1411                NoModificationAllowedError: 7,1412                NotFoundError: 8,1413                NotSupportedError: 9,1414                InUseAttributeError: 10,1415                InvalidStateError: 11,1416                SyntaxError: 12,1417                InvalidModificationError: 13,1418                NamespaceError: 14,1419                InvalidAccessError: 15,1420                TypeMismatchError: 17,1421                SecurityError: 18,1422                NetworkError: 19,1423                AbortError: 20,1424                URLMismatchError: 21,1425                QuotaExceededError: 22,1426                TimeoutError: 23,1427                InvalidNodeTypeError: 24,1428                DataCloneError: 25,1429                EncodingError: 0,1430                NotReadableError: 0,1431                UnknownError: 0,1432                ConstraintError: 0,1433                DataError: 0,1434                TransactionInactiveError: 0,1435                ReadOnlyError: 0,1436                VersionError: 0,1437                OperationError: 0,1438                NotAllowedError: 01439            };1440            var code_name_map = {};1441            for (var key in name_code_map) {1442                if (name_code_map[key] > 0) {1443                    code_name_map[name_code_map[key]] = key;1444                }1445            }1446            var required_props = {};1447            var name;1448            if (typeof type === "number") {1449                if (type === 0) {1450                    throw new AssertionError('Test bug: ambiguous DOMException code 0 passed to assert_throws_dom()');1451                } else if (!(type in code_name_map)) {1452                    throw new AssertionError('Test bug: unrecognized DOMException code "' + type + '" passed to assert_throws_dom()');1453                }1454                name = code_name_map[type];1455                required_props.code = type;1456            } else if (typeof type === "string") {1457                name = type in codename_name_map ? codename_name_map[type] : type;1458                if (!(name in name_code_map)) {1459                    throw new AssertionError('Test bug: unrecognized DOMException code name or name "' + type + '" passed to assert_throws_dom()');1460                }1461                required_props.code = name_code_map[name];1462            }1463            if (required_props.code === 0 ||1464               ("name" in e &&1465                e.name !== e.name.toUpperCase() &&1466                e.name !== "DOMException")) {1467                required_props.name = name;1468            }1469            for (var prop in required_props) {1470                assert(prop in e && e[prop] == required_props[prop],1471                       assertion_type, description,1472                       "${func} threw ${e} that is not a DOMException " + type + ": property ${prop} is equal to ${actual}, expected ${expected}",1473                       {func:func, e:e, prop:prop, actual:e[prop], expected:required_props[prop]});1474            }1475            assert(e.constructor === constructor,1476                   assertion_type, description,1477                   "${func} threw an exception from the wrong global",1478                   {func});1479        }1480    }1481     * Assert the provided value is thrown.1482     *1483     * @param {value} exception The expected exception.1484     * @param {Function} func Function which should throw.1485     * @param {string} description Error description for the case that the error is not thrown.1486    function assert_throws_exactly(exception, func, description)1487    {1488        assert_throws_exactly_impl(exception, func, description,1489                                   "assert_throws_exactly");1490    }1491    expose_assert(assert_throws_exactly, "assert_throws_exactly");1492     * Like assert_throws_exactly but allows specifying the assertion type1493     * (assert_throws_exactly or promise_rejects_exactly, in practice).1494    function assert_throws_exactly_impl(exception, func, description,1495                                        assertion_type)1496    {1497        try {1498            func.call(this);1499            assert(false, assertion_type, description,1500                   "${func} did not throw", {func:func});1501        } catch (e) {1502            if (e instanceof AssertionError) {1503                throw e;1504            }1505            assert(same_value(e, exception), assertion_type, description,1506                   "${func} threw ${e} but we expected it to throw ${exception}",1507                   {func:func, e:e, exception:exception});1508        }1509    }1510    function assert_unreached(description) {1511         assert(false, "assert_unreached", description,1512                "Reached unreachable code");1513    }1514    expose_assert(assert_unreached, "assert_unreached");1515    function assert_any(assert_func, actual, expected_array)1516    {1517        var args = [].slice.call(arguments, 3);1518        var errors = [];1519        var passed = false;1520        forEach(expected_array,1521                function(expected)1522                {1523                    try {1524                        assert_func.apply(this, [actual, expected].concat(args));1525                        passed = true;1526                    } catch (e) {1527                        errors.push(e.message);1528                    }1529                });1530        if (!passed) {1531            throw new AssertionError(errors.join("\n\n"));1532        }1533    }1534    expose(assert_any, "assert_any");1535     * Assert that a feature is implemented, based on a 'truthy' condition.1536     *1537     * This function should be used to early-exit from tests in which there is1538     * no point continuing without support for a non-optional spec or spec1539     * feature. For example:1540     *1541     *     assert_implements(window.Foo, 'Foo is not supported');1542     *1543     * @param {object} condition The truthy value to test1544     * @param {string} description Error description for the case that the condition is not truthy.1545    function assert_implements(condition, description) {1546        assert(!!condition, "assert_implements", description);1547    }1548    expose_assert(assert_implements, "assert_implements")1549     * Assert that an optional feature is implemented, based on a 'truthy' condition.1550     *1551     * This function should be used to early-exit from tests in which there is1552     * no point continuing without support for an explicitly optional spec or1553     * spec feature. For example:1554     *1555     *                                "webm video playback not supported");1556     *1557     * @param {object} condition The truthy value to test1558     * @param {string} description Error description for the case that the condition is not truthy.1559    function assert_implements_optional(condition, description) {1560        if (!condition) {1561            throw new OptionalFeatureUnsupportedError(description);1562        }1563    }1564    expose_assert(assert_implements_optional, "assert_implements_optional")1565    function Test(name, properties)1566    {1567        if (tests.file_is_test && tests.tests.length) {1568            throw new Error("Tried to create a test with file_is_test");1569        }1570        this.name = name;1571        this.phase = (tests.is_aborted || tests.phase === tests.phases.COMPLETE) ?1572            this.phases.COMPLETE : this.phases.INITIAL;1573        this.status = this.NOTRUN;1574        this.timeout_id = null;1575        this.index = null;1576        this.properties = properties || {};1577        this.timeout_length = settings.test_timeout;1578        if (this.timeout_length !== null) {...Using AI Code Generation
1var assert = expose_assert();2var test = expose_test();3var done = expose_done();4var promise_test = expose_promise_test();5var promise_done = expose_promise_done();6var promise_rejects = expose_promise_rejects();7var promise_rejects_dom = expose_promise_rejects_dom();8var promise_rejects_exactly = expose_promise_rejects_exactly();9var promise_rejects_js = expose_promise_rejects_js();10var promise_rejects_unreached = expose_promise_rejects_unreached();11var promise_rejects_not_reached = expose_promise_rejects_not_reached();12var promise_rejects_not_equals = expose_promise_rejects_not_equals();13var promise_rejects_not_deep_equals = expose_promise_rejects_not_deep_equals();14var promise_rejects_not_same = expose_promise_rejects_not_same();15var promise_rejects_not_object_equals = expose_promise_rejects_not_object_equals();16var promise_rejects_not_array_equals = expose_promise_rejects_not_array_equals();Using AI Code Generation
1var assert = expose_assert();2var test = expose_test();3var promise_test = expose_promise_test();4var async_test = expose_async_test();5var done = expose_done();6test(function() {7  assert_equals(1, 1, "1 equals 1");8}, "Test 1");9promise_test(function() {10  return new Promise(function(resolve, reject) {11    setTimeout(function() {12      resolve();13    }, 100);14  });15}, "Test 2");16async_test(function(t) {17  setTimeout(function() {18    t.done();19  }, 100);20}, "Test 3");21async_test(function(t) {22  setTimeout(function() {23    t.done();24  }, 100);25}, "Test 4");26test(function() {27  assert_equals(1, 1, "1 equals 1");28}, "Test 5");29promise_test(function() {30  return new Promise(function(resolve, reject) {31    setTimeout(function() {32      resolve();33    }, 100);34  });35}, "Test 6");36async_test(function(t) {37  setTimeout(function() {38    t.done();39  }, 100);40}, "Test 7");41async_test(function(t) {42  setTimeout(function() {43    t.done();44  }, 100);45}, "Test 8");46test(function() {47  assert_equals(1, 1, "1 equals 1");48}, "Test 9");49promise_test(function() {50  return new Promise(function(resolve, reject) {51    setTimeout(function() {52      resolve();53    }, 100);54  });55}, "Test 10");56async_test(function(t) {57  setTimeout(function() {58    t.done();59  }, 100);60}, "Test 11");61async_test(function(t) {62  setTimeout(function() {63    t.done();64  }, 100);65}, "Test 12");66test(function() {67  assert_equals(1, 1, "1 equals 1");68}, "Test 13");69promise_test(function() {70  return new Promise(function(resolve, reject) {71    setTimeout(function() {72      resolve();73    }, 100);74  });75}, "Test 14");76async_test(function(t) {77  setTimeout(function() {Using AI Code Generation
1expose_assert(assert, 'assert');2expose_promise_test(test, 'test');3expose_promise_setup(setup, 'setup');4expose_promise_teardown(teardown, 'teardown');5import { expose_assert, expose_promise_test } from 'wpt-runner';6const assert = expose_assert(assert, 'assert');7const test = expose_promise_test(test, 'test');8test('test', () => {9  assert.equal(1, 1);10});11module.exports = {12};13### expose_assert(assert, name)14### expose_promise_test(test, name)15### expose_promise_setup(setup, name)16### expose_promise_teardown(teardown, name)Using AI Code Generation
1var assert = require("assert");2var wpt = require("wpt-runner");3wpt.expose_assert(assert);4var assert = require("assert");5var wpt = require("wpt-runner");6wpt.expose_assert(assert);7var assert = require("assert");8var wpt = require("wpt-runner");9wpt.expose_assert(assert);10var assert = require("assert");11var wpt = require("wpt-runner");12wpt.expose_assert(assert);13var assert = require("assert");14var wpt = require("wpt-runner");15wpt.expose_assert(assert);16var assert = require("assert");17var wpt = require("wpt-runner");18wpt.expose_assert(assert);19var assert = require("assert");20var wpt = require("wpt-runner");21wpt.expose_assert(assert);22var assert = require("assert");23var wpt = require("wpt-runner");24wpt.expose_assert(assert);25var assert = require("assert");26var wpt = require("wpt-runner");27wpt.expose_assert(assert);28var assert = require("assert");29var wpt = require("wpt-runner");30wpt.expose_assert(assert);31var assert = require("assert");32var wpt = require("wpt-runner");33wpt.expose_assert(assert);34var assert = require("assert");35var wpt = require("wpt-runner");36wpt.expose_assert(assert);37var assert = require("assert");38var wpt = require("wpt-runner");39wpt.expose_assert(assert);40var assert = require("assert");41var wpt = require("wpt-runner");42wpt.expose_assert(assert);Using AI Code Generation
1var assert = require('assert');2var expose_assert = require('wpt-runner').expose_assert;3expose_assert(assert);4var assert = require('wpt-runner').assert;5var assert = require('wpt-runner').assert;6var assert = require('wpt-runner').assert;7var assert = require('wpt-runner').assert;8var assert = require('wpt-runner').assert;9var assert = require('wpt-runner').assert;10var assert = require('wpt-runner').assert;11var assert = require('wpt-runner').assert;12var assert = require('wpt-runner').assert;13var assert = require('wpt-runner').assert;14var assert = require('wpt-runner').assert;15var assert = require('wpt-runner').assert;16var assert = require('wpt-runner').assert;17var assert = require('wpt-runner').assert;18var assert = require('wpt-runner').assert;19var assert = require('wpt-runner').assert;20var assert = require('wpt-runner').assert;Using AI Code Generation
1var assert = wpt.expose_assert();2assert.ok(true, "assert.ok() is working");3assert.equal(1, 1, "assert.equal() is working");4assert.notEqual(1, 2, "assert.notEqual() is working");5assert.deepEqual({a:1}, {a:1}, "assert.deepEqual() is working");6assert.notDeepEqual({a:1}, {a:2}, "assert.notDeepEqual() is working");7assert.strictEqual(1, 1, "assert.strictEqual() is working");8assert.notStrictEqual(1, "1", "assert.notStrictEqual() is working");9assert.throws(function() { throw new Error("error message") }, "assert.throws() is working");10assert.doesNotThrow(function() { }, "assert.doesNotThrow() is working");11assert.ifError(false, "assert.ifError() is working");12### wpt.expose_assert()Using AI Code Generation
1var assert = require('assert');2var expose_assert = require('wpt-runner').expose_assert;3expose_assert(assert, this);4function add(a,b){5  return a+b;6}7test("add", function(){8  assert_equals(add(1,2),3);9  assert_equals(add(1,2),4);10});11var assert = require('assert');12var expose_assert = require('wpt-runner').expose_assert;13expose_assert(assert, this);14function add(a,b,callback){15  setTimeout(function(){16    callback(a+b);17  },1000);18}19test_async("add", function(cb){20  add(1,2,function(result){21    assert_equals(result,3);22    cb();23  });24});25var assert = require('assert');26var expose_assert = require('wpt-runner').expose_assert;27expose_assert(assert, this);28function add(a,b){29  return a+b;30}31test("add", function(){32  assert_equals(add(1,2),3);33  assert_equals(add(1,2),4);34});35skip("add", function(){36  assert_equals(add(1,2),3);37  assert_equals(add(1,2),4);38});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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
