use std::collections::HashMap;
use truecalc_core::{Engine, ErrorKind, Value};
fn eval(formula: &str) -> Value {
Engine::sheets().evaluate(formula, &HashMap::new())
}
#[test]
fn date_wrong_arity_carries_google_sheets_message() {
let v = eval("=DATE()");
assert_eq!(v.error_kind(), Some(&ErrorKind::NA));
assert_eq!(v.error_kind().unwrap().to_string(), "#N/A");
assert_eq!(
v.error_message(),
Some("Wrong number of arguments to DATE. Expected 3 arguments, but got 0 arguments.")
);
assert!(matches!(v, Value::ErrorMsg(ErrorKind::NA, _)));
}
#[test]
fn function_name_is_upper_cased_for_parity() {
let v = eval("=date()");
assert_eq!(
v.error_message(),
Some("Wrong number of arguments to DATE. Expected 3 arguments, but got 0 arguments.")
);
}
#[test]
fn bounded_range_arity_uses_between_phrasing() {
let v = eval("=SUM()");
assert_eq!(v.error_kind(), Some(&ErrorKind::NA));
assert_eq!(
v.error_message(),
Some("Wrong number of arguments to SUM. Expected between 1 and 255 arguments, but got 0 arguments.")
);
}
#[test]
fn messageless_error_has_no_message() {
let v = eval("=1/0");
assert_eq!(v.error_kind(), Some(&ErrorKind::DivByZero));
assert_eq!(v.error_message(), None);
assert!(matches!(v, Value::Error(ErrorKind::DivByZero)));
assert!(v.is_error());
}
#[test]
fn arity_error_still_equals_bare_error_of_same_code() {
assert_eq!(eval("=DATE()"), Value::Error(ErrorKind::NA));
}
#[test]
fn arity_error_propagates_through_arithmetic() {
let v = eval("=DATE()+1");
assert_eq!(v.error_kind(), Some(&ErrorKind::NA));
assert_eq!(
v.error_message(),
Some("Wrong number of arguments to DATE. Expected 3 arguments, but got 0 arguments.")
);
}
#[test]
fn iferror_catches_arity_error() {
assert_eq!(eval("=IFERROR(DATE(), \"caught\")"), Value::Text("caught".into()));
}
#[test]
fn iserror_and_isna_see_arity_error() {
assert_eq!(eval("=ISERROR(DATE())"), Value::Bool(true));
assert_eq!(eval("=ISNA(DATE())"), Value::Bool(true));
}
#[test]
fn lazy_fn_propagates_nested_arity_error() {
assert_eq!(eval("=TZNOW(DATE())").error_kind(), Some(&ErrorKind::NA));
}
#[test]
fn arity_error_inside_array_literal_is_not_dropped() {
assert_eq!(
eval("=FTEST({1,2,3,DATE()},{4,5,6,7})").error_kind(),
Some(&ErrorKind::NA)
);
}