* Assert that ``actual`` and ``expected`` are both arrays, and that the array properties of * ``actual`` and ``expected`` are all the same value (as for :js:func:`assert_equals`). * * @param {Array} actual - Test array. * @param {Array} expected - Array that is expected to contai
(actual, expected, description)
| 1694 | * @param {string} [description] - Description of the condition being tested. |
| 1695 | */ |
| 1696 | function assert_array_equals(actual, expected, description) |
| 1697 | { |
| 1698 | const max_array_length = 20; |
| 1699 | function shorten_array(arr, offset = 0) { |
| 1700 | // Make ", …" only show up when it would likely reduce the length, not accounting for |
| 1701 | // fonts. |
| 1702 | if (arr.length < max_array_length + 2) { |
| 1703 | return arr; |
| 1704 | } |
| 1705 | // By default we want half the elements after the offset and half before |
| 1706 | // But if that takes us past the end of the array, we have more before, and |
| 1707 | // if it takes us before the start we have more after. |
| 1708 | const length_after_offset = Math.floor(max_array_length / 2); |
| 1709 | let upper_bound = Math.min(length_after_offset + offset, arr.length); |
| 1710 | const lower_bound = Math.max(upper_bound - max_array_length, 0); |
| 1711 | |
| 1712 | if (lower_bound === 0) { |
| 1713 | upper_bound = max_array_length; |
| 1714 | } |
| 1715 | |
| 1716 | const output = arr.slice(lower_bound, upper_bound); |
| 1717 | if (lower_bound > 0) { |
| 1718 | output.beginEllipsis = true; |
| 1719 | } |
| 1720 | if (upper_bound < arr.length) { |
| 1721 | output.endEllipsis = true; |
| 1722 | } |
| 1723 | return output; |
| 1724 | } |
| 1725 | |
| 1726 | assert(typeof actual === "object" && actual !== null && "length" in actual, |
| 1727 | "assert_array_equals", description, |
| 1728 | "value is ${actual}, expected array", |
| 1729 | {actual:actual}); |
| 1730 | assert(actual.length === expected.length, |
| 1731 | "assert_array_equals", description, |
| 1732 | "lengths differ, expected array ${expected} length ${expectedLength}, got ${actual} length ${actualLength}", |
| 1733 | {expected:shorten_array(expected, expected.length - 1), expectedLength:expected.length, |
| 1734 | actual:shorten_array(actual, actual.length - 1), actualLength:actual.length |
| 1735 | }); |
| 1736 | |
| 1737 | for (var i = 0; i < actual.length; i++) { |
| 1738 | assert(actual.hasOwnProperty(i) === expected.hasOwnProperty(i), |
| 1739 | "assert_array_equals", description, |
| 1740 | "expected property ${i} to be ${expected} but was ${actual} (expected array ${arrayExpected} got ${arrayActual})", |
| 1741 | {i:i, expected:expected.hasOwnProperty(i) ? "present" : "missing", |
| 1742 | actual:actual.hasOwnProperty(i) ? "present" : "missing", |
| 1743 | arrayExpected:shorten_array(expected, i), arrayActual:shorten_array(actual, i)}); |
| 1744 | assert(same_value(expected[i], actual[i]), |
| 1745 | "assert_array_equals", description, |
| 1746 | "expected property ${i} to be ${expected} but got ${actual} (expected array ${arrayExpected} got ${arrayActual})", |
| 1747 | {i:i, expected:expected[i], actual:actual[i], |
| 1748 | arrayExpected:shorten_array(expected, i), arrayActual:shorten_array(actual, i)}); |
| 1749 | } |
| 1750 | } |
| 1751 | expose_assert(assert_array_equals, "assert_array_equals"); |
| 1752 | |
| 1753 | /** |
no test coverage detected
searching dependent graphs…