Skip to content

Commit

Permalink
Do deep eval for doctests (#2110)
Browse files Browse the repository at this point in the history
* Do deep eval for doctests

* Update snapshot

* Fix some stdlib tests
  • Loading branch information
jneem authored Dec 24, 2024
1 parent cae9894 commit 691a522
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cli/src/doctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ fn run_tests(
let _ = std::io::stdout().flush();

// Undo the test's lazy wrapper.
let result = prog.eval_closure(Closure {
let result = prog.eval_deep_closure(Closure {
body: mk_app!(val.clone(), Term::Null),
env: Environment::new(),
});
Expand Down
22 changes: 15 additions & 7 deletions cli/tests/snapshot/inputs/doctest/fail_unexpected_error.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@
{
foo
| doc m%"
```nickel
foo + "1"
```
```nickel
foo + "1"
```

```nickel
foo + "1"
# => 2
```
```nickel
foo + "1"
# => 2
```
"%
= 1,

bar
| doc m%"
```nickel
{ foo = 1 }
# => { foo = 2 }
```
"%
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
source: cli/tests/snapshot/main.rs
expression: err
---
2 failures
3 failures
error: tests failed
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ expression: out
---
testing foo/0...FAILED
testing foo/1...FAILED
testing bar/0...FAILED
test foo/0 failed
error: dynamic type error
┌─ [INPUTS_PATH]/doctest/fail_unexpected_error.ncl:1:7
Expand All @@ -21,3 +22,15 @@ error: dynamic type error
│ ^^^ this expression has type String, but Number was expected
= (+) expects its 2nd argument to be a Number

test bar/0 failed
error: contract broken by a value
┌─ <unknown> (generated by evaluation):1:1
1std.contract.Equal { foo = 2, }
│ ------------------------------- expected type
┌─ [INPUTS_PATH]/doctest/fail_unexpected_error.ncl:1:9
1 │ { foo = 1 }
^ applied to this expression
8 changes: 8 additions & 0 deletions core/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,14 @@ impl<EC: EvalCache> Program<EC> {
Ok(self.vm.eval_deep_closure(prepared)?)
}

/// Same as `eval_closure`, but does a full evaluation and does not substitute all variables.
///
/// (Or, same as `eval_deep` but takes a closure.)
pub fn eval_deep_closure(&mut self, closure: Closure) -> Result<RichTerm, EvalError> {
self.vm.reset();
self.vm.eval_deep_closure(closure)
}

/// Prepare for evaluation, then fetch the metadata of `self.field`, or list the fields of the
/// whole program if `self.field` is empty.
pub fn query(&mut self) -> Result<Field, Error> {
Expand Down
4 changes: 2 additions & 2 deletions core/stdlib/std.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -3013,7 +3013,7 @@
# => [ "one", "two" ]

std.record.fields_with_opts { one = 1, two = 2, three_opt | optional }
# => [ "one", "two", "three_opt" ]
# => [ "one", "three_opt", "two" ]
```
"%
= fun r => %record/fields_with_opts% r,
Expand Down Expand Up @@ -3363,7 +3363,7 @@

```nickel
std.record.to_array { hello = "world", foo = "bar" }
# => [ { field = "hello", value = "world" }, { field = "foo", value = "bar" } ]
# => [ { field = "foo", value = "bar" }, { field = "hello", value = "world" } ]
```
"%
= fun record =>
Expand Down

0 comments on commit 691a522

Please sign in to comment.