This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 463
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #629 from EOSIO/accept-1-and-0
Accept number equal to 1 or 0 while checking data type of bool.
- Loading branch information
Showing
2 changed files
with
84 additions
and
2 deletions.
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,82 @@ | ||
import { ec } from 'elliptic'; | ||
|
||
import { createInitialTypes, Type, SerialBuffer } from '../eosjs-serialize'; | ||
|
||
describe('Serialize', () => { | ||
let types: Map<string, Type>; | ||
|
||
beforeAll(() => { | ||
types = createInitialTypes(); | ||
}); | ||
|
||
it('should be able to createInitialTypes', () => { | ||
expect(types).toBeTruthy(); | ||
}); | ||
|
||
describe('bool', () => { | ||
let boolType: Type; | ||
let mockedBuffer: SerialBuffer; | ||
|
||
const shouldThrowErrorForValue = (value: any) => { | ||
try { | ||
boolType.serialize(mockedBuffer, value); | ||
} catch (e) { | ||
expect(e.message).toBe('Expected boolean or number equal to 1 or 0'); | ||
} | ||
}; | ||
|
||
const shouldNotThrowErrorForValue = (value: any) => { | ||
expect(() => { | ||
boolType.serialize(mockedBuffer, value); | ||
}).not.toThrow(); | ||
}; | ||
|
||
beforeAll(() => { | ||
boolType = types.get('bool'); | ||
mockedBuffer = Object.create(SerialBuffer); | ||
mockedBuffer.push = jest.fn().mockImplementation((value) => { | ||
return; | ||
}); | ||
}); | ||
|
||
it('should be able to create bool type', () => { | ||
expect(boolType).toBeTruthy(); | ||
}); | ||
|
||
it('should throw error when calling serialize when type is not boolean or number', () => { | ||
const dataValue = 'string'; | ||
|
||
shouldThrowErrorForValue(dataValue); | ||
}); | ||
|
||
it('should throw error when calling serialize when number that is not 1 or 0', () => { | ||
const dataValue = 10; | ||
|
||
shouldThrowErrorForValue(dataValue); | ||
}); | ||
|
||
it('should not throw error when calling serialize with false', () => { | ||
const dataValue = false; | ||
|
||
shouldNotThrowErrorForValue(dataValue); | ||
}); | ||
|
||
it('should not throw error when calling serialize with true', () => { | ||
const dataValue = true; | ||
|
||
shouldNotThrowErrorForValue(dataValue); | ||
}); | ||
|
||
it('should not throw error when calling serialize with 0', () => { | ||
const dataValue = 0; | ||
|
||
shouldNotThrowErrorForValue(dataValue); | ||
}); | ||
|
||
it('should not throw error when calling serialize with 1', () => { | ||
const dataValue = 1; | ||
|
||
shouldNotThrowErrorForValue(dataValue); | ||
}); | ||
}); | ||
}); |