Skip to content

Commit d1a8c62

Browse files
committed
adding tests for helper functions
1 parent c981f9b commit d1a8c62

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

test/unit/types/generate-types.js

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import { suite, test, expect } from 'vitest';
2+
import {
3+
normalizeClassName,
4+
generateTypeFromTag,
5+
generateParamDeclaration
6+
} from '../../../utils/helper.js';
7+
8+
// Sample data for tests
9+
const validData = [{
10+
"description": "",
11+
"tags": [
12+
{
13+
"title": "module",
14+
"description": null,
15+
"lineNumber": 1,
16+
"type": null,
17+
"name": "Math"
18+
},
19+
{
20+
"title": "submodule",
21+
"description": "Calculation",
22+
"lineNumber": 2
23+
},
24+
{
25+
"title": "for",
26+
"description": "p5",
27+
"lineNumber": 3
28+
},
29+
{
30+
"title": "requires",
31+
"description": null,
32+
"lineNumber": 4,
33+
"name": "core"
34+
}
35+
],
36+
"loc": {
37+
"start": {
38+
"line": 1,
39+
"column": 0,
40+
"index": 0
41+
},
42+
"end": {
43+
"line": 6,
44+
"column": 3,
45+
"index": 78
46+
}
47+
},
48+
"context": {
49+
"loc": {
50+
"start": {
51+
"line": 8,
52+
"column": 0,
53+
"index": 80
54+
},
55+
"end": {
56+
"line": 1116,
57+
"column": 1,
58+
"index": 28034
59+
}
60+
},
61+
"file": "C:\\Users\\diyas\\Documents\\p5.js\\src\\math\\calculation.js"
62+
},
63+
"augments": [],
64+
"examples": [],
65+
"implements": [],
66+
"params": [],
67+
"properties": [],
68+
"returns": [],
69+
"sees": [],
70+
"throws": [],
71+
"todos": [],
72+
"yields": [],
73+
"kind": "module",
74+
"name": "Math",
75+
"members": {
76+
"global": [],
77+
"inner": [],
78+
"instance": [],
79+
"events": [],
80+
"static": []
81+
},
82+
"path": [
83+
{
84+
"name": "Math",
85+
"kind": "module"
86+
}
87+
],
88+
"namespace": "Math"
89+
}];
90+
91+
const emptyData = [];
92+
const malformedData = [{ name: 'BrokenItem' }]; // Missing "kind", "members", etc.
93+
94+
suite('Helper Functions', () => {
95+
test('normalizeClassName should handle different class name formats', () => {
96+
expect(normalizeClassName('p5')).toBe('p5');
97+
expect(normalizeClassName('Vector')).toBe('p5.Vector');
98+
expect(normalizeClassName('p5.Color')).toBe('p5.Color');
99+
expect(normalizeClassName()).toBe('p5');
100+
});
101+
102+
test('generateTypeFromTag should handle different type expressions', () => {
103+
// Simple type
104+
expect(generateTypeFromTag({
105+
type: { type: 'NameExpression', name: 'Number' }
106+
})).toBe('number');
107+
108+
109+
expect(generateTypeFromTag({
110+
type: {
111+
type: 'TypeApplication',
112+
expression: { type: 'NameExpression', name: 'Array' },
113+
applications: [{ type: 'NameExpression', name: 'String' }]
114+
}
115+
})).toBe('string[]');
116+
117+
118+
expect(generateTypeFromTag({
119+
type: {
120+
type: 'UnionType',
121+
elements: [
122+
{ type: 'NameExpression', name: 'Number' },
123+
{ type: 'NameExpression', name: 'String' }
124+
]
125+
}
126+
})).toBe('number | string');
127+
});
128+
129+
test('generateParamDeclaration should format parameters correctly', () => {
130+
const requiredParam = {
131+
name: 'x',
132+
type: { type: 'NameExpression', name: 'Number' }
133+
};
134+
expect(generateParamDeclaration(requiredParam)).toBe('x: number');
135+
136+
const optionalParam = {
137+
name: 'y',
138+
type: {
139+
type: 'OptionalType',
140+
expression: { type: 'NameExpression', name: 'String' }
141+
}
142+
};
143+
expect(generateParamDeclaration(optionalParam)).toBe('y?: string');
144+
});
145+
});
146+

0 commit comments

Comments
 (0)