microsoft/typespec

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
a6b7827fcd2db3bf604d42e235216c8d5b244f13

Branches

Tags

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

Clone

HTTPS

Download ZIP

packages/http/test/plaindata.test.ts

61lines · modecode

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