Skip to content

Commit 2e3b448

Browse files
committed
initial
1 parent 63e0ea2 commit 2e3b448

File tree

4 files changed

+143
-141
lines changed

4 files changed

+143
-141
lines changed

runtime/Belt_HashMap.resi

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,10 @@ We can add elements to the collection:
5858
## Examples
5959
6060
```rescript
61-
// TODO(aspeddro): doctests should unique for all examples in one docstrins
62-
// let () = {
63-
// Belt.HashMap.set(s0, 0, 3)
64-
// Belt.HashMap.set(s1, 1, "3")
65-
// }
61+
let () = {
62+
Belt.HashMap.set(s0, 0, 3)
63+
Belt.HashMap.set(s1, 1, "3")
64+
}
6665
```
6766
6867
Since this is an mutable data strucure, `s1` will contain two pairs.
@@ -291,6 +290,12 @@ s0
291290
->Belt.HashMap.reduce("", (acc, _, value) => acc ++ (", " ++ value))
292291
->assert_eq(", value1, value2")
293292
```
293+
294+
## More Examples
295+
296+
```rescript
297+
Console.log("lol")
298+
```
294299
*/
295300
let reduce: (t<'key, 'value, 'id>, 'c, ('c, 'key, 'value) => 'c) => 'c
296301

runtime/Belt_HashSet.resi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ value should be the same.
6060
Here the compiler would infer `s0` and `s1` having different type so that it
6161
would not mix.
6262
63-
## Examples
63+
Signatures:
6464
6565
```
6666
let s0: Belt.HashSet.t<int, I0.identity>

tests/docstrings_examples/DocTest.res

Lines changed: 68 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ let getCodeBlocks = example => {
292292
->List.fromArray
293293
->loop(list{})
294294
->List.toArray
295+
->Array.join("\n")
295296
}
296297

297298
let main = async () => {
@@ -302,6 +303,7 @@ let main = async () => {
302303
// Ignore Js modules and RescriptTools for now
303304
->Array.filter(f => !String.startsWith(f, "Js") && !String.startsWith(f, "RescriptTools"))
304305
->Array.filter(f => f->String.endsWith(".res") || f->String.endsWith(".resi"))
306+
// ->Array.filter(f => f == "Belt_HashMap.resi")
305307
->Array.reduce([], (acc, cur) => {
306308
let isInterface = cur->String.endsWith(".resi")
307309

@@ -322,73 +324,88 @@ let main = async () => {
322324
await modules
323325
->Array.map(async example => {
324326
let id = example.id->String.replaceAll(".", "_")
325-
let codes = example->getCodeBlocks
326-
let results =
327-
await codes
328-
->Array.mapWithIndex(async (code, int) => {
329-
let id = `${id}_${Int.toString(int)}`
330-
(code, await compileTest(~id, ~code))
331-
})
332-
->Promise.all
333-
(example, results)
327+
let rescriptCode = example->getCodeBlocks
328+
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
337+
(example, (rescriptCode, jsCode))
334338
})
335339
->Promise.all
336340

337-
let examples = results->Array.map(((example, results)) => {
338-
let (compiled, errors) = results->Array.reduce(([], []), (acc, (resCode, result)) => {
339-
let (oks, errors) = acc
340-
switch result {
341-
| Ok(jsCode) => ([...oks, (resCode, jsCode)], errors)
342-
| Error(output) => (oks, [...errors, ReScript({error: output})])
343-
}
344-
})
345-
(example, (compiled, errors))
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(([], []), (
353+
acc,
354+
(example, (rescriptCode, compiled)),
355+
) => {
356+
let (lhs, rhs) = acc
357+
switch compiled {
358+
| Ok(jsCode) => lhs->Array.push((example, rescriptCode, jsCode))
359+
| Error(err) => rhs->Array.push((example, ReScript({error: err})))
360+
}
361+
(lhs, rhs)
346362
})
347363

348364
let exampleErrors =
349-
await examples
350-
->Array.filter((({id}, _)) => !Array.includes(ignoreRuntimeTests, id))
351-
->Array.map(async ((example, (compiled, errors))) => {
352-
let nodeTests =
353-
await compiled
354-
->Array.map(async ((res, js)) => (res, js, await runtimeTests(js)))
355-
->Promise.all
356-
357-
let runtimeErrors = nodeTests->Belt.Array.keepMap(((res, js, output)) =>
358-
switch output {
359-
| Ok(_) => None
360-
| Error(error) => Some(Runtime({rescript: res, js, error}))
361-
}
362-
)
363-
364-
(example, Array.concat(runtimeErrors, errors))
365+
await compiled
366+
->Array.filter((({id}, _, _)) => !Array.includes(ignoreRuntimeTests, id))
367+
->Array.map(async ((example, compiled, errors)) => {
368+
let nodeTests = await errors->runtimeTests
369+
switch nodeTests {
370+
| Ok(_) => None
371+
| Error(err) => Some(example, Runtime({rescript: compiled, js: errors, error: err}))
372+
}
365373
})
366374
->Promise.all
367375

376+
let exampleErrors = exampleErrors->Array.filterMap(i =>
377+
switch i {
378+
| Some(i) => Some(i)
379+
| None => None
380+
}
381+
)
382+
383+
let allErros = Array.concat(exampleErrors, notCompiled)
384+
368385
// Print Errors
369-
let () = exampleErrors->Array.forEach(((example, errors)) => {
386+
let () = allErros->Array.forEach(((example, errors)) => {
370387
let red = s => `\x1B[1;31m${s}\x1B[0m`
371388
let cyan = s => `\x1b[36m${s}\x1b[0m`
372389
let kind = switch example.kind {
373390
| "moduleAlias" => "module alias"
374391
| other => other
375392
}
376393

377-
let errorMessage = errors->Array.map(err =>
378-
switch err {
379-
| ReScript({error}) =>
380-
let err =
381-
error
382-
->String.split("\n")
383-
->Array.filterWithIndex((_, i) => i !== 2)
384-
->Array.join("\n")
394+
// let errorMessage = errors->Array.map(err =>
395+
let a = switch errors {
396+
| ReScript({error}) =>
397+
let err =
398+
error
399+
->String.split("\n")
400+
->Array.filterWithIndex((_, i) => i !== 2)
401+
->Array.join("\n")
385402

386-
`${"error"->red}: failed to compile examples from ${kind} ${example.id->cyan}
403+
`${"error"->red}: failed to compile examples from ${kind} ${example.id->cyan}
387404
${err}`
388-
| Runtime({rescript, js, error}) =>
389-
let indent = String.repeat(" ", 2)
405+
| Runtime({rescript, js, error}) =>
406+
let indent = String.repeat(" ", 2)
390407

391-
`${"runtime error"->red}: failed to run examples from ${kind} ${example.id->cyan}
408+
`${"runtime error"->red}: failed to run examples from ${kind} ${example.id->cyan}
392409
393410
${indent}${"ReScript"->cyan}
394411
@@ -402,13 +419,13 @@ ${indent}${"stacktrace"->red}
402419
403420
${error->indentOutputCode}
404421
`
405-
}
406-
)
422+
}
407423

408-
errorMessage->Array.forEach(e => Process.stderrWrite(e))
424+
Process.stderrWrite(a)
425+
// errorMessage->Array.forEach(e => Process.stderrWrite(e))
409426
})
410427

411-
let someError = exampleErrors->Array.some(((_, err)) => Array.length(err) > 0)
428+
let someError = allErros->Array.length > 0
412429

413430
someError ? 1 : 0
414431
}

tests/docstrings_examples/DocTest.res.mjs

Lines changed: 64 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ 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";
1312
import * as Pervasives from "rescript/lib/es6/Pervasives.js";
1413
import * as Child_process from "child_process";
1514
import * as Primitive_option from "rescript/lib/es6/Primitive_option.js";
@@ -323,7 +322,7 @@ function getCodeBlocks(example) {
323322
continue;
324323
};
325324
};
326-
return List.toArray(loop(List.fromArray($$Array.reduce(example.docstrings, [], (acc, docstring) => acc.concat(docstring.split("\n")))), /* [] */0));
325+
return List.toArray(loop(List.fromArray($$Array.reduce(example.docstrings, [], (acc, docstring) => acc.concat(docstring.split("\n")))), /* [] */0)).join("\n");
327326
}
328327

329328
async function main() {
@@ -353,105 +352,86 @@ async function main() {
353352
}).map(f => getExamples(extractDocFromFile(Path.join("runtime", f)))).flat();
354353
let results = await Promise.all(modules.map(async example => {
355354
let id = example.id.replaceAll(".", "_");
356-
let codes = getCodeBlocks(example);
357-
let results = await Promise.all(codes.map(async (code, int) => {
358-
let id$1 = id + "_" + int.toString();
359-
return [
360-
code,
361-
await compileTest(id$1, code)
362-
];
363-
}));
355+
let rescriptCode = getCodeBlocks(example);
356+
let jsCode = await compileTest(id, rescriptCode);
364357
return [
365358
example,
366-
results
359+
[
360+
rescriptCode,
361+
jsCode
362+
]
367363
];
368364
}));
369-
let examples = results.map(param => {
370-
let match = $$Array.reduce(param[1], [
371-
[],
372-
[]
373-
], (acc, param) => {
374-
let errors = acc[1];
375-
let oks = acc[0];
376-
let result = param[1];
377-
if (result.TAG === "Ok") {
378-
return [
379-
Belt_Array.concatMany([
380-
oks,
381-
[[
382-
param[0],
383-
result._0
384-
]]
385-
]),
386-
errors
387-
];
388-
} else {
389-
return [
390-
oks,
391-
Belt_Array.concatMany([
392-
errors,
393-
[{
394-
TAG: "ReScript",
395-
error: result._0
396-
}]
397-
])
398-
];
399-
}
400-
});
401-
return [
402-
param[0],
403-
[
365+
let match = $$Array.reduce(results, [
366+
[],
367+
[]
368+
], (acc, param) => {
369+
let rhs = acc[1];
370+
let lhs = acc[0];
371+
let match = param[1];
372+
let compiled = match[1];
373+
let example = param[0];
374+
if (compiled.TAG === "Ok") {
375+
lhs.push([
376+
example,
404377
match[0],
405-
match[1]
406-
]
378+
compiled._0
379+
]);
380+
} else {
381+
rhs.push([
382+
example,
383+
{
384+
TAG: "ReScript",
385+
error: compiled._0
386+
}
387+
]);
388+
}
389+
return [
390+
lhs,
391+
rhs
407392
];
408393
});
409-
let exampleErrors = await Promise.all(examples.filter(param => !ignoreRuntimeTests.includes(param[0].id)).map(async param => {
410-
let match = param[1];
411-
let nodeTests = await Promise.all(match[0].map(async param => {
412-
let js = param[1];
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);
397+
if (nodeTests.TAG === "Ok") {
398+
return;
399+
} else {
413400
return [
414401
param[0],
415-
js,
416-
await runtimeTests(js)
417-
];
418-
}));
419-
let runtimeErrors = Belt_Array.keepMap(nodeTests, param => {
420-
let output = param[2];
421-
if (output.TAG === "Ok") {
422-
return;
423-
} else {
424-
return {
402+
{
425403
TAG: "Runtime",
426-
rescript: param[0],
427-
js: param[1],
428-
error: output._0
429-
};
430-
}
431-
});
432-
return [
433-
param[0],
434-
runtimeErrors.concat(match[1])
435-
];
404+
rescript: param[1],
405+
js: errors,
406+
error: nodeTests._0
407+
}
408+
];
409+
}
436410
}));
437-
exampleErrors.forEach(param => {
411+
let exampleErrors$1 = $$Array.filterMap(exampleErrors, i => {
412+
if (i !== undefined) {
413+
return i;
414+
}
415+
416+
});
417+
let allErros = exampleErrors$1.concat(match[1]);
418+
allErros.forEach(param => {
419+
let errors = param[1];
438420
let example = param[0];
439421
let cyan = s => "\x1b[36m" + s + "\x1b[0m";
440422
let other = example.kind;
441423
let kind = other === "moduleAlias" ? "module alias" : other;
442-
let errorMessage = param[1].map(err => {
443-
if (err.TAG === "ReScript") {
444-
let err$1 = err.error.split("\n").filter((param, i) => i !== 2).join("\n");
445-
return "\x1B[1;31merror\x1B[0m: failed to compile examples from " + kind + " " + cyan(example.id) + "\n" + err$1;
446-
}
424+
let a;
425+
if (errors.TAG === "ReScript") {
426+
let err = errors.error.split("\n").filter((param, i) => i !== 2).join("\n");
427+
a = "\x1B[1;31merror\x1B[0m: failed to compile examples from " + kind + " " + cyan(example.id) + "\n" + err;
428+
} else {
447429
let indent = " ".repeat(2);
448-
return "\x1B[1;31mruntime error\x1B[0m: failed to run examples from " + kind + " " + cyan(example.id) + "\n\n" + indent + "\x1b[36mReScript\x1b[0m\n\n" + indentOutputCode(err.rescript) + "\n\n" + indent + "\x1b[36mCompiled Js\x1b[0m\n\n" + indentOutputCode(err.js) + "\n\n" + indent + "\x1B[1;31mstacktrace\x1B[0m\n\n" + indentOutputCode(err.error) + "\n";
449-
});
450-
errorMessage.forEach(e => {
451-
process.stderr.write(e);
452-
});
430+
a = "\x1B[1;31mruntime error\x1B[0m: failed to run examples from " + kind + " " + cyan(example.id) + "\n\n" + indent + "\x1b[36mReScript\x1b[0m\n\n" + indentOutputCode(errors.rescript) + "\n\n" + indent + "\x1b[36mCompiled Js\x1b[0m\n\n" + indentOutputCode(errors.js) + "\n\n" + indent + "\x1B[1;31mstacktrace\x1B[0m\n\n" + indentOutputCode(errors.error) + "\n";
431+
}
432+
process.stderr.write(a);
453433
});
454-
let someError = exampleErrors.some(param => param[1].length > 0);
434+
let someError = allErros.length > 0;
455435
if (someError) {
456436
return 1;
457437
} else {

0 commit comments

Comments
 (0)