Skip to content

Commit

Permalink
fix(contract): stringify maps as arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Aug 30, 2022
1 parent e328edd commit b80e3b4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/utils/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,25 @@ function getTabs(tabs) {
return ' '.repeat(tabs * 4);
}

const JsonStringifyBigInt = (object, spaced) => JSON.stringify(
const JsonStringifyEs = (object, spaced) => JSON.stringify(
object,
(key, value) => (typeof value === 'bigint' ? `${value}` : value),
(key, value) => {
if (typeof value === 'bigint') return `${value}`;
if (value instanceof Map) return [...value.entries()];
return value;
},
spaced ? 2 : undefined,
);

// Print helper
export function print(msg, obj) {
if (typeof msg === 'object') {
console.log(JsonStringifyBigInt(msg, true));
console.log(JsonStringifyEs(msg, true));
return;
}
if (obj) {
console.log(msg);
console.log(JsonStringifyBigInt(obj, true));
console.log(JsonStringifyEs(obj, true));
} else {
console.log(msg);
}
Expand All @@ -60,7 +64,7 @@ export function printUnderscored(key, val) {
print([
key,
'_'.repeat(WIDTH - key.length),
typeof val !== 'object' ? val : JsonStringifyBigInt(val),
typeof val !== 'object' ? val : JsonStringifyEs(val),
].join(' '));
}

Expand Down
12 changes: 12 additions & 0 deletions test/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ contract Identity =
record state = { z: int }
entrypoint init(_z: int) = { z = _z }
entrypoint test(x : int, y: int) = x + y + state.z
entrypoint getMap(): map(int, int) = {[1] = 2, [3] = 4}
`;

describe('Contract Module', function contractTests() {
Expand Down Expand Up @@ -194,6 +195,17 @@ describe('Contract Module', function contractTests() {
callResponse.decodedResult.should.equal('6');
});

it('returns Maps correctly', async () => {
const callResponse = await executeContract([
'call',
'--json',
'--descrPath', deployDescriptorFile,
'getMap',
'--callStatic',
]);
expect(callResponse.decodedResult).to.be.eql([['1', '2'], ['3', '4']]);
});

it('calls contract by contract source and address', async () => {
const callResponse = await executeContract([
'call',
Expand Down

0 comments on commit b80e3b4

Please sign in to comment.