forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocTest.res.js
More file actions
174 lines (161 loc) · 5.12 KB
/
DocTest.res.js
File metadata and controls
174 lines (161 loc) · 5.12 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Generated by ReScript, PLEASE EDIT WITH CARE
import * as Nodefs from "node:fs";
import * as Nodeos from "node:os";
import * as Nodeurl from "node:url";
import * as Nodepath from "node:path";
import * as ArrayUtils from "./ArrayUtils.res.js";
import * as SpawnAsync from "./SpawnAsync.res.js";
import * as Stdlib_Int from "@rescript/runtime/lib/es6/Stdlib_Int.js";
import * as Stdlib_Dict from "@rescript/runtime/lib/es6/Stdlib_Dict.js";
import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
import * as Primitive_string from "@rescript/runtime/lib/es6/Primitive_string.js";
import * as Promises from "node:fs/promises";
import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
import * as BinsJs from "../../cli/common/bins.js";
let rescript_tools_exe = BinsJs.rescript_tools_exe;
let nodeVersion = Stdlib_Option.getOrThrow(Stdlib_Int.fromString(Stdlib_Option.getOrThrow(process.version.replace("v", "").split(".")[0], "Failed to find major version of Node")), "Failed to convert node version to Int");
let ignoreRuntimeTests = [
[
20,
[
"Stdlib_Array.toReversed",
"Stdlib_Array.toSorted"
]
],
[
22,
[
"Stdlib_Promise.withResolvers",
"Stdlib_Set.union",
"Stdlib_Set.isSupersetOf",
"Stdlib_Set.isSubsetOf",
"Stdlib_Set.isDisjointFrom",
"Stdlib_Set.intersection",
"Stdlib_Set.symmetricDifference",
"Stdlib_Set.difference"
]
],
[
24,
["Stdlib_RegExp.escape"]
],
[
1000,
[
"Stdlib_DataView.getFloat16",
"Stdlib_DataView.setFloat16"
]
]
];
function getOutput(buffer) {
return buffer.map(e => e.toString()).join("");
}
async function extractDocFromFile(file) {
let match = await SpawnAsync.run(rescript_tools_exe, [
"extract-codeblocks",
file,
"--transform-assert-equal"
], undefined);
try {
return JSON.parse(getOutput(match.stdout));
} catch (raw_e) {
let e = Primitive_exceptions.internalToException(raw_e);
if (e.RE_EXN_ID === "JsExn") {
console.error(e._1);
return Stdlib_JsError.panic(`Failed to extract code blocks from ` + file);
}
throw e;
}
}
let batchSize = Nodeos.cpus().length;
let runtimePath = Nodepath.join("packages", "@rescript", "runtime");
async function extractExamples() {
let files = Nodefs.readdirSync(runtimePath);
let docFiles = files.filter(f => {
if (f.startsWith("Js") || f.startsWith("RescriptTools")) {
return false;
} else if (f.endsWith(".resi")) {
return true;
} else if (f.endsWith(".res")) {
return !files.includes(f + "i");
} else {
return false;
}
});
console.log(`Extracting examples from ` + docFiles.length.toString() + ` runtime files...`);
let examples = [];
await ArrayUtils.forEachAsyncInBatches(docFiles, batchSize, async f => {
let doc = await extractDocFromFile(Nodepath.join(runtimePath, f));
if (doc.TAG === "Ok") {
examples.push(...doc._0.filter(d => d.code.includes("assertEqual(")));
return;
}
console.error(doc._0);
return Stdlib_JsError.panic(`Error extracting code blocks for ` + f);
});
examples.sort((a, b) => Primitive_string.compare(a.id, b.id));
return examples;
}
async function main() {
let examples = await extractExamples();
let dict = {};
examples.forEach(cur => {
let modulePath = cur.id.split(".");
let id = modulePath.slice(0, modulePath.length - 1 | 0).join(".");
let p = dict[id];
let previous = p !== undefined ? p : [];
dict[id] = [cur].concat(previous);
});
let output = [];
Stdlib_Dict.forEachWithKey(dict, (examples, key) => {
examples.sort((a, b) => Primitive_string.compare(a.name, b.name));
let codeExamples = Stdlib_Array.filterMap(examples, example => {
let ignoreExample = ignoreRuntimeTests.some(param => {
if (nodeVersion < param[0]) {
return param[1].includes(example.id);
} else {
return false;
}
});
if (ignoreExample) {
console.warn(`Ignoring ` + example.id + ` tests. Not supported by Node ` + nodeVersion.toString());
return;
}
let code = example.code;
if (code.length === 0) {
return;
} else if (code.includes("await")) {
return `testAsync("` + example.name + `", async () => {
module Test = {
` + code + `
}
()
})`;
} else {
return `test("` + example.name + `", () => {
module Test = {
` + code + `
}
()
})`;
}
});
if (codeExamples.length === 0) {
return;
}
let content = `describe("` + key + `", () => {
` + codeExamples.join("\n") + `
})`;
output.push(content);
});
let dirname = Nodepath.dirname(Nodeurl.fileURLToPath(import.meta.url));
let filepath = Nodepath.join(dirname, "generated_mocha_test.res");
let fileContent = `open Mocha
@@warning("-32-34-60-37-109-3-44")
` + output.join("\n");
return await Promises.writeFile(filepath, fileContent);
}
await main();
/* rescript_tools_exe Not a pure module */