Skip to content

Commit 90cde20

Browse files
committed
fix tests
1 parent 2e3b448 commit 90cde20

File tree

2 files changed

+48
-68
lines changed

2 files changed

+48
-68
lines changed

tests/docstrings_examples/DocTest.res

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ let indentOutputCode = code => {
194194
}
195195

196196
type error =
197-
| ReScript({error: string})
198-
| Runtime({rescript: string, js: string, error: string})
197+
| ReScriptError(string)
198+
| RuntimeError({rescript: string, js: string, error: string})
199199

200200
let extractDocFromFile = file => {
201201
let toolsBin = Path.join([Process.cwd(), "cli", "rescript-tools"])
@@ -292,7 +292,8 @@ let getCodeBlocks = example => {
292292
->List.fromArray
293293
->loop(list{})
294294
->List.toArray
295-
->Array.join("\n")
295+
->Belt.Array.reverse
296+
->Array.join("\n\n")
296297
}
297298

298299
let main = async () => {
@@ -303,7 +304,6 @@ let main = async () => {
303304
// Ignore Js modules and RescriptTools for now
304305
->Array.filter(f => !String.startsWith(f, "Js") && !String.startsWith(f, "RescriptTools"))
305306
->Array.filter(f => f->String.endsWith(".res") || f->String.endsWith(".resi"))
306-
// ->Array.filter(f => f == "Belt_HashMap.resi")
307307
->Array.reduce([], (acc, cur) => {
308308
let isInterface = cur->String.endsWith(".resi")
309309

@@ -312,75 +312,56 @@ let main = async () => {
312312
// If .resi files exists append to array
313313
if !isInterface && Fs.existsSync(resi) {
314314
Array.concat(acc, [cur ++ "i"])
315+
} else if !Array.includes(acc, cur) {
316+
Array.concat(acc, [cur])
315317
} else {
316-
let a = !Array.includes(acc, cur) ? Array.concat(acc, [cur]) : acc
317-
a
318+
acc
318319
}
319320
})
320321
->Array.map(f => extractDocFromFile(Path.join(["runtime", f]))->getExamples)
321322
->Array.flat
322323

323-
let results =
324+
let compilationResults =
324325
await modules
325326
->Array.map(async example => {
326-
let id = example.id->String.replaceAll(".", "_")
327+
let id = example.id->String.replaceAll(".", "__")
327328
let rescriptCode = example->getCodeBlocks
328329
let jsCode = await compileTest(~id, ~code=rescriptCode)
329-
// let id = `${id}_${Int.toString(int)}`
330-
// let results =
331-
// await [codes]
332-
// ->Array.mapWithIndex(async (code, int) => {
333-
// let id = `${id}_${Int.toString(int)}`
334-
// (code, await compileTest(~id, ~code))
335-
// })
336-
// ->Promise.all
337330
(example, (rescriptCode, jsCode))
338331
})
339332
->Promise.all
340333

341-
// let examples = results->Array.map(((example, result)) => {
342-
// let (compiled, errors) = results->Array.reduce(([], []), (acc, (resCode, result)) => {
343-
// let (oks, errors) = acc
344-
// switch result {
345-
// | Ok(jsCode) => ([...oks, (resCode, jsCode)], errors)
346-
// | Error(output) => (oks, [...errors, ReScript({error: output})])
347-
// }
348-
// })
349-
// (example, (compiled, errors))
350-
// })
351-
352-
let (compiled, notCompiled) = results->Array.reduce(([], []), (
334+
let (compiled, compilationErrors) = compilationResults->Array.reduce(([], []), (
353335
acc,
354-
(example, (rescriptCode, compiled)),
336+
(example, (rescriptCode, jsCode)),
355337
) => {
356338
let (lhs, rhs) = acc
357-
switch compiled {
339+
switch jsCode {
358340
| Ok(jsCode) => lhs->Array.push((example, rescriptCode, jsCode))
359-
| Error(err) => rhs->Array.push((example, ReScript({error: err})))
341+
| Error(err) => rhs->Array.push((example, ReScriptError(err)))
360342
}
361343
(lhs, rhs)
362344
})
363345

364-
let exampleErrors =
365-
await compiled
346+
let runtimeErrors =
347+
(await compiled
366348
->Array.filter((({id}, _, _)) => !Array.includes(ignoreRuntimeTests, id))
367-
->Array.map(async ((example, compiled, errors)) => {
368-
let nodeTests = await errors->runtimeTests
349+
->Array.map(async ((example, rescriptCode, jsCode)) => {
350+
let nodeTests = await jsCode->runtimeTests
369351
switch nodeTests {
370352
| Ok(_) => None
371-
| Error(err) => Some(example, Runtime({rescript: compiled, js: errors, error: err}))
353+
| Error(error) => Some(example, RuntimeError({rescript: rescriptCode, js: jsCode, error}))
372354
}
373355
})
374-
->Promise.all
375-
376-
let exampleErrors = exampleErrors->Array.filterMap(i =>
377-
switch i {
378-
| Some(i) => Some(i)
379-
| None => None
380-
}
381-
)
356+
->Promise.all)
357+
->Array.filterMap(i =>
358+
switch i {
359+
| Some(i) => Some(i)
360+
| None => None
361+
}
362+
)
382363

383-
let allErros = Array.concat(exampleErrors, notCompiled)
364+
let allErros = Array.concat(runtimeErrors, compilationErrors)
384365

385366
// Print Errors
386367
let () = allErros->Array.forEach(((example, errors)) => {
@@ -391,18 +372,18 @@ let main = async () => {
391372
| other => other
392373
}
393374

394-
// let errorMessage = errors->Array.map(err =>
395375
let a = switch errors {
396-
| ReScript({error}) =>
376+
| ReScriptError(error) =>
397377
let err =
398378
error
399379
->String.split("\n")
380+
// Drop line of filename
400381
->Array.filterWithIndex((_, i) => i !== 2)
401382
->Array.join("\n")
402383

403384
`${"error"->red}: failed to compile examples from ${kind} ${example.id->cyan}
404385
${err}`
405-
| Runtime({rescript, js, error}) =>
386+
| RuntimeError({rescript, js, error}) =>
406387
let indent = String.repeat(" ", 2)
407388

408389
`${"runtime error"->red}: failed to run examples from ${kind} ${example.id->cyan}
@@ -422,7 +403,6 @@ ${error->indentOutputCode}
422403
}
423404

424405
Process.stderrWrite(a)
425-
// errorMessage->Array.forEach(e => Process.stderrWrite(e))
426406
})
427407

428408
let someError = allErros->Array.length > 0

tests/docstrings_examples/DocTest.res.mjs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as $$Array from "rescript/lib/es6/Array.js";
99
import * as $$Error from "rescript/lib/es6/Error.js";
1010
import * as Belt_List from "rescript/lib/es6/Belt_List.js";
1111
import * as Nodeutil from "node:util";
12+
import * as Belt_Array from "rescript/lib/es6/Belt_Array.js";
1213
import * as Pervasives from "rescript/lib/es6/Pervasives.js";
1314
import * as Child_process from "child_process";
1415
import * as Primitive_option from "rescript/lib/es6/Primitive_option.js";
@@ -322,7 +323,7 @@ function getCodeBlocks(example) {
322323
continue;
323324
};
324325
};
325-
return List.toArray(loop(List.fromArray($$Array.reduce(example.docstrings, [], (acc, docstring) => acc.concat(docstring.split("\n")))), /* [] */0)).join("\n");
326+
return Belt_Array.reverse(List.toArray(loop(List.fromArray($$Array.reduce(example.docstrings, [], (acc, docstring) => acc.concat(docstring.split("\n")))), /* [] */0))).join("\n\n");
326327
}
327328

328329
async function main() {
@@ -350,8 +351,8 @@ async function main() {
350351
return acc.concat([cur]);
351352
}
352353
}).map(f => getExamples(extractDocFromFile(Path.join("runtime", f)))).flat();
353-
let results = await Promise.all(modules.map(async example => {
354-
let id = example.id.replaceAll(".", "_");
354+
let compilationResults = await Promise.all(modules.map(async example => {
355+
let id = example.id.replaceAll(".", "__");
355356
let rescriptCode = getCodeBlocks(example);
356357
let jsCode = await compileTest(id, rescriptCode);
357358
return [
@@ -362,27 +363,27 @@ async function main() {
362363
]
363364
];
364365
}));
365-
let match = $$Array.reduce(results, [
366+
let match = $$Array.reduce(compilationResults, [
366367
[],
367368
[]
368369
], (acc, param) => {
369370
let rhs = acc[1];
370371
let lhs = acc[0];
371372
let match = param[1];
372-
let compiled = match[1];
373+
let jsCode = match[1];
373374
let example = param[0];
374-
if (compiled.TAG === "Ok") {
375+
if (jsCode.TAG === "Ok") {
375376
lhs.push([
376377
example,
377378
match[0],
378-
compiled._0
379+
jsCode._0
379380
]);
380381
} else {
381382
rhs.push([
382383
example,
383384
{
384-
TAG: "ReScript",
385-
error: compiled._0
385+
TAG: "ReScriptError",
386+
_0: jsCode._0
386387
}
387388
]);
388389
}
@@ -391,39 +392,38 @@ async function main() {
391392
rhs
392393
];
393394
});
394-
let exampleErrors = await Promise.all(match[0].filter(param => !ignoreRuntimeTests.includes(param[0].id)).map(async param => {
395-
let errors = param[2];
396-
let nodeTests = await runtimeTests(errors);
395+
let runtimeErrors = $$Array.filterMap(await Promise.all(match[0].filter(param => !ignoreRuntimeTests.includes(param[0].id)).map(async param => {
396+
let jsCode = param[2];
397+
let nodeTests = await runtimeTests(jsCode);
397398
if (nodeTests.TAG === "Ok") {
398399
return;
399400
} else {
400401
return [
401402
param[0],
402403
{
403-
TAG: "Runtime",
404+
TAG: "RuntimeError",
404405
rescript: param[1],
405-
js: errors,
406+
js: jsCode,
406407
error: nodeTests._0
407408
}
408409
];
409410
}
410-
}));
411-
let exampleErrors$1 = $$Array.filterMap(exampleErrors, i => {
411+
})), i => {
412412
if (i !== undefined) {
413413
return i;
414414
}
415415

416416
});
417-
let allErros = exampleErrors$1.concat(match[1]);
417+
let allErros = runtimeErrors.concat(match[1]);
418418
allErros.forEach(param => {
419419
let errors = param[1];
420420
let example = param[0];
421421
let cyan = s => "\x1b[36m" + s + "\x1b[0m";
422422
let other = example.kind;
423423
let kind = other === "moduleAlias" ? "module alias" : other;
424424
let a;
425-
if (errors.TAG === "ReScript") {
426-
let err = errors.error.split("\n").filter((param, i) => i !== 2).join("\n");
425+
if (errors.TAG === "ReScriptError") {
426+
let err = errors._0.split("\n").filter((param, i) => i !== 2).join("\n");
427427
a = "\x1B[1;31merror\x1B[0m: failed to compile examples from " + kind + " " + cyan(example.id) + "\n" + err;
428428
} else {
429429
let indent = " ".repeat(2);

0 commit comments

Comments
 (0)