You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2018/10/03 09:13:01 UTC

[1/2] tinkerpop git commit: TINKERPOP-2055 Added special number handling in javascript

Repository: tinkerpop
Updated Branches:
  refs/heads/TINKERPOP-2055 854914e6e -> b54202782


TINKERPOP-2055 Added special number handling in javascript


Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/2d3041f2
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/2d3041f2
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/2d3041f2

Branch: refs/heads/TINKERPOP-2055
Commit: 2d3041f226310379c966214461c79cf47432f4c9
Parents: 854914e
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Wed Oct 3 04:33:40 2018 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Wed Oct 3 04:33:40 2018 -0400

----------------------------------------------------------------------
 .../lib/structure/io/type-serializers.js        | 30 ++++++++++++++-
 .../test/unit/graphson-test.js                  | 39 ++++++++++++++++++++
 2 files changed, 67 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/2d3041f2/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/type-serializers.js
----------------------------------------------------------------------
diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/type-serializers.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/type-serializers.js
index fdf049f..5583c47 100644
--- a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/type-serializers.js
+++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/type-serializers.js
@@ -49,11 +49,37 @@ class TypeSerializer {
 
 class NumberSerializer extends TypeSerializer {
   serialize(item) {
-    return item;
+    if (isNaN(item)) {
+      return {
+        [typeKey]: 'g:Double',
+        [valueKey]: 'NaN'
+      };
+    } else if (item === Number.POSITIVE_INFINITY) {
+      return {
+        [typeKey]: 'g:Double',
+        [valueKey]: 'Infinity'
+      };
+    } else if (item === Number.NEGATIVE_INFINITY) {
+      return {
+        [typeKey]: 'g:Double',
+        [valueKey]: '-Infinity'
+      };
+    } else {
+      return item;
+    }
   }
 
   deserialize(obj) {
-    return parseFloat(obj[valueKey]);
+    var val = obj[valueKey];
+    if (val === 'NaN') {
+      return NaN;
+    } else if (val === 'Infinity') {
+      return Number.POSITIVE_INFINITY;
+    } else if (val === '-Infinity') {
+      return Number.NEGATIVE_INFINITY;
+    } else {
+      return parseFloat(val);
+    }
   }
 
   canBeUsedFor(value) {

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/2d3041f2/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/graphson-test.js
----------------------------------------------------------------------
diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/graphson-test.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/graphson-test.js
index b459407..a1ac0d6 100644
--- a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/graphson-test.js
+++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/graphson-test.js
@@ -46,6 +46,30 @@ describe('GraphSONReader', function () {
       assert.strictEqual(result, item[1]);
     });
   });
+  it('should parse GraphSON Nan from GraphSON', function () {
+      const reader = new GraphSONReader();
+      var result = reader.read({
+                "@type": "g:Double",
+                "@value": "NaN"
+              });
+      assert.ok(isNaN(result));
+  });
+  it('should parse GraphSON -Infinity and Nan from GraphSON', function () {
+      const reader = new GraphSONReader();
+      var result = reader.read({
+                "@type": "g:Double",
+                "@value": "-Infinity"
+              });
+      assert.strictEqual(result, Number.NEGATIVE_INFINITY);
+  });
+  it('should parse GraphSON Infinity and Nan from GraphSON', function () {
+      const reader = new GraphSONReader();
+      var result = reader.read({
+                "@type": "g:Double",
+                "@value": "Infinity"
+              });
+      assert.strictEqual(result, Number.POSITIVE_INFINITY);
+  });
   it('should parse Date', function() {
     const obj = { "@type" : "g:Date", "@value" : 1481750076295 };
     const reader = new GraphSONReader();
@@ -102,6 +126,21 @@ describe('GraphSONWriter', function () {
     const writer = new GraphSONWriter();
     assert.strictEqual(writer.write(2), '2');
   });
+  it('should write NaN', function () {
+    const writer = new GraphSONWriter();
+    const expected = JSON.stringify({ "@type" : "g:Double", "@value" : "NaN" });
+    assert.strictEqual(writer.write(NaN), expected);
+  });
+  it('should write Infinity', function () {
+    const writer = new GraphSONWriter();
+    const expected = JSON.stringify({ "@type" : "g:Double", "@value" : "Infinity" });
+    assert.strictEqual(writer.write(Number.POSITIVE_INFINITY), expected);
+  });
+  it('should write -Infinity', function () {
+    const writer = new GraphSONWriter();
+    const expected = JSON.stringify({ "@type" : "g:Double", "@value" : "-Infinity" });
+    assert.strictEqual(writer.write(Number.NEGATIVE_INFINITY), expected);
+  });
   it('should write Date', function() {
     const writer = new GraphSONWriter();
     const expected = JSON.stringify({ "@type" : "g:Date", "@value" : 1481750076295 });


[2/2] tinkerpop git commit: TINKERPOP-2055 Added tests for .Net on special numbers

Posted by sp...@apache.org.
TINKERPOP-2055 Added tests for .Net on special numbers


Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/b5420278
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/b5420278
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/b5420278

Branch: refs/heads/TINKERPOP-2055
Commit: b542027825fe905c0c46b81a00fe7dfd5275e8c6
Parents: 2d3041f
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Wed Oct 3 05:11:56 2018 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Wed Oct 3 05:11:56 2018 -0400

----------------------------------------------------------------------
 .../IO/GraphSON/GraphSONReaderTests.cs          | 38 ++++++++++++++++++--
 .../IO/GraphSON/GraphSONWriterTests.cs          | 30 ++++++++++++++++
 2 files changed, 66 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/b5420278/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs b/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs
index 74bf385..08a91ae 100644
--- a/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs
+++ b/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs
@@ -24,11 +24,9 @@
 using System;
 using System.Collections.Generic;
 using System.Numerics;
-using Gremlin.Net.Process.Traversal;
 using Gremlin.Net.Structure;
 using Gremlin.Net.Structure.IO.GraphSON;
 using Moq;
-using Newtonsoft.Json;
 using Newtonsoft.Json.Linq;
 using Xunit;
 
@@ -178,6 +176,42 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
         }
 
         [Fact]
+        public void ShouldDeserializeNaN()
+        {
+            var serializedValue = "{\"@type\":\"g:Double\",\"@value\":'NaN'}";
+            var reader = CreateStandardGraphSONReader();
+
+            var jObject = JObject.Parse(serializedValue);
+            var deserializedValue = reader.ToObject(jObject);
+
+            Assert.Equal(Double.NaN, deserializedValue);
+        }
+
+        [Fact]
+        public void ShouldDeserializePositiveInfinity()
+        {
+            var serializedValue = "{\"@type\":\"g:Double\",\"@value\":'Infinity'}";
+            var reader = CreateStandardGraphSONReader();
+
+            var jObject = JObject.Parse(serializedValue);
+            var deserializedValue = reader.ToObject(jObject);
+
+            Assert.Equal(Double.PositiveInfinity, deserializedValue);
+        }
+
+        [Fact]
+        public void ShouldDeserializeNegativeInfinity()
+        {
+            var serializedValue = "{\"@type\":\"g:Double\",\"@value\":'-Infinity'}";
+            var reader = CreateStandardGraphSONReader();
+
+            var jObject = JObject.Parse(serializedValue);
+            var deserializedValue = reader.ToObject(jObject);
+
+            Assert.Equal(Double.NegativeInfinity, deserializedValue);
+        }
+
+        [Fact]
         public void ShouldDeserializeDecimal()
         {
             var serializedValue = "{\"@type\":\"gx:BigDecimal\",\"@value\":-8.201}";

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/b5420278/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONWriterTests.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONWriterTests.cs b/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONWriterTests.cs
index a544fb3..13fbddc 100644
--- a/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONWriterTests.cs
+++ b/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONWriterTests.cs
@@ -81,6 +81,36 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
         }
 
         [Fact]
+        public void ShouldSerializeNaN()
+        {
+            var writer = CreateStandardGraphSONWriter();
+
+            var graphSon = writer.WriteObject(Double.NaN);
+
+            Assert.Equal("{\"@type\":\"g:Double\",\"@value\":\"NaN\"}", graphSon);
+        }
+
+        [Fact]
+        public void ShouldSerializePositiveInfinity()
+        {
+            var writer = CreateStandardGraphSONWriter();
+
+            var graphSon = writer.WriteObject(Double.PositiveInfinity);
+
+            Assert.Equal("{\"@type\":\"g:Double\",\"@value\":\"Infinity\"}", graphSon);
+        }
+
+        [Fact]
+        public void ShouldSerializeNegativeInfinity()
+        {
+            var writer = CreateStandardGraphSONWriter();
+
+            var graphSon = writer.WriteObject(Double.NegativeInfinity);
+
+            Assert.Equal("{\"@type\":\"g:Double\",\"@value\":\"-Infinity\"}", graphSon);
+        }
+
+        [Fact]
         public void ShouldSerializeDecimal()
         {
             var writer = CreateStandardGraphSONWriter();