diff --git a/.gitignore b/.gitignore index d8427d2..d754d5c 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,5 @@ logs results npm-debug.log + +node_modules/**/* \ No newline at end of file diff --git a/js/rison.spec.js b/js/rison.spec.js new file mode 100644 index 0000000..74585d1 --- /dev/null +++ b/js/rison.spec.js @@ -0,0 +1,82 @@ +var rison = require('./rison'); +var chai = require('chai'); +var expect = chai.expect; + + + +describe('Rison', function() { + + it('Should do what the README says it does', function() { + + var encoded = rison.encode({any: "json", yes:true}); + + expect(encoded).to.equal(`(any:json,yes:!t)`); + + var decoded = `(any:json,yes:!t)`; + + var decodedValue = rison.decode(decoded); + + expect(decodedValue).to.deep.equal({any:'json', yes:true}); + }); + + it('Should handle deeply nested objects', function() { + var deeplyNested = { + A: { + B: { + C: { + D: 'E', + F: 'G' + } + }, + H: { + I: { + J:'K', + L:'M' + } + } + } + }; + + var encoded = rison.encode(deeplyNested); + + expect(encoded).to.equal(`(A:(B:(C:(D:E,F:G)),H:(I:(J:K,L:M))))`); + + var serializedDeeplyNested = `(A:(B:(C:(D:E,F:G)),H:(I:(J:K,L:M))))`; + + var deserializedDeeplyNested = rison.decode(serializedDeeplyNested); + + expect(deserializedDeeplyNested).to.deep.equal(deeplyNested); + }) +}) + +describe('O-Rison', function() { + + it('Should do what the README says it does', function() { + + var encoded = rison.encode_object({supportsObjects: true, ints: 435}); + + expect(encoded).to.equal(`ints:435,supportsObjects:!t`); + + var decoded = `ints:435,supportsObjects:!t`; + + var decodedValue = rison.decode_object(decoded); + + expect(decodedValue).to.deep.equal({supportsObjects: true, ints: 435}); + }); +}) + +describe('A-Rison', function() { + + it('Should do what the README says it does', function() { + + var encoded = rison.encode_array(['A', 'B', {supportsObjects: true}]); + + expect(encoded).to.equal(`A,B,(supportsObjects:!t)`); + + var decoded = `A,B,(supportsObjects:!t)`; + + var decodedValue = rison.decode_array(decoded); + + expect(decodedValue).to.deep.equal(['A', 'B', {supportsObjects:true}]); + }); +}) diff --git a/package.json b/package.json index 1c8ed8e..0a730e1 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,19 @@ - { - "name": "rison", - "version": "0.1.2", - "author": "Metaweb Technologies ", - "description": "URI friendly encoding for JSON structures", - "license": "MIT", + "name": "rison", + "version": "0.1.2", + "scripts": { + "test": "mocha js/rison.spec.js" + }, + "author": "Metaweb Technologies ", + "description": "URI friendly encoding for JSON structures", + "license": "MIT", "repository": { - "type": "git", - "url": "https://github.com/Nanonid/rison.git" + "type": "git", + "url": "https://github.com/Nanonid/rison.git" }, - "main": "js/rison.js" + "main": "js/rison.js", + "devDependencies": { + "chai": "^3.5.0", + "mocha": "^3.2.0" + } }