Skip to content

Commit

Permalink
feat: isEmpty map method (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusarich authored Apr 19, 2024
1 parent 88e3c38 commit f2bb897
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Display an error for integer overflow at compile-time: PR [#200](https://github.com/tact-lang/tact/pull/200)
- Non-modifying `StringBuilder`'s `concat` method for chained string concatenations: PR [#217](https://github.com/tact-lang/tact/pull/217)
- `toString` extension function for `Address` type: PR [#224](https://github.com/tact-lang/tact/pull/224)
- `isEmpty` method for `Map` type: PR [#266](https://github.com/tact-lang/tact/pull/266)

### Changed

Expand Down
29 changes: 29 additions & 0 deletions src/abi/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,33 @@ export const MapFunctions: Map<string, AbiFunction> = new Map([
},
},
],
[
"isEmpty",
{
name: "isEmpty",
resolve(ctx, args, ref) {
// Check arguments
if (args.length !== 1) {
throwError("isEmpty expects one argument", ref); // Ignore self argument
}
const self = args[0];
if (!self || self.kind !== "map") {
throwError("isEmpty expects a map as self argument", ref); // Should not happen
}

return { kind: "ref", name: "Bool", optional: false };
},
generate: (ctx, args, exprs, ref) => {
if (args.length !== 1) {
throwError("isEmpty expects one argument", ref); // Ignore self argument
}
const self = args[0];
if (!self || self.kind !== "map") {
throwError("isEmpty expects a map as self argument", ref); // Should not happen
}

return `null?(${writeExpression(exprs[0], ctx)})`;
},
},
],
]);
23 changes: 23 additions & 0 deletions src/test/feature-map.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe("feature-map", () => {

// Initial state
expect((await contract.getIntMap1()).size).toBe(0);
expect(await contract.getIntMap1IsEmpty()).toBe(true);
expect((await contract.getIntMap2()).size).toBe(0);
expect((await contract.getIntMap3()).size).toBe(0);
expect((await contract.getIntMap4()).size).toBe(0);
Expand Down Expand Up @@ -518,6 +519,28 @@ describe("feature-map", () => {
expect(await contract.getAddrMap7_5Value(addr)).toBe(null);
expect(await contract.getAddrMap7_6Value(addr)).toBe(null);
}

// Test isEmpty

expect(await contract.getIntMap1IsEmpty()).toBe(true);

await contract.send(
treasure,
{ value: toNano(1) },
{ $$type: "SetIntMap1", key: 1n, value: 1n },
);
await system.run();

expect(await contract.getIntMap1IsEmpty()).toBe(false);

await contract.send(
treasure,
{ value: toNano(1) },
{ $$type: "SetIntMap1", key: 1n, value: null },
);
await system.run();

expect(await contract.getIntMap1IsEmpty()).toBe(true);
} catch (e) {
if (e instanceof ComputeError) {
if (e.logs) {
Expand Down
4 changes: 4 additions & 0 deletions src/test/features/maps.tact
Original file line number Diff line number Diff line change
Expand Up @@ -775,4 +775,8 @@ contract MapTestContract {
get fun mapAsCell(): Cell? {
return self.addrMap7_6.asCell();
}

get fun intMap1IsEmpty(): Bool {
return self.intMap1.isEmpty();
}
}

0 comments on commit f2bb897

Please sign in to comment.