Skip to content
This repository has been archived by the owner on May 10, 2020. It is now read-only.

Commit

Permalink
Add tests for JSON converters
Browse files Browse the repository at this point in the history
  • Loading branch information
slowli committed Oct 1, 2017
1 parent cc885e5 commit d58cb86
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/jsonConverters.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function convertTreeJSON (json) {
}

export function convertMapJSON (json) {
if (typeof json !== 'object') {
if (!json || typeof json !== 'object') {
throw new TypeError('Invalid JSON for MapView; object expected')
}

Expand Down
150 changes: 150 additions & 0 deletions test/jsonConverters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/* eslint-env mocha */

import chai from 'chai'
import dirtyChai from 'dirty-chai'

import { convertListJSON, convertMapJSON } from '../src/jsonConverters'

const expect = chai
.use(dirtyChai)
.expect

describe('convertListJSON', () => {
it('should add tags to JSON', () => {
const json = {
left: '0000000000000000000000000000000000000000000000000000000000000000',
right: {
left: {
left: '0000000000000000000000000000000000000000000000000000000000000000',
right: { val: [1, 2, 3] }
},
right: {
left: { val: [4, 5, 6] }
}
}
}

expect(convertListJSON(json)).to.deep.equal({
branch: {
left: { hash: '0000000000000000000000000000000000000000000000000000000000000000' },
right: {
branch: {
left: {
branch: {
left: { hash: '0000000000000000000000000000000000000000000000000000000000000000' },
right: { val: [1, 2, 3] }
}
},
right: {
stub: { val: [4, 5, 6] }
}
}
}
}
})
})

it('should throw on unparseable JSON', () => {
const json = {
right: {
left: {
left: '0000000000000000000000000000000000000000000000000000000000000000',
right: { val: [1, 2, 3] }
},
right: {
left: { val: [4, 5, 6] }
}
}
}

expect(() => convertListJSON(json)).to.throw('Invalid list JSON')
})
})

describe('convertMapJSON', () => {
it('should throw if supplied with non-object', () => {
expect(() => convertMapJSON(null)).to.throw('Invalid JSON for MapView; object expected')
expect(() => convertMapJSON(undefined)).to.throw('Invalid JSON for MapView; object expected')
expect(() => convertMapJSON(5)).to.throw('Invalid JSON for MapView; object expected')
expect(() => convertMapJSON('map')).to.throw('Invalid JSON for MapView; object expected')
expect(() => convertMapJSON(true)).to.throw('Invalid JSON for MapView; object expected')
})

it('should convert empty map', () => {
expect(convertMapJSON({})).to.be.null()
})

it('should convert map with single element', () => {
const json = {
'1000101': { val: 'abcdef' }
}

expect(convertMapJSON(json)).to.deep.equal({
key: '1000101',
value: { val: 'abcdef' }
})
})

it('should convert simple branching JSON', () => {
const json = {
'0': { val: [1, 2, 3] },
'1': {
'0': { val: [4, 5, 6] },
'111': '0000000000000000000000000000000000000000000000000000000000000000'
}
}

expect(convertMapJSON(json)).to.deep.equal({
branch: {
leftKey: '0',
rightKey: '1',
left: { val: [1, 2, 3] },
right: {
branch: {
leftKey: '0',
rightKey: '111',
left: { val: [4, 5, 6] },
right: { hash: '0000000000000000000000000000000000000000000000000000000000000000' }
}
}
}
})
})

it('should throw if root node has greater number of properties than expected', () => {
const json = {
'0': { val: [1, 2, 3] },
'1': {
'0': { val: [4, 5, 6] },
'111': '0000000000000000000000000000000000000000000000000000000000000000'
},
'111': { val: [7, 8, 9] }
}

expect(() => convertMapJSON(json)).to.throw('object with <=2 properties expected')
})

it('should throw if intermediate node has extra properties', () => {
const json = {
'0': { val: [1, 2, 3] },
'1': {
'0': { val: [4, 5, 6] },
'111': '0000000000000000000000000000000000000000000000000000000000000000',
'1101': { val: [7, 8, 9] }
}
}

expect(() => convertMapJSON(json)).to.throw('Invalid proof node')
})

it('should throw if intermediate node has insuffiecient properties', () => {
const json = {
'0': { val: [1, 2, 3] },
'1': {
'0': { val: [4, 5, 6] }
}
}

expect(() => convertMapJSON(json)).to.throw('Invalid proof node')
})
})

0 comments on commit d58cb86

Please sign in to comment.