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