Skip to content

Commit cecf214

Browse files
aspeddrocknitt
authored andcommitted
adjusts
1 parent d519e04 commit cecf214

File tree

7 files changed

+151
-41213
lines changed

7 files changed

+151
-41213
lines changed

scripts/test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,19 @@ async function runTests() {
137137
stdio: [0, 1, 2],
138138
});
139139

140+
// Generate rescript file with all tests `generated_mocha_test.res`
140141
cp.execSync(`node ${path.join("tests", "docstrings_examples", "DocTest.res.mjs")}`, {
141142
cwd: path.join(__dirname, ".."),
142143
stdio: [0, 1, 2],
143144
})
144145

146+
// Build again to check if generated_mocha_test.res has syntax or type erros
145147
cp.execSync(`${rescript_exe} build`, {
146148
cwd: path.join(__dirname, "..", "tests/docstrings_examples"),
147149
stdio: [0, 1, 2],
148150
});
149151

152+
// Format generated_mocha_test.res
150153
console.log("Formatting generated_mocha_test.res")
151154
cp.execSync(`./cli/rescript format ${path.join("tests", "docstrings_examples", "generated_mocha_test.res")}`, {
152155
cwd: path.join(__dirname, ".."),
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
generated_mocha_test.res
2+
generated_mocha_test.res.mjs

tests/docstrings_examples/DocTest.res

Lines changed: 88 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,33 @@ type example = {
99
docstrings: array<string>,
1010
}
1111

12-
// Ignore some tests not supported by node v18
12+
// Only major version
13+
let nodeVersion =
14+
Process.version
15+
->String.replace("v", "")
16+
->String.split(".")
17+
->Array.get(0)
18+
->Option.getExn(~message="Failed to find major version of Node")
19+
->Int.fromString
20+
->Option.getExn(~message="Failed to convert node version to Int")
21+
1322
let ignoreRuntimeTests = [
14-
"Array.toReversed",
15-
"Array.toSorted",
16-
"Promise.withResolvers",
17-
"Set.union",
18-
"Set.isSupersetOf",
19-
"Set.isSubsetOf",
20-
"Set.isDisjointFrom",
21-
"Set.intersection",
22-
"Set.symmetricDifference",
23-
"Set.difference",
23+
(
24+
// Ignore some tests not supported by node v18
25+
18,
26+
[
27+
"Array.toReversed",
28+
"Array.toSorted",
29+
"Promise.withResolvers",
30+
"Set.union",
31+
"Set.isSupersetOf",
32+
"Set.isSubsetOf",
33+
"Set.isDisjointFrom",
34+
"Set.intersection",
35+
"Set.symmetricDifference",
36+
"Set.difference",
37+
],
38+
),
2439
]
2540

2641
let getOutput = buffer =>
@@ -155,41 +170,81 @@ let extractExamples = async () => {
155170

156171
let main = async () => {
157172
let examples = await extractExamples()
158-
examples->Array.sort((a, b) =>
159-
Obj.magic(a.id) > Obj.magic(b.id) ? Ordering.fromInt(1) : Ordering.fromInt(-1)
160-
)
161-
let testsContent =
162-
examples
163-
->Array.filterMap(example => {
164-
let codeExamples = getCodeBlocks(example)
165173

166-
let ignore = Array.includes(ignoreRuntimeTests, example.id)
174+
examples->Array.sort((a, b) => String.compare(a.id, b.id))
167175

168-
switch String.length(codeExamples) == 0 {
169-
| true => None
170-
| false =>
171-
ignore
172-
? None
173-
: Some(
174-
`describe("${example.id}", () => {
175-
test("${example.id}", () => {
176-
module Test = {
177-
${codeExamples}
176+
let dict = dict{}
177+
178+
examples->Array.forEach(cur => {
179+
let modulePath = cur.id->String.split(".")
180+
181+
let id =
182+
modulePath
183+
->Array.slice(~start=0, ~end=Array.length(modulePath) - 1)
184+
->Array.join(".")
185+
186+
let previous = switch dict->Dict.get(id) {
187+
| Some(p) => p
188+
| None => []
178189
}
179-
()
190+
191+
dict->Dict.set(id, Array.concat([cur], previous))
180192
})
193+
194+
let output = []
195+
196+
dict->Dict.forEachWithKey((examples, key) => {
197+
let codeExamples = examples->Array.filterMap(example => {
198+
let ignoreExample = Array.find(
199+
ignoreRuntimeTests,
200+
((version, tests)) => {
201+
nodeVersion === version && Array.includes(tests, example.id)
202+
},
203+
)->Option.isSome
204+
205+
switch ignoreExample {
206+
| true =>
207+
Console.warn(
208+
`Ignoring ${example.id} tests. Not supported by Node ${nodeVersion->Int.toString}`,
209+
)
210+
None
211+
| false =>
212+
let code = getCodeBlocks(example)
213+
214+
switch String.length(code) === 0 {
215+
| true => None
216+
| false =>
217+
// Let's add the examples inside a Test module because some examples
218+
// have type definitions that are not supported inside a block.
219+
// Also add unit type `()`
220+
Some(
221+
`test("${example.id->String.split(".")->Array.at(-1)->Option.getExn}", () => {
222+
module Test = {
223+
${code}
224+
}
225+
()
181226
})`,
182-
)
227+
)
228+
}
183229
}
184230
})
185-
->Array.join("\n\n")
231+
232+
switch Array.length(codeExamples) > 0 {
233+
| true =>
234+
let content = `describe("${key}", () => {
235+
${codeExamples->Array.join("\n")}
236+
})`
237+
output->Array.push(content)
238+
| false => ()
239+
}
240+
})
186241

187242
let dirname = url->URL.fileURLToPath->Path.dirname
188243
let filepath = Path.join([dirname, "generated_mocha_test.res"])
189244
let fileContent = `open Mocha
190245
@@warning("-32-34-60-37-109-3-44")
191246
192-
${testsContent}`
247+
${output->Array.join("\n")}`
193248

194249
await Fs.writeFile(filepath, fileContent)
195250
}

tests/docstrings_examples/DocTest.res.mjs

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,41 @@
33
import * as Fs from "fs";
44
import * as Os from "os";
55
import * as Exn from "rescript/lib/es6/Exn.js";
6+
import * as Int from "rescript/lib/es6/Int.js";
67
import * as Url from "url";
8+
import * as Dict from "rescript/lib/es6/Dict.js";
79
import * as List from "rescript/lib/es6/List.js";
810
import * as Path from "path";
911
import * as $$Array from "rescript/lib/es6/Array.js";
1012
import * as $$Error from "rescript/lib/es6/Error.js";
11-
import * as Ordering from "rescript/lib/es6/Ordering.js";
13+
import * as Option from "rescript/lib/es6/Option.js";
1214
import * as Belt_List from "rescript/lib/es6/Belt_List.js";
1315
import * as ArrayUtils from "./ArrayUtils.res.mjs";
1416
import * as Belt_Array from "rescript/lib/es6/Belt_Array.js";
1517
import * as Pervasives from "rescript/lib/es6/Pervasives.js";
1618
import * as SpawnAsync from "./SpawnAsync.res.mjs";
17-
import * as Primitive_object from "rescript/lib/es6/Primitive_object.js";
19+
import * as Primitive_string from "rescript/lib/es6/Primitive_string.js";
1820
import * as Promises from "node:fs/promises";
1921
import * as Primitive_exceptions from "rescript/lib/es6/Primitive_exceptions.js";
2022
import * as RescriptTools_Docgen from "rescript/lib/es6/RescriptTools_Docgen.js";
2123

22-
let ignoreRuntimeTests = [
23-
"Array.toReversed",
24-
"Array.toSorted",
25-
"Promise.withResolvers",
26-
"Set.union",
27-
"Set.isSupersetOf",
28-
"Set.isSubsetOf",
29-
"Set.isDisjointFrom",
30-
"Set.intersection",
31-
"Set.symmetricDifference",
32-
"Set.difference"
33-
];
24+
let nodeVersion = Option.getExn(Int.fromString(Option.getExn(process.version.replace("v", "").split(".")[0], "Failed to find major version of Node"), undefined), "Failed to convert node version to Int");
25+
26+
let ignoreRuntimeTests = [[
27+
18,
28+
[
29+
"Array.toReversed",
30+
"Array.toSorted",
31+
"Promise.withResolvers",
32+
"Set.union",
33+
"Set.isSupersetOf",
34+
"Set.isSubsetOf",
35+
"Set.isDisjointFrom",
36+
"Set.intersection",
37+
"Set.symmetricDifference",
38+
"Set.difference"
39+
]
40+
]];
3441

3542
function getOutput(buffer) {
3643
return buffer.map(e => e.toString()).join("");
@@ -53,7 +60,7 @@ async function extractDocFromFile(file) {
5360
RE_EXN_ID: "Assert_failure",
5461
_1: [
5562
"DocTest.res",
56-
43,
63+
58,
5764
9
5865
],
5966
Error: new Error()
@@ -216,28 +223,48 @@ async function extractExamples() {
216223

217224
async function main() {
218225
let examples = await extractExamples();
219-
examples.sort((a, b) => {
220-
if (Primitive_object.greaterthan(a.id, b.id)) {
221-
return Ordering.fromInt(1);
222-
} else {
223-
return Ordering.fromInt(-1);
224-
}
226+
examples.sort((a, b) => Primitive_string.compare(a.id, b.id));
227+
let dict = {};
228+
examples.forEach(cur => {
229+
let modulePath = cur.id.split(".");
230+
let id = modulePath.slice(0, modulePath.length - 1 | 0).join(".");
231+
let p = dict[id];
232+
let previous = p !== undefined ? p : [];
233+
dict[id] = [cur].concat(previous);
225234
});
226-
let testsContent = $$Array.filterMap(examples, example => {
227-
let codeExamples = getCodeBlocks(example);
228-
let ignore = ignoreRuntimeTests.includes(example.id);
229-
if (codeExamples.length === 0 || ignore) {
235+
let output = [];
236+
Dict.forEachWithKey(dict, (examples, key) => {
237+
let codeExamples = $$Array.filterMap(examples, example => {
238+
let ignoreExample = Option.isSome(ignoreRuntimeTests.find(param => {
239+
if (nodeVersion === param[0]) {
240+
return param[1].includes(example.id);
241+
} else {
242+
return false;
243+
}
244+
}));
245+
if (ignoreExample) {
246+
console.warn("Ignoring " + example.id + " tests. Not supported by Node " + nodeVersion.toString());
247+
return;
248+
}
249+
let code = getCodeBlocks(example);
250+
if (code.length === 0) {
251+
return;
252+
} else {
253+
return "test(\"" + Option.getExn(example.id.split(".").at(-1), undefined) + "\", () => {\n module Test = {\n " + code + "\n }\n ()\n})";
254+
}
255+
});
256+
if (codeExamples.length <= 0) {
230257
return;
231-
} else {
232-
return "describe(\"" + example.id + "\", () => {\n test(\"" + example.id + "\", () => {\n module Test = {\n " + codeExamples + "\n }\n ()\n })\n})";
233258
}
234-
}).join("\n\n");
259+
let content = "describe(\"" + key + "\", () => {\n" + codeExamples.join("\n") + "\n })";
260+
output.push(content);
261+
});
235262
let dirname = Path.dirname(Url.fileURLToPath(import.meta.url));
236263
let filepath = Path.join(dirname, "generated_mocha_test.res");
237-
let fileContent = "open Mocha\n@@warning(\"-32-34-60-37-109-3-44\")\n\n" + testsContent;
264+
let fileContent = "open Mocha\n@@warning(\"-32-34-60-37-109-3-44\")\n\n" + output.join("\n");
238265
return await Promises.writeFile(filepath, fileContent);
239266
}
240267

241268
await main();
242269

243-
/* batchSize Not a pure module */
270+
/* nodeVersion Not a pure module */

tests/docstrings_examples/Node.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Path = {
55

66
module Process = {
77
@scope("process") external cwd: unit => string = "cwd"
8+
@scope("process") @val external version: string = "version"
89
}
910

1011
module Fs = {

0 commit comments

Comments
 (0)