Skip to content

Commit beb20c3

Browse files
author
wangw19
committed
feat: 补充code传入方式获取jsonschema单测用例
1 parent 3d6cf1d commit beb20c3

File tree

6 files changed

+201
-2
lines changed

6 files changed

+201
-2
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Type类型 1`] = `
4+
Object {
5+
"anyOf": Array [
6+
Object {
7+
"type": "string",
8+
},
9+
Object {
10+
"enum": Object {
11+
"enum": Array [
12+
"home",
13+
"about",
14+
"contact",
15+
],
16+
},
17+
"type": "string",
18+
},
19+
],
20+
}
21+
`;
22+
23+
exports[`嵌套接口 1`] = `
24+
Object {
25+
"additionalProperties": false,
26+
"definitions": Object {
27+
"BBB": Object {
28+
"additionalProperties": false,
29+
"properties": Object {
30+
"some": Object {
31+
"type": "string",
32+
},
33+
},
34+
"required": Array [
35+
"some",
36+
],
37+
"type": "object",
38+
},
39+
},
40+
"properties": Object {
41+
"some": Object {
42+
"$ref": "#/definitions/BBB",
43+
},
44+
"title": Object {
45+
"type": "string",
46+
},
47+
},
48+
"required": Array [
49+
"title",
50+
"some",
51+
],
52+
"type": "object",
53+
}
54+
`;
55+
56+
exports[`嵌套泛型 1`] = `
57+
Object {
58+
"additionalProperties": false,
59+
"definitions": Object {},
60+
"properties": Object {
61+
"value1": Object {
62+
"type": "string",
63+
},
64+
"value2": Object {
65+
"type": "string",
66+
},
67+
},
68+
"required": Array [
69+
"value1",
70+
"value2",
71+
],
72+
"type": "object",
73+
}
74+
`;
75+
76+
exports[`枚举 1`] = `
77+
Object {
78+
"enum": Array [
79+
"Maintainer",
80+
"Developer",
81+
],
82+
"type": "string",
83+
}
84+
`;
85+
86+
exports[`泛型 1`] = `
87+
Object {
88+
"additionalProperties": false,
89+
"definitions": Object {
90+
"T": Object {},
91+
},
92+
"properties": Object {
93+
"value1": Object {
94+
"type": "string",
95+
},
96+
"value2": Object {
97+
"$ref": "#/definitions/T",
98+
},
99+
},
100+
"required": Array [
101+
"value1",
102+
"value2",
103+
],
104+
"type": "object",
105+
}
106+
`;
107+
108+
exports[`简单接口 1`] = `
109+
Object {
110+
"additionalProperties": false,
111+
"properties": Object {
112+
"some": Object {
113+
"type": "string",
114+
},
115+
},
116+
"required": Array [
117+
"some",
118+
],
119+
"type": "object",
120+
}
121+
`;

src/__tests__/__snapshots__/generic.test.ts.snap

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,28 @@ Object {
256256
}
257257
`;
258258

259+
exports[`Generic类型_3 1`] = `
260+
Object {
261+
"additionalProperties": false,
262+
"definitions": Object {
263+
"T": Object {},
264+
},
265+
"properties": Object {
266+
"value1": Object {
267+
"type": "string",
268+
},
269+
"value2": Object {
270+
"$ref": "#/definitions/T",
271+
},
272+
},
273+
"required": Array [
274+
"value1",
275+
"value2",
276+
],
277+
"type": "object",
278+
}
279+
`;
280+
259281
exports[`Generic默认值_类型_1 1`] = `
260282
Object {
261283
"additionalProperties": false,

src/__tests__/code.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import genTypeSchema from '../index';
2+
3+
const code = `
4+
import AAA from './aaa'
5+
export type Generic_10 = Generic_type_1<string>
6+
export interface Generic_type_1<T> {
7+
value1: string;
8+
value2: T;
9+
}
10+
interface BBB {
11+
some: string;
12+
}
13+
export enum Role {
14+
Maintainer = 'Maintainer',
15+
Developer = 'Developer',
16+
}
17+
type Page = 'home' | 'about' | 'contact' | string;
18+
interface PageInfo {
19+
title: string;
20+
some: BBB
21+
}
22+
`
23+
genTypeSchema.genJsonDataFromCode(code);
24+
25+
const getSchema = (type: string) => {
26+
const schema = genTypeSchema.getJsonSchema(type);
27+
return schema;
28+
};
29+
30+
test('枚举', () => {
31+
expect(getSchema('Role')).toMatchSnapshot();
32+
});
33+
34+
test('Type类型', () => {
35+
expect(getSchema('Page')).toMatchSnapshot();
36+
});
37+
38+
test('简单接口', () => {
39+
expect(getSchema('BBB')).toMatchSnapshot();
40+
});
41+
42+
test('嵌套接口', () => {
43+
expect(getSchema('PageInfo')).toMatchSnapshot();
44+
});
45+
46+
test('泛型', () => {
47+
expect(getSchema('Generic_type_1')).toMatchSnapshot();
48+
});
49+
50+
test('嵌套泛型', () => {
51+
expect(getSchema('Generic_10')).toMatchSnapshot();
52+
});
53+

src/__tests__/generic.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ test('Generic类型_2', () => {
2222
expect(getSchema('Generic_5')).toMatchSnapshot();
2323
});
2424

25+
test('Generic类型_3', () => {
26+
expect(getSchema('Generic_type_1')).toMatchSnapshot();
27+
});
28+
2529
test('Generic默认值_类型_1', () => {
2630
expect(getSchema('Generic_7')).toMatchSnapshot();
2731
});

src/get-jsonschema-from-data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default class genTypeSchema extends typescriptToFileDatas {
3232
*/
3333
getJsonSchema(
3434
file: string,
35-
type: string,
35+
type?: string,
3636
entry?: { keySet: Set<string>; refKeyTime: Record<string, number> },
3737
isLast?: boolean
3838
): AnyOption {

src/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export function genAst(file: string): AnyOption | null {
4444
* @return {*} {(AnyOption | null)}
4545
*/
4646
export function genAstFromCode(code: string): AnyOption | null {
47-
if (!code) return null;
4847
try {
4948
const ast: AstType = parse(code, parseConfig);
5049
return ast;

0 commit comments

Comments
 (0)