This repository has been archived by the owner on May 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |