microsoft/typespec

Public

mirrored from https://github.com/microsoft/typespecAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
3040a83d6de0cc6876163b48ec9be61eefa3ebdd

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

packages/http/test/plaindata.test.ts

62lines · modecode

1import { TestHost } from "@typespec/compiler/testing";
2import { ok, strictEqual } from "assert";
3import { beforeEach, describe, it } from "vitest";
4import { isBody, isHeader, isPathParam, isQueryParam } from "../src/decorators.js";
5import { createHttpTestHost } from "./test-host.js";
6
7describe("http: plain data", () => {
8 let testHost: TestHost;
9
10 beforeEach(async () => {
11 testHost = await createHttpTestHost();
12 });
13
14 it("removes header/query/body/path", async () => {
15 testHost.addTypeSpecFile(
16 "main.tsp",
17 `
18 import "@typespec/http";
19 using TypeSpec.Http;
20
21 @test
22 model Before {
23 @header a: string;
24 @query b: string;
25 @path c: string;
26 @body d: string;
27 }
28
29 @test
30 model After is PlainData<Before> {}
31
32 @test
33 model Spread {
34 ...After
35 }
36 `
37 );
38
39 const { Before, After, Spread } = await testHost.compile("main.tsp");
40 const program = testHost.program;
41
42 strictEqual(Before.kind, "Model" as const);
43 ok(isHeader(program, Before.properties.get("a")!), "header expected");
44 ok(isBody(program, Before.properties.get("d")!), "body expected");
45 ok(isQueryParam(testHost.program, Before.properties.get("b")!), "query expected");
46 ok(isPathParam(testHost.program, Before.properties.get("c")!), "path expected");
47
48 for (const model of [After, Spread]) {
49 strictEqual(model.kind, "Model" as const);
50 ok(!isHeader(program, model.properties.get("a")!), `header not expected in ${model.name}`);
51 ok(!isBody(program, model.properties.get("d")!), `body not expected in ${model.name}`);
52 ok(
53 !isQueryParam(testHost.program, model.properties.get("b")!),
54 `query not expected in ${model.name}`
55 );
56 ok(
57 !isPathParam(testHost.program, model.properties.get("c")!),
58 `path not expected in ${model.name}`
59 );
60 }
61 });
62});