104 lines
3.3 KiB
Rust
104 lines
3.3 KiB
Rust
mod utils;
|
|
|
|
use nix_js::context::Context;
|
|
use nix_js::value::Value;
|
|
|
|
#[test]
|
|
fn import_absolute_path() {
|
|
let mut ctx = Context::new().unwrap();
|
|
|
|
let temp_dir = tempfile::tempdir().unwrap();
|
|
let lib_path = temp_dir.path().join("nix_test_lib.nix");
|
|
|
|
std::fs::write(&lib_path, "{ add = a: b: a + b; }").unwrap();
|
|
|
|
let expr = format!(r#"(import "{}").add 3 5"#, lib_path.display());
|
|
assert_eq!(ctx.eval_code(&expr).unwrap(), Value::Int(8));
|
|
}
|
|
|
|
#[test]
|
|
fn import_nested() {
|
|
let mut ctx = Context::new().unwrap();
|
|
|
|
let temp_dir = tempfile::tempdir().unwrap();
|
|
|
|
let lib_path = temp_dir.path().join("lib.nix");
|
|
std::fs::write(&lib_path, "{ add = a: b: a + b; }").unwrap();
|
|
|
|
let main_path = temp_dir.path().join("main.nix");
|
|
let main_content = format!(
|
|
r#"let lib = import {}; in {{ result = lib.add 10 20; }}"#,
|
|
lib_path.display()
|
|
);
|
|
std::fs::write(&main_path, main_content).unwrap();
|
|
|
|
let expr = format!(r#"(import "{}").result"#, main_path.display());
|
|
assert_eq!(ctx.eval_code(&expr).unwrap(), Value::Int(30));
|
|
}
|
|
|
|
#[test]
|
|
fn import_relative_path() {
|
|
let mut ctx = Context::new().unwrap();
|
|
|
|
let temp_dir = tempfile::tempdir().unwrap();
|
|
let subdir = temp_dir.path().join("subdir");
|
|
std::fs::create_dir_all(&subdir).unwrap();
|
|
|
|
let lib_path = temp_dir.path().join("lib.nix");
|
|
std::fs::write(&lib_path, "{ multiply = a: b: a * b; }").unwrap();
|
|
|
|
let helper_path = subdir.join("helper.nix");
|
|
std::fs::write(&helper_path, "{ subtract = a: b: a - b; }").unwrap();
|
|
|
|
let main_path = temp_dir.path().join("main.nix");
|
|
let main_content = r#"
|
|
let
|
|
lib = import ./lib.nix;
|
|
helper = import ./subdir/helper.nix;
|
|
in {
|
|
result1 = lib.multiply 3 4;
|
|
result2 = helper.subtract 10 3;
|
|
}
|
|
"#;
|
|
std::fs::write(&main_path, main_content).unwrap();
|
|
|
|
let expr = format!(r#"let x = import "{}"; in x.result1"#, main_path.display());
|
|
assert_eq!(ctx.eval_code(&expr).unwrap(), Value::Int(12));
|
|
|
|
let expr = format!(r#"let x = import "{}"; in x.result2"#, main_path.display());
|
|
assert_eq!(ctx.eval_code(&expr).unwrap(), Value::Int(7));
|
|
}
|
|
|
|
#[test]
|
|
fn import_returns_function() {
|
|
let mut ctx = Context::new().unwrap();
|
|
|
|
let temp_dir = tempfile::tempdir().unwrap();
|
|
let func_path = temp_dir.path().join("nix_test_func.nix");
|
|
std::fs::write(&func_path, "x: x * 2").unwrap();
|
|
|
|
let expr = format!(r#"(import "{}") 5"#, func_path.display());
|
|
assert_eq!(ctx.eval_code(&expr).unwrap(), Value::Int(10));
|
|
}
|
|
|
|
#[test]
|
|
fn import_with_complex_dependency_graph() {
|
|
let mut ctx = Context::new().unwrap();
|
|
let temp_dir = tempfile::tempdir().unwrap();
|
|
|
|
let utils_path = temp_dir.path().join("utils.nix");
|
|
std::fs::write(&utils_path, "{ double = x: x * 2; }").unwrap();
|
|
|
|
let math_path = temp_dir.path().join("math.nix");
|
|
let math_content =
|
|
r#"let utils = import ./utils.nix; in { triple = x: x + utils.double x; }"#;
|
|
std::fs::write(&math_path, math_content).unwrap();
|
|
|
|
let main_path = temp_dir.path().join("main.nix");
|
|
let main_content = r#"let math = import ./math.nix; in math.triple 5"#;
|
|
std::fs::write(&main_path, main_content).unwrap();
|
|
|
|
let expr = format!(r#"import "{}""#, main_path.display());
|
|
assert_eq!(ctx.eval_code(&expr).unwrap(), Value::Int(15));
|
|
}
|