Skip to content

Commit

Permalink
fix(ts-wrappers): fix TypeScript wrappers generation for messages wit…
Browse files Browse the repository at this point in the history
…h single quote (#1106)

Fixes #972
  • Loading branch information
i582 authored Dec 3, 2024
1 parent 60346a9 commit f5c84f2
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `as coins` map value serialization type is now handled correctly: PR [#987](https://github.com/tact-lang/tact/pull/987)
- Type checking for `foreach` loops in trait methods: PR [#1017](https://github.com/tact-lang/tact/pull/1017)
- The `sha256()` function no longer throws on statically known strings of any length: PR [#907](https://github.com/tact-lang/tact/pull/907)
- TypeScript wrappers generation for messages with single quote: PR [#1106](https://github.com/tact-lang/tact/pull/1106)

### Release contributors

Expand Down
4 changes: 2 additions & 2 deletions src/bindings/writeTypescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ export function writeTypescript(
r.message.text !== null &&
r.message.text !== undefined
) {
receivers.push(`'${r.message.text}'`);
receivers.push(JSON.stringify(r.message.text));
} else {
receivers.push(`string`);
}
Expand Down Expand Up @@ -420,7 +420,7 @@ export function writeTypescript(
w.append(`}`);
} else {
w.append(
`if (message === '${msg.text}') {`,
`if (message === ${JSON.stringify(msg.text)}) {`,
);
w.inIndent(() => {
w.append(
Expand Down
25 changes: 25 additions & 0 deletions src/test/e2e-emulated/contracts/text-message-receivers.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import "@stdlib/deploy";

contract TextMessageReceivers with Deployable {
counter: Int = 0;

receive("increment'") {
self.counter += 1;
}

receive("increment-2\"") {
self.counter += 2;
}

receive("increment-3`") {
self.counter += 3;
}

receive("\\increment-4\\") {
self.counter += 4;
}

get fun getCounter(): Int {
return self.counter;
}
}
89 changes: 89 additions & 0 deletions src/test/e2e-emulated/text-message-receivers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { toNano } from "@ton/core";
import { Blockchain, SandboxContract, TreasuryContract } from "@ton/sandbox";
import { TextMessageReceivers } from "./contracts/output/text-message-receivers_TextMessageReceivers";
import "@ton/test-utils";

describe("text-message-receivers", () => {
let blockchain: Blockchain;
let treasure: SandboxContract<TreasuryContract>;
let contract: SandboxContract<TextMessageReceivers>;

beforeEach(async () => {
blockchain = await Blockchain.create();
blockchain.verbosity.print = false;
treasure = await blockchain.treasury("treasure");

contract = blockchain.openContract(
await TextMessageReceivers.fromInit(),
);
});

it("should deploy", async () => {
// Deploy the contract
const deployResult = await contract.send(
treasure.getSender(),
{ value: toNano("1") },
{ $$type: "Deploy", queryId: 0n },
);

expect(deployResult.transactions).toHaveTransaction({
from: treasure.address,
to: contract.address,
success: true,
deploy: true,
});

// Verify initial state
expect(await contract.getGetCounter()).toBe(0n);
});

it("should increment counter with different text messages", async () => {
// Deploy the contract
const deployResult = await contract.send(
treasure.getSender(),
{ value: toNano("1") },
{ $$type: "Deploy", queryId: 0n },
);
expect(deployResult.transactions).toHaveTransaction({
from: treasure.address,
to: contract.address,
success: true,
deploy: true,
});

// Verify initial state
expect(await contract.getGetCounter()).toBe(0n);

const sendMessage = async (
message:
| "increment'"
| 'increment-2\\"'
| "increment-3`"
| "\\\\increment-4\\\\",
) => {
const incrementResult1 = await contract.send(
treasure.getSender(),
{ value: toNano("1") },
message,
);
expect(incrementResult1.transactions).toHaveTransaction({
from: treasure.address,
to: contract.address,
success: true,
});
};

// Increment counter
await sendMessage("increment'");
expect(await contract.getGetCounter()).toBe(1n);

await sendMessage('increment-2\\"');
expect(await contract.getGetCounter()).toBe(3n);

await sendMessage("increment-3`");
expect(await contract.getGetCounter()).toBe(6n);

await sendMessage("\\\\increment-4\\\\");
expect(await contract.getGetCounter()).toBe(10n);
});
});
8 changes: 8 additions & 0 deletions tact.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,14 @@
"path": "./src/test/e2e-emulated/contracts/asm-functions.tact",
"output": "./src/test/e2e-emulated/contracts/output"
},
{
"name": "text-message-receivers",
"path": "./src/test/e2e-emulated/contracts/text-message-receivers.tact",
"output": "./src/test/e2e-emulated/contracts/output",
"options": {
"debug": true
}
},
{
"name": "tact-reserved-contract-errors",
"path": "./src/test/exit-codes/contracts/tact-reserved-contract-errors.tact",
Expand Down

0 comments on commit f5c84f2

Please sign in to comment.