This is an automated email from the ASF dual-hosted git repository. jorgebg pushed a commit to branch TINKERPOP-2332 in repository https://gitbox.apache.org/repos/asf/tinkerpop.git
commit 2c60aa1ce10b36b55f1a181b3d9ff4f85a6a32f1 Author: Jorge Bay Gondra <jorgebaygon...@gmail.com> AuthorDate: Mon Jan 27 10:23:03 2020 +0100 TINKERPOP-2332 JavaScript GLV: Fix structure toString() methods --- .../gremlin-javascript/lib/structure/graph.js | 34 ++++++--- .../test/unit/structure-types-test.js | 87 ++++++++++++++++++++++ 2 files changed, 110 insertions(+), 11 deletions(-) diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/graph.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/graph.js index 98d9c77..5019a9c 100644 --- a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/graph.js +++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/graph.js @@ -65,7 +65,7 @@ class Vertex extends Element { } toString() { - return 'v[' + this.id + ']'; + return `v[${this.id}]`; } } @@ -85,7 +85,10 @@ class Edge extends Element { } toString() { - return 'e[' + this.id + '][' + this.outV.id + '-' + this.label + '->' + this.inV.id + ']'; + const outVId = this.outV ? this.outV.id : '?'; + const inVId = this.inV ? this.inV.id : '?'; + + return `e[${this.id}][${outVId}-${this.label}->${inVId}]`; } } @@ -98,7 +101,7 @@ class VertexProperty extends Element { } toString() { - return 'vp[' + this.label + '->' + this.value.substr(0, 20) + ']'; + return `vp[${this.label}->${summarize(this.value)}]`; } } @@ -109,7 +112,7 @@ class Property { } toString() { - return 'p[' + this.key + '->' + this.value.substr(0, 20) + ']'; + return `p[${this.key}->${summarize(this.value)}]`; } equals(other) { @@ -130,7 +133,7 @@ class Path { } toString() { - return 'path[' + this.objects.join(", ") + ']'; + return `path[${(this.objects || []).join(", ")}]`; } equals(other) { @@ -165,11 +168,20 @@ function areEqual(obj1, obj2) { return false; } +function summarize(value) { + if (value === null || value === undefined) { + return value; + } + + const strValue = value.toString(); + return strValue.length > 20 ? strValue.substr(0, 20) : strValue; +} + module.exports = { - Edge: Edge, - Graph: Graph, - Path: Path, - Property: Property, - Vertex: Vertex, - VertexProperty: VertexProperty + Edge, + Graph, + Path, + Property, + Vertex, + VertexProperty }; \ No newline at end of file diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/structure-types-test.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/structure-types-test.js new file mode 100644 index 0000000..29bebfa --- /dev/null +++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/structure-types-test.js @@ -0,0 +1,87 @@ +/* + * 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. + */ + +/** + * @author Jorge Bay Gondra + */ +'use strict'; + +const { assert } = require('chai'); +const { VertexProperty, Property, Vertex, Edge, Path } = require('../../lib/structure/graph'); + +describe('Edge', () => { + describe('#toString()', () => { + it('should support empty outV and inV', () => { + const element = new Edge('123', null, 'label1', undefined, null); + assert.strictEqual(element.toString(), `e[123][?-label1->?]`); + }); + }); +}); + +describe('Vertex', () => { + describe('#toString()', () => { + it('should return the string representation based on the id', () => { + const element = new Vertex(-200, 'label1', null); + assert.strictEqual(element.toString(), `v[-200]`); + }); + }); +}); + +describe('VertexProperty', () => { + describe('#toString()', () => { + it('should return the string representation and summarize', () => { + [ + [ new VertexProperty(1, 'label1', 'value1'), 'vp[label1->value1]' ], + [ new VertexProperty(1, 'label2', null), 'vp[label2->null]' ], + [ new VertexProperty(1, 'label3', undefined), 'vp[label3->undefined]' ], + [ new VertexProperty(1, 'label4', 'abcdefghijklmnopqrstuvwxyz'), 'vp[label4->abcdefghijklmnopqrst]' ] + ].forEach(item => { + assert.strictEqual(item[0].toString(), item[1]); + }); + }); + }); +}); + +describe('Property', () => { + describe('#toString()', () => { + it('should return the string representation and summarize', () => { + [ + [ new Property('key1', 'value1'), 'p[key1->value1]' ], + [ new Property('key2', null), 'p[key2->null]' ], + [ new Property('key3', undefined), 'p[key3->undefined]' ], + [ new Property('key4', 'abcdefghijklmnopqrstuvwxyz'), 'p[key4->abcdefghijklmnopqrst]' ] + ].forEach(item => { + assert.strictEqual(item[0].toString(), item[1]); + }); + }); + }); +}); + +describe('Path', () => { + describe('#toString()', () => { + it('should return the string representation and summarize', () => { + [ + [ new Path(['a', 'b'], [1, 2]), 'path[1, 2]' ], + [ new Path(['a', 'b'], null), 'path[]' ] + ].forEach(item => { + assert.strictEqual(item[0].toString(), item[1]); + }); + }); + }); +}); \ No newline at end of file