Skip to content

feat: add test stub generation#3342

Open
skywardboundd wants to merge 34 commits into
mainfrom
3287-we-should-generate-test-template-as-wrappers
Open

feat: add test stub generation#3342
skywardboundd wants to merge 34 commits into
mainfrom
3287-we-should-generate-test-template-as-wrappers

Conversation

@skywardboundd

Copy link
Copy Markdown
Contributor

Closes #3287

@skywardboundd skywardboundd linked an issue Jun 2, 2025 that may be closed by this pull request
@skywardboundd skywardboundd marked this pull request as ready for review June 2, 2025 14:20
@skywardboundd skywardboundd requested a review from a team as a code owner June 2, 2025 14:20
@skywardboundd

skywardboundd commented Jun 2, 2025

Copy link
Copy Markdown
Contributor Author

Example

// !!THIS FILE IS GENERATED BY TACT. THIS FILE IS REGENERATED EVERY TIME, COPY IT TO YOUR PROJECT MANUALLY!!
// https://docs.tact-lang.org/book/debug/

import { Escrow } from './escrow_Escrow';
import { Blockchain, createShardAccount } from "@ton/sandbox";

export type FromInitEscrow = typeof Escrow.fromInit;

export type TestCase = (fromInit: FromInitEscrow) => void;

export const testEscrow = (fromInit: FromInitEscrow) => {
    describe("Escrow Contract", () => {
        // Test receivers
        testInternalMessageFunding(fromInit);
        testInternalMessageUpdateJettonWalletCode(fromInit);
        testInternalMessageJettonNotification(fromInit);
        testInternalMessageApprove(fromInit);
        testInternalMessageCancel(fromInit);
        testInternalMessageProvideEscrowData(fromInit);
        // Test getters
        getterTestcalculateRoyaltyAmount(fromInit);
        getterTestwalletAddress(fromInit);
        getterTestescrowInfo(fromInit);
    });
};

const globalSetup = async (fromInit: FromInitEscrow) => {
    const blockchain = await Blockchain.create();
    // @ts-ignore
    const contract = await blockchain.openContract(await fromInit(
        // TODO: implement default values
    ));
        
    // Universal method for deploy contract without sending message
    await blockchain.setShardAccount(contract.address, createShardAccount({
        address: contract.address,
        code: contract.init!.code,
        data: contract.init!.data,
        balance: 0n,
        workchain: 0
    }));
        
    const owner = await blockchain.treasury("owner");
    const notOwner = await blockchain.treasury("notOwner");
    
    return { blockchain, contract, owner, notOwner };
};

const testInternalMessageFunding: TestCase = (fromInit) => {
    describe("InternalMessageFunding", () => {
        const setup = async () => {
            return await globalSetup(fromInit);
        };
        
        // !!THIS FILE IS GENERATED BY TACT. THIS FILE IS REGENERATED EVERY TIME, COPY IT TO YOUR PROJECT MANUALLY!!
        // TODO: You can write tests for InternalMessageFunding here
        
        it("should perform correctly", async () => {
            const { blockchain, contract, owner, notOwner } = await setup();
        });
    });
};
...

@Kaladin13 Kaladin13 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea behind the PR, it's very fresh and could be useful

However, I think we should discuss the end result more, because all Tact users would be affected by such test template generation, so it needs to be polished

One way is to move it to CLI instead of default build pipeline, or maybe add flag to the tact.config

Comment thread src/pipeline/tests.ts
Comment thread src/bindings/writeTests.ts
Comment thread src/bindings/writeTests.ts Outdated
Comment thread src/bindings/writeTests.ts Outdated
Comment thread src/bindings/writeTests.ts Outdated
Comment thread src/bindings/writeTests.ts Outdated
Comment thread src/bindings/writeTests.ts Outdated
@xpyctumo

xpyctumo commented Jun 2, 2025

Copy link
Copy Markdown
Contributor

I think maintaining a template file will be clearer and easier than generating it on-fly with many f.write functions

// !!THIS FILE IS GENERATED BY TACT. COPY IT TO YOUR PROJECT MANUALLY!!
// https://docs.tact-lang.org/book/debug/

{{#imports}}
import {{.}};
{{/imports}}

export type FromInit{{contractName}} = typeof {{contractName}}.fromInit;
export type TestCase = (fromInit: FromInit{{contractName}}) => void;

export const test{{contractName}} = (fromInit: FromInit{{contractName}}) => {
    describe("{{contractName}} Contract", () => {
        // Test receivers
        {{#receivers}}
        test{{.}}(fromInit);
        {{/receivers}}

        // Test getters
        {{#getters}}
        getterTest{{.}}(fromInit);
        {{/getters}}
    });
};

const globalSetup = async (fromInit: FromInit{{contractName}}) => {
    const blockchain = await Blockchain.create();
    // @ts-ignore
    const contract = await blockchain.openContract(await fromInit(
        // TODO: implement default values
    ));

    // Universal deploy without message
    await blockchain.setShardAccount(contract.address, createShardAccount({
        address: contract.address,
        code: contract.init!.code,
        data: contract.init!.data,
        balance: 0n,
        workchain: 0,
    }));

    const owner = await blockchain.treasury("owner");
    const notOwner = await blockchain.treasury("notOwner");

    return { blockchain, contract, owner, notOwner };
};

{{#receiverBlocks}}
{{{.}}}
{{/receiverBlocks}}

{{#getterBlocks}}
{{{.}}}
{{/getterBlocks}}

// entry point
test{{contractName}}({{contractName}}.fromInit.bind({{contractName}}));

@skywardboundd skywardboundd marked this pull request as draft June 5, 2025 11:45
@Kaladin13 Kaladin13 requested review from Kaladin13 and novusnota June 6, 2025 09:40
Kaladin13
Kaladin13 previously approved these changes Jun 6, 2025

@Kaladin13 Kaladin13 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work here! PR approved, please fix small nitpicks :shipit:

Comment thread docs/src/content/docs/book/debug.mdx Outdated
Comment thread docs/src/content/docs/book/debug.mdx

@novusnota novusnota left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! Left a couple of nitpicks to resolve altogether

Comment thread docs/src/content/docs/book/config.mdx Outdated
Comment thread docs/src/content/docs/book/compile.mdx Outdated
Comment thread docs/src/content/docs/book/compile.mdx Outdated
Comment thread docs/src/content/docs/book/compile.mdx Outdated
Comment thread docs/src/content/docs/book/compile.mdx Outdated
Comment thread docs/src/content/docs/book/debug.mdx Outdated
Comment thread docs/src/content/docs/book/debug.mdx
Comment thread docs/src/content/docs/book/debug.mdx Outdated
Comment thread docs/src/content/docs/book/debug.mdx Outdated
Comment thread docs/src/content/docs/book/debug.mdx Outdated

@novusnota novusnota left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, chose the wrong radio button :)

skywardboundd and others added 11 commits June 6, 2025 15:51
Co-authored-by: Novus Nota <68142933+novusnota@users.noreply.github.com>
Co-authored-by: Novus Nota <68142933+novusnota@users.noreply.github.com>
Co-authored-by: Novus Nota <68142933+novusnota@users.noreply.github.com>
Co-authored-by: Novus Nota <68142933+novusnota@users.noreply.github.com>
Co-authored-by: Novus Nota <68142933+novusnota@users.noreply.github.com>
Co-authored-by: Novus Nota <68142933+novusnota@users.noreply.github.com>
Co-authored-by: Novus Nota <68142933+novusnota@users.noreply.github.com>
Co-authored-by: Novus Nota <68142933+novusnota@users.noreply.github.com>
Co-authored-by: Novus Nota <68142933+novusnota@users.noreply.github.com>
Co-authored-by: Novus Nota <68142933+novusnota@users.noreply.github.com>
Co-authored-by: Novus Nota <68142933+novusnota@users.noreply.github.com>
@skywardboundd skywardboundd marked this pull request as ready for review June 6, 2025 12:53

@novusnota novusnota left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

We should generate test template as wrappers

4 participants