This is an automated email from the ASF dual-hosted git repository.

colegreer pushed a commit to branch 3.7-dev
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git


The following commit(s) were added to refs/heads/3.7-dev by this push:
     new d2eaf93dd5 [TINKERPOP-2872] Change JS tests to compare elements by ID 
for consistency with other GLVs (#2422)
d2eaf93dd5 is described below

commit d2eaf93dd5af64c05395d9dea9c572443ff4b1b0
Author: Ryan Tan <65996005+r...@users.noreply.github.com>
AuthorDate: Thu Jan 11 15:50:50 2024 -0800

    [TINKERPOP-2872] Change JS tests to compare elements by ID for consistency 
with other GLVs (#2422)
    
    Passes an ID comparator for deep-eql to use when object is type Element, 
then overwrites chai's members() and isSubsetOf() to use this comparator.
---
 CHANGELOG.asciidoc                                 |   1 +
 LICENSE                                            |   4 +-
 .../src/main/javascript/gremlin-javascript/LICENSE |  10 +-
 .../javascript/gremlin-javascript/licenses/chai    |  21 ++
 .../test/cucumber/element-comparison.js            | 123 +++++++++
 .../test/cucumber/feature-steps.js                 |  10 +-
 .../test/unit/element-comparison-test.js           | 299 +++++++++++++++++++++
 7 files changed, 463 insertions(+), 5 deletions(-)

diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc
index bf47d7849e..7d725351cc 100644
--- a/CHANGELOG.asciidoc
+++ b/CHANGELOG.asciidoc
@@ -55,6 +55,7 @@ This release also includes changes from <<release-3-6-6, 
3.6.6>> and <<release-3
 * Fixed bug in `union()` as a start step where the `Path` was including the 
starting dummy traverser.
 * Moved some TinkerGraph specific transaction tests from 
`TransactionMultiThreadedTest` to `TinkerTransactionGraphTest`
 * Fixed incorrect read operations in some cases for `TinkerTransactionGraph`.
+* Updated JavaScript tests to check equality on only id and class when 
comparing elements for consistency with other GLVs.
 * Improved performance for Element comparison by comparing hashCode() prior to 
doing more expensive checks.
 
 ==== Bugs
diff --git a/LICENSE b/LICENSE
index 47708b1f56..6317047c22 100644
--- a/LICENSE
+++ b/LICENSE
@@ -210,5 +210,5 @@ The Apache TinkerPop project bundles the following 
components under the MIT Lice
      bootstrap 5.0.0 (http://getbootstrap.com/) - for details, see 
licenses/bootstrap
      jquery 1.11.0 (https://jquery.com/) - for details, see licenses/jquery
      normalize.css 2.1.2 (https://necolas.github.io/normalize.css/) - for 
details, see licenses/normalize
-     prism.css/js 1.27.0 (https://prismjs.com) - for details, see 
licenses/prism
-     wow.js 1.1.2 (https://wowjs.uk/) - for details, see licenses/wow
\ No newline at end of file
+     prism.css/js 1.27.0 (https://prismjs.com/) - for details, see 
licenses/prism
+     wow.js 1.1.2 (https://wowjs.uk/) - for details, see licenses/wow
diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/LICENSE 
b/gremlin-javascript/src/main/javascript/gremlin-javascript/LICENSE
index 7a4a3ea242..1a1a07d01d 100644
--- a/gremlin-javascript/src/main/javascript/gremlin-javascript/LICENSE
+++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/LICENSE
@@ -199,4 +199,12 @@
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
-   limitations under the License.
\ No newline at end of file
+   limitations under the License.
+
+   ========================================================================
+   MIT Licenses
+   ========================================================================
+
+   The Apache TinkerPop project bundles the following components under the MIT 
License:
+
+        chai 5.0.0 (https://www.chaijs.com/) - for details, see licenses/chai
diff --git 
a/gremlin-javascript/src/main/javascript/gremlin-javascript/licenses/chai 
b/gremlin-javascript/src/main/javascript/gremlin-javascript/licenses/chai
new file mode 100644
index 0000000000..eedbe238ac
--- /dev/null
+++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/licenses/chai
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2017 Chai.js Assertion Library
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git 
a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/element-comparison.js
 
b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/element-comparison.js
new file mode 100644
index 0000000000..7f60f47396
--- /dev/null
+++ 
b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/element-comparison.js
@@ -0,0 +1,123 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+/*
+ * Portions of this code are based on the Chai Assertion Library
+ * at https://www.chaijs.com/, which is licensed under the MIT License.
+ * The functions deepMembersById, flag, and isSubsetOf are adapted from
+ * Chai's source code.
+ * See licenses/chai for full license.
+ */
+
+const chai = require('chai');
+const deepEqual = require('deep-eql');
+const { Edge, Vertex, VertexProperty } = require('../../lib/structure/graph');
+
+function isElement(obj) {
+    return obj instanceof Edge || obj instanceof Vertex || obj instanceof 
VertexProperty;
+}
+
+const opt = {comparator: compareElements};
+
+function isSubsetOf(subset, superset, cmp, contains, ordered) {
+    if (!contains) {
+        if (subset.length !== superset.length) return false;
+        superset = superset.slice();
+    }
+
+    return subset.every(function(elem, idx) {
+        if (ordered) return cmp ? cmp(elem, superset[idx], opt) : elem === 
superset[idx];
+
+        if (!cmp) {
+            var matchIdx = superset.indexOf(elem);
+            if (matchIdx === -1) return false;
+
+            // Remove match from superset so not counted twice if duplicate in 
subset.
+            if (!contains) superset.splice(matchIdx, 1);
+            return true;
+        }
+
+        return superset.some(function(elem2, matchIdx) {
+            if (!cmp(elem, elem2, opt)) return false;
+
+            // Remove match from superset so not counted twice if duplicate in 
subset.
+            if (!contains) superset.splice(matchIdx, 1);
+            return true;
+        });
+    });
+}
+
+function flag(obj, key, value) {
+    var flags = obj.__flags || (obj.__flags = Object.create(null));
+    if (arguments.length === 3) {
+        flags[key] = value;
+    } else {
+        return flags[key];
+    }
+};
+
+function deepMembersById (subset, msg) {
+    if (msg) flag(this, 'message', msg);
+    var obj = flag(this, 'object')
+        , flagMsg = flag(this, 'message')
+        , ssfi = flag(this, 'ssfi');
+
+    new chai.Assertion(obj, flagMsg, ssfi, true).to.be.an('array');
+    new chai.Assertion(subset, flagMsg, ssfi, true).to.be.an('array');
+
+    var contains = flag(this, 'contains');
+    var ordered = flag(this, 'ordered');
+
+    var subject, failMsg, failNegateMsg;
+
+    if (contains) {
+        subject = ordered ? 'an ordered superset' : 'a superset';
+        failMsg = 'expected #{this} to be ' + subject + ' of #{exp}';
+        failNegateMsg = 'expected #{this} to not be ' + subject + ' of #{exp}';
+    } else {
+        subject = ordered ? 'ordered members' : 'members';
+        failMsg = 'expected #{this} to have the same ' + subject + ' as 
#{exp}';
+        failNegateMsg = 'expected #{this} to not have the same ' + subject + ' 
as #{exp}';
+    }
+
+    var cmp = flag(this, 'deep') ? deepEqual : undefined;
+
+    this.assert(
+        isSubsetOf(subset, obj, cmp, contains, ordered)
+        , failMsg
+        , failNegateMsg
+        , subset
+        , obj
+        , true
+    );
+}
+
+function compareElements(a, b) {
+    if (!isElement(a) || !isElement(b)) {
+        return null;
+    } else {
+        return a.constructor === b.constructor && a.id === b.id;
+    }
+}
+
+module.exports = {
+    deepMembersById,
+    compareElements,
+    opt
+}
\ No newline at end of file
diff --git 
a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/feature-steps.js
 
b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/feature-steps.js
index 8794efc456..3e6ed1e525 100644
--- 
a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/feature-steps.js
+++ 
b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/feature-steps.js
@@ -41,7 +41,7 @@ const t = traversalModule.t;
 const P = traversalModule.P;
 const direction = traversalModule.direction;
 const merge = traversalModule.merge;
-
+const deepMembersById = require('./element-comparison').deepMembersById;
 const parsers = [
   [ 'str\\[(.*)\\]', (stringValue) => stringValue ], //returns the string 
value as is
   [ 'vp\\[(.+)\\]', toVertexProperty ],
@@ -64,6 +64,12 @@ const parsers = [
   [ 'M\\[(.+)\\]', toMerge ]
 ].map(x => [ new RegExp('^' + x[0] + '$'), x[1] ]);
 
+chai.use(function (chai, chaiUtils) {
+  chai.Assertion.overwriteMethod('members', function (_super) {
+    return deepMembersById;
+  });
+});
+
 const ignoreReason = {
   nullKeysInMapNotSupportedWell: "Javascript does not nicely support 'null' as 
a key in Map instances",
   setNotSupported: "There is no Set support in gremlin-javascript",
@@ -240,7 +246,7 @@ Then(/^the result should be (\w+)$/, function 
assertResult(characterizedAs, resu
       
expect(toCompare(this.result)).to.have.deep.ordered.members(expectedResult);
       break;
     case 'unordered':
-      expect(toCompare(this.result)).to.have.deep.members(expectedResult);
+      
expect(toCompare(this.result)).to.have.deep.members(toCompare(expectedResult));
       break;
     case 'of':
       // result is a subset of the expected
diff --git 
a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/element-comparison-test.js
 
b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/element-comparison-test.js
new file mode 100644
index 0000000000..fc459e57ef
--- /dev/null
+++ 
b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/element-comparison-test.js
@@ -0,0 +1,299 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+const chai = require('chai')
+const { expect } = require('chai');
+const { VertexProperty, Property, Vertex, Edge, Path } = 
require('../../lib/structure/graph');
+const { deepMembersById, compareElements, opt } = 
require('../cucumber/element-comparison');
+const deepEqual = require('deep-eql');
+
+chai.use(function (chai, chaiUtils) {
+  chai.Assertion.overwriteMethod('members', function (_super) {
+    return deepMembersById;
+  });
+});
+
+describe('primitives', function () {
+    it('should pass', function () {
+        expect(deepEqual(1, 1, opt)).to.be.true;
+        expect(deepEqual(false, false, opt)).to.be.true;
+        expect(deepEqual(null, null, opt)).to.be.true;
+    });
+
+    it('should fail', function () {
+        expect(deepEqual(1, 2, opt)).to.be.false;
+        expect(deepEqual(true, false, opt)).to.be.false;
+        expect(deepEqual(0, "0", opt)).to.be.false;
+        expect(deepEqual(0, false, opt)).to.be.false;
+        expect(deepEqual(0, null, opt)).to.be.false;
+        expect(deepEqual(false, null, opt)).to.be.false;
+    });
+});
+
+describe('elements', function () {
+    const v1 = new Vertex(1, "dog", undefined);
+    const v2 = new Vertex(1, "cat", undefined);
+    const v3 = new Vertex(2, "cat", undefined);
+
+    const e1 = new Edge(2, v1, "chases", v3, undefined);
+    const e2 = new Edge(3, v1, "chases", v3, undefined);
+    const e3 = new Edge(3, v2, "chases", v3, undefined);
+
+    const vp1 = new VertexProperty(3, "size", "small", undefined);
+    const vp2 = new VertexProperty(3, "size", "large", undefined);
+    const vp3 = new VertexProperty(4, "size", "large", undefined);
+
+    it('should pass with same id, different values', function () {
+        expect(deepEqual(v1, v2, opt)).to.be.true;
+        expect(deepEqual(e2, e3, opt)).to.be.true;
+        expect(deepEqual(vp1, vp2, opt)).to.be.true;
+    });
+
+    it('should fail with different id, same values', function () {
+        expect(deepEqual(v2, v3, opt)).to.be.false;
+        expect(deepEqual(e1, e2, opt)).to.be.false;
+        expect(deepEqual(vp2, vp3, opt)).to.be.false;
+    });
+
+    it('should fail with same id, different type', function () {
+        expect(deepEqual(v3, e1, opt)).to.be.false;
+        expect(deepEqual(e3, vp1, opt)).to.be.false;
+    });
+
+    describe('element arrays', function () {
+        const ea1 = [v1, e2, v3];
+        const ea2 = [v1, e2, v3];
+        const ea3 = [e2, v1, v3];
+        const ea4 = [e2, e2, v3];
+        const ea5 = [v1, e2, v3, vp1];
+
+        it('unordered', function () {
+            expect(ea1).to.have.deep.members(ea2);
+            expect(ea1).to.have.deep.members(ea3);
+            expect(ea1).to.not.have.deep.members(ea4);
+            expect(ea1).to.not.have.deep.members(ea5);
+            expect(ea5).to.not.have.deep.members(ea1);
+        });
+
+        it('ordered', function () {
+            expect(ea1).to.have.deep.ordered.members(ea2);
+            expect(ea1).to.not.have.deep.ordered.members(ea3);
+            expect(ea1).to.not.have.deep.ordered.members(ea4);
+            expect(ea1).to.not.have.deep.ordered.members(ea5);
+            expect(ea5).to.not.have.deep.ordered.members(ea1);
+        });
+
+        it('include', function () {
+            expect(ea1).to.include.deep.members(ea2);
+            expect(ea1).to.include.deep.members(ea3);
+            expect(ea1).to.include.deep.members(ea4);
+            expect(ea1).to.not.include.deep.members(ea5);
+
+            expect(ea2).to.include.deep.members(ea1);
+            expect(ea3).to.include.deep.members(ea1);
+            expect(ea4).to.not.include.deep.members(ea1);
+            expect(ea5).to.include.deep.members(ea1);
+        });
+    });
+});
+
+describe('property', function () {
+    const p1 = new Property("a", "1");
+    const p2 = new Property("a", "1");
+    const p3 = new Property("a", 1);
+    const p4 = new Property(1, 1);
+    const p5 = new Property(1, "a");
+
+    it('should pass only properties that match exactly', function () {
+        expect(deepEqual(p1, p2, opt)).to.be.true;
+        expect(deepEqual(p1, p3, opt)).to.be.false;
+        expect(deepEqual(p1, p4, opt)).to.be.false;
+        expect(deepEqual(p3, p5, opt)).to.be.false;
+    });
+});
+
+describe('arrays', function () {
+    const a1 = [1, 2, 3];
+    const a2 = [1, 2, 3];
+    const a3 = [2, 1, 3];
+    const a4 = [2, 2, 3];
+    const a5 = [1, 2, 3, 4];
+
+    it('unordered', function () {
+        expect(a1).to.have.deep.members(a2);
+        expect(a1).to.have.deep.members(a3);
+        expect(a1).to.not.have.deep.members(a4);
+        expect(a1).to.not.have.deep.members(a5);
+        expect(a5).to.not.have.deep.members(a1);
+    });
+
+    it('ordered', function () {
+        expect(a1).to.have.deep.ordered.members(a2);
+        expect(a1).to.not.have.deep.ordered.members(a3);
+        expect(a1).to.not.have.deep.ordered.members(a4);
+        expect(a1).to.not.have.deep.ordered.members(a5);
+        expect(a5).to.not.have.deep.ordered.members(a1);
+    });
+
+    const a6 = [1, 2, a1];
+    const a7 = [1, a3, 2];
+    const a8 = [1, 2, a5];
+
+    const a9 = [1, a1, a1];
+    const a10 = [1, a1, a3];
+
+    const a11 = [a9]
+    const a12 = [a10]
+
+    it('unordered nested', function () {
+        expect(a6).to.have.not.deep.members(a7); // nested arrays ordered 
differently don't pass as the same item
+        expect(a6).to.not.have.deep.members(a8);
+        expect(a9).to.not.have.deep.members(a10);
+        expect(a11).to.not.have.deep.members(a12);
+    });
+
+    it('ordered nested', function () {
+        expect(a6).to.have.deep.ordered.members(a6);
+        expect(a6).to.not.have.deep.ordered.members(a7);
+        expect(a6).to.not.have.deep.ordered.members(a8);
+        expect(a9).to.not.have.deep.ordered.members(a10);
+        expect(a10).to.have.deep.ordered.members(a10);
+        expect(a11).to.have.deep.ordered.members(a11);
+        expect(a11).to.not.have.deep.ordered.members(a12);
+    });
+});
+
+describe('map', function () {
+    const m1 = [new Map([
+        ['ripple', [new Vertex(5, 'software', undefined)]],
+        ['peter', [new Vertex(6, 'person', undefined)]],
+        ['vadas', [new Vertex(2, 'person', undefined)]]
+    ])];
+    const m2 = [new Map([
+        ['ripple', [new Vertex(5, 'software', undefined)]],
+        ['peter', [new Vertex(6, 'person', undefined)]]
+    ])];
+    const m3 = [new Map([
+        ['ripple', [new Vertex(5, 'software', undefined)]],
+        ['vadas', [new Vertex(2, 'person', undefined)]],
+        ['peter', [new Vertex(6, 'person', undefined)]]
+    ])];
+    const m4 = [new Map([
+        ['ripple', [new Vertex(5, 'software', undefined)]],
+        ['vadas', [new Vertex(6, 'person', undefined)]],
+        ['peter', [new Vertex(2, 'person', undefined)]]
+    ])];
+
+    it('unordered', function () {
+        expect(m1).to.have.deep.members(m1);
+        expect(m1).to.not.have.deep.members(m2);
+        expect(m1).to.have.deep.members(m3);
+        expect(m1).to.not.have.deep.members(m4);
+    });
+
+    it('ordered', function () {
+        expect(m1).to.have.deep.ordered.members(m1);
+        expect(m1).to.not.have.deep.ordered.members(m2);
+        expect(m1).to.have.deep.ordered.members(m3); // map entries can be 
unordered
+        expect(m1).to.not.have.deep.ordered.members(m4);
+    });
+});
+
+describe('objects', function () {
+    const obj1 = [{
+        k1: "v1",
+        k2: "v2",
+        k3: { k4: "v4", k5: "v5" }
+    }];
+    const obj2 = [{
+        k1: "v1",
+        k2: "v2",
+        k3: { k4: "v4", k5: "v5" }
+    }];
+    const obj3 = [{
+        k2: "v2",
+        k1: "v1",
+        k3: { k5: "v5", k4: "v4" }
+    }];
+    const obj4 = [{
+        k1: "v1",
+        k2: "v2",
+        k3: { k4: "v4" }
+    }];
+    const obj5 = [{
+        k1: "v1",
+        k2: "v2",
+        k3: { k4: "v5", k5: "v4" }
+    }];
+
+    it('should pass', function () {
+        expect(obj1).to.have.deep.members(obj2); // identical
+        expect(obj1).to.have.deep.members(obj3); // order swapped
+    });
+
+    it('should fail', function () {
+        expect(obj1).to.not.have.deep.members(obj4); // missing nested kvp
+        expect(obj1).to.not.have.deep.members(obj5); // values swapped
+    });
+
+    const obj6 = [{
+        k1: "v1",
+        k2: "v2",
+        k3: [ 1, 2, 3 ]
+    }];
+    const obj7 = [{
+        k3: [ 1, 2, 3 ],
+        k1: "v1",
+        k2: "v2"
+    }];
+    const obj8 = [{
+        k1: "v1",
+        k2: "v2",
+        k3: [ 2, 1, 3 ]
+    }];
+
+    it('unordered', function () {
+        expect(obj6).to.have.deep.members(obj7);
+        expect(obj6).to.not.have.deep.members(obj8);
+    });
+
+    it('ordered', function () {
+        expect(obj6).to.have.deep.ordered.members(obj7); // kvp can be 
unordered
+        expect(obj6).to.not.have.deep.ordered.members(obj8); // array must be 
ordered
+    });
+
+    const path1 = new Path(["path"], [new Vertex(1, 'person', undefined),
+                                      new Vertex(2, 'person', undefined)]);
+    const path2 = new Path(["path"], [new Vertex(1, 'person', undefined)]);
+    const path3 = new Path(["path"], [new Vertex(2, 'person', undefined),
+                                      new Vertex(1, 'person', undefined)]);
+
+    it('unordered', function () {
+        expect(deepEqual(path1, path1)).to.be.true;
+        expect(deepEqual(path1, path2)).to.be.false;
+        expect(deepEqual(path1, path3)).to.be.false;
+    });
+
+    it('ordered', function () {
+        expect(deepEqual(path1, path1)).to.be.true;
+        expect(deepEqual(path1, path2)).to.be.false;
+        expect(deepEqual(path1, path3)).to.be.false;
+    });
+
+});

Reply via email to