Skip to content

Commit

Permalink
add test cases for Xml lib (#4306)
Browse files Browse the repository at this point in the history
- set the attribute value via encodedName
- set the value on scalar via encodedName
- emit error if missing the 2nd argument

---------

Co-authored-by: Kyle Zhang <[email protected]>
  • Loading branch information
skywing918 and Kyle Zhang authored Sep 3, 2024
1 parent a5546ca commit 663d057
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions packages/xml/test/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ beforeEach(async () => {
});

describe("@name", () => {
it("set the value via encodedName", async () => {
const { Blob } = (await runner.compile(`@test @Xml.name("XmlBlob") model Blob {}`)) as {
Blob: Model;
};

expect(resolveEncodedName(runner.program, Blob, "application/xml")).toEqual("XmlBlob");
it.each([
["model", `@test @Xml.name("XmlName") model Blob {}`],
["model prop", `model Blob {@Xml.name("XmlName") @test title:string}`],
["scalar", `@Xml.name("XmlName") @test scalar Blob extends string;`],
])("%s", async (_, code) => {
const result = await runner.compile(`${code}`);
const curr = (result.Blob || result.title) as Model;
expect(resolveEncodedName(runner.program, curr, "application/xml")).toEqual("XmlName");
});
});

Expand Down Expand Up @@ -153,6 +155,19 @@ describe("@ns", () => {
});
});

it("emit error if missing the 2nd argument", async () => {
const diagnostics = await runner.diagnose(`
model Blob {
@Xml.ns("https://example.com/ns1") id : string;
}
`);

expectDiagnostics(diagnostics, {
code: "@typespec/xml/ns-missing-prefix",
message: "When using a string namespace you must provide a prefix as the 2nd argument.",
});
});

it("emit error if providing prefix param with enum member namespace", async () => {
const diagnostics = await runner.diagnose(`
@Xml.nsDeclarations
Expand Down

0 comments on commit 663d057

Please sign in to comment.