Tests for writing bytes to nested bytes
This commit is contained in:
@@ -409,14 +409,96 @@ describe('LibBytes', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
/// @TODO Implement test cases for writeUint256. Test template below.
|
describe.only('writeBytes', () => {
|
||||||
/// Currently, the generated contract wrappers do not support this library's write methods.
|
it('should successfully write short, nested array of bytes when it takes up the whole array)', async () => {
|
||||||
/*
|
const testBytesOffset = new BigNumber(0);
|
||||||
describe('writeBytes', () => {
|
const emptyByteArray = ethUtil.bufferToHex(new Buffer(shortTestBytesAsBuffer.byteLength));
|
||||||
it('should successfully write bytes when it takes up the whole array)', async () => {});
|
const bytesWritten = await libBytes.publicWriteBytes.callAsync(emptyByteArray, testBytesOffset, shortData);
|
||||||
it('should successfully write bytes when it is offset in the array)', async () => {});
|
const bytesRead = await libBytes.publicReadBytes.callAsync(bytesWritten, testBytesOffset);
|
||||||
it('should fail if the byte array is too short to hold the nested bytes)', async () => {});
|
return expect(bytesRead).to.be.equal(shortData);
|
||||||
it('should fail if the length between the offset and end of the byte array is too short to hold the nested bytes)', async () => {});
|
});
|
||||||
|
|
||||||
|
it('should successfully write short, nested array of bytes when it is offset in the array', async () => {
|
||||||
|
// Write a prefix to the array
|
||||||
|
const prefixData = '0xabcdef';
|
||||||
|
const prefixDataAsBuffer = ethUtil.toBuffer(prefixData);
|
||||||
|
const prefixOffset = new BigNumber(0);
|
||||||
|
const emptyByteArray = ethUtil.bufferToHex(
|
||||||
|
new Buffer(prefixDataAsBuffer.byteLength + shortTestBytesAsBuffer.byteLength),
|
||||||
|
);
|
||||||
|
let bytesWritten = await libBytes.publicWriteBytes.callAsync(emptyByteArray, prefixOffset, prefixData);
|
||||||
|
// Write data after prefix
|
||||||
|
const testBytesOffset = new BigNumber(prefixDataAsBuffer.byteLength);
|
||||||
|
bytesWritten = await libBytes.publicWriteBytes.callAsync(bytesWritten, testBytesOffset, shortData);
|
||||||
|
// Read data after prefix and validate
|
||||||
|
const bytes = await libBytes.publicReadBytes.callAsync(bytesWritten, testBytesOffset);
|
||||||
|
return expect(bytes).to.be.equal(shortData);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should successfully write a nested array of bytes - one word in length - when it takes up the whole array)', async () => {
|
||||||
|
const testBytesOffset = new BigNumber(0);
|
||||||
|
const emptyByteArray = ethUtil.bufferToHex(new Buffer(wordOfTestBytesAsBuffer.byteLength));
|
||||||
|
const bytesWritten = await libBytes.publicWriteBytes.callAsync(emptyByteArray, testBytesOffset, wordOfData);
|
||||||
|
const bytesRead = await libBytes.publicReadBytes.callAsync(bytesWritten, testBytesOffset);
|
||||||
|
return expect(bytesRead).to.be.equal(wordOfData);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should successfully write a nested array of bytes - one word in length - when it is offset in the array', async () => {
|
||||||
|
// Write a prefix to the array
|
||||||
|
const prefixData = '0xabcdef';
|
||||||
|
const prefixDataAsBuffer = ethUtil.toBuffer(prefixData);
|
||||||
|
const prefixOffset = new BigNumber(0);
|
||||||
|
const emptyByteArray = ethUtil.bufferToHex(
|
||||||
|
new Buffer(prefixDataAsBuffer.byteLength + wordOfTestBytesAsBuffer.byteLength),
|
||||||
|
);
|
||||||
|
let bytesWritten = await libBytes.publicWriteBytes.callAsync(emptyByteArray, prefixOffset, prefixData);
|
||||||
|
// Write data after prefix
|
||||||
|
const testBytesOffset = new BigNumber(prefixDataAsBuffer.byteLength);
|
||||||
|
bytesWritten = await libBytes.publicWriteBytes.callAsync(bytesWritten, testBytesOffset, wordOfData);
|
||||||
|
// Read data after prefix and validate
|
||||||
|
const bytes = await libBytes.publicReadBytes.callAsync(bytesWritten, testBytesOffset);
|
||||||
|
return expect(bytes).to.be.equal(wordOfData);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should successfully write a long, nested bytes when it takes up the whole array)', async () => {
|
||||||
|
const testBytesOffset = new BigNumber(0);
|
||||||
|
const emptyByteArray = ethUtil.bufferToHex(new Buffer(longTestBytesAsBuffer.byteLength));
|
||||||
|
const bytesWritten = await libBytes.publicWriteBytes.callAsync(emptyByteArray, testBytesOffset, longData);
|
||||||
|
const bytesRead = await libBytes.publicReadBytes.callAsync(bytesWritten, testBytesOffset);
|
||||||
|
return expect(bytesRead).to.be.equal(longData);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should successfully write long, nested array of bytes when it is offset in the array', async () => {
|
||||||
|
// Write a prefix to the array
|
||||||
|
const prefixData = '0xabcdef';
|
||||||
|
const prefixDataAsBuffer = ethUtil.toBuffer(prefixData);
|
||||||
|
const prefixOffset = new BigNumber(0);
|
||||||
|
const emptyByteArray = ethUtil.bufferToHex(
|
||||||
|
new Buffer(prefixDataAsBuffer.byteLength + longTestBytesAsBuffer.byteLength),
|
||||||
|
);
|
||||||
|
let bytesWritten = await libBytes.publicWriteBytes.callAsync(emptyByteArray, prefixOffset, prefixData);
|
||||||
|
// Write data after prefix
|
||||||
|
const testBytesOffset = new BigNumber(prefixDataAsBuffer.byteLength);
|
||||||
|
bytesWritten = await libBytes.publicWriteBytes.callAsync(bytesWritten, testBytesOffset, longData);
|
||||||
|
// Read data after prefix and validate
|
||||||
|
const bytes = await libBytes.publicReadBytes.callAsync(bytesWritten, testBytesOffset);
|
||||||
|
return expect(bytes).to.be.equal(longData);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should fail if the byte array is too short to hold the length of a nested byte array)', async () => {
|
||||||
|
const offset = new BigNumber(0);
|
||||||
|
const emptyByteArray = ethUtil.bufferToHex(new Buffer(1));
|
||||||
|
return expect(libBytes.publicWriteBytes.callAsync(emptyByteArray, offset, longData)).to.be.rejectedWith(
|
||||||
|
constants.REVERT,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should fail if the length between the offset and end of the byte array is too short to hold the length of a nested byte array)', async () => {
|
||||||
|
const emptyByteArray = ethUtil.bufferToHex(new Buffer(shortTestBytesAsBuffer.byteLength));
|
||||||
|
const badOffset = new BigNumber(ethUtil.toBuffer(shortTestBytesAsBuffer).byteLength);
|
||||||
|
return expect(libBytes.publicWriteBytes.callAsync(emptyByteArray, badOffset, shortData)).to.be.rejectedWith(
|
||||||
|
constants.REVERT,
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
*/
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user