-
Notifications
You must be signed in to change notification settings - Fork 814
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TINKERPOP-2872] Change JS tests to compare elements by ID for consis…
…tency 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.
- Loading branch information
Showing
7 changed files
with
463 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
gremlin-javascript/src/main/javascript/gremlin-javascript/licenses/chai
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
123 changes: 123 additions & 0 deletions
123
...lin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/element-comparison.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.