Skip to content

Commit

Permalink
test: initOf for parent-child contracts (#520)
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-trunov committed Jul 2, 2024
1 parent 7a7047a commit 1be9e54
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/test/e2e-emulated/contracts/initof.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
contract Self {

init() { }

receive() { }

get fun testInitOfAddress(): Address {
return contractAddress(initOf Self());
}

get fun testMyAddress(): Address {
return myAddress();
}
}

message ChildAddress {
address: Address;
}

contract Child {

owner: Address;

init(owner: Address) {
self.owner = owner;
}

receive() {
send(SendParameters {
to: sender(),
value: 0,
mode: SendRemainingValue | SendIgnoreErrors,
bounce: false,
body: ChildAddress { address: myAddress() }.toCell(),
});
}
}

contract Parent {

childMyAddress: Address;

init() {
self.childMyAddress = myAddress(); // dummy value to be replaced by the child
}

receive() {
let ci = initOf Child(myAddress());
send(SendParameters {
to: contractAddress(ci),
value: 0,
mode: SendRemainingValue | SendIgnoreErrors,
bounce: false,
body: beginCell().endCell(),
code: ci.code,
data: ci.data,
});
}

receive(msg: ChildAddress) {
self.childMyAddress = msg.address;
}

get fun testInitOfAddressChild(): Address {
return contractAddress(initOf Child(myAddress()));
}

get fun testMyAddressChild(): Address {
return self.childMyAddress;
}
}
51 changes: 51 additions & 0 deletions src/test/e2e-emulated/initof.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { toNano } from "@ton/core";
import { ContractSystem } from "@tact-lang/emulator";
import { __DANGER_resetNodeId } from "../../grammar/ast";
import { Self } from "./contracts/output/initof_Self";
import { Parent } from "./contracts/output/initof_Parent";
import { consoleLogger } from "../../logger";

describe("initOf", () => {
beforeAll(() => {
jest.spyOn(consoleLogger, "error").mockImplementation(() => {});
});

beforeEach(() => {
__DANGER_resetNodeId();
});

afterAll(() => {
(consoleLogger.error as jest.Mock).mockRestore();
});

afterEach(() => {
(consoleLogger.error as jest.Mock).mockClear();
});
it("should implement initOf correctly - 1", async () => {
// Init
const system = await ContractSystem.create();
const treasure = system.treasure("treasure");
const contract = system.open(await Self.fromInit());
await contract.send(treasure, { value: toNano("10") }, null);
await system.run();

const addr1 = (await contract.getTestInitOfAddress()).toRawString();
const addr2 = (await contract.getTestMyAddress()).toRawString();
expect(addr1).toEqual(addr2);
});
it("should implement initOf correctly - 2", async () => {
// Init
const system = await ContractSystem.create();
const treasure = system.treasure("treasure");
const contract = system.open(await Parent.fromInit());
await contract.send(treasure, { value: toNano("10") }, null);
await system.run();
const addrChildInitOf = (
await contract.getTestInitOfAddressChild()
).toRawString();
const addrChildMyAddress = (
await contract.getTestMyAddressChild()
).toRawString();
expect(addrChildInitOf).toEqual(addrChildMyAddress);
});
});
8 changes: 8 additions & 0 deletions tact.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,14 @@
"name": "structs",
"path": "./src/test/e2e-emulated/contracts/structs.tact",
"output": "./src/test/e2e-emulated/contracts/output"
},
{
"name": "initof",
"path": "./src/test/e2e-emulated/contracts/initof.tact",
"output": "./src/test/e2e-emulated/contracts/output",
"options": {
"debug": true
}
}
]
}

0 comments on commit 1be9e54

Please sign in to comment.