-
Notifications
You must be signed in to change notification settings - Fork 203
feat: add test stub generation #3342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
skywardboundd
wants to merge
34
commits into
main
Choose a base branch
from
3287-we-should-generate-test-template-as-wrappers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
587b516
feat: add test stub generation
skywardboundd aafb8dc
fmt
skywardboundd 621821a
may be fix
skywardboundd a9dc455
fmt
skywardboundd 95fde95
x2 fix
skywardboundd 2f0e22c
fix getter names
skywardboundd cdb4077
fix :(
skywardboundd 55f5585
use cleanText in text recievers
skywardboundd 9cd771c
back eslintrc
skywardboundd 7aea274
use mustache
skywardboundd c02a836
fmt
skywardboundd d2e85d9
review
skywardboundd 15b9eef
Merge remote-tracking branch 'origin/main' into 3287-we-should-genera…
skywardboundd 918d13c
fix
skywardboundd ab166e8
upd
skywardboundd df7ea9e
fix
skywardboundd 743c6bd
fix
skywardboundd eec818c
fmt
skywardboundd 7d7c54c
Merge branch 'main' into 3287-we-should-generate-test-template-as-wra…
skywardboundd 7e67feb
fix windows shvindows bobush vobush
skywardboundd 3da6ee7
Merge branch '3287-we-should-generate-test-template-as-wrappers' of h…
skywardboundd 8c5945e
fmt aggaaain
skywardboundd 916dec5
update docs
skywardboundd e885c60
Update docs/src/content/docs/book/debug.mdx
skywardboundd ada9062
Update docs/src/content/docs/book/debug.mdx
skywardboundd 39d2d0f
Update docs/src/content/docs/book/debug.mdx
skywardboundd ce0bb6a
Update docs/src/content/docs/book/debug.mdx
skywardboundd 4c33fb7
Update docs/src/content/docs/book/compile.mdx
skywardboundd 40cd7a7
Update docs/src/content/docs/book/compile.mdx
skywardboundd c94d3f6
Update docs/src/content/docs/book/debug.mdx
skywardboundd e946949
Update docs/src/content/docs/book/compile.mdx
skywardboundd 5ecc307
Update docs/src/content/docs/book/compile.mdx
skywardboundd e12ec2c
Update docs/src/content/docs/book/config.mdx
skywardboundd e6ad976
Update docs/src/content/docs/book/compile.mdx
skywardboundd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import * as fs from "node:fs/promises"; | ||
import * as path from "node:path"; | ||
import * as glob from "glob"; | ||
|
||
const cp = async (fromGlob: string, toPath: string) => { | ||
for (const file of glob.sync(path.join(fromGlob, "**/*"), { | ||
windowsPathsNoEscape: true, | ||
})) { | ||
const relPath = path.relative(fromGlob, file); | ||
const pathTo = path.join(toPath, relPath); | ||
const stat = await fs.stat(file); | ||
if (stat.isDirectory()) { | ||
await fs.mkdir(pathTo, { recursive: true }); | ||
} else { | ||
await fs.mkdir(path.dirname(pathTo), { recursive: true }); | ||
await fs.copyFile(file, pathTo); | ||
} | ||
} | ||
}; | ||
|
||
const main = async () => { | ||
try { | ||
await cp("./src/bindings/templates/", "./dist/bindings/templates/"); | ||
} catch (e) { | ||
console.error(e); | ||
process.exit(1); | ||
} | ||
}; | ||
|
||
void main(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// !!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/ | ||
|
||
{{#imports}} | ||
import {{{.}}}; | ||
{{/imports}} | ||
|
||
export const test{{contractName}} = () => { | ||
describe("{{contractName}} Contract", () => { | ||
// Test receivers | ||
{{#receivers}} | ||
test{{.}}(); | ||
{{/receivers}} | ||
// Test getters | ||
{{#getters}} | ||
getterTest{{.}}(); | ||
{{/getters}} | ||
}); | ||
}; | ||
|
||
const globalSetup = async () => { | ||
const blockchain = await Blockchain.create(); | ||
// @ts-ignore | ||
const contract = await blockchain.openContract(await {{contractName}}.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 }; | ||
}; | ||
|
||
{{#receiverBlocks}} | ||
{{{.}}} | ||
{{/receiverBlocks}} | ||
{{#getterBlocks}} | ||
{{{.}}} | ||
{{/getterBlocks}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { readFileSync } from "fs"; | ||
import { resolve } from "path"; | ||
import Mustache from "mustache"; | ||
import type { ABIArgument, ContractABI, ABIReceiver } from "@ton/core"; | ||
import type { WrappersConstantDescription } from "@/bindings/writeTypescript"; | ||
import type { CompilerContext } from "@/context/context"; | ||
import type { TypeDescription } from "@/types/types"; | ||
|
||
function getReceiverFunctionName(receiver: ABIReceiver): string { | ||
const receiverType = receiver.receiver; // 'internal' or 'external' | ||
const messageKind = receiver.message.kind; // 'empty', 'typed', 'text', 'any' | ||
|
||
let name = receiverType.charAt(0).toUpperCase() + receiverType.slice(1); // Internal or External | ||
|
||
switch (messageKind) { | ||
case "empty": | ||
name += "Empty"; | ||
break; | ||
case "typed": | ||
name += "Message"; | ||
name += receiver.message.type ?? "Typed"; | ||
break; | ||
case "text": | ||
name += "Text"; | ||
if (receiver.message.text) { | ||
const cleanText = receiver.message.text.replace( | ||
/[^a-zA-Z0-9]/g, | ||
"", | ||
); | ||
name += cleanText.charAt(0).toUpperCase() + cleanText.slice(1); | ||
} | ||
break; | ||
case "any": | ||
name += "Any"; | ||
break; | ||
default: | ||
name += "Unknown"; | ||
} | ||
|
||
return name; | ||
} | ||
|
||
export function writeTests( | ||
skywardboundd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
abi: ContractABI, | ||
_ctx: CompilerContext, | ||
_constants: readonly WrappersConstantDescription[], | ||
_contract: undefined | TypeDescription, | ||
generatedContractPath: string, | ||
_init?: { | ||
code: string; | ||
system: string | null; | ||
args: ABIArgument[]; | ||
prefix?: | ||
| { | ||
value: number; | ||
bits: number; | ||
} | ||
| undefined; | ||
}, | ||
) { | ||
const contractName = abi.name ?? "Contract"; | ||
|
||
const templateData = { | ||
contractName, | ||
imports: [ | ||
`{ ${contractName} } from '../${generatedContractPath}'`, | ||
'{ Blockchain, createShardAccount } from "@ton/sandbox"', | ||
], | ||
receivers: abi.receivers?.map(getReceiverFunctionName) ?? [], | ||
getters: abi.getters?.map((g) => g.name) ?? [], | ||
receiverBlocks: (abi.receivers ?? []).map((r) => { | ||
const fn = getReceiverFunctionName(r); | ||
return `const test${fn} = async () => { | ||
describe("${fn}", () => { | ||
const setup = async () => { | ||
return await globalSetup(); | ||
}; | ||
|
||
// !!THIS FILE IS GENERATED BY TACT. THIS FILE IS REGENERATED EVERY TIME, COPY IT TO YOUR PROJECT MANUALLY!! | ||
// TODO: You can write tests for ${fn} here | ||
|
||
it("should perform correctly", async () => { | ||
const { blockchain, contract, owner, notOwner } = await setup(); | ||
}); | ||
}); | ||
}; | ||
`; | ||
}), | ||
getterBlocks: (abi.getters ?? []).map((g) => { | ||
const fn = g.name; | ||
return `const getterTest${fn} = async () => { | ||
describe("${fn}", () => { | ||
const setup = async () => { | ||
return await globalSetup(); | ||
}; | ||
|
||
// !!THIS FILE IS GENERATED BY TACT. THIS FILE IS REGENERATED EVERY TIME, COPY IT TO YOUR PROJECT MANUALLY!! | ||
// TODO: You can write tests for ${fn} here | ||
|
||
it("should perform correctly", async () => { | ||
const { blockchain, contract, owner, notOwner } = await setup(); | ||
}); | ||
}); | ||
}; | ||
`; | ||
}), | ||
}; | ||
|
||
const templatePath = resolve(__dirname, "templates", "test.mustache"); | ||
const template = readFileSync(templatePath, "utf-8"); | ||
const rendered = Mustache.render(template, templateData); | ||
|
||
return rendered; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.