|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// Requirements |
| 3 | +//------------------------------------------------------------------------------ |
| 4 | + |
| 5 | +import assert from "node:assert"; |
| 6 | + |
| 7 | +import { deepMergeArrays } from "../../../lib/shared/deep-merge-arrays.js"; |
| 8 | + |
| 9 | +//------------------------------------------------------------------------------ |
| 10 | +// Tests |
| 11 | +//------------------------------------------------------------------------------ |
| 12 | + |
| 13 | +/** |
| 14 | + * Turns a value into its string equivalent for a test name. |
| 15 | + * @param {unknown} value Value to be stringified. |
| 16 | + * @returns {string} String equivalent of the value. |
| 17 | + */ |
| 18 | +function toTestCaseName(value) { |
| 19 | + return typeof value === "object" ? JSON.stringify(value) : `${value}`; |
| 20 | +} |
| 21 | + |
| 22 | +describe("deepMerge", () => { |
| 23 | + for (const [first, second, result] of [ |
| 24 | + [void 0, void 0, []], |
| 25 | + [[], void 0, []], |
| 26 | + [["abc"], void 0, ["abc"]], |
| 27 | + [void 0, ["abc"], ["abc"]], |
| 28 | + [[], ["abc"], ["abc"]], |
| 29 | + [[void 0], ["abc"], ["abc"]], |
| 30 | + [[void 0, void 0], ["abc"], ["abc", void 0]], |
| 31 | + [[void 0, void 0], ["abc", "def"], ["abc", "def"]], |
| 32 | + [[void 0, null], ["abc"], ["abc", null]], |
| 33 | + [[void 0, null], ["abc", "def"], ["abc", "def"]], |
| 34 | + [[null], ["abc"], ["abc"]], |
| 35 | + [[123], [void 0], [123]], |
| 36 | + [[123], [null], [null]], |
| 37 | + [[123], [{ a: 0 }], [{ a: 0 }]], |
| 38 | + [["abc"], [void 0], ["abc"]], |
| 39 | + [["abc"], [null], [null]], |
| 40 | + [["abc"], ["def"], ["def"]], |
| 41 | + [["abc"], [{ a: 0 }], [{ a: 0 }]], |
| 42 | + [[["abc"]], [null], [null]], |
| 43 | + [[["abc"]], ["def"], ["def"]], |
| 44 | + [[["abc"]], [{ a: 0 }], [{ a: 0 }]], |
| 45 | + [[{ abc: true }], ["def"], ["def"]], |
| 46 | + [[{ abc: true }], [["def"]], [["def"]]], |
| 47 | + [[null], [{ abc: true }], [{ abc: true }]], |
| 48 | + [[{ a: void 0 }], [{ a: 0 }], [{ a: 0 }]], |
| 49 | + [[{ a: null }], [{ a: 0 }], [{ a: 0 }]], |
| 50 | + [[{ a: null }], [{ a: { b: 0 } }], [{ a: { b: 0 } }]], |
| 51 | + [[{ a: 0 }], [{ a: 1 }], [{ a: 1 }]], |
| 52 | + [[{ a: 0 }], [{ a: null }], [{ a: null }]], |
| 53 | + [[{ a: 0 }], [{ a: void 0 }], [{ a: 0 }]], |
| 54 | + [[{ a: 0 }], ["abc"], ["abc"]], |
| 55 | + [[{ a: 0 }], [123], [123]], |
| 56 | + [[[{ a: 0 }]], [123], [123]], |
| 57 | + [ |
| 58 | + [{ a: ["b"] }], |
| 59 | + [{ a: ["c"] }], |
| 60 | + [{ a: ["c"] }] |
| 61 | + ], |
| 62 | + [ |
| 63 | + [{ a: [{ b: "c" }] }], |
| 64 | + [{ a: [{ d: "e" }] }], |
| 65 | + [{ a: [{ d: "e" }] }] |
| 66 | + ], |
| 67 | + [ |
| 68 | + [{ a: { b: "c" }, d: true }], |
| 69 | + [{ a: { e: "f" } }], |
| 70 | + [{ a: { b: "c", e: "f" }, d: true }] |
| 71 | + ], |
| 72 | + [ |
| 73 | + [{ a: { b: "c" } }], |
| 74 | + [{ a: { e: "f" }, d: true }], |
| 75 | + [{ a: { b: "c", e: "f" }, d: true }] |
| 76 | + ], |
| 77 | + [ |
| 78 | + [{ a: { b: "c" } }, { d: true }], |
| 79 | + [{ a: { e: "f" } }, { f: 123 }], |
| 80 | + [{ a: { b: "c", e: "f" } }, { d: true, f: 123 }] |
| 81 | + ], |
| 82 | + [ |
| 83 | + [{ hasOwnProperty: true }], |
| 84 | + [{}], |
| 85 | + [{ hasOwnProperty: true }] |
| 86 | + ], |
| 87 | + [ |
| 88 | + [{ hasOwnProperty: false }], |
| 89 | + [{}], |
| 90 | + [{ hasOwnProperty: false }] |
| 91 | + ], |
| 92 | + [ |
| 93 | + [{ hasOwnProperty: null }], |
| 94 | + [{}], |
| 95 | + [{ hasOwnProperty: null }] |
| 96 | + ], |
| 97 | + [ |
| 98 | + [{ hasOwnProperty: void 0 }], |
| 99 | + [{}], |
| 100 | + [{ hasOwnProperty: void 0 }] |
| 101 | + ], |
| 102 | + [ |
| 103 | + [{}], |
| 104 | + [{ hasOwnProperty: null }], |
| 105 | + [{ hasOwnProperty: null }] |
| 106 | + ], |
| 107 | + [ |
| 108 | + [{}], |
| 109 | + [{ hasOwnProperty: void 0 }], |
| 110 | + [{ hasOwnProperty: void 0 }] |
| 111 | + ], |
| 112 | + [ |
| 113 | + [{ |
| 114 | + allow: [], |
| 115 | + ignoreDestructuring: false, |
| 116 | + ignoreGlobals: false, |
| 117 | + ignoreImports: false, |
| 118 | + properties: "always" |
| 119 | + }], |
| 120 | + [], |
| 121 | + [{ |
| 122 | + allow: [], |
| 123 | + ignoreDestructuring: false, |
| 124 | + ignoreGlobals: false, |
| 125 | + ignoreImports: false, |
| 126 | + properties: "always" |
| 127 | + }] |
| 128 | + ] |
| 129 | + ]) { |
| 130 | + it(`${toTestCaseName(first)}, ${toTestCaseName(second)}`, () => { |
| 131 | + assert.deepStrictEqual(deepMergeArrays(first, second), result); |
| 132 | + }); |
| 133 | + } |
| 134 | +}); |
0 commit comments