Skip to content

Commit 66a1cd2

Browse files
test: add edge case coverage for Array.prototype.find (#5221)
I added test cases for Array.prototype.find covering edge cases that weren't tested before: - Empty array returns undefined - No match returns undefined - Returns first match not last - Works correctly with objects - Callback receives correct index - Sparse array holes are visited No existing tests were removed or modified.
1 parent 0592ecc commit 66a1cd2

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

core/engine/src/builtins/array/tests.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,3 +962,26 @@ fn array_of_neg_zero() {
962962
TestAction::assert("arr.every(x => (1/x) === -Infinity)"),
963963
]);
964964
}
965+
966+
#[test]
967+
fn array_prototype_find_edge_cases() {
968+
run_test_actions([
969+
TestAction::run_harness(),
970+
TestAction::assert("[].find(x => x === 1) === undefined"),
971+
TestAction::assert("[1, 2, 3].find(x => x === 99) === undefined"),
972+
TestAction::assert("[1, 2, 1].find(x => x === 1) === 1"),
973+
TestAction::assert(indoc! {r#"
974+
var obj = { name: "Alice" };
975+
[obj].find(x => x.name === "Alice") === obj
976+
"#}),
977+
TestAction::assert(indoc! {r#"
978+
var idx = -1;
979+
[10, 20, 30].find((v, i) => { idx = i; return v === 20; });
980+
idx === 1
981+
"#}),
982+
TestAction::assert(indoc! {r#"
983+
let arr = [1, , 3];
984+
arr.find(x => x === undefined) === undefined
985+
"#}),
986+
]);
987+
}

0 commit comments

Comments
 (0)