forked from aeternity/aepp-sdk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbytes.js
More file actions
66 lines (54 loc) · 2.48 KB
/
Copy pathbytes.js
File metadata and controls
66 lines (54 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* ISC License (ISC)
* Copyright (c) 2018 aeternity developers
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
import '../'
import { describe, it } from 'mocha'
import { expect } from 'chai'
import BigNumber from 'bignumber.js'
import { leftPad, rightPad, toBytes, bigNumberToByteArray } from '../../es/utils/bytes'
import { isBase64, snakeOrKebabToPascal, snakeToPascal, pascalToSnake } from '../../es/utils/string'
describe('Bytes', function () {
const bytes = Buffer.from('hello')
it('left/right pad', () => {
rightPad(7, bytes).should.be.eql(Buffer.from([...bytes, 0, 0]))
rightPad(4, bytes).should.be.eql(bytes)
leftPad(7, bytes).should.be.eql(Buffer.from([0, 0, ...bytes]))
leftPad(4, bytes).should.be.eql(bytes)
})
it('toBytes: invalid input', () => {
try {
toBytes(true)
} catch (e) {
e.message.should.be.equal('Byte serialization not supported')
}
})
it('is base64 string', () => isBase64(bytes.toString('base64')).should.be.equal(true))
it('is not base64 string', () => isBase64('hello').should.be.equal(false))
const testCase = 'test_test-testTest'
it('converts snake or kebab to pascal case', () => snakeOrKebabToPascal(testCase)
.should.be.equal('testTestTestTest'))
it('converts snake to pascal case', () => snakeToPascal(testCase)
.should.be.equal('testTest-testTest'))
it('converts pascal to snake case', () => pascalToSnake(testCase)
.should.be.equal('test_test-test_test'))
describe('bigNumberToByteArray', () => {
it('converts BigNumber to Buffer', () => bigNumberToByteArray(new BigNumber('1000'))
.readInt16BE().should.be.equal(1000))
it('throws error if BigNumber is not integer', () =>
expect(() => bigNumberToByteArray(new BigNumber('1.5')))
.to.throw(/Unexpected not integer value:/))
})
})