-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: CLValueMap deserialization from bytes, added unit tests for Tupl…
…e, Map, Numeric CLValues
- Loading branch information
1 parent
28e794d
commit 975c9d1
Showing
8 changed files
with
386 additions
and
43 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,86 @@ | ||
import { expect } from 'chai'; | ||
|
||
import { CLValueMap } from './Map'; | ||
import { CLTypeBool, CLTypeInt32, CLTypeMap, CLTypeString } from './cltype'; | ||
import { CLValueBool } from './Bool'; | ||
import { CLValueString } from './String'; | ||
import { CLValueInt32 } from './Numeric'; | ||
import { CLValueParser } from './Parser'; | ||
|
||
describe('CLValue CLMap implementation', () => { | ||
it('Maps should return proper clType', () => { | ||
const mapType = new CLTypeMap(CLTypeBool, CLTypeBool); | ||
const testMap = new CLValueMap(mapType); | ||
testMap.append( | ||
CLValueBool.newCLValueBool(true), | ||
CLValueBool.newCLValueBool(false) | ||
); | ||
|
||
expect(testMap.toString()).to.be.eq('(true="false")'); | ||
}); | ||
|
||
it('Should be able to create Map with proper values - correct by construction', () => { | ||
const myKey = CLValueString.newCLString('ABC'); | ||
const myVal = CLValueInt32.newCLInt32(123); | ||
const mapType = new CLTypeMap(CLTypeString, CLTypeInt32); | ||
const testMap = new CLValueMap(mapType); | ||
testMap.append(myKey, myVal); | ||
|
||
expect(testMap).to.be.an.instanceof(CLValueMap); | ||
}); | ||
|
||
it('Should be able to return proper values by calling .get() on Map', () => { | ||
const myKey = CLValueString.newCLString('ABC'); | ||
const myVal = CLValueInt32.newCLInt32(10); | ||
const mapType = new CLTypeMap(CLTypeString, CLTypeInt32); | ||
const testMap = new CLValueMap(mapType); | ||
testMap.append(myKey, myVal); | ||
|
||
expect(testMap.get('ABC')).to.be.deep.eq(myVal); | ||
}); | ||
|
||
it('Get() should return undefined on non-existing key', () => { | ||
const mapType = new CLTypeMap(CLTypeString, CLTypeInt32); | ||
const testMap = new CLValueMap(mapType); | ||
|
||
expect(testMap.get('DEF')).to.be.deep.eq(undefined); | ||
}); | ||
|
||
it('Should able to create empty Map by providing type', () => { | ||
const mapType = new CLTypeMap(CLTypeString, CLTypeString); | ||
const testMap = new CLValueMap(mapType); | ||
const len = testMap.length(); | ||
|
||
expect(len).to.equal(0); | ||
}); | ||
|
||
it('fromBytes() / toBytes()', () => { | ||
const myKey = CLValueString.newCLString('ABC'); | ||
const myVal = CLValueInt32.newCLInt32(10); | ||
const clValueMap = CLValueMap.newCLMap(CLTypeString, CLTypeInt32); | ||
clValueMap.map?.append(myKey, myVal); | ||
|
||
const bytes = clValueMap.bytes(); | ||
const fromBytes = CLValueParser.fromBytesByType(bytes, clValueMap.type) | ||
.result; | ||
|
||
expect(fromBytes).to.be.deep.eq(clValueMap); | ||
}); | ||
|
||
it('fromJSON() / toJSON()', () => { | ||
const myKey = CLValueString.newCLString('ABC'); | ||
const myVal = CLValueInt32.newCLInt32(10); | ||
const clValueMap = CLValueMap.newCLMap(CLTypeString, CLTypeInt32); | ||
clValueMap.map?.append(myKey, myVal); | ||
|
||
const json = CLValueParser.toJSON(clValueMap); | ||
const expectedJson = JSON.parse( | ||
'{"bytes":"01000000030000004142430a000000","cl_type":{"Map":{"key":"String","value":"I32"}}}' | ||
); | ||
|
||
const fromJson = CLValueParser.fromJSON(expectedJson); | ||
|
||
expect(fromJson).to.be.deep.eq(clValueMap); | ||
expect(json).to.be.deep.eq(expectedJson); | ||
}); | ||
}); |
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
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,98 @@ | ||
import { expect } from 'chai'; | ||
import { CLValueUInt32 } from './Uint32'; | ||
import { CLValueUInt128 } from './Uint128'; | ||
import { CLValueParser } from '../Parser'; | ||
import { | ||
CLTypeInt64, | ||
CLTypeUInt32, | ||
CLTypeUInt64, | ||
CLTypeUInt8 | ||
} from '../cltype'; | ||
import { CLValueUInt64 } from './Uint64'; | ||
import { CLValueUInt8 } from './Uint8'; | ||
import { CLValueInt64 } from './Int64'; | ||
|
||
const MAX_I64 = '9223372036854775807'; | ||
const MAX_U8 = 255; | ||
const MAX_U32 = 4294967295; | ||
const MAX_U64 = '18446744073709551615'; | ||
|
||
describe('Numeric implementation tests', () => { | ||
it('Numeric value() should return proper value', () => { | ||
const num = new CLValueUInt32(10); | ||
expect(num.toNumber()).to.be.eq(10); | ||
}); | ||
|
||
it('Numeric clType() should return proper type', () => { | ||
const num = CLValueUInt128.newCLUInt128(20000); | ||
expect(num.getType().toString()).to.be.eq('U128'); | ||
}); | ||
|
||
it('CLI32 do proper toBytes()/fromBytes()', () => { | ||
const num1 = CLValueUInt32.newCLUInt32(10); | ||
const num1bytes = num1.bytes(); | ||
|
||
const num2 = CLValueUInt32.newCLUInt32(1); | ||
const num2bytes = num2.bytes(); | ||
|
||
expect( | ||
CLValueParser.fromBytesByType(num1bytes, CLTypeUInt32).result | ||
).to.be.deep.eq(num1); | ||
expect( | ||
CLValueParser.fromBytesByType(num2bytes, CLTypeUInt32).result | ||
).to.be.deep.eq(num2); | ||
}); | ||
|
||
it('CLI64 do proper toBytes()/fromBytes()', () => { | ||
const num1 = CLValueInt64.newCLInt64(10); | ||
const num1bytes = num1.bytes(); | ||
|
||
const num2 = CLValueInt64.newCLInt64(MAX_I64); | ||
const num2bytes = num2.bytes(); | ||
|
||
expect( | ||
CLValueParser.fromBytesByType(num1bytes, CLTypeInt64).result | ||
).to.be.deep.eq(num1); | ||
expect( | ||
CLValueParser.fromBytesByType(num2bytes, CLTypeInt64).result | ||
).to.be.deep.eq(num2); | ||
}); | ||
|
||
it('CLU8 do proper toBytes()/fromBytes()', () => { | ||
const num1 = CLValueUInt8.newCLUint8(MAX_U8); | ||
const num1bytes = num1.bytes(); | ||
|
||
expect( | ||
CLValueParser.fromBytesByType(num1bytes, CLTypeUInt8).result | ||
).to.be.deep.eq(num1); | ||
}); | ||
|
||
it('CLU32 do proper toBytes()/fromBytes()', () => { | ||
const num1 = CLValueUInt32.newCLUInt32(MAX_U32); | ||
const num1bytes = num1.bytes(); | ||
|
||
expect( | ||
CLValueParser.fromBytesByType(num1bytes, CLTypeUInt32).result | ||
).to.be.deep.eq(num1); | ||
}); | ||
|
||
it('CLU64 do proper toBytes()/fromBytes()', () => { | ||
const num1 = CLValueUInt64.newCLUint64(MAX_U64); | ||
const num1bytes = num1.bytes(); | ||
|
||
expect( | ||
CLValueParser.fromBytesByType(num1bytes, CLTypeUInt64).result | ||
).to.be.deep.eq(num1); | ||
}); | ||
|
||
it('CLU64 toJSON() / fromJSON()', () => { | ||
const num1 = CLValueUInt64.newCLUint64(MAX_U64); | ||
const num1JSON = CLValueParser.toJSON(num1); | ||
const expectedJson = JSON.parse( | ||
'{"bytes":"ffffffffffffffff","cl_type":"U64"}' | ||
); | ||
|
||
expect(num1JSON).to.be.deep.eq(expectedJson); | ||
expect(CLValueParser.fromJSON(expectedJson)).to.be.deep.eq(num1); | ||
}); | ||
}); |
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
Oops, something went wrong.