Tests for writing bytes to nested bytes

This commit is contained in:
Greg Hysen
2018-05-25 18:19:25 -07:00
parent d17e031259
commit b3c253ea2a

View File

@@ -409,14 +409,96 @@ describe('LibBytes', () => {
});
});
/// @TODO Implement test cases for writeUint256. Test template below.
/// Currently, the generated contract wrappers do not support this library's write methods.
/*
describe('writeBytes', () => {
it('should successfully write bytes when it takes up the whole array)', async () => {});
it('should successfully write bytes when it is offset in the array)', async () => {});
it('should fail if the byte array is too short to hold the nested bytes)', async () => {});
it('should fail if the length between the offset and end of the byte array is too short to hold the nested bytes)', async () => {});
describe.only('writeBytes', () => {
it('should successfully write short, nested array of bytes when it takes up the whole array)', async () => {
const testBytesOffset = new BigNumber(0);
const emptyByteArray = ethUtil.bufferToHex(new Buffer(shortTestBytesAsBuffer.byteLength));
const bytesWritten = await libBytes.publicWriteBytes.callAsync(emptyByteArray, testBytesOffset, shortData);
const bytesRead = await libBytes.publicReadBytes.callAsync(bytesWritten, testBytesOffset);
return expect(bytesRead).to.be.equal(shortData);
});
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,
);
});
});
*/
});