Skip to content

Runtime tests using examples from docstrings #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
more fixes
  • Loading branch information
aspeddro committed Apr 23, 2024
commit 464efbafc88b6cb7c74dae49828c63e5ab9cfe00
49 changes: 34 additions & 15 deletions src/Core__AsyncIterator.resi
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@ Returns the next value of the iterator, if any.
See [async iterator protocols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols) on MDN.

## Examples
- A simple example, getting the next value:
```rescript
@val external asyncIterator: AsyncIterator.t<int> = "someAsyncIterator"
let {AsyncIterator.done, value} = await asyncIterator->AsyncIterator.next
```

- Complete example, including looping over all values:
```rescript
// Let's pretend we get an async iterator returning ints from somewhere.
@val external asyncIterator: AsyncIterator.t<int> = "someAsyncIterator"
// A simple example, getting the next value:
let asyncIterator: AsyncIterator.t<(string, string)> = %raw(`
(() => {
var map1 = new Map();

map1.set('first', '1');
map1.set('second', '2');

var iterator1 = map1[Symbol.iterator]();
return iterator1;
})()
`)

let processMyAsyncIterator = async () => {
// ReScript doesn't have `for ... of` loops, but it's easy to mimic using a while loop.
Expand All @@ -50,10 +53,15 @@ let processMyAsyncIterator = async () => {
// Exit the while loop if the iterator says it's done
break := done

// This will log the (int) value of the current async iteration, if a value was returned.
Console.log(value)
if done {
value
->Option.isNone
->assert_eq(true)
}
}
}

processMyAsyncIterator()->ignore
```
*/
@send
Expand All @@ -65,16 +73,27 @@ external next: t<'a> => promise<value<'a>> = "next"
See [iterator protocols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) on MDN.

## Examples

```rescript
// Let's pretend we get an async iterator returning ints from somewhere.
@val external asyncIterator: AsyncIterator.t<int> = "someAsyncIterator"
let asyncIterator: AsyncIterator.t<(string, string)> = %raw(`
(() => {
var map1 = new Map();

map1.set('first', '1');
map1.set('second', '2');

var iterator1 = map1[Symbol.iterator]();
return iterator1;
})()
`)

await asyncIterator->AsyncIterator.forEach(value =>
switch value {
| Some(value) if value > 10 => Console.log("More than 10!")
await asyncIterator->AsyncIterator.forEach(v => {
switch v {
| Some(("second", value)) => assert_eq(value, "2")
| _ => ()
}
)
})
```
*/
let forEach: (t<'a>, option<'a> => unit) => promise<unit>
21 changes: 17 additions & 4 deletions src/Core__Dict.resi
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,24 @@ external fromArray: array<(string, 'a)> => t<'a> = "Object.fromEntries"
`fromIterator(entries)` creates a new dictionary from the provided iterator of key/value pairs.

## Examples
```rescript
// Pretend we have an iterator of the correct shape
@val external someIterator: Iterator.t<(string, int)> = "someIterator"

let dict = Dict.fromIterator(someIterator) // Dict.t<int>
```rescript
let iterator: Iterator.t<(string, int)> = %raw(`
(() => {
var map1 = new Map();

map1.set('first', 1);
map1.set('second', 2);

var iterator1 = map1[Symbol.iterator]();
return iterator1;
})()
`)

iterator
->Dict.fromIterator
->Dict.valuesToArray
->assert_eq([1, 2])
```
*/
@val
Expand Down
6 changes: 5 additions & 1 deletion src/Core__Error.resi
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,12 @@ handled. Compared to a ReScript exception this will give a better stack trace an
debugging experience.

## Examples

```rescript
Error.panic("Uh oh. This was unexpected!")
switch Error.panic("Uh oh. This was unexpected!") {
| exception _ => assert(true)
| _ => assert(false)
}
```
*/
let panic: string => 'a
39 changes: 28 additions & 11 deletions src/Core__Iterator.resi
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,18 @@ Returns the next value of the iterator, if any.
See [iterator protocols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) on MDN.

## Examples
```rescript
@val external someIterator: Iterator.t<int> = "someIterator"

// Pulls out the next value of the iterator
let {Iterator.done, value} = someIterator->Iterator.next
```rescript
let iterator: Iterator.t<string> = %raw(`
(() => {
var array1 = ['a'];
var iterator1 = array1[Symbol.iterator]();
return iterator1
})()
`)

(iterator->Iterator.next).done->assert_eq(false)
(iterator->Iterator.next).done->assert_eq(true)
```
*/
@send
Expand Down Expand Up @@ -87,15 +94,25 @@ external toArrayWithMapper: (t<'a>, 'a => 'b) => array<'b> = "Array.from"
See [iterator protocols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) on MDN.

## Examples
```rescript
@val external someIterator: Iterator.t<int> = "someIterator"

someIterator->Iterator.forEach(value =>
switch value {
| Some(value) if value > 10 => Console.log("More than 10!")
| _ => ()
```rescript
let iterator: Iterator.t<string> = %raw(`
(() => {
var array1 = ['a', 'b', 'c'];
var iterator1 = array1[Symbol.iterator]();
return iterator1
})()
`)

iterator->Iterator.forEach(v => {
switch v {
| Some("a" | "b" | "c") => assert(true)
| other =>
other
->Option.isNone
->assert_eq(true)
}
)
})
```
*/
let forEach: (t<'a>, option<'a> => unit) => unit
Loading