ethers.js/tests/run-contract-interface.js

111 lines
2.7 KiB
JavaScript
Raw Normal View History

2017-02-24 22:44:19 +03:00
'use strict';
var utils = (function() {
2017-03-01 10:26:50 +03:00
var bigNumber = require('../utils/bignumber');
var convert = require('../utils/convert');
2017-02-24 22:44:19 +03:00
return {
arrayify: convert.arrayify,
bigNumberify: bigNumber.bigNumberify,
isHexString: convert.isHexString,
2017-02-24 22:44:19 +03:00
};
})();
2017-03-01 10:26:50 +03:00
2017-02-24 22:44:19 +03:00
function equals(a, b) {
// Array (treat recursively)
if (Array.isArray(a)) {
if (!Array.isArray(b) || a.length !== b.length) { return false; }
for (var i = 0; i < a.length; i++) {
if (!equals(a[i], b[i])) { return false; }
}
return true;
}
// BigNumber
if (a.eq) {
if (!a.eq(b)) { return false; }
2017-02-24 22:44:19 +03:00
return true;
}
// Uint8Array
if (a.buffer) {
if (!utils.isHexString(b)) { return false; }
b = utils.arrayify(b);
2017-02-24 22:44:19 +03:00
if (!b.buffer || a.length !== b.length) { return false; }
for (var i = 0; i < a.length; i++) {
if (a[i] !== b[i]) { return false; }
}
return true;
}
// Something else
return a === b;
}
function getValues(object) {
if (Array.isArray(object)) {
var result = [];
object.forEach(function(object) {
result.push(getValues(object));
});
return result;
}
switch (object.type) {
case 'number':
return utils.bigNumberify(object.value);
case 'boolean':
case 'string':
return object.value;
case 'buffer':
return utils.arrayify(object.value);
default:
throw new Error('invalid type - ' + object.type);
}
}
function testContractInterface(test) {
var Interface = require('../contracts/index.js').Interface;
var testcases = require('./tests/contract-interface.json');
testcases.forEach(function(testcase) {
var values = getValues(JSON.parse(testcase.normalizedValues));
var types = JSON.parse(testcase.types);
var result = testcase.result;
try {
var encoded = Interface.encodeParams(types, values);
test.equal(result, encoded, 'failed to encode data - ' + testcase.name);
2017-02-24 22:44:19 +03:00
} catch (error) {
test.ok(false, 'Failed Encode (' + testcase.name + ') - ' + error.message);
}
try {
var decoded = Interface.decodeParams(types, result);
2017-03-01 10:26:50 +03:00
var decodedArray = Array.prototype.slice.call(decoded);;
2017-02-24 22:44:19 +03:00
test.ok(equals(values, decodedArray), 'failed to decode parameters - ' + testcase.name);
2017-02-24 22:44:19 +03:00
} catch (error) {
test.ok(false, 'Failed Decode (' + testcase.name + ') - ' + error.message);
}
});
test.done();
}
module.exports = {
"contract-interface": testContractInterface,
}