-
Notifications
You must be signed in to change notification settings - Fork 879
/
functions_test.ts
115 lines (105 loc) · 4.16 KB
/
functions_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* @license
* Copyright 2022 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
import {suite} from 'uvu';
// eslint-disable-next-line import/extensions
import * as assert from 'uvu/assert';
import {fileURLToPath} from 'url';
import {getSourceFilename, languages} from '../utils.js';
import {
createPackageAnalyzer,
Analyzer,
AbsolutePath,
Module,
} from '../../index.js';
for (const lang of languages) {
const test = suite<{
analyzer: Analyzer;
packagePath: AbsolutePath;
module: Module;
}>(`Module tests (${lang})`);
test.before((ctx) => {
try {
const packagePath = fileURLToPath(
new URL(`../../test-files/${lang}/functions`, import.meta.url).href
) as AbsolutePath;
const analyzer = createPackageAnalyzer(packagePath);
const result = analyzer.getPackage();
const file = getSourceFilename('functions', lang);
const module = result.modules.find((m) => m.sourcePath === file);
if (module === undefined) {
throw new Error(`Analyzer did not analyze file '${file}'`);
}
ctx.packagePath = packagePath;
ctx.analyzer = analyzer;
ctx.module = module;
} catch (error) {
// Uvu has a bug where it silently ignores failures in before and after,
// see https://github.com/lukeed/uvu/issues/191.
console.error('uvu before error', error);
process.exit(1);
}
});
test('Function 1', ({module}) => {
const exportedFn = module.getResolvedExport('function1');
const fn = module.getDeclaration('function1');
assert.equal(fn, exportedFn);
assert.ok(fn?.isFunctionDeclaration());
assert.equal(fn.name, `function1`);
assert.equal(fn.description, `Function 1 description`);
assert.equal(fn.summary, undefined);
assert.equal(fn.parameters?.length, 0);
assert.equal(fn.deprecated, undefined);
});
test('Function 2', ({module}) => {
const exportedFn = module.getResolvedExport('function2');
const fn = module.getDeclaration('function2');
assert.equal(fn, exportedFn);
assert.ok(fn?.isFunctionDeclaration());
assert.equal(fn.name, `function2`);
assert.equal(fn.summary, `Function 2 summary\nwith wraparound`);
assert.equal(fn.description, `Function 2 description\nwith wraparound`);
assert.equal(fn.parameters?.length, 3);
assert.equal(fn.parameters?.[0].name, 'a');
assert.equal(fn.parameters?.[0].description, 'Param a description');
assert.equal(fn.parameters?.[0].summary, undefined);
assert.equal(fn.parameters?.[0].type?.text, 'string');
assert.equal(fn.parameters?.[0].default, undefined);
assert.equal(fn.parameters?.[0].rest, false);
assert.equal(fn.parameters?.[1].name, 'b');
assert.equal(
fn.parameters?.[1].description,
'Param b description\nwith wraparound'
);
assert.equal(fn.parameters?.[1].type?.text, 'boolean');
assert.equal(fn.parameters?.[1].optional, true);
assert.equal(fn.parameters?.[1].default, 'false');
assert.equal(fn.parameters?.[1].rest, false);
assert.equal(fn.parameters?.[2].name, 'c');
assert.equal(fn.parameters?.[2].description, 'Param c description');
assert.equal(fn.parameters?.[2].summary, undefined);
assert.equal(fn.parameters?.[2].type?.text, 'number[]');
assert.equal(fn.parameters?.[2].optional, false);
assert.equal(fn.parameters?.[2].default, undefined);
assert.equal(fn.parameters?.[2].rest, true);
assert.equal(fn.return?.type?.text, 'string');
assert.equal(fn.return?.description, 'Function 2 return description');
assert.equal(fn.deprecated, 'Function 2 deprecated');
});
test('Default function', ({module}) => {
const exportedFn = module.getResolvedExport('default');
const fn = module.getDeclaration('default');
assert.equal(fn, exportedFn);
assert.ok(fn?.isFunctionDeclaration());
assert.equal(fn.name, `default`);
assert.equal(fn.description, `Default function description`);
assert.equal(fn.summary, undefined);
assert.equal(fn.parameters?.length, 0);
assert.equal(fn.return?.type?.text, 'string');
assert.equal(fn.return?.description, 'Default function return description');
assert.equal(fn.deprecated, undefined);
});
test.run();
}