Skip to content

Commit

Permalink
Infra: make assert_array_equals more useful
Browse files Browse the repository at this point in the history
Fixes part of #7179.

Co-authored-by: jgraham <[email protected]>
  • Loading branch information
annevk and jgraham authored Jan 9, 2020
1 parent 0b7d1a4 commit 54170d1
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 8 deletions.
163 changes: 163 additions & 0 deletions resources/test/tests/functional/assert-array-equals.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<!DOCTYPE HTML>
<title>assert_array_equals</title>
<script src="../../variants.js"></script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>
test(() => {
assert_array_equals([], []);
}, "empty and equal");
test(() => {
assert_array_equals([1], [1]);
}, "non-empty and equal");
test(() => {
assert_array_equals([], [1]);
}, "length differs");
test(() => {
assert_array_equals([1], [,]);
}, "property is present");
test(() => {
assert_array_equals([,], [1]);
}, "property is missing");
test(() => {
assert_array_equals([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20], ["x",1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]);
}, "property 0 differs");
test(() => {
assert_array_equals([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20], [0,1,2,3,4,"x",6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]);
}, "property 5 differs");
test(() => {
assert_array_equals([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21], [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]);
}, "lengths differ and input array beyond display limit");
test(() => {
assert_array_equals([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21], [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]);
}, "lengths differ and expected array beyond display limit");
test(() => {
assert_array_equals([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21], [,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]);
}, "property 0 is present and arrays are beyond display limit");
test(() => {
assert_array_equals([,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21], [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]);
}, "property 0 is missing and arrays are beyond display limit");
test(() => {
assert_array_equals([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21], [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,,19,20,21]);
}, "property 18 is present and arrays are beyond display limit");
test(() => {
assert_array_equals([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,,19,20,21], [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]);
}, "property 18 is missing and arrays are beyond display limit");
test(() => {
assert_array_equals([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21], ["x",1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]);
}, "property 0 differs and arrays are beyond display limit");
test(() => {
assert_array_equals([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21], [0,1,2,3,4,"x",6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]);
}, "property 5 differs and arrays are beyond display limit");
test(() => {
assert_array_equals([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], [0,1,2,3,4,5,6,7,8,9,10,11,"x",13,14,15,16,17,18,19,20,21,22,23,24,25,26]);
}, "property 5 differs and arrays are beyond display limit on both sides");
</script>
<script type="text/json" id="expected">
{
"type": "complete",
"summarized_status": {
"message": null,
"status_string": "OK"
},
"summarized_tests": [
{
"name": "empty and equal",
"message": null,
"properties": {},
"status_string": "PASS"
},
{
"name": "length differs",
"message": "assert_array_equals: lengths differ, expected array [1] length 1, got [] length 0",
"properties": {},
"status_string": "FAIL"
},
{
"name": "lengths differ and expected array beyond display limit",
"message": "assert_array_equals: lengths differ, expected array [\u2026, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21] length 22, got [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21] length 21",
"properties": {},
"status_string": "FAIL"
},
{
"name": "lengths differ and input array beyond display limit",
"message": "assert_array_equals: lengths differ, expected array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21] length 21, got [\u2026, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21] length 22",
"properties": {},
"status_string": "FAIL"
},
{
"name": "non-empty and equal",
"message": null,
"properties": {},
"status_string": "PASS"
},
{
"name": "property 0 differs",
"message": "assert_array_equals: expected property 0 to be \"x\" but got 0 (expected array [\"x\", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] got [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])",
"properties": {},
"status_string": "FAIL"
},
{
"name": "property 0 differs and arrays are beyond display limit",
"message": "assert_array_equals: expected property 0 to be \"x\" but got 0 (expected array [\"x\", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, \u2026] got [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, \u2026])",
"properties": {},
"status_string": "FAIL"
},
{
"name": "property 0 is missing and arrays are beyond display limit",
"message": "assert_array_equals: expected property 0 to be \"present\" but was \"missing\" (expected array [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, \u2026] got [, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, \u2026])",
"properties": {},
"status_string": "FAIL"
},
{
"name": "property 0 is present and arrays are beyond display limit",
"message": "assert_array_equals: expected property 0 to be \"missing\" but was \"present\" (expected array [, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, \u2026] got [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, \u2026])",
"properties": {},
"status_string": "FAIL"
},
{
"name": "property 18 is missing and arrays are beyond display limit",
"message": "assert_array_equals: expected property 18 to be \"present\" but was \"missing\" (expected array [\u2026, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21] got [\u2026, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, , 19, 20, 21])",
"properties": {},
"status_string": "FAIL"
},
{
"name": "property 18 is present and arrays are beyond display limit",
"message": "assert_array_equals: expected property 18 to be \"missing\" but was \"present\" (expected array [\u2026, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, , 19, 20, 21] got [\u2026, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21])",
"properties": {},
"status_string": "FAIL"
},
{
"name": "property 5 differs",
"message": "assert_array_equals: expected property 5 to be \"x\" but got 5 (expected array [0, 1, 2, 3, 4, \"x\", 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] got [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])",
"properties": {},
"status_string": "FAIL"
},
{
"name": "property 5 differs and arrays are beyond display limit",
"message": "assert_array_equals: expected property 5 to be \"x\" but got 5 (expected array [0, 1, 2, 3, 4, \"x\", 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, \u2026] got [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, \u2026])",
"properties": {},
"status_string": "FAIL"
},
{
"name": "property 5 differs and arrays are beyond display limit on both sides",
"message": "assert_array_equals: expected property 12 to be \"x\" but got 12 (expected array [\u2026, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, \"x\", 13, 14, 15, 16, 17, 18, 19, 20, 21, \u2026] got [\u2026, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, \u2026])",
"properties": {},
"status_string": "FAIL"
},
{
"name": "property is missing",
"message": "assert_array_equals: expected property 0 to be \"present\" but was \"missing\" (expected array [1] got [])",
"properties": {},
"status_string": "FAIL"
},
{
"name": "property is present",
"message": "assert_array_equals: expected property 0 to be \"missing\" but was \"present\" (expected array [] got [1])",
"properties": {},
"status_string": "FAIL"
}
]
}
</script>
56 changes: 48 additions & 8 deletions resources/testharness.js
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,15 @@ policies and contribution forms [3].
seen.push(val);
}
if (Array.isArray(val)) {
return "[" + val.map(function(x) {return format_value(x, seen);}).join(", ") + "]";
let output = "[";
if (val.beginEllipsis !== undefined) {
output += "…, ";
}
output += val.map(function(x) {return format_value(x, seen);}).join(", ");
if (val.endEllipsis !== undefined) {
output += ", …";
}
return output + "]";
}

switch (typeof val) {
Expand Down Expand Up @@ -1178,25 +1186,57 @@ policies and contribution forms [3].

function assert_array_equals(actual, expected, description)
{
const max_array_length = 20;
function shorten_array(arr, offset = 0) {
// Make ", …" only show up when it would likely reduce the length, not accounting for
// fonts.
if (arr.length < max_array_length + 2) {
return arr;
}
// By default we want half the elements after the offset and half before
// But if that takes us past the end of the array, we have more before, and
// if it takes us before the start we have more after.
const length_after_offset = Math.floor(max_array_length / 2);
let upper_bound = Math.min(length_after_offset + offset, arr.length);
const lower_bound = Math.max(upper_bound - max_array_length, 0);

if (lower_bound === 0) {
upper_bound = max_array_length;
}

const output = arr.slice(lower_bound, upper_bound);
if (lower_bound > 0) {
output.beginEllipsis = true;
}
if (upper_bound < arr.length) {
output.endEllipsis = true;
}
return output;
}

assert(typeof actual === "object" && actual !== null && "length" in actual,
"assert_array_equals", description,
"value is ${actual}, expected array",
{actual:actual});
assert(actual.length === expected.length,
"assert_array_equals", description,
"lengths differ, expected ${expected} got ${actual}",
{expected:expected.length, actual:actual.length});
"lengths differ, expected array ${expected} length ${expectedLength}, got ${actual} length ${actualLength}",
{expected:shorten_array(expected, expected.length - 1), expectedLength:expected.length,
actual:shorten_array(actual, actual.length - 1), actualLength:actual.length
});

for (var i = 0; i < actual.length; i++) {
assert(actual.hasOwnProperty(i) === expected.hasOwnProperty(i),
"assert_array_equals", description,
"property ${i}, property expected to be ${expected} but was ${actual}",
"expected property ${i} to be ${expected} but was ${actual} (expected array ${arrayExpected} got ${arrayActual})",
{i:i, expected:expected.hasOwnProperty(i) ? "present" : "missing",
actual:actual.hasOwnProperty(i) ? "present" : "missing"});
actual:actual.hasOwnProperty(i) ? "present" : "missing",
arrayExpected:shorten_array(expected, i), arrayActual:shorten_array(actual, i)});
assert(same_value(expected[i], actual[i]),
"assert_array_equals", description,
"property ${i}, expected ${expected} but got ${actual}",
{i:i, expected:expected[i], actual:actual[i]});
"expected property ${i} to be ${expected} but got ${actual} (expected array ${arrayExpected} got ${arrayActual})",
{i:i, expected:expected[i], actual:actual[i],
arrayExpected:shorten_array(expected, i), arrayActual:shorten_array(actual, i)});
}
}
expose(assert_array_equals, "assert_array_equals");
Expand All @@ -1216,7 +1256,7 @@ policies and contribution forms [3].
"assert_array_approx_equals", description,
"property ${i}, property expected to be ${expected} but was ${actual}",
{i:i, expected:expected.hasOwnProperty(i) ? "present" : "missing",
actual:actual.hasOwnProperty(i) ? "present" : "missing"});
actual:actual.hasOwnProperty(i) ? "present" : "missing"});
assert(typeof actual[i] === "number",
"assert_array_approx_equals", description,
"property ${i}, expected a number but got a ${type_actual}",
Expand Down

0 comments on commit 54170d1

Please sign in to comment.