2021-03-14 16:14:57 -07:00
|
|
|
mod func_tests {
|
2023-02-17 22:10:54 -08:00
|
|
|
use relish::ast::lex;
|
2023-03-01 11:38:02 -08:00
|
|
|
use relish::ast::{Args, Ctr, Seg, Symbol, ValueType};
|
2023-02-24 15:29:17 -08:00
|
|
|
use relish::ast::{SymTable, Type, UserFn};
|
2023-03-01 11:38:02 -08:00
|
|
|
use std::rc::Rc;
|
2021-03-14 16:14:57 -07:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn decl_and_call_internal_func() {
|
2023-02-24 15:29:17 -08:00
|
|
|
let mut syms = SymTable::new();
|
2023-02-17 22:10:54 -08:00
|
|
|
let test_internal_func: Symbol = Symbol {
|
2022-01-16 22:02:40 -08:00
|
|
|
name: String::from("test_func_in"),
|
2023-02-24 15:29:17 -08:00
|
|
|
conditional_branches: false,
|
2023-03-05 22:18:49 -08:00
|
|
|
docs: String::new(),
|
2021-03-14 16:14:57 -07:00
|
|
|
args: Args::Strict(vec![Type::Bool]),
|
2023-02-27 22:53:54 -08:00
|
|
|
value: ValueType::Internal(Rc::new(
|
|
|
|
|
|a: &Seg, _: &mut SymTable| -> Result<Ctr, String> {
|
2023-02-17 22:10:54 -08:00
|
|
|
let inner = a;
|
2021-03-14 16:14:57 -07:00
|
|
|
let mut is_bool = false;
|
2023-02-17 22:10:54 -08:00
|
|
|
if let Ctr::Bool(_) = *inner.car {
|
2021-03-14 16:14:57 -07:00
|
|
|
is_bool = true;
|
|
|
|
|
}
|
2023-02-27 22:53:54 -08:00
|
|
|
Ok(Ctr::Bool(is_bool))
|
2022-01-16 22:02:40 -08:00
|
|
|
},
|
|
|
|
|
)),
|
2021-03-14 16:14:57 -07:00
|
|
|
};
|
2023-03-01 11:38:02 -08:00
|
|
|
let args = Seg::from(Box::new(Ctr::Bool(true)), Box::new(Ctr::None));
|
2023-02-24 15:29:17 -08:00
|
|
|
syms.insert(String::from("test_func_in"), test_internal_func);
|
2023-03-05 22:21:18 -08:00
|
|
|
if let Ctr::Bool(b) = *syms
|
|
|
|
|
.call_symbol(&"test_func_in".to_string(), &args, true)
|
|
|
|
|
.unwrap()
|
|
|
|
|
{
|
2023-03-05 22:18:49 -08:00
|
|
|
assert!(b)
|
2021-03-14 16:14:57 -07:00
|
|
|
}
|
|
|
|
|
}
|
2021-06-06 10:24:19 -07:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn decl_and_call_external_func_singlet() {
|
2023-02-24 15:29:17 -08:00
|
|
|
let mut syms = SymTable::new();
|
2023-03-05 22:18:49 -08:00
|
|
|
let finner = lex(&"input".to_string()).unwrap();
|
|
|
|
|
let test_external_func: Symbol = Symbol {
|
|
|
|
|
name: String::from("echo"),
|
|
|
|
|
conditional_branches: false,
|
|
|
|
|
args: Args::Lazy(1),
|
|
|
|
|
docs: String::new(),
|
|
|
|
|
value: ValueType::FuncForm(UserFn {
|
|
|
|
|
arg_syms: vec!["input".to_string()],
|
|
|
|
|
ast: finner,
|
|
|
|
|
}),
|
|
|
|
|
};
|
2023-02-17 22:10:54 -08:00
|
|
|
|
2023-03-05 22:18:49 -08:00
|
|
|
let args = Seg::from(
|
|
|
|
|
Box::new(Ctr::String("test".to_string())),
|
|
|
|
|
Box::new(Ctr::None),
|
|
|
|
|
);
|
2021-06-06 10:24:19 -07:00
|
|
|
|
2023-03-05 22:18:49 -08:00
|
|
|
syms.insert(String::from("test_func_in"), test_external_func);
|
2023-03-05 22:21:18 -08:00
|
|
|
if let Ctr::Bool(b) = *syms
|
|
|
|
|
.call_symbol(&"test_func_in".to_string(), &args, true)
|
|
|
|
|
.unwrap()
|
|
|
|
|
{
|
2023-03-05 22:18:49 -08:00
|
|
|
assert!(b)
|
2021-06-06 10:24:19 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2021-07-19 23:59:03 -07:00
|
|
|
fn decl_and_call_external_func_multi_body() {
|
2023-02-24 15:29:17 -08:00
|
|
|
let mut syms = SymTable::new();
|
2023-03-05 22:18:49 -08:00
|
|
|
let finner = lex(&"input".to_string()).unwrap();
|
|
|
|
|
let test_external_func: Symbol = Symbol {
|
|
|
|
|
name: String::from("echo_2"),
|
|
|
|
|
conditional_branches: false,
|
|
|
|
|
args: Args::Lazy(1),
|
|
|
|
|
docs: String::new(),
|
|
|
|
|
value: ValueType::FuncForm(UserFn {
|
|
|
|
|
arg_syms: vec!["input".to_string()],
|
|
|
|
|
ast: finner,
|
|
|
|
|
}),
|
|
|
|
|
};
|
2023-02-17 22:10:54 -08:00
|
|
|
|
2023-03-05 22:18:49 -08:00
|
|
|
let args = Seg::from(
|
|
|
|
|
Box::new(Ctr::String("test".to_string())),
|
|
|
|
|
Box::new(Ctr::None),
|
|
|
|
|
);
|
2023-02-20 19:42:48 -08:00
|
|
|
|
2023-03-05 22:18:49 -08:00
|
|
|
syms.insert(String::from("echo_2"), test_external_func);
|
2023-03-05 22:21:18 -08:00
|
|
|
assert_eq!(
|
|
|
|
|
*syms
|
|
|
|
|
.call_symbol(&"echo_2".to_string(), &args, true)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.to_string(),
|
|
|
|
|
"'test'".to_string()
|
|
|
|
|
);
|
2021-06-06 10:24:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn decl_and_call_func_with_nested_call() {
|
2023-02-24 15:29:17 -08:00
|
|
|
let mut syms = SymTable::new();
|
2023-02-17 22:10:54 -08:00
|
|
|
let inner_func: Symbol = Symbol {
|
2021-06-06 10:24:19 -07:00
|
|
|
name: String::from("test_inner"),
|
2023-02-24 15:29:17 -08:00
|
|
|
conditional_branches: false,
|
2021-06-06 10:24:19 -07:00
|
|
|
args: Args::Strict(vec![Type::Bool]),
|
2023-03-05 22:18:49 -08:00
|
|
|
docs: String::new(),
|
2023-02-27 22:53:54 -08:00
|
|
|
value: ValueType::Internal(Rc::new(
|
|
|
|
|
|a: &Seg, _: &mut SymTable| -> Result<Ctr, String> {
|
2023-02-17 22:10:54 -08:00
|
|
|
let inner = a;
|
|
|
|
|
if let Ctr::Bool(b) = *inner.car {
|
|
|
|
|
if b {
|
2023-02-27 22:53:54 -08:00
|
|
|
Ok(Ctr::String("test".to_string()))
|
2021-06-06 10:24:19 -07:00
|
|
|
} else {
|
2023-02-27 22:53:54 -08:00
|
|
|
Ok(Ctr::None)
|
2021-06-06 10:24:19 -07:00
|
|
|
}
|
|
|
|
|
} else {
|
2023-02-27 22:53:54 -08:00
|
|
|
Err("not a bool".to_string())
|
2021-06-06 10:24:19 -07:00
|
|
|
}
|
2022-01-16 22:02:40 -08:00
|
|
|
},
|
|
|
|
|
)),
|
2021-06-06 10:24:19 -07:00
|
|
|
};
|
|
|
|
|
|
2023-03-05 22:18:49 -08:00
|
|
|
let finner = lex(&"((test_inner true))".to_string()).unwrap();
|
|
|
|
|
let outer_func: Symbol = Symbol {
|
|
|
|
|
name: String::from("test_outer"),
|
|
|
|
|
conditional_branches: false,
|
|
|
|
|
args: Args::Lazy(1),
|
|
|
|
|
docs: String::new(),
|
|
|
|
|
value: ValueType::FuncForm(UserFn {
|
|
|
|
|
arg_syms: vec!["input".to_string()],
|
|
|
|
|
ast: finner,
|
|
|
|
|
}),
|
|
|
|
|
};
|
2021-06-06 10:24:19 -07:00
|
|
|
|
2023-03-05 22:18:49 -08:00
|
|
|
let args = Seg::from(Box::new(Ctr::Bool(true)), Box::new(Ctr::None));
|
|
|
|
|
syms.insert(String::from("test_inner"), inner_func);
|
|
|
|
|
syms.insert(String::from("test_outer"), outer_func);
|
2023-03-05 22:21:18 -08:00
|
|
|
assert_eq!(
|
|
|
|
|
syms.call_symbol(&"test_outer".to_string(), &args, true)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.to_string(),
|
|
|
|
|
"'test'".to_string()
|
|
|
|
|
);
|
2021-06-06 10:24:19 -07:00
|
|
|
}
|
|
|
|
|
|
2021-06-22 00:50:37 -07:00
|
|
|
#[test]
|
2023-02-24 16:05:10 -08:00
|
|
|
fn arg_type_mismatch() {
|
|
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
let test_internal_func: Symbol = Symbol {
|
|
|
|
|
name: String::from("test_func_in"),
|
|
|
|
|
conditional_branches: false,
|
|
|
|
|
args: Args::Strict(vec![Type::Bool]),
|
2023-03-05 22:18:49 -08:00
|
|
|
docs: String::new(),
|
2023-02-27 22:53:54 -08:00
|
|
|
value: ValueType::Internal(Rc::new(
|
|
|
|
|
|a: &Seg, _: &mut SymTable| -> Result<Ctr, String> {
|
2023-02-24 16:05:10 -08:00
|
|
|
let inner = a;
|
|
|
|
|
let mut is_bool = false;
|
|
|
|
|
if let Ctr::Bool(_) = *inner.car {
|
|
|
|
|
is_bool = true;
|
|
|
|
|
}
|
2023-02-27 22:53:54 -08:00
|
|
|
Ok(Ctr::Bool(is_bool))
|
2023-02-24 16:05:10 -08:00
|
|
|
},
|
|
|
|
|
)),
|
|
|
|
|
};
|
2023-03-01 11:38:02 -08:00
|
|
|
let args = Seg::from(Box::new(Ctr::Integer(1)), Box::new(Ctr::None));
|
2021-06-22 00:50:37 -07:00
|
|
|
|
2023-02-24 16:05:10 -08:00
|
|
|
syms.insert(String::from("test_func_in"), test_internal_func);
|
2023-03-05 22:18:49 -08:00
|
|
|
assert_eq!(
|
|
|
|
|
syms.call_symbol(&"test_func_in".to_string(), &args, true)
|
|
|
|
|
.err()
|
|
|
|
|
.unwrap(),
|
|
|
|
|
"arg 1 expected to be bool".to_string(),
|
|
|
|
|
);
|
2021-06-22 00:50:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn too_many_args() {
|
2023-02-24 16:05:10 -08:00
|
|
|
let mut syms = SymTable::new();
|
2023-03-05 22:18:49 -08:00
|
|
|
let finner = lex(&"(input)".to_string()).unwrap();
|
|
|
|
|
let test_external_func: Symbol = Symbol {
|
|
|
|
|
name: String::from("echo"),
|
|
|
|
|
conditional_branches: false,
|
|
|
|
|
args: Args::Lazy(1),
|
|
|
|
|
docs: String::new(),
|
|
|
|
|
value: ValueType::FuncForm(UserFn {
|
|
|
|
|
arg_syms: vec!["input".to_string()],
|
|
|
|
|
ast: finner,
|
|
|
|
|
}),
|
|
|
|
|
};
|
2021-06-22 00:50:37 -07:00
|
|
|
|
2023-03-05 22:18:49 -08:00
|
|
|
let args = Seg::from(
|
|
|
|
|
Box::new(Ctr::String("test".to_string())),
|
|
|
|
|
Box::new(Ctr::Seg(Seg::from_mono(Box::new(Ctr::Integer(1))))),
|
|
|
|
|
);
|
2021-06-22 00:50:37 -07:00
|
|
|
|
2023-03-05 22:18:49 -08:00
|
|
|
syms.insert(String::from("test_func_in"), test_external_func);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
syms.call_symbol(&"test_func_in".to_string(), &args, true)
|
|
|
|
|
.err()
|
|
|
|
|
.unwrap(),
|
|
|
|
|
"expected 1 args. Got 2.".to_string(),
|
|
|
|
|
);
|
2021-06-22 00:50:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2023-02-24 16:05:10 -08:00
|
|
|
fn too_few_args() {
|
|
|
|
|
let mut syms = SymTable::new();
|
2023-03-05 22:18:49 -08:00
|
|
|
let finner = lex(&"(input)".to_string()).unwrap();
|
|
|
|
|
let test_external_func: Symbol = Symbol {
|
|
|
|
|
name: String::from("echo"),
|
|
|
|
|
conditional_branches: false,
|
|
|
|
|
args: Args::Lazy(1),
|
|
|
|
|
docs: String::new(),
|
|
|
|
|
value: ValueType::FuncForm(UserFn {
|
|
|
|
|
arg_syms: vec!["input".to_string()],
|
|
|
|
|
ast: finner,
|
|
|
|
|
}),
|
|
|
|
|
};
|
2023-02-24 16:05:10 -08:00
|
|
|
|
2023-03-05 22:18:49 -08:00
|
|
|
let args = Seg::new();
|
|
|
|
|
syms.insert(String::from("test_func_in"), test_external_func);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
syms.call_symbol(&"test_func_in".to_string(), &args, true)
|
|
|
|
|
.err()
|
|
|
|
|
.unwrap(),
|
|
|
|
|
"expected 1 args. Got 0.".to_string(),
|
|
|
|
|
);
|
2021-06-22 00:50:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2023-02-24 16:05:10 -08:00
|
|
|
fn arg_cant_eval() {
|
|
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
let test_internal_func: Symbol = Symbol {
|
|
|
|
|
name: String::from("test_func_in"),
|
|
|
|
|
conditional_branches: false,
|
|
|
|
|
args: Args::Strict(vec![Type::Bool]),
|
2023-03-05 22:18:49 -08:00
|
|
|
docs: String::new(),
|
2023-02-27 22:53:54 -08:00
|
|
|
value: ValueType::Internal(Rc::new(
|
|
|
|
|
|a: &Seg, _: &mut SymTable| -> Result<Ctr, String> {
|
2023-02-24 16:05:10 -08:00
|
|
|
let inner = a;
|
|
|
|
|
let mut is_bool = false;
|
|
|
|
|
if let Ctr::Bool(_) = *inner.car {
|
|
|
|
|
is_bool = true;
|
|
|
|
|
}
|
2023-02-27 22:53:54 -08:00
|
|
|
Ok(Ctr::Bool(is_bool))
|
2023-02-24 16:05:10 -08:00
|
|
|
},
|
|
|
|
|
)),
|
|
|
|
|
};
|
|
|
|
|
let args = Seg::from(
|
|
|
|
|
Box::new(Ctr::Symbol("undefined-symbol".to_string())),
|
2023-03-01 11:38:02 -08:00
|
|
|
Box::new(Ctr::None),
|
2023-02-24 16:05:10 -08:00
|
|
|
);
|
2021-06-22 00:50:37 -07:00
|
|
|
|
2023-02-24 16:05:10 -08:00
|
|
|
syms.insert(String::from("test_func_in"), test_internal_func);
|
2023-03-05 22:18:49 -08:00
|
|
|
assert_eq!(
|
|
|
|
|
syms.call_symbol(&"test_func_in".to_string(), &args, true)
|
|
|
|
|
.err()
|
|
|
|
|
.unwrap(),
|
2023-03-05 22:21:18 -08:00
|
|
|
"error in call to undefined-symbol: undefined symbol: undefined-symbol".to_string(),
|
2023-03-05 22:18:49 -08:00
|
|
|
);
|
2023-02-24 16:05:10 -08:00
|
|
|
}
|
2021-03-14 16:14:57 -07:00
|
|
|
}
|