Skip to content

Commit

Permalink
Add regresstion test for type path panic (tweag#1021)
Browse files Browse the repository at this point in the history
  • Loading branch information
yannham committed Jan 3, 2023
1 parent 3b38d58 commit 2d69761
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
22 changes: 21 additions & 1 deletion tests/integration/contracts_fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use assert_matches::assert_matches;
use codespan::Files;
use nickel_lang::error::{Error, EvalError, ToDiagnostic};

use nickel_lang_utilities::eval;
use nickel_lang_utilities::{eval, program_from_expr};

macro_rules! assert_raise_blame {
($term:expr) => {{
Expand Down Expand Up @@ -285,3 +285,23 @@ fn enum_complex() {
)
.unwrap_err();
}

// Regression test for https://github.com/tweag/nickel/issues/1021
#[test]
fn type_path_with_aliases() {
#[track_caller]
fn assert_blame_dont_panic(expr: &str) {
let mut p = program_from_expr(expr);
let result = p.eval();
assert_matches!(result, Err(Error::EvalError(EvalError::BlameError(..))));
// Check that the diagnostic is correctly produced
p.report(result.unwrap_err());
}

assert_blame_dont_panic("let Foo = Array Num in %force% ([\"a\"] | Foo)");
assert_blame_dont_panic("let Foo = Num -> Num in ((fun x => \"a\") | Foo) 0");
assert_blame_dont_panic("let Foo = Num -> Num in ((fun x => x) | Foo) \"a\"");
assert_blame_dont_panic(
"let Foo = {foo: Num} in %force% (((fun x => {foo = \"a\"}) | Dyn -> Foo) null)",
);
}
8 changes: 6 additions & 2 deletions utilities/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ use std::{io::Cursor, path::PathBuf};
pub type EC = CBNCache;
pub type TestProgram = Program<EC>;

/// Create a program from a Nickel expression provided as a string.
pub fn program_from_expr(s: impl std::string::ToString) -> Program<EC> {
Program::<EC>::new_from_source(Cursor::new(s.to_string()), "test").unwrap()
}

pub fn eval(s: impl std::string::ToString) -> Result<Term, Error> {
let mut p = Program::<EC>::new_from_source(Cursor::new(s.to_string()), "test").unwrap();
p.eval().map(Term::from)
program_from_expr(s).eval().map(Term::from)
}

pub fn eval_file(f: &str) -> Result<Term, Error> {
Expand Down

0 comments on commit 2d69761

Please sign in to comment.