Skip to content

Commit 5148a04

Browse files
refactor: 拆分模块
1 parent c437e5d commit 5148a04

14 files changed

Lines changed: 488 additions & 468 deletions

packages/typed/tests/json/primitives-and-generics.ts renamed to packages/typed/tests/json/collections.ts

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,71 +4,6 @@ import { parse, toJSONSchema } from '@mirascript/typed';
44

55
const schema = (s: object) => ({ $schema: 'https://json-schema.org/draft/2020-12/schema', ...s });
66

7-
test('primitive JSON schemas', (t) => {
8-
t.deepEqual(toJSONSchema(parse('string')), schema({ type: 'string' }));
9-
t.deepEqual(toJSONSchema(parse('number')), schema({ type: 'number' }));
10-
t.deepEqual(toJSONSchema(parse('boolean')), schema({ type: 'boolean' }));
11-
t.deepEqual(toJSONSchema(parse('nil')), schema({ type: 'null' }));
12-
t.deepEqual(toJSONSchema(parse('array')), schema({ type: 'array', items: true }));
13-
t.deepEqual(toJSONSchema(parse('record')), schema({ type: 'object' }));
14-
t.deepEqual(toJSONSchema(parse('any')), schema({}));
15-
t.deepEqual(toJSONSchema(parse('unknown')), schema({}));
16-
t.deepEqual(toJSONSchema(parse('never')), schema({ not: true }));
17-
t.deepEqual(toJSONSchema(parse('extern')), schema({}));
18-
});
19-
20-
test('named type JSON schema', (t) => {
21-
t.deepEqual(toJSONSchema(parse('MyType')), schema({}));
22-
});
23-
24-
test('string literal JSON schema', (t) => {
25-
t.deepEqual(
26-
toJSONSchema(parse('"hello"')),
27-
schema({
28-
const: 'hello',
29-
}),
30-
);
31-
});
32-
33-
test('boolean literal JSON schemas', (t) => {
34-
t.deepEqual(toJSONSchema(parse('true')), schema({ const: true }));
35-
t.deepEqual(toJSONSchema(parse('false')), schema({ const: false }));
36-
});
37-
38-
test('literal union JSON schema', (t) => {
39-
t.deepEqual(toJSONSchema(parse('"on" | "off"')), schema({ enum: ['on', 'off'] }));
40-
});
41-
42-
test('mixed literal union JSON schema', (t) => {
43-
t.deepEqual(toJSONSchema(parse('"on" | true')), schema({ enum: ['on', true] }));
44-
});
45-
46-
test('preserve literal union and primitive types in mixed union JSON schema', (t) => {
47-
t.deepEqual(
48-
toJSONSchema(parse('string | "on" | "off"')),
49-
schema({
50-
anyOf: [{ type: 'string' }, { enum: ['on', 'off'] }],
51-
}),
52-
);
53-
});
54-
55-
test('mixed union JSON schema', (t) => {
56-
t.deepEqual(
57-
toJSONSchema(parse('| "private" | "public" | (search: string)')),
58-
schema({
59-
anyOf: [
60-
{
61-
type: 'object',
62-
properties: { search: { type: 'string' } },
63-
required: ['search'],
64-
additionalProperties: false,
65-
},
66-
{ enum: ['private', 'public'] },
67-
],
68-
}),
69-
);
70-
});
71-
727
test('array JSON schema', (t) => {
738
t.deepEqual(
749
toJSONSchema(parse('number[]')),
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import test from 'ava';
2+
import { REG_NUMBER } from '@mirascript/constants';
3+
import { parse, toJSONSchema } from '@mirascript/typed';
4+
5+
const schema = (s: object) => ({ $schema: 'https://json-schema.org/draft/2020-12/schema', ...s });
6+
7+
test('function type JSON schema', (t) => {
8+
t.deepEqual(toJSONSchema(parse('fn(arg: number, ..rest: string) -> boolean')), schema({ not: true }));
9+
t.deepEqual(toJSONSchema(parse('fn() -> number')), schema({ not: true }));
10+
t.deepEqual(toJSONSchema(parse('fn(callback: fn(result: string) -> any)')), schema({ not: true }));
11+
});
12+
13+
test('template type JSON schema', (t) => {
14+
for (const name of ['name', 'array', 'record', 'extern', 'any', 'unknown', 'name[]']) {
15+
t.deepEqual(
16+
toJSONSchema(parse(`"hello $(${name})"`)),
17+
schema({
18+
type: 'string',
19+
pattern: '^hello (.*?)$',
20+
}),
21+
);
22+
}
23+
t.deepEqual(
24+
toJSONSchema(parse('`value: $(string | number)`')),
25+
schema({
26+
type: 'string',
27+
pattern: `^value: (.*?)$`,
28+
}),
29+
);
30+
t.deepEqual(
31+
toJSONSchema(parse('`value: $(string & number)`')),
32+
schema({
33+
type: 'string',
34+
pattern: `^value: (.*?)$`,
35+
}),
36+
);
37+
t.deepEqual(
38+
toJSONSchema(parse('`value: $(boolean | number)`')),
39+
schema({
40+
type: 'string',
41+
pattern: `^value: (true|false|${REG_NUMBER.source})$`,
42+
}),
43+
);
44+
t.deepEqual(
45+
toJSONSchema(parse('`flag: $(true | false | nil)`')),
46+
schema({
47+
type: 'string',
48+
pattern: '^flag: (true|false)?$',
49+
}),
50+
);
51+
t.deepEqual(
52+
toJSONSchema(parse('`prefix$(nil)suffix`')),
53+
schema({
54+
type: 'string',
55+
pattern: '^prefix()suffix$',
56+
}),
57+
);
58+
t.deepEqual(
59+
toJSONSchema(parse('`prefix$(number | nil)suffix`')),
60+
schema({
61+
type: 'string',
62+
pattern: `^prefix(${REG_NUMBER.source})?suffix$`,
63+
}),
64+
);
65+
t.deepEqual(
66+
toJSONSchema(parse('`prefix$("x" | "y" | nil)suffix`')),
67+
schema({
68+
type: 'string',
69+
pattern: `^prefix(x|y)?suffix$`,
70+
}),
71+
);
72+
t.deepEqual(
73+
toJSONSchema(parse('`prefix$(boolean | "")suffix`')),
74+
schema({
75+
type: 'string',
76+
pattern: `^prefix(true|false)?suffix$`,
77+
}),
78+
);
79+
});
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import test from 'ava';
2+
import { parse, toJSONSchema } from '@mirascript/typed';
3+
4+
const schema = (s: object) => ({ $schema: 'https://json-schema.org/draft/2020-12/schema', ...s });
5+
6+
test('primitive JSON schemas', (t) => {
7+
t.deepEqual(toJSONSchema(parse('string')), schema({ type: 'string' }));
8+
t.deepEqual(toJSONSchema(parse('number')), schema({ type: 'number' }));
9+
t.deepEqual(toJSONSchema(parse('boolean')), schema({ type: 'boolean' }));
10+
t.deepEqual(toJSONSchema(parse('nil')), schema({ type: 'null' }));
11+
t.deepEqual(toJSONSchema(parse('array')), schema({ type: 'array', items: true }));
12+
t.deepEqual(toJSONSchema(parse('record')), schema({ type: 'object' }));
13+
t.deepEqual(toJSONSchema(parse('any')), schema({}));
14+
t.deepEqual(toJSONSchema(parse('unknown')), schema({}));
15+
t.deepEqual(toJSONSchema(parse('never')), schema({ not: true }));
16+
t.deepEqual(toJSONSchema(parse('extern')), schema({}));
17+
});
18+
19+
test('named type JSON schema', (t) => {
20+
t.deepEqual(toJSONSchema(parse('MyType')), schema({}));
21+
});
22+
23+
test('string literal JSON schema', (t) => {
24+
t.deepEqual(
25+
toJSONSchema(parse('"hello"')),
26+
schema({
27+
const: 'hello',
28+
}),
29+
);
30+
});
31+
32+
test('boolean literal JSON schemas', (t) => {
33+
t.deepEqual(toJSONSchema(parse('true')), schema({ const: true }));
34+
t.deepEqual(toJSONSchema(parse('false')), schema({ const: false }));
35+
});
36+
37+
test('literal union JSON schema', (t) => {
38+
t.deepEqual(toJSONSchema(parse('"on" | "off"')), schema({ enum: ['on', 'off'] }));
39+
});
40+
41+
test('mixed literal union JSON schema', (t) => {
42+
t.deepEqual(toJSONSchema(parse('"on" | true')), schema({ enum: ['on', true] }));
43+
});
44+
45+
test('preserve literal union and primitive types in mixed union JSON schema', (t) => {
46+
t.deepEqual(
47+
toJSONSchema(parse('string | "on" | "off"')),
48+
schema({
49+
anyOf: [{ type: 'string' }, { enum: ['on', 'off'] }],
50+
}),
51+
);
52+
});
53+
54+
test('mixed union JSON schema', (t) => {
55+
t.deepEqual(
56+
toJSONSchema(parse('| "private" | "public" | (search: string)')),
57+
schema({
58+
anyOf: [
59+
{
60+
type: 'object',
61+
properties: { search: { type: 'string' } },
62+
required: ['search'],
63+
additionalProperties: false,
64+
},
65+
{ enum: ['private', 'public'] },
66+
],
67+
}),
68+
);
69+
});

packages/typed/tests/json/records-and-templates.ts renamed to packages/typed/tests/json/records-and-edge-cases.ts

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import test from 'ava';
2-
import { REG_NUMBER } from '@mirascript/constants';
32
import { parse, toJSONSchema } from '@mirascript/typed';
43

54
const schema = (s: object) => ({ $schema: 'https://json-schema.org/draft/2020-12/schema', ...s });
@@ -108,77 +107,3 @@ test('record with string field name JSON schema', (t) => {
108107
}),
109108
);
110109
});
111-
112-
test('function type JSON schema', (t) => {
113-
t.deepEqual(toJSONSchema(parse('fn(arg: number, ..rest: string) -> boolean')), schema({ not: true }));
114-
t.deepEqual(toJSONSchema(parse('fn() -> number')), schema({ not: true }));
115-
t.deepEqual(toJSONSchema(parse('fn(callback: fn(result: string) -> any)')), schema({ not: true }));
116-
});
117-
118-
test('template type JSON schema', (t) => {
119-
for (const name of ['name', 'array', 'record', 'extern', 'any', 'unknown', 'name[]']) {
120-
t.deepEqual(
121-
toJSONSchema(parse(`"hello $(${name})"`)),
122-
schema({
123-
type: 'string',
124-
pattern: '^hello (.*?)$',
125-
}),
126-
);
127-
}
128-
t.deepEqual(
129-
toJSONSchema(parse('`value: $(string | number)`')),
130-
schema({
131-
type: 'string',
132-
pattern: `^value: (.*?)$`,
133-
}),
134-
);
135-
t.deepEqual(
136-
toJSONSchema(parse('`value: $(string & number)`')),
137-
schema({
138-
type: 'string',
139-
pattern: `^value: (.*?)$`,
140-
}),
141-
);
142-
t.deepEqual(
143-
toJSONSchema(parse('`value: $(boolean | number)`')),
144-
schema({
145-
type: 'string',
146-
pattern: `^value: (true|false|${REG_NUMBER.source})$`,
147-
}),
148-
);
149-
t.deepEqual(
150-
toJSONSchema(parse('`flag: $(true | false | nil)`')),
151-
schema({
152-
type: 'string',
153-
pattern: '^flag: (true|false)?$',
154-
}),
155-
);
156-
t.deepEqual(
157-
toJSONSchema(parse('`prefix$(nil)suffix`')),
158-
schema({
159-
type: 'string',
160-
pattern: '^prefix()suffix$',
161-
}),
162-
);
163-
t.deepEqual(
164-
toJSONSchema(parse('`prefix$(number | nil)suffix`')),
165-
schema({
166-
type: 'string',
167-
pattern: `^prefix(${REG_NUMBER.source})?suffix$`,
168-
}),
169-
);
170-
t.deepEqual(
171-
toJSONSchema(parse('`prefix$("x" | "y" | nil)suffix`')),
172-
schema({
173-
type: 'string',
174-
pattern: `^prefix(x|y)?suffix$`,
175-
}),
176-
);
177-
t.deepEqual(
178-
toJSONSchema(parse('`prefix$(boolean | "")suffix`')),
179-
schema({
180-
type: 'string',
181-
pattern: `^prefix(true|false)?suffix$`,
182-
}),
183-
);
184-
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import test from 'ava';
2+
import { parse, toJSONSchema } from '@mirascript/typed';
3+
4+
const schema = (s: object) => ({ $schema: 'https://json-schema.org/draft/2020-12/schema', ...s });
5+
6+
test('reflection type JSON schema', (t) => {
7+
t.deepEqual(toJSONSchema(parse('type(MyVar)')), schema({}));
8+
});
9+
10+
test('reflection type in union JSON schema', (t) => {
11+
t.deepEqual(toJSONSchema(parse('type(A) | string')), schema({}));
12+
});
13+
14+
test('never union JSON schema', (t) => {
15+
t.deepEqual(
16+
toJSONSchema({
17+
kind: 'union',
18+
types: [],
19+
}),
20+
schema({ not: true }),
21+
);
22+
t.deepEqual(
23+
toJSONSchema({
24+
kind: 'union',
25+
types: ['never', 'never'],
26+
}),
27+
schema({ not: true }),
28+
);
29+
t.deepEqual(
30+
toJSONSchema({
31+
kind: 'union',
32+
types: ['string', 'never'],
33+
}),
34+
schema({ type: 'string' }),
35+
);
36+
});

packages/typed/tests/json/tuples-and-reflection.ts renamed to packages/typed/tests/json/tuples.ts

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -130,35 +130,3 @@ test('nested tuple JSON schema', (t) => {
130130
}),
131131
);
132132
});
133-
134-
test('reflection type JSON schema', (t) => {
135-
t.deepEqual(toJSONSchema(parse('type(MyVar)')), schema({}));
136-
});
137-
138-
test('reflection type in union JSON schema', (t) => {
139-
t.deepEqual(toJSONSchema(parse('type(A) | string')), schema({}));
140-
});
141-
142-
test('never union JSON schema', (t) => {
143-
t.deepEqual(
144-
toJSONSchema({
145-
kind: 'union',
146-
types: [],
147-
}),
148-
schema({ not: true }),
149-
);
150-
t.deepEqual(
151-
toJSONSchema({
152-
kind: 'union',
153-
types: ['never', 'never'],
154-
}),
155-
schema({ not: true }),
156-
);
157-
t.deepEqual(
158-
toJSONSchema({
159-
kind: 'union',
160-
types: ['string', 'never'],
161-
}),
162-
schema({ type: 'string' }),
163-
);
164-
});

0 commit comments

Comments
 (0)