From a781bc63e10e8a294961abb720d638641d457305 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Thu, 25 Jun 2015 20:21:38 -0400 Subject: [PATCH] add tests for json parse/stringify of bigints --- package.json | 6 ++++-- test/json.js | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 test/json.js diff --git a/package.json b/package.json index 4ad9b22..1842846 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Datadog API Node.JS Client", "main": "lib/index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", + "test": "./node_modules/.bin/mocha ./test", "docs": "node ./docs/create.js > index.html" }, "bin": { @@ -38,9 +38,11 @@ "rc": "^1.0.0" }, "devDependencies": { + "bignumber.js": "^2.0.7", "docast": "^0.1.1", "glob": "^5.0.3", "js-yaml": "^3.2.7", - "marked": "^0.3.3" + "marked": "^0.3.3", + "mocha": "^2.2.5" } } diff --git a/test/json.js b/test/json.js new file mode 100644 index 0000000..a1e2dff --- /dev/null +++ b/test/json.js @@ -0,0 +1,26 @@ +var assert = require("assert"); +var BigNumber = require("bignumber.js"); +var json = require("../lib/json"); + +describe("json", function(){ + describe("#parse()", function(){ + it("should properly parse big integers", function(){ + // DEV: This test case is from: https://github.com/brettlangdon/node-dogapi/issues/16 + var data = "{\"id\": 2868860079149422351}"; + var parsed = json.parse(data); + // `parsed.id` is an instance of `BigNumber` + assert.equal(parsed.id.toString(), "2868860079149422351"); + }); + }); + + describe("#stringify()", function(){ + it("should properly parse big integers", function(){ + // DEV: This test case is from: https://github.com/brettlangdon/node-dogapi/issues/16 + var data = {"id": new BigNumber('2868860079149422351')}; + var stringified = json.stringify(data); + // Yeah, it ends up being a string and not an int, but mostly we + // want to make sure it doesn't throw an error or provide the wrong number + assert.equal(stringified, "{\"id\":\"2868860079149422351\"}"); + }); + }); +});