|
1 | | -import { expect, test } from 'vitest'; |
2 | | -import { parseSchemaType } from '../../src/lib/parse-schema'; |
| 1 | +import { describe, expect, test } from 'vitest'; |
| 2 | +import { parseSchema } from '../../src/lib/parse-schema'; |
3 | 3 | import { getBasicDocument } from '../mocks/get-basic-document'; |
4 | 4 |
|
5 | 5 | const docs = getBasicDocument(); |
6 | 6 |
|
7 | | -test('字符串', () => { |
8 | | - const type = parseSchemaType(docs, { type: 'string' }); |
9 | | - expect(type).toMatchInlineSnapshot(`"string"`); |
10 | | -}); |
| 7 | +describe('常规', () => { |
| 8 | + test('数字', () => { |
| 9 | + expect(parseSchema(docs, { type: 'number' })).toMatchInlineSnapshot(`"number"`); |
| 10 | + expect(parseSchema(docs, { type: 'integer' })).toMatchInlineSnapshot(`"number"`); |
| 11 | + }); |
11 | 12 |
|
12 | | -test('数字', () => { |
13 | | - expect(parseSchemaType(docs, { type: 'number' })).toMatchInlineSnapshot(`"number"`); |
14 | | - expect(parseSchemaType(docs, { type: 'integer' })).toMatchInlineSnapshot(`"number"`); |
15 | | -}); |
| 13 | + test('字符串', () => { |
| 14 | + const type = parseSchema(docs, { type: 'string' }); |
| 15 | + expect(type).toMatchInlineSnapshot(`"string"`); |
| 16 | + }); |
16 | 17 |
|
17 | | -test('布尔', () => { |
18 | | - const type = parseSchemaType(docs, { type: 'boolean' }); |
19 | | - expect(type).toMatchInlineSnapshot(`"boolean"`); |
20 | | -}); |
| 18 | + test('布尔', () => { |
| 19 | + const type = parseSchema(docs, { type: 'boolean' }); |
| 20 | + expect(type).toMatchInlineSnapshot(`"boolean"`); |
| 21 | + }); |
21 | 22 |
|
22 | | -test('数组', () => { |
23 | | - const type = parseSchemaType(docs, { type: 'array', items: { type: 'string' } }); |
24 | | - expect(type).toMatchInlineSnapshot(`"string[]"`); |
25 | | -}); |
| 23 | + test('数组', () => { |
| 24 | + const type = parseSchema(docs, { type: 'array', items: { type: 'string' } }); |
| 25 | + expect(type).toMatchInlineSnapshot(`"(string)[]"`); |
| 26 | + }); |
26 | 27 |
|
27 | | -test('对象数组', () => { |
28 | | - const type = parseSchemaType(docs, { |
29 | | - type: 'array', |
30 | | - items: { type: 'object', properties: { foo: { type: 'string' } }, required: ['foo'] }, |
| 28 | + test('对象数组', () => { |
| 29 | + const type = parseSchema(docs, { |
| 30 | + type: 'array', |
| 31 | + items: { |
| 32 | + type: 'object', |
| 33 | + properties: { foo: { type: 'string' } }, |
| 34 | + required: ['foo'], |
| 35 | + }, |
| 36 | + }); |
| 37 | + expect(type).toMatchInlineSnapshot(`"({ foo: string })[]"`); |
31 | 38 | }); |
32 | | - expect(type).toMatchInlineSnapshot(`"{ foo: string }[]"`); |
33 | | -}); |
34 | 39 |
|
35 | | -test('对象', () => { |
36 | | - const type = parseSchemaType(docs, { |
37 | | - type: 'object', |
38 | | - properties: { foo: { type: 'string' } }, |
| 40 | + test('对象', () => { |
| 41 | + const type = parseSchema(docs, { |
| 42 | + type: 'object', |
| 43 | + properties: { foo: { type: 'string' } }, |
| 44 | + }); |
| 45 | + expect(type).toMatchInlineSnapshot(`"{ foo?: string }"`); |
39 | 46 | }); |
40 | | - expect(type).toMatchInlineSnapshot(`"{ foo?: string }"`); |
41 | | -}); |
42 | 47 |
|
43 | | -test('对象属性包含描述', () => { |
44 | | - const type = parseSchemaType(docs, { |
45 | | - type: 'object', |
46 | | - properties: { foo: { type: 'string', description: 'foo=bar' } }, |
47 | | - }); |
48 | | - expect(type).toMatchInlineSnapshot(` |
49 | | - "{ |
50 | | - /** |
51 | | - * foo=bar |
52 | | - */ |
53 | | - foo?: string }" |
54 | | - `); |
55 | | -}); |
| 48 | + test('对象属性包含描述', () => { |
| 49 | + const type = parseSchema(docs, { |
| 50 | + type: 'object', |
| 51 | + properties: { foo: { type: 'string', description: 'foo=bar' } }, |
| 52 | + }); |
| 53 | + expect(type).toMatchInlineSnapshot(` |
| 54 | + "{ |
| 55 | + /** |
| 56 | + * foo=bar |
| 57 | + */ |
| 58 | + foo?: string }" |
| 59 | + `); |
| 60 | + }); |
56 | 61 |
|
57 | | -test('空对象', () => { |
58 | | - const type = parseSchemaType(docs, { type: 'object' }); |
59 | | - expect(type).toMatchInlineSnapshot(`"{ }"`); |
60 | | -}); |
| 62 | + test('空对象', () => { |
| 63 | + const type = parseSchema(docs, { type: 'object' }); |
| 64 | + expect(type).toMatchInlineSnapshot(`"{ }"`); |
| 65 | + }); |
| 66 | + |
| 67 | + test('文件', () => { |
| 68 | + const type = parseSchema(docs, { type: 'string', format: 'binary' }); |
| 69 | + expect(type).toMatchInlineSnapshot(`"Blob"`); |
| 70 | + }); |
61 | 71 |
|
62 | | -test('文件', () => { |
63 | | - const type = parseSchemaType(docs, { type: 'string', format: 'binary' }); |
64 | | - expect(type).toMatchInlineSnapshot(`"Blob"`); |
| 72 | + test('oneOf', () => { |
| 73 | + const type = parseSchema(docs, { |
| 74 | + oneOf: [{ type: 'string' }, { type: 'boolean' }, { type: 'integer' }], |
| 75 | + }); |
| 76 | + expect(type).toMatchInlineSnapshot(`"string | boolean | number"`); |
| 77 | + }); |
65 | 78 | }); |
66 | 79 |
|
67 | | -test('oneOf', () => { |
68 | | - const type = parseSchemaType(docs, { |
69 | | - oneOf: [{ type: 'string' }, { type: 'boolean' }, { type: 'integer' }], |
| 80 | +describe('nullable', () => { |
| 81 | + test('数字', () => { |
| 82 | + expect(parseSchema(docs, { type: 'number', nullable: true })).toMatchInlineSnapshot( |
| 83 | + `"number | null"`, |
| 84 | + ); |
| 85 | + }); |
| 86 | + |
| 87 | + test('字符串', () => { |
| 88 | + expect(parseSchema(docs, { type: 'string', nullable: true })).toMatchInlineSnapshot( |
| 89 | + `"string | null"`, |
| 90 | + ); |
| 91 | + }); |
| 92 | + |
| 93 | + test('布尔', () => { |
| 94 | + expect(parseSchema(docs, { type: 'boolean', nullable: true })).toMatchInlineSnapshot( |
| 95 | + `"boolean | null"`, |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + test('数组', () => { |
| 100 | + expect( |
| 101 | + parseSchema(docs, { type: 'array', items: { type: 'string' }, nullable: true }), |
| 102 | + ).toMatchInlineSnapshot(`"(string)[] | null"`); |
| 103 | + }); |
| 104 | + |
| 105 | + test('数组内', () => { |
| 106 | + expect( |
| 107 | + parseSchema(docs, { type: 'array', items: { type: 'string', nullable: true } }), |
| 108 | + ).toMatchInlineSnapshot(`"(string | null)[]"`); |
| 109 | + }); |
| 110 | + |
| 111 | + test('对象', () => { |
| 112 | + expect(parseSchema(docs, { type: 'object', nullable: true })).toMatchInlineSnapshot( |
| 113 | + `"{ } | null"`, |
| 114 | + ); |
| 115 | + }); |
| 116 | + |
| 117 | + test('对象内', () => { |
| 118 | + expect( |
| 119 | + parseSchema(docs, { |
| 120 | + type: 'object', |
| 121 | + properties: { foo: { type: 'string', nullable: true } }, |
| 122 | + }), |
| 123 | + ).toMatchInlineSnapshot(`"{ foo?: string | null }"`); |
| 124 | + }); |
| 125 | + |
| 126 | + test('二进制', () => { |
| 127 | + expect( |
| 128 | + parseSchema(docs, { type: 'string', format: 'binary', nullable: true }), |
| 129 | + ).toMatchInlineSnapshot(`"Blob | null"`); |
70 | 130 | }); |
71 | | - expect(type).toMatchInlineSnapshot(`"string | boolean | number"`); |
72 | 131 | }); |
73 | 132 |
|
74 | 133 | test('未知类型', () => { |
75 | 134 | // @ts-expect-error |
76 | | - const type = parseSchemaType(docs, { type: 'any' }); |
77 | | - expect(type).toMatchInlineSnapshot(`"never"`); |
| 135 | + const type = parseSchema(docs, { type: 'any' }); |
| 136 | + expect(type).toMatchInlineSnapshot(`"unknown"`); |
78 | 137 | }); |
0 commit comments