Skip to content

Commit 1a8dfdb

Browse files
Fix result examples (#7914)
* Wrap docstring tests in async context to make "toplevel" await work * Fix result example tests * Changelog * Gate testAsync in naive await detection
1 parent 645e1a9 commit 1a8dfdb

File tree

5 files changed

+59
-13
lines changed

5 files changed

+59
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
#### :bug: Bug fix
2222

23+
- Fix result examples. https://github.com/rescript-lang/rescript/pull/7914
24+
2325
#### :memo: Documentation
2426

2527
#### :nail_care: Polish

packages/@rescript/runtime/Stdlib_Result.resi

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,16 @@ When `res` is `Error`, returns the error unchanged.
330330
## Examples
331331
332332
```rescript
333-
let asyncSquare = async x => x * x
333+
let square = x => x * x
334334
335-
let result1 = Result.mapOkAsync(Promise.resolve(Ok(4)), asyncSquare) // Returns promise that resolves to Ok(16)
336-
let result2 = Result.mapOkAsync(Promise.resolve(Error("invalid")), asyncSquare) // Returns promise that resolves to Error("invalid")
335+
let result1 = await Result.mapOkAsync(Promise.resolve(Ok(4)), square)
336+
result1 == Ok(16)
337+
338+
let result2 = await Result.mapOkAsync(
339+
Promise.resolve(Error("invalid")),
340+
square,
341+
)
342+
result2 == Error("invalid")
337343
```
338344
*/
339345
let mapOkAsync: (
@@ -349,10 +355,13 @@ When `res` is `Ok`, returns the ok value unchanged.
349355
## Examples
350356
351357
```rescript
352-
let asyncFormatError = async e => `Error: ${e}`
358+
let formatError = e => `Error: ${e}`
353359
354-
let result1 = Result.mapErrorAsync(Promise.resolve(Ok(42)), asyncFormatError) // Returns promise that resolves to Ok(42)
355-
let result2 = Result.mapErrorAsync(Promise.resolve(Error("invalid")), asyncFormatError) // Returns promise that resolves to Error("Error: invalid")
360+
let result1 = await Result.mapErrorAsync(Promise.resolve(Ok(42)), formatError)
361+
result1 == Ok(42)
362+
363+
let result2 = await Result.mapErrorAsync(Promise.resolve(Error("invalid")), formatError)
364+
result2 == Error("Error: invalid")
356365
```
357366
*/
358367
let mapErrorAsync: (
@@ -368,15 +377,21 @@ When `res` is `Error`, returns the error unchanged.
368377
## Examples
369378
370379
```rescript
371-
let asyncValidate = async x =>
380+
let asyncValidate = async x =>
372381
if x > 0 {
373382
Ok(x * 2)
374383
} else {
375384
Error("Must be positive")
376385
}
377386
378-
let result1 = Result.flatMapOkAsync(Promise.resolve(Ok(5)), asyncValidate) // Returns promise that resolves to Ok(10)
379-
let result2 = Result.flatMapOkAsync(Promise.resolve(Error("Already failed")), asyncValidate) // Returns promise that resolves to Error("Already failed")
387+
let result1 = await Result.flatMapOkAsync(Promise.resolve(Ok(5)), asyncValidate)
388+
result1 == Ok(10)
389+
390+
let result2 = await Result.flatMapOkAsync(
391+
Promise.resolve(Error("Already failed")),
392+
asyncValidate,
393+
)
394+
result2 == Error("Already failed")
380395
```
381396
*/
382397
let flatMapOkAsync: (
@@ -392,15 +407,24 @@ When `res` is `Ok`, returns the ok value unchanged.
392407
## Examples
393408
394409
```rescript
395-
let asyncRecover = async error =>
410+
let asyncRecover = async error =>
396411
if error === "timeout" {
397412
Ok("default")
398413
} else {
399414
Error(error)
400415
}
401416
402-
let result1 = Result.flatMapErrorAsync(Promise.resolve(Error("timeout")), asyncRecover) // Returns promise that resolves to Ok("default")
403-
let result2 = Result.flatMapErrorAsync(Promise.resolve(Ok("default")), asyncRecover) // Returns promise that resolves to Ok("default")
417+
let result1 = await Result.flatMapErrorAsync(
418+
Promise.resolve(Error("timeout")),
419+
asyncRecover,
420+
)
421+
result1 == Ok("default")
422+
423+
let result2 = await Result.flatMapErrorAsync(
424+
Promise.resolve(Ok("default")),
425+
asyncRecover,
426+
)
427+
result2 == Ok("default")
404428
```
405429
*/
406430
let flatMapErrorAsync: (

tests/docstring_tests/DocTest.res

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,20 @@ let main = async () => {
138138

139139
if code->String.length === 0 {
140140
None
141+
} else if code->String.includes("await") {
142+
// Same as below, but wrap in async to make top-level awaits work.
143+
Some(
144+
`testAsync("${example.name}", async () => {
145+
module Test = {
146+
${code}
147+
}
148+
()
149+
})`,
150+
)
141151
} else {
142152
// Let's add the examples inside a Test module because some examples
143153
// have type definitions that are not supported inside a block.
144-
// Also add unit type `()`
154+
// Also add unit type `()`.
145155
Some(
146156
`test("${example.name}", () => {
147157
module Test = {

tests/docstring_tests/DocTest.res.js

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/docstring_tests/Mocha.res

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
@module("mocha")
22
external test: (string, unit => unit) => unit = "test"
33

4+
@module("mocha")
5+
external testAsync: (string, unit => promise<unit>) => unit = "test"
6+
47
@module("mocha")
58
external describe: (string, unit => unit) => unit = "describe"

0 commit comments

Comments
 (0)