-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🧪 Added testing for bytes
functions imported from ethers.js
#134
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
16a82bf
🧪 Init test files for remaining untested Bytes functions
arimgibson d431a7a
🧪 hexConcat()
arimgibson e2a3f3d
🧪 hexDataLength()
arimgibson 2dffbac
🧪 hexStripZeros()
arimgibson e6aecbc
🧪 hexValue()
arimgibson d366a61
🧪 isBytesLike()
arimgibson e6ad743
🧪 isHexString()
arimgibson 56b20e0
🧪 stripZeros()
arimgibson bfe756e
🧪 zeroPad()
arimgibson 6669f70
🧪 Bytes @ 93.93% coverage
arimgibson bfd78c9
🧪 Bytes @ 96.36% coverage
arimgibson 133ba22
🧪 Bytes @ 100% coverage
arimgibson b639f9d
Merge remote-tracking branch 'origin/master' into test/BytesFns
dawsbot 0f098e3
run prettier
dawsbot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,28 @@ | ||
import { utils as ethers } from 'ethers'; | ||
import { hexConcat } from '../../bytes'; | ||
|
||
describe('utils.hexConcat', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠 This is great testing style |
||
it('should match ethers.js - hex values', () => { | ||
const values = ['0x2048', '0x6917', '0x85616379']; | ||
expect(hexConcat(values)).toBe(ethers.hexConcat(values)); | ||
}); | ||
it('should match ethers.js - UInt8Array values', () => { | ||
const values = [ | ||
[5, 10, 247, 22], | ||
[50, 255, 3], | ||
[59, 36, 18, 46, 198, 234], | ||
]; | ||
expect(hexConcat(values)).toBe(ethers.hexConcat(values)); | ||
}); | ||
it('should match ethers.js - hex & UInt8Array values', () => { | ||
const values = [ | ||
'0x2048', | ||
[5, 10, 247, 22], | ||
'0x6917', | ||
[50, 255, 3], | ||
'0x85616379', | ||
[59, 36, 18, 46, 198, 234], | ||
]; | ||
expect(hexConcat(values)).toBe(ethers.hexConcat(values)); | ||
}); | ||
}); |
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,27 @@ | ||
import { utils as ethers } from 'ethers'; | ||
import { hexDataLength } from '../../bytes'; | ||
|
||
describe('utils.hexDataLength', () => { | ||
it('should match ethers.js - hex values', () => { | ||
const values = ['0x9347', '0x185754', '0x00005823']; | ||
values.forEach((value) => { | ||
expect(hexDataLength(value)).toBe(ethers.hexDataLength(value)); | ||
}); | ||
}); | ||
it('should match ethers.js - UInt8Array values', () => { | ||
const values = [ | ||
[9, 58, 29, 24], | ||
[185, 203], | ||
[239, 30, 49, 41, 5, 10, 42], | ||
]; | ||
values.forEach((value) => { | ||
expect(hexDataLength(value)).toBe(ethers.hexDataLength(value)); | ||
}); | ||
}); | ||
it('should return null - not hex value or not divisible by 2', () => { | ||
const values = ['0x383', 'non-hex string']; | ||
values.forEach((value) => { | ||
expect(hexDataLength(value)).toBeNull(); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,35 +1,41 @@ | ||
import * as ethers from 'ethers'; | ||
import { hexDataSlice } from '../../../'; | ||
import { utils as ethers } from 'ethers'; | ||
import { hexDataSlice } from '../../bytes'; | ||
|
||
describe('hexDataSlice', () => { | ||
describe('utils.hexDataSlice', () => { | ||
it('numbers - matches ethers strings', () => { | ||
const decimalValues = ['0x123456']; | ||
decimalValues.forEach((decimalValue) => { | ||
expect(hexDataSlice(decimalValue, 0, 2)).toStrictEqual( | ||
ethers.utils.hexDataSlice(decimalValue, 0, 2), | ||
ethers.hexDataSlice(decimalValue, 0, 2), | ||
); | ||
}); | ||
}); | ||
it('numbers - matches ethers hex numbers', () => { | ||
const decimalValues = [0x1234567891011]; | ||
decimalValues.forEach((decimalValue) => { | ||
expect(hexDataSlice(decimalValue, 3)).toStrictEqual( | ||
ethers.utils.hexDataSlice(decimalValue as any, 3), | ||
ethers.hexDataSlice(decimalValue as any, 3), | ||
); | ||
expect(hexDataSlice(decimalValue, 2, 4)).toStrictEqual( | ||
ethers.utils.hexDataSlice(decimalValue as any, 2, 4), | ||
ethers.hexDataSlice(decimalValue as any, 2, 4), | ||
); | ||
expect(hexDataSlice(decimalValue, 100)).toStrictEqual( | ||
ethers.utils.hexDataSlice(decimalValue as any, 100), | ||
ethers.hexDataSlice(decimalValue as any, 100), | ||
); | ||
}); | ||
}); | ||
it('numbers - matches ethers decimals', () => { | ||
const decimalValues = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]; | ||
decimalValues.forEach((decimalValue) => { | ||
expect(hexDataSlice(decimalValue, 1, 2)).toStrictEqual( | ||
ethers.utils.hexDataSlice(decimalValue as any, 1, 2), | ||
ethers.hexDataSlice(decimalValue as any, 1, 2), | ||
); | ||
}); | ||
}); | ||
it('should throw error - invalid hexData', () => { | ||
const values = ['non-hex string', '0x938']; | ||
values.forEach((value) => { | ||
expect(() => hexDataSlice(value, 1, 3)).toThrow('invalid hexData'); | ||
}); | ||
}); | ||
}); |
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,27 @@ | ||
import { utils as ethers } from 'ethers'; | ||
import { hexStripZeros } from '../../bytes'; | ||
|
||
describe('utils.hexStripZeros', () => { | ||
it('should match ethers.js - hex values', () => { | ||
const values = ['0x00009347', '0x00185754', '0x00000000005823']; | ||
values.forEach((value) => { | ||
expect(hexStripZeros(value)).toBe(ethers.hexStripZeros(value)); | ||
}); | ||
}); | ||
it('should match ethers.js - UInt8Array values', () => { | ||
const values = [ | ||
[0, 0, 0, 9, 58, 29, 24], | ||
[0, 185, 203], | ||
[0, 0, 0, 0, 239, 30, 49, 41, 5, 10, 42], | ||
]; | ||
values.forEach((value) => { | ||
expect(hexStripZeros(value)).toBe(ethers.hexStripZeros(value)); | ||
}); | ||
}); | ||
it('should throw error - invalid hex string', () => { | ||
const values = ['non-hex string']; | ||
values.forEach((value) => { | ||
expect(() => hexStripZeros(value)).toThrow('invalid hex string'); | ||
}); | ||
}); | ||
}); |
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,46 @@ | ||
import { utils as ethers } from 'ethers'; | ||
import { hexValue } from '../../bytes'; | ||
import { tinyBig } from './../../../shared/tiny-big/tiny-big'; | ||
|
||
describe('utils.hexValue', () => { | ||
it('should match ethers.js - hex string', () => { | ||
const values = ['0x9347', '0x185754', '0x00005823']; | ||
values.forEach((value) => { | ||
expect(hexValue(value)).toBe(ethers.hexValue(value)); | ||
}); | ||
}); | ||
it('should match ethers.js - UInt8Array', () => { | ||
const values = [ | ||
[4, 50, 2], | ||
[231, 49, 40, 70, 19], | ||
[10, 68, 20, 98], | ||
]; | ||
values.forEach((value) => { | ||
expect(hexValue(value)).toBe(ethers.hexValue(value)); | ||
}); | ||
}); | ||
it('should match ethers.js - TinyBig (hexable)', () => { | ||
const values = [tinyBig('29389'), tinyBig(2834), tinyBig(402)]; | ||
values.forEach((value) => { | ||
expect(hexValue(value)).toBe(ethers.hexValue(value)); | ||
}); | ||
}); | ||
it('should match ethers.js - number', () => { | ||
const values = [624, 457, 23451]; | ||
values.forEach((value) => { | ||
expect(hexValue(value)).toBe(ethers.hexValue(value)); | ||
}); | ||
}); | ||
it('should match ethers.js - BigInt', () => { | ||
const values = [BigInt(204), BigInt('23491'), BigInt(4183459235723491)]; | ||
values.forEach((value) => { | ||
expect(hexValue(value)).toBe(ethers.hexValue(value)); | ||
}); | ||
}); | ||
it('should return 0x0 - only zero data given', () => { | ||
const values = [BigInt(0), 0, '0x0000', [0, 0, 0]]; | ||
values.forEach((value) => { | ||
expect(hexValue(value)).toBe('0x0'); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,14 +1,40 @@ | ||
import * as ethers from 'ethers'; | ||
import { hexlify } from '../../../'; | ||
import { utils as ethers } from 'ethers'; | ||
import { hexlify } from '../../bytes'; | ||
import { BytesLike } from './../../bytes'; | ||
|
||
describe('hexlify', () => { | ||
describe('utils.hexlify', () => { | ||
it('numbers - matches ethers strings', () => { | ||
const decimalValues = [0, 4, 5, 16, BigInt(0), BigInt(16)]; | ||
decimalValues.forEach((decimalValue) => { | ||
expect(hexlify(decimalValue)).toBe(ethers.utils.hexlify(decimalValue)); | ||
expect(hexlify(decimalValue)).toBe(ethers.hexlify(decimalValue)); | ||
}); | ||
|
||
const opts = { allowMissingPrefix: true }; | ||
expect(hexlify('22', opts)).toBe(ethers.utils.hexlify('22', opts)); | ||
expect(hexlify('22', opts)).toBe(ethers.hexlify('22', opts)); | ||
}); | ||
it('should match ethers.js - hexPad options', () => { | ||
const values = [ | ||
['0x3342e95', { hexPad: 'left' }], | ||
['0x41c942c42', { hexPad: 'right' }], | ||
]; | ||
values.forEach((value) => { | ||
expect(hexlify(value[0] as any, value[1] as any)).toBe( | ||
ethers.hexlify(value[0] as any, value[1] as any), | ||
); | ||
}); | ||
}); | ||
it('should throw error - hex data is odd-length', () => { | ||
const values = ['0x931', '0x34414']; | ||
values.forEach((value) => { | ||
expect(() => hexlify(value)).toThrow('hex data is odd-length'); | ||
}); | ||
}); | ||
it('should throw error - invalid hexlify value', () => { | ||
const values = ['non-hex string', false]; | ||
values.forEach((value) => { | ||
expect(() => hexlify(value as BytesLike)).toThrow( | ||
'invalid hexlify value', | ||
); | ||
}); | ||
}); | ||
}); |
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,39 @@ | ||
import { utils as ethers } from 'ethers'; | ||
import { isBytesLike } from '../../bytes'; | ||
|
||
describe('utils.isBytesLike', () => { | ||
it('should match ethers.js - hex string', () => { | ||
const values = ['0x9347', '0x185754', '0x00005823']; | ||
values.forEach((value) => { | ||
expect(isBytesLike(value)).toBe(ethers.isBytesLike(value)); | ||
}); | ||
}); | ||
it('should match ethers.js - UInt8Array', () => { | ||
const values = [ | ||
[9, 58, 29, 24], | ||
[185, 203], | ||
[239, 30, 49, 41, 5, 10, 42], | ||
]; | ||
values.forEach((value) => { | ||
expect(isBytesLike(value)).toBe(ethers.isBytesLike(value)); | ||
}); | ||
}); | ||
it('should match ethers.js - number', () => { | ||
const values = [152, 513, 2354]; | ||
values.forEach((value) => { | ||
expect(isBytesLike(value)).toBe(ethers.isBytesLike(value)); | ||
}); | ||
}); | ||
it('should match ethers.js - non-hex string', () => { | ||
const values = ['essential-eth', 'ethers.js', 'ethereum']; | ||
values.forEach((value) => { | ||
expect(isBytesLike(value)).toBe(ethers.isBytesLike(value)); | ||
}); | ||
}); | ||
it('should match ethers.js - boolean', () => { | ||
const values = [false, true]; | ||
values.forEach((value) => { | ||
expect(isBytesLike(value)).toBe(ethers.isBytesLike(value)); | ||
}); | ||
}); | ||
}); |
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,51 @@ | ||
import { utils as ethers } from 'ethers'; | ||
import { isHexString } from '../../bytes'; | ||
|
||
describe('utils.isHexString', () => { | ||
it('should match ethers.js - hex string', () => { | ||
const values = ['0x9347', '0x185754', '0x00005823']; | ||
values.forEach((value) => { | ||
expect(isHexString(value)).toBe(ethers.isHexString(value)); | ||
}); | ||
}); | ||
it('should match ethers.js - hex string of specific length', () => { | ||
const values = [ | ||
['0x9347', 2], | ||
['0x185754', 5], | ||
['0x00005823', 4], | ||
]; | ||
values.forEach((value) => { | ||
expect(isHexString(value[0], value[1] as number)).toBe( | ||
ethers.isHexString(value[0], value[1] as number), | ||
); | ||
}); | ||
}); | ||
it('should match ethers.js - UInt8Array', () => { | ||
const values = [ | ||
[9, 58, 29, 24], | ||
[185, 203], | ||
[239, 30, 49, 41, 5, 10, 42], | ||
]; | ||
values.forEach((value) => { | ||
expect(isHexString(value)).toBe(ethers.isHexString(value)); | ||
}); | ||
}); | ||
it('should match ethers.js - number', () => { | ||
const values = [152, 513, 2354]; | ||
values.forEach((value) => { | ||
expect(isHexString(value)).toBe(ethers.isHexString(value)); | ||
}); | ||
}); | ||
it('should match ethers.js - non-hex string', () => { | ||
const values = ['essential-eth', 'ethers.js', 'ethereum']; | ||
values.forEach((value) => { | ||
expect(isHexString(value)).toBe(ethers.isHexString(value)); | ||
}); | ||
}); | ||
it('should match ethers.js - boolean', () => { | ||
const values = [false, true]; | ||
values.forEach((value) => { | ||
expect(isHexString(value)).toBe(ethers.isHexString(value)); | ||
}); | ||
}); | ||
}); |
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,27 @@ | ||
import { utils as ethers } from 'ethers'; | ||
import { stripZeros } from '../../bytes'; | ||
|
||
describe('utils.stripZeros', () => { | ||
it('should match ethers.js - hex string', () => { | ||
const values = ['0x00009347', '0x00185754', '0x00000000005823']; | ||
values.forEach((value) => { | ||
expect(stripZeros(value)).toStrictEqual(ethers.stripZeros(value)); | ||
}); | ||
}); | ||
it('should match ethers.js - UInt8Array', () => { | ||
const values = [ | ||
[0, 0, 0, 9, 58, 29, 24], | ||
[0, 185, 203], | ||
[0, 0, 0, 0, 239, 30, 49, 41, 5, 10, 42], | ||
]; | ||
values.forEach((value) => { | ||
expect(stripZeros(value)).toStrictEqual(ethers.stripZeros(value)); | ||
}); | ||
}); | ||
it('should match ethers.js - empty array', () => { | ||
const values = [[], '0x']; | ||
values.forEach((value) => { | ||
expect(stripZeros(value)).toStrictEqual(ethers.stripZeros(value)); | ||
}); | ||
}); | ||
}); |
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,41 @@ | ||
import { utils as ethers } from 'ethers'; | ||
import { zeroPad } from '../../bytes'; | ||
import { BytesLike } from './../../bytes'; | ||
|
||
describe('utils.zeroPad', () => { | ||
it('should match ethers.js - hex string', () => { | ||
const values = [ | ||
['0x9347', 10], | ||
['0x185754', 5], | ||
['0x00005823', 7], | ||
]; | ||
values.forEach((value) => { | ||
expect(zeroPad(value[0] as string, value[1] as number)).toStrictEqual( | ||
ethers.zeroPad(value[0] as string, value[1] as number), | ||
); | ||
}); | ||
}); | ||
it('should match ethers.js - UInt8Array', () => { | ||
const values = [ | ||
[[9, 58, 29, 24], 5], | ||
[[185, 203], 4], | ||
[[239, 30, 49, 41, 5, 10, 42], 10], | ||
]; | ||
values.forEach((value) => { | ||
expect(zeroPad(value[0] as BytesLike, value[1] as number)).toStrictEqual( | ||
ethers.zeroPad(value[0] as BytesLike, value[1] as number), | ||
); | ||
}); | ||
}); | ||
it('should throw error - value out of range', () => { | ||
const values = [ | ||
[[9, 58, 29, 24], 3], | ||
['0x185754', 1], | ||
]; | ||
values.forEach((value) => { | ||
expect(() => zeroPad(value[0] as BytesLike, value[1] as number)).toThrow( | ||
'value out of range', | ||
); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧼 Great cleanup