-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-utils.ts
More file actions
72 lines (69 loc) · 1.92 KB
/
test-utils.ts
File metadata and controls
72 lines (69 loc) · 1.92 KB
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
// @ts-expect-error TS2307
import { ELLanguage, expressionlanguage, _utils } from "@valtzu/codemirror-lang-el";
import { EditorState } from "@codemirror/state";
import { syntaxTree } from "@codemirror/language";
import * as assert from "node:assert";
const config = {
types: {
"custom44": {
identifiers: [
{name: "property11", type: ["any"]},
{name: "property22", type: ["any"]},
],
functions: [
{name: "firstMethod", args: [], returnType: ["custom44"]},
]
}
},
identifiers: [
{name: "foobar"},
{name: "foobaz"},
{name: "obj", type: ["custom44"]},
// Typed array examples
{name: "arr", type: ["string[]"]},
{name: "arr2", type: ["string[][]"]}
],
functions: [
{name: "smh", args: [], returnType: ["string"]},
{name: "smash_my_head", args: [{name: "object"}]},
{name: "getObject", returnType: ["custom44"]},
],
};
function get(doc: string) {
return EditorState.create({
doc,
selection: {anchor: 0},
extensions: [
ELLanguage.data.of({
expressionLanguageConfig: config,
}),
expressionlanguage(config)
],
});
}
describe("Type resolving", () => {
[
['obj', 'custom44'],
['true', 'bool'],
['true || true', 'bool'],
['obj ?? true', 'custom44|bool'],
['(obj ?? true)', 'custom44|bool'],
['true or false', 'bool'],
['1 + 2', 'number'],
['not 1', 'bool'],
['!1', 'bool'],
['+false', 'number'],
['-true', 'number'],
// Typed array indexing should yield element type
['arr[0]', 'string'],
// Nested array indexing: string[][] -> string[] then string
['arr2[0]', 'string[]'],
['arr2[0][0]', 'string'],
].forEach(([doc, type]) =>
it(`${doc} -> ${type}`, () => {
const state = get(doc);
const types = _utils.resolveTypes(state, syntaxTree(state).topNode.firstChild, config, true);
assert.equal(type, [...types].join('|'));
}),
);
});