|
1 | | -import { Prose, namekey } from "@alloy-js/core"; |
| 1 | +import { Prose, namekey, refkey } from "@alloy-js/core"; |
2 | 2 | import { d } from "@alloy-js/core/testing"; |
3 | 3 | import { describe, expect, it } from "vitest"; |
4 | 4 | import { dataclassesModule } from "../src/builtins/python.js"; |
5 | 5 | import * as py from "../src/index.js"; |
6 | | -import { toSourceText } from "./utils.jsx"; |
| 6 | +import { assertFileContents, toSourceText, toSourceTextMultiple } from "./utils.jsx"; |
7 | 7 |
|
8 | 8 | describe("DataclassDeclaration", () => { |
9 | 9 | it("Creates a dataclass with a class doc", () => { |
@@ -582,4 +582,64 @@ describe("DataclassDeclaration", () => { |
582 | 582 | `, |
583 | 583 | ); |
584 | 584 | }); |
| 585 | + |
| 586 | + it("Forwards refkey prop for symbol resolution in type references", () => { |
| 587 | + const userRefkey = refkey(); |
| 588 | + const res = toSourceTextMultiple( |
| 589 | + [ |
| 590 | + <py.SourceFile path="models.py"> |
| 591 | + <py.DataclassDeclaration name="User" refkey={userRefkey}> |
| 592 | + <py.VariableDeclaration |
| 593 | + instanceVariable |
| 594 | + omitNone |
| 595 | + name="id" |
| 596 | + type="int" |
| 597 | + /> |
| 598 | + <py.VariableDeclaration |
| 599 | + instanceVariable |
| 600 | + omitNone |
| 601 | + name="name" |
| 602 | + type="str" |
| 603 | + /> |
| 604 | + </py.DataclassDeclaration> |
| 605 | + </py.SourceFile>, |
| 606 | + <py.SourceFile path="services.py"> |
| 607 | + <py.FunctionDeclaration name="get_user" returnType={userRefkey}> |
| 608 | + <py.VariableDeclaration |
| 609 | + name="user" |
| 610 | + type={userRefkey} |
| 611 | + initializer={ |
| 612 | + <py.ClassInstantiation |
| 613 | + target="User" |
| 614 | + args={['1', '"Alice"']} |
| 615 | + /> |
| 616 | + } |
| 617 | + /> |
| 618 | + <hbr /> |
| 619 | + {"return user"} |
| 620 | + </py.FunctionDeclaration> |
| 621 | + </py.SourceFile>, |
| 622 | + ], |
| 623 | + { externals: [dataclassesModule] }, |
| 624 | + ); |
| 625 | + assertFileContents(res, { |
| 626 | + "models.py": ` |
| 627 | + from dataclasses import dataclass |
| 628 | +
|
| 629 | + @dataclass |
| 630 | + class User: |
| 631 | + id: int |
| 632 | + name: str |
| 633 | +
|
| 634 | + `, |
| 635 | + "services.py": ` |
| 636 | + from models import User |
| 637 | +
|
| 638 | + def get_user() -> User: |
| 639 | + user: User = User(1, "Alice") |
| 640 | + return user |
| 641 | +
|
| 642 | + `, |
| 643 | + }); |
| 644 | + }); |
585 | 645 | }); |
0 commit comments