1
1
mirror of https://github.com/tweag/nickel.git synced 2024-10-04 23:27:15 +03:00
nickel/tests/imports.rs
2022-03-06 22:27:57 +01:00

115 lines
2.8 KiB
Rust

use assert_matches::assert_matches;
use nickel_lang::error::{Error, EvalError, TypecheckError};
use nickel_lang::program::Program;
use nickel_lang::term::Term;
use std::io::BufReader;
use std::path::PathBuf;
fn mk_import(file: &str) -> String {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push(format!("tests/imports/{}", file));
format!(
"import \"{}\"",
path.into_os_string().into_string().unwrap()
)
}
#[test]
fn nested() {
let mut prog = Program::new_from_source(
BufReader::new(mk_import("nested.ncl").as_bytes()),
"should_be = 3",
)
.unwrap();
assert_eq!(prog.eval().map(Term::from), Ok(Term::Num(3.)));
}
#[test]
fn root_path() {
let mut prog = Program::new_from_source(
BufReader::new(mk_import("root_path.ncl").as_bytes()),
"should_be = 44",
)
.unwrap();
assert_eq!(prog.eval().map(Term::from), Ok(Term::Num(44.)));
}
#[test]
fn multi_imports() {
let mut prog = Program::new_from_source(
BufReader::new(mk_import("multi_imports.ncl").as_bytes()),
"should_be = 5",
)
.unwrap();
assert_eq!(prog.eval().map(Term::from), Ok(Term::Num(5.)));
}
#[test]
fn contract_fail() {
let mut prog = Program::new_from_source(
BufReader::new(mk_import("contract-fail.ncl").as_bytes()),
"should_fail",
)
.unwrap();
assert_matches!(
prog.eval(),
Err(Error::EvalError(EvalError::BlameError(..)))
);
}
#[test]
fn typecheck_fail() {
let mut prog = Program::new_from_source(
BufReader::new(mk_import("typecheck-fail.ncl").as_bytes()),
"should_fail",
)
.unwrap();
assert_matches!(
prog.eval(),
Err(Error::TypecheckError(TypecheckError::TypeMismatch(..)))
);
}
#[test]
fn static_typing_fail() {
let mut prog = Program::new_from_source(
BufReader::new(format!("(let x = {} in x) : Str", mk_import("two.ncl")).as_bytes()),
"should_fail",
)
.unwrap();
assert_matches!(
prog.eval(),
Err(Error::TypecheckError(TypecheckError::TypeMismatch(..)))
);
}
#[test]
fn serialize() {
use nickel_lang::term::Term;
let mut prog = Program::new_from_source(
BufReader::new(
format!(
"(builtin.serialize `Json ({})) == (builtin.serialize `Json ({{foo = \"ab\"}}))",
mk_import("record.ncl")
)
.as_bytes(),
),
"shoud success",
)
.unwrap();
assert_eq!(prog.eval().map(Term::from), Ok(Term::Bool(true)));
}
#[test]
fn circular_imports_fail() {
let mut prog = Program::new_from_source(
BufReader::new(mk_import("cycle.ncl").as_bytes()),
"should_fail",
)
.unwrap();
assert_matches!(
prog.eval().map(Term::from),
Ok(Term::RecRecord(..)) | Ok(Term::Record(..))
);
}