Skip to content

Commit c0d43cc

Browse files
committed
polish
1 parent 704919d commit c0d43cc

File tree

2 files changed

+32
-48
lines changed

2 files changed

+32
-48
lines changed

scripts/ExamplesTest.mjs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ function createFileInTempDir(id) {
111111
return Path.join(Os.tmpdir(), id);
112112
}
113113

114-
async function testCodeBlock(id, _kind, code) {
114+
async function testCode(id, code) {
115115
var tempFileName = Path.join(Os.tmpdir(), id);
116116
await Promises.writeFile(tempFileName + ".res", code);
117117
var args = [
@@ -149,7 +149,7 @@ async function testCodeBlock(id, _kind, code) {
149149
}
150150
}
151151

152-
function extractDoc(file) {
152+
function extractDocFromFile(file) {
153153
var toolsBin = Path.join(Path.dirname(dirname), "node_modules", ".bin", "rescript-tools");
154154
var spawn = Child_process.spawnSync(toolsBin, [
155155
"doc",
@@ -158,7 +158,7 @@ function extractDoc(file) {
158158
return Tools_Docgen.decodeFromJson(JSON.parse(spawn.stdout.toString()));
159159
}
160160

161-
function getDocstrings(param) {
161+
function getExamples(param) {
162162
var loop = function (_items, _acc) {
163163
while(true) {
164164
var acc = _acc;
@@ -277,28 +277,19 @@ function getCodeBlocks(example) {
277277
}))), /* [] */0));
278278
}
279279

280-
var tests = getDocstrings(extractDoc("src/RescriptCore.res")).map(function (doc) {
281-
return [
282-
doc,
283-
getCodeBlocks(doc)
284-
];
285-
}).filter(function (param) {
286-
return param[1].length > 0;
287-
});
288-
289-
var moduleTestsResults = await Promise.all(tests.map(async function (param) {
290-
var docTest = param[0];
291-
var results = await Promise.all(param[1].map(async function (code) {
292-
var id = docTest.id.replaceAll(".", "_");
293-
return await testCodeBlock(id, docTest.kind, code);
280+
var results = await Promise.all(getExamples(extractDocFromFile("src/RescriptCore.res")).map(async function (example) {
281+
var id = example.id.replaceAll(".", "_");
282+
var codes = getCodeBlocks(example);
283+
var results = await Promise.all(codes.map(async function (code) {
284+
return await testCode(id, code);
294285
}));
295286
return [
296-
docTest,
287+
example,
297288
results
298289
];
299290
}));
300291

301-
var errors = Belt_Array.keepMap(moduleTestsResults, (function (param) {
292+
var errors = Belt_Array.keepMap(results, (function (param) {
302293
var errors = Belt_Array.keepMap(param[1], (function (result) {
303294
if (result.TAG === "Ok") {
304295
return ;
@@ -331,7 +322,7 @@ errors.forEach(function (param) {
331322
process.stderr.write(message);
332323
});
333324

334-
process.exit(errors.length);
325+
process.exit(errors.length === 0 ? 0 : 1);
335326

336327
var Docgen;
337328

@@ -346,12 +337,11 @@ export {
346337
rescriptJson ,
347338
prepareCompiler ,
348339
createFileInTempDir ,
349-
testCodeBlock ,
350-
extractDoc ,
351-
getDocstrings ,
340+
testCode ,
341+
extractDocFromFile ,
342+
getExamples ,
352343
getCodeBlocks ,
353-
tests ,
354-
moduleTestsResults ,
344+
results ,
355345
errors ,
356346
}
357347
/* dirname Not a pure module */

scripts/ExamplesTest.res

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ let prepareCompiler = () => {
8686

8787
prepareCompiler()
8888

89-
type docTest = {
89+
type example = {
9090
id: string,
9191
kind: string,
9292
name: string,
@@ -95,7 +95,7 @@ type docTest = {
9595

9696
let createFileInTempDir = id => Path.join2(OS.tmpdir(), id)
9797

98-
let testCodeBlock = async (id, _kind, code) => {
98+
let testCode = async (~id, ~code) => {
9999
let tempFileName = createFileInTempDir(id)
100100

101101
let () = await Fs.writeFile(tempFileName ++ ".res", code)
@@ -132,7 +132,7 @@ let testCodeBlock = async (id, _kind, code) => {
132132
}
133133
}
134134

135-
let extractDoc = file => {
135+
let extractDocFromFile = file => {
136136
let toolsBin = Path.join([Path.dirname(dirname), "node_modules", ".bin", "rescript-tools"])
137137
let spawn = ChildProcess.spawnSync(toolsBin, ["doc", file])
138138

@@ -142,8 +142,8 @@ let extractDoc = file => {
142142
->Docgen.decodeFromJson
143143
}
144144

145-
let getDocstrings = ({items}: Docgen.doc) => {
146-
let rec loop = (items: list<Docgen.item>, acc: list<docTest>) => {
145+
let getExamples = ({items}: Docgen.doc) => {
146+
let rec loop = (items: list<Docgen.item>, acc: list<example>) => {
147147
switch items {
148148
| list{Value({docstrings, id, name}), ...rest} =>
149149
loop(rest, list{{id, name, docstrings, kind: "value"}, ...acc})
@@ -218,27 +218,21 @@ let getCodeBlocks = example => {
218218
->List.toArray
219219
}
220220

221-
let tests =
222-
extractDoc("src/RescriptCore.res")
223-
->getDocstrings
224-
->Array.map(doc => (doc, doc->getCodeBlocks))
225-
->Array.filter(((_, b)) => Array.length(b) > 0)
226-
227-
let moduleTestsResults =
228-
await tests
229-
->Array.map(async ((docTest, codeblocks)) => {
221+
let results =
222+
await extractDocFromFile("src/RescriptCore.res")
223+
->getExamples
224+
->Array.map(async example => {
225+
let id = example.id->String.replaceAll(".", "_")
226+
let codes = example->getCodeBlocks
230227
let results =
231-
await codeblocks
232-
->Array.map(async code => {
233-
let id = docTest.id->String.replaceAll(".", "_")
234-
await testCodeBlock(id, docTest.kind, code)
235-
})
228+
await codes
229+
->Array.map(async code => await testCode(~id, ~code))
236230
->Promise.all
237-
(docTest, results)
231+
(example, results)
238232
})
239233
->Promise.all
240234

241-
let errors = moduleTestsResults->Belt.Array.keepMap(((test, results)) => {
235+
let errors = results->Belt.Array.keepMap(((example, results)) => {
242236
let errors = results->Belt.Array.keepMap(result =>
243237
switch result {
244238
| Ok() => None
@@ -247,7 +241,7 @@ let errors = moduleTestsResults->Belt.Array.keepMap(((test, results)) => {
247241
)
248242

249243
if Array.length(errors) > 0 {
250-
Some((test, errors))
244+
Some((example, errors))
251245
} else {
252246
None
253247
}
@@ -278,4 +272,4 @@ let () = errors->Array.forEach(((test, errors)) => {
278272
Process.stderrWrite(message)
279273
})
280274

281-
Process.exit(errors->Array.length)
275+
Process.exit(errors->Array.length == 0 ? 0 : 1)

0 commit comments

Comments
 (0)