Skip to content

Commit ef2e6a1

Browse files
committed
test(printer): 重构测试用例以验证代码生成的准确性- 修改测试用例以使用新的打印结果结构
- 增加对类型定义和 Zod验证代码的断言 - 更新示例测试以使用新的配置参数 - 优化代码格式和断言逻辑以提高可读性
1 parent e3739ba commit ef2e6a1

13 files changed

+1191
-941
lines changed

test/printer/arg.test.ts

Lines changed: 158 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -59,41 +59,48 @@ it('1*path + 1*query + 1*header', () => {
5959
},
6060
},
6161
});
62+
const result = printer.print({
63+
hideImports: true,
64+
hideHeaders: true,
65+
hideFooters: true,
66+
hideInfo: true,
67+
hideAlert: true,
68+
});
6269

63-
expect(
64-
printer.print({
65-
hideImports: true,
66-
hideHeaders: true,
67-
hideFooters: true,
68-
hideInfo: true,
69-
hideAlert: true,
70-
}),
71-
).toMatchInlineSnapshot(`
72-
"export type GetPetPath = string;
73-
export type GetPetData = string;
74-
export type GetPetHeaders = string;
75-
export type GetPetParams = string;
76-
export type GetPetResponse = string;
77-
78-
/**
70+
expect(result.main.code).toMatchInlineSnapshot(`
71+
"/**
7972
* @param petId request path "pet-id"
8073
* @param data request data
8174
* @param [xAuthKey] request headers "x-auth-key"
8275
* @param [categoryId] request params "category-id"
8376
* @param [config] request config
8477
* @returns pet name
8578
*/
86-
export async function getPet(petId:GetPetPath,data:GetPetData,xAuthKey?:GetPetHeaders,categoryId?:GetPetParams,config?:AxiosRequestConfig): Promise<AxiosResponse<GetPetResponse>> {
87-
return axios({
88-
method: "GET",
89-
url: \`/pets/\${petId}\`,
79+
export async function getPet(petId:Type.GetPetPath,data:Type.GetPetData,xAuthKey?:Type.GetPetHeaders,categoryId?:Type.GetPetParams,config?:AxiosRequestConfig): Promise<AxiosResponse<Type.GetPetResponse>> {
80+
return axios({
81+
method: "GET",
82+
url: \`/pets/\${petId}\`,
9083
data: data,
9184
headers: {"x-auth-key": xAuthKey},
9285
params: {"category-id": categoryId},
9386
...config
94-
});
87+
})
9588
}"
9689
`);
90+
expect(result.type.code).toMatchInlineSnapshot(`
91+
"export type GetPetPath = string;
92+
export type GetPetData = string;
93+
export type GetPetHeaders = string;
94+
export type GetPetParams = string;
95+
export type GetPetResponse = string;"
96+
`);
97+
expect(result.zod.code).toMatchInlineSnapshot(`
98+
"export const zGetPetPath = z.string();
99+
export const zGetPetData = z.string();
100+
export const zGetPetHeaders = z.string();
101+
export const zGetPetParams = z.string();
102+
export const zGetPetResponse = z.string();"
103+
`);
97104
});
98105

99106
it('n*path + 1*query + 1*header', () => {
@@ -151,41 +158,51 @@ it('n*path + 1*query + 1*header', () => {
151158
},
152159
});
153160

154-
expect(
155-
printer.print({
156-
hideImports: true,
157-
hideHeaders: true,
158-
hideFooters: true,
159-
hideInfo: true,
160-
hideAlert: true,
161-
}),
162-
).toMatchInlineSnapshot(`
163-
"export type GetPetPath = {
164-
"pet-id":string;
165-
"zoo-id":string;
166-
};
167-
export type GetPetData = string;
168-
export type GetPetHeaders = string;
169-
export type GetPetParams = string;
161+
const result = printer.print({
162+
hideImports: true,
163+
hideHeaders: true,
164+
hideFooters: true,
165+
hideInfo: true,
166+
hideAlert: true,
167+
});
170168

171-
/**
169+
expect(result.main.code).toMatchInlineSnapshot(`
170+
"/**
172171
* @param path request path
173172
* @param data request data
174173
* @param [xAuthKey] request headers "x-auth-key"
175174
* @param [categoryId] request params "category-id"
176175
* @param [config] request config
177176
*/
178-
export async function getPet(path:GetPetPath,data:GetPetData,xAuthKey?:GetPetHeaders,categoryId?:GetPetParams,config?:AxiosRequestConfig): Promise<AxiosResponse<unknown>> {
179-
return axios({
180-
method: "GET",
181-
url: \`/zoo/\${path["zoo-id"]}/pets/\${path["pet-id"]}\`,
177+
export async function getPet(path:Type.GetPetPath,data:Type.GetPetData,xAuthKey?:Type.GetPetHeaders,categoryId?:Type.GetPetParams,config?:AxiosRequestConfig): Promise<AxiosResponse<unknown>> {
178+
return axios({
179+
method: "GET",
180+
url: \`/zoo/\${path["zoo-id"]}/pets/\${path["pet-id"]}\`,
182181
data: data,
183182
headers: {"x-auth-key": xAuthKey},
184183
params: {"category-id": categoryId},
185184
...config
186-
});
185+
})
187186
}"
188187
`);
188+
expect(result.type.code).toMatchInlineSnapshot(`
189+
"export type GetPetPath = {
190+
"pet-id":string;
191+
"zoo-id":string;
192+
};
193+
export type GetPetData = string;
194+
export type GetPetHeaders = string;
195+
export type GetPetParams = string;"
196+
`);
197+
expect(result.zod.code).toMatchInlineSnapshot(`
198+
"export const zGetPetPath = z.object({
199+
"pet-id": z.string(),
200+
"zoo-id": z.string(),
201+
});
202+
export const zGetPetData = z.string();
203+
export const zGetPetHeaders = z.string();
204+
export const zGetPetParams = z.string();"
205+
`);
189206
});
190207

191208
it('n*path + n*query + 1*header', () => {
@@ -250,44 +267,57 @@ it('n*path + n*query + 1*header', () => {
250267
},
251268
});
252269

253-
expect(
254-
printer.print({
255-
hideImports: true,
256-
hideHeaders: true,
257-
hideFooters: true,
258-
hideInfo: true,
259-
hideAlert: true,
260-
}),
261-
).toMatchInlineSnapshot(`
262-
"export type GetPetPath = {
263-
"pet-id":string;
264-
"zoo-id":string;
265-
};
266-
export type GetPetData = string;
267-
export type GetPetHeaders = string;
268-
export type GetPetParams = {
269-
"category-id"?:string;
270-
"kind-id"?:string;
271-
};
270+
const result = printer.print({
271+
hideImports: true,
272+
hideHeaders: true,
273+
hideFooters: true,
274+
hideInfo: true,
275+
hideAlert: true,
276+
});
272277

273-
/**
278+
expect(result.main.code).toMatchInlineSnapshot(`
279+
"/**
274280
* @param path request path
275281
* @param data request data
276282
* @param [xAuthKey] request headers "x-auth-key"
277283
* @param [params] request params
278284
* @param [config] request config
279285
*/
280-
export async function getPet(path:GetPetPath,data:GetPetData,xAuthKey?:GetPetHeaders,params?:GetPetParams,config?:AxiosRequestConfig): Promise<AxiosResponse<unknown>> {
281-
return axios({
282-
method: "GET",
283-
url: \`/zoo/\${path["zoo-id"]}/pets/\${path["pet-id"]}\`,
286+
export async function getPet(path:Type.GetPetPath,data:Type.GetPetData,xAuthKey?:Type.GetPetHeaders,params?:Type.GetPetParams,config?:AxiosRequestConfig): Promise<AxiosResponse<unknown>> {
287+
return axios({
288+
method: "GET",
289+
url: \`/zoo/\${path["zoo-id"]}/pets/\${path["pet-id"]}\`,
284290
data: data,
285291
headers: {"x-auth-key": xAuthKey},
286292
params: params,
287293
...config
288-
});
294+
})
289295
}"
290296
`);
297+
expect(result.type.code).toMatchInlineSnapshot(`
298+
"export type GetPetPath = {
299+
"pet-id":string;
300+
"zoo-id":string;
301+
};
302+
export type GetPetData = string;
303+
export type GetPetHeaders = string;
304+
export type GetPetParams = {
305+
"category-id"?:string;
306+
"kind-id"?:string;
307+
};"
308+
`);
309+
expect(result.zod.code).toMatchInlineSnapshot(`
310+
"export const zGetPetPath = z.object({
311+
"pet-id": z.string(),
312+
"zoo-id": z.string(),
313+
});
314+
export const zGetPetData = z.string();
315+
export const zGetPetHeaders = z.string();
316+
export const zGetPetParams = z.object({
317+
"category-id": z.optional(z.string()),
318+
"kind-id": z.optional(z.string()),
319+
});"
320+
`);
291321
});
292322

293323
it('n*path + n*query + n*header', () => {
@@ -359,47 +389,63 @@ it('n*path + n*query + n*header', () => {
359389
},
360390
});
361391

362-
expect(
363-
printer.print({
364-
hideImports: true,
365-
hideHeaders: true,
366-
hideFooters: true,
367-
hideInfo: true,
368-
hideAlert: true,
369-
}),
370-
).toMatchInlineSnapshot(`
371-
"export type GetPetPath = {
372-
"pet-id":string;
373-
"zoo-id":string;
374-
};
375-
export type GetPetData = string;
376-
export type GetPetHeaders = {
377-
"x-auth-key"?:string;
378-
"x-auth-ver"?:string;
379-
};
380-
export type GetPetParams = {
381-
"category-id"?:string;
382-
"kind-id"?:string;
383-
};
392+
const result = printer.print({
393+
hideImports: true,
394+
hideHeaders: true,
395+
hideFooters: true,
396+
hideInfo: true,
397+
hideAlert: true,
398+
});
384399

385-
/**
400+
expect(result.main.code).toMatchInlineSnapshot(`
401+
"/**
386402
* @param path request path
387403
* @param data request data
388404
* @param [headers] request headers
389405
* @param [params] request params
390406
* @param [config] request config
391407
*/
392-
export async function getPet(path:GetPetPath,data:GetPetData,headers?:GetPetHeaders,params?:GetPetParams,config?:AxiosRequestConfig): Promise<AxiosResponse<unknown>> {
393-
return axios({
394-
method: "GET",
395-
url: \`/zoo/\${path["zoo-id"]}/pets/\${path["pet-id"]}\`,
408+
export async function getPet(path:Type.GetPetPath,data:Type.GetPetData,headers?:Type.GetPetHeaders,params?:Type.GetPetParams,config?:AxiosRequestConfig): Promise<AxiosResponse<unknown>> {
409+
return axios({
410+
method: "GET",
411+
url: \`/zoo/\${path["zoo-id"]}/pets/\${path["pet-id"]}\`,
396412
data: data,
397413
headers: headers,
398414
params: params,
399415
...config
400-
});
416+
})
401417
}"
402418
`);
419+
expect(result.type.code).toMatchInlineSnapshot(`
420+
"export type GetPetPath = {
421+
"pet-id":string;
422+
"zoo-id":string;
423+
};
424+
export type GetPetData = string;
425+
export type GetPetHeaders = {
426+
"x-auth-key"?:string;
427+
"x-auth-ver"?:string;
428+
};
429+
export type GetPetParams = {
430+
"category-id"?:string;
431+
"kind-id"?:string;
432+
};"
433+
`);
434+
expect(result.zod.code).toMatchInlineSnapshot(`
435+
"export const zGetPetPath = z.object({
436+
"pet-id": z.string(),
437+
"zoo-id": z.string(),
438+
});
439+
export const zGetPetData = z.string();
440+
export const zGetPetHeaders = z.object({
441+
"x-auth-key": z.optional(z.string()),
442+
"x-auth-ver": z.optional(z.string()),
443+
});
444+
export const zGetPetParams = z.object({
445+
"category-id": z.optional(z.string()),
446+
"kind-id": z.optional(z.string()),
447+
});"
448+
`);
403449
});
404450

405451
it('path name unique', () => {
@@ -427,27 +473,27 @@ it('path name unique', () => {
427473
},
428474
});
429475

430-
expect(
431-
printer.print({
432-
hideImports: true,
433-
hideHeaders: true,
434-
hideFooters: true,
435-
hideInfo: true,
436-
hideAlert: true,
437-
}),
438-
).toMatchInlineSnapshot(`
439-
"export type GetPetPath = string;
476+
const result = printer.print({
477+
hideImports: true,
478+
hideHeaders: true,
479+
hideFooters: true,
480+
hideInfo: true,
481+
hideAlert: true,
482+
});
440483

441-
/**
484+
expect(result.main.code).toMatchInlineSnapshot(`
485+
"/**
442486
* @param type request path "type"
443487
* @param [config] request config
444488
*/
445-
export async function getPet(type:GetPetPath,config?:AxiosRequestConfig): Promise<AxiosResponse<unknown>> {
446-
return axios({
447-
method: "GET",
448-
url: \`/pets/\${type}\`,
489+
export async function getPet(type:Type.GetPetPath,config?:AxiosRequestConfig): Promise<AxiosResponse<unknown>> {
490+
return axios({
491+
method: "GET",
492+
url: \`/pets/\${type}\`,
449493
...config
450-
});
494+
})
451495
}"
452496
`);
497+
expect(result.type.code).toMatchInlineSnapshot(`"export type GetPetPath = string;"`);
498+
expect(result.zod.code).toMatchInlineSnapshot(`"export const zGetPetPath = z.string();"`);
453499
});

test/printer/example.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ import { exampleTest } from '../helpers';
55
// @ref https://github.com/swagger-api/swagger-petstore/tree/v31?tab=readme-ov-file
66

77
it('pet-store2.0', () => {
8-
exampleTest('2.0', 'pet-store', (document) => {
9-
const printer = new Printer(migrate(document));
10-
return printer.print();
8+
exampleTest('2.0', 'pet-store', (document, configs) => {
9+
const printer = new Printer(migrate(document), { runtimeValidate: true });
10+
return printer.print(configs);
1111
});
1212
});
1313

1414
it('pet-store3.0', () => {
15-
exampleTest('3.0', 'pet-store', (document) => {
16-
const printer = new Printer(migrate(document));
17-
return printer.print();
15+
exampleTest('3.0', 'pet-store', (document, configs) => {
16+
const printer = new Printer(migrate(document), { runtimeValidate: true });
17+
return printer.print(configs);
1818
});
1919
});
2020

2121
it('pet-store3.1', () => {
22-
exampleTest('3.1', 'pet-store', (document) => {
23-
const printer = new Printer(migrate(document));
24-
return printer.print();
22+
exampleTest('3.1', 'pet-store', (document, configs) => {
23+
const printer = new Printer(migrate(document), { runtimeValidate: true });
24+
return printer.print(configs);
2525
});
2626
});

0 commit comments

Comments
 (0)