diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-11-09 21:47:25 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-09 21:47:25 -0500 |
commit | 219dfb4917f5b19da19b85ff223de663db561fe7 (patch) | |
tree | 5f1763073b4cb2e6b1347b27a6e535c51f524ae6 /test/test-core.js | |
parent | eb8069a4944c5060e9c3ff15814291c918c04068 (diff) |
Add a core deepEqual function (#1018)
* Add a core deepEqual function
* Add tests
Diffstat (limited to 'test/test-core.js')
-rw-r--r-- | test/test-core.js | 132 |
1 files changed, 131 insertions, 1 deletions
diff --git a/test/test-core.js b/test/test-core.js index 3e8b8414..2b29b7f1 100644 --- a/test/test-core.js +++ b/test/test-core.js @@ -32,7 +32,7 @@ const vm = new VM({ vm.execute([ 'mixed/js/core.js' ]); -const [DynamicProperty] = vm.get(['DynamicProperty']); +const [DynamicProperty, deepEqual] = vm.get(['DynamicProperty', 'deepEqual']); function testDynamicProperty() { @@ -161,9 +161,139 @@ function testDynamicProperty() { } } +function testDeepEqual() { + const data = [ + // Simple tests + { + value1: 0, + value2: 0, + expected: true + }, + { + value1: null, + value2: null, + expected: true + }, + { + value1: 'test', + value2: 'test', + expected: true + }, + { + value1: true, + value2: true, + expected: true + }, + { + value1: 0, + value2: 1, + expected: false + }, + { + value1: null, + value2: false, + expected: false + }, + { + value1: 'test1', + value2: 'test2', + expected: false + }, + { + value1: true, + value2: false, + expected: false + }, + + // Simple object tests + { + value1: {}, + value2: {}, + expected: true + }, + { + value1: {}, + value2: [], + expected: false + }, + { + value1: [], + value2: [], + expected: true + }, + { + value1: {}, + value2: null, + expected: false + }, + + // Complex object tests + { + value1: [1], + value2: [], + expected: false + }, + { + value1: [1], + value2: [1], + expected: true + }, + { + value1: [1], + value2: [2], + expected: false + }, + + { + value1: {}, + value2: {test: 1}, + expected: false + }, + { + value1: {test: 1}, + value2: {test: 1}, + expected: true + }, + { + value1: {test: 1}, + value2: {test: {test2: false}}, + expected: false + }, + { + value1: {test: {test2: true}}, + value2: {test: {test2: false}}, + expected: false + }, + { + value1: {test: {test2: [true]}}, + value2: {test: {test2: [true]}}, + expected: true + }, + + // Recursive + { + value1: (() => { const x = {}; x.x = x; return x; })(), + value2: (() => { const x = {}; x.x = x; return x; })(), + expected: false + } + ]; + + let index = 0; + for (const {value1, value2, expected} of data) { + const actual1 = deepEqual(value1, value2); + assert.strictEqual(actual1, expected, `Failed for test ${index}`); + + const actual2 = deepEqual(value2, value1); + assert.strictEqual(actual2, expected, `Failed for test ${index}`); + + ++index; + } +} + function main() { testDynamicProperty(); + testDeepEqual(); } |