use super::super::function;
use super::string::{parse_decimal_integer, DecimalInteger};
use super::*;
use crate::graph::Graph;
use crate::row::Row;
use std::collections::BTreeMap;
use relate_ir::Expr;
use relate_names::SmallName;
#[test]
fn to_boolean_or_null_and_to_string_or_null_match_neo4j() {
let g = Graph::default();
let graph = ExecGraph::read(&g);
let row = Row::new();
let params = BTreeMap::new();
let call =
|name: &str, arg: Expr| function(&graph, &row, ¶ms, name, false, &[arg]).unwrap();
assert_eq!(
call("toBooleanOrNull", Expr::String("true".into())),
Value::Bool(true)
);
assert_eq!(
call("toBooleanOrNull", Expr::Bool(false)),
Value::Bool(false)
);
assert_eq!(call("toBoolean", Expr::Int(0)), Value::Bool(false));
assert_eq!(call("toBoolean", Expr::Int(1)), Value::Bool(true));
assert_eq!(call("toBoolean", Expr::Int(-7)), Value::Bool(true));
assert_eq!(
call("toBooleanOrNull", Expr::String("nope".into())),
Value::Null
);
assert_eq!(call("toBooleanOrNull", Expr::Null), Value::Null);
assert_eq!(
call("toStringOrNull", Expr::Int(7)),
Value::String("7".into())
);
assert_eq!(call("toStringOrNull", Expr::Null), Value::Null);
}
#[test]
fn tostring_renders_every_list_value() {
let g = Graph::default();
let graph = ExecGraph::read(&g);
let params = BTreeMap::new();
let mut row = Row::new();
row.insert(
"xs".into(),
Value::List(vec![Value::Int(1), Value::Int(2)].into()),
);
row.insert("bytes".into(), Value::Bytes(vec![1, 2].into()));
let call = |name: &str, arg: &Expr| {
function(
&graph,
&row,
¶ms,
name,
false,
std::slice::from_ref(arg),
)
};
enum Want {
Renders(&'static str),
Refused,
}
for (what, arg, want) in [
(
"mixed",
Expr::List(vec![Expr::Int(1), Expr::String("a".into())]),
Want::Renders("[1, a]"),
),
("empty", Expr::List(Vec::new()), Want::Renders("[]")),
(
"with null",
Expr::List(vec![Expr::Int(1), Expr::Null, Expr::Int(2)]),
Want::Renders("[1, null, 2]"),
),
(
"numeric classes",
Expr::List(vec![Expr::Int(1), Expr::Float(2.0)]),
Want::Renders("[1, 2.0]"),
),
(
"variable",
Expr::Variable("xs".into()),
Want::Renders("[1, 2]"),
),
("byte array", Expr::Variable("bytes".into()), Want::Refused),
] {
match want {
Want::Renders(expected) => {
for name in ["toString", "toStringOrNull"] {
assert_eq!(
call(name, &arg).unwrap(),
Value::String(expected.into()),
"{name} of {what}"
);
}
}
Want::Refused => {
assert!(
call("toString", &arg).is_err(),
"toString of {what} must be a type error"
);
assert_eq!(
call("toStringOrNull", &arg).unwrap(),
Value::Null,
"toStringOrNull of {what}"
);
}
}
}
}
#[test]
fn tostring_map_and_scalar_render_in_display_form() {
let g = Graph::default();
let graph = ExecGraph::read(&g);
let row = Row::new();
let params = BTreeMap::new();
let call = |name: &str, arg: Expr| function(&graph, &row, ¶ms, name, false, &[arg]);
let map = Expr::Map(vec![("a".into(), Expr::Int(1))]);
assert_eq!(
call("toStringOrNull", map).unwrap(),
Value::String("{a: 1}".into())
);
assert_eq!(
call("toString", Expr::Int(7)).unwrap(),
Value::String("7".into())
);
}
#[test]
fn to_integer_or_null_and_to_float_or_null_match_neo4j() {
let g = Graph::default();
let graph = ExecGraph::read(&g);
let row = Row::new();
let params = BTreeMap::new();
let call =
|name: &str, arg: Expr| function(&graph, &row, ¶ms, name, false, &[arg]).unwrap();
assert_eq!(
call("toIntegerOrNull", Expr::String("42".into())),
Value::Int(42)
);
assert_eq!(
call("toIntegerOrNull", Expr::String("3.5".into())),
Value::Int(3)
);
assert_eq!(call("toIntegerOrNull", Expr::Int(7)), Value::Int(7));
assert_eq!(call("toIntegerOrNull", Expr::Float(2.9)), Value::Int(2));
assert_eq!(call("toIntegerOrNull", Expr::Null), Value::Null);
assert_eq!(
call("toFloatOrNull", Expr::String("3.5".into())),
Value::Float(3.5)
);
assert_eq!(
call("toFloatOrNull", Expr::String("42".into())),
Value::Float(42.0)
);
assert_eq!(call("toFloatOrNull", Expr::Int(3)), Value::Float(3.0));
assert_eq!(call("toFloatOrNull", Expr::Float(1.25)), Value::Float(1.25));
assert_eq!(call("toFloatOrNull", Expr::Null), Value::Null);
assert_eq!(
call("toIntegerOrNull", Expr::String("abc".into())),
Value::Null
);
assert_eq!(
call("toFloatOrNull", Expr::String("nope".into())),
Value::Null
);
assert_eq!(call("toIntegerOrNull", Expr::Bool(true)), Value::Int(1));
assert_eq!(call("toFloatOrNull", Expr::Bool(true)), Value::Null);
assert_eq!(
call("toIntegerOrNull", Expr::List(vec![Expr::Int(1)])),
Value::Null
);
assert_eq!(
call("toFloatOrNull", Expr::List(vec![Expr::Int(1)])),
Value::Null
);
assert_eq!(
function(
&graph,
&row,
¶ms,
"toInteger",
false,
&[Expr::Bool(true)]
)
.unwrap(),
Value::Int(1)
);
assert!(function(&graph, &row, ¶ms, "toFloat", false, &[Expr::Bool(true)]).is_err());
}
enum Want {
Is(Value),
OutOfRange,
}
const NULL: Want = Want::Is(Value::Null);
const OUT: Want = Want::OutOfRange;
const OUT_OF_RANGE: &str = "type error: InvalidArgumentValue: toInteger()";
fn int(i: i64) -> Want {
Want::Is(Value::Int(i))
}
fn float(f: f64) -> Want {
Want::Is(Value::Float(f))
}
fn ints(xs: &[i64]) -> Want {
Want::Is(Value::List(
xs.iter()
.copied()
.map(Value::Int)
.collect::<Vec<_>>()
.into(),
))
}
fn list(xs: Vec<Value>) -> Want {
Want::Is(Value::List(xs.into()))
}
fn same(a: &Value, b: &Value) -> bool {
match (a, b) {
(Value::Float(x), Value::Float(y)) => x == y || (x.is_nan() && y.is_nan()),
(Value::List(xs), Value::List(ys)) => {
xs.len() == ys.len() && xs.iter().zip(ys.iter()).all(|(x, y)| same(x, y))
}
_ => a == b,
}
}
fn check_probes(rows: Vec<(&str, Want)>) {
let g = Graph::default();
let graph = ExecGraph::read(&g);
for (cypher, want) in rows {
let parsed = relate_parser::parser::parse_expression_str(cypher).expect(cypher);
let expr = relate_front::lower_expr(&parsed).expect(cypher);
let got = crate::eval::eval(&graph, &Row::new(), &expr, &BTreeMap::new());
match (want, got) {
(Want::Is(want), Ok(got)) => {
assert!(same(&got, &want), "{cypher}: got {got:?}, want {want:?}")
}
(Want::OutOfRange, Err(e)) => assert!(
e.to_string().starts_with(OUT_OF_RANGE),
"{cypher}: got error {e}, want {OUT_OF_RANGE}"
),
(Want::Is(want), Err(e)) => panic!("{cypher}: got error {e}, want {want:?}"),
(Want::OutOfRange, Ok(got)) => {
panic!("{cypher}: got {got:?}, want error {OUT_OF_RANGE}")
}
}
}
}
#[test]
fn conversions_match_adr_0168_probe_table() {
let (max, min) = (i64::MAX, i64::MIN);
let (nan, inf, ninf) = (f64::NAN, f64::INFINITY, f64::NEG_INFINITY);
check_probes(vec![
("toInteger(0.0/0.0)", int(0)),
("toInteger(1.0/0.0)", int(max)),
("toInteger(-1.0/0.0)", int(min)),
("toInteger(1e300)", int(max)),
("toInteger(-1e300)", int(min)),
("toInteger(9.3e18)", int(max)),
("toInteger(-9.3e18)", int(min)),
("toInteger(9.2e18)", int(9_200_000_000_000_000_000)),
("toInteger('NaN')", NULL),
("toInteger('nan')", NULL),
("toInteger('Infinity')", NULL),
("toInteger('-Infinity')", NULL),
("toInteger('inf')", NULL),
("toInteger('1e300')", OUT),
("toInteger('9.3e18')", OUT),
("toIntegerOrNull(0.0/0.0)", int(0)),
("toIntegerOrNull(1.0/0.0)", int(max)),
("toIntegerOrNull(-1.0/0.0)", int(min)),
("toIntegerOrNull(1e300)", int(max)),
("toIntegerOrNull('NaN')", NULL),
("toIntegerOrNull('1e300')", NULL),
("toIntegerList([0.0/0.0])", ints(&[0])),
("toIntegerList([1.0/0.0])", ints(&[max])),
("toIntegerList([-1.0/0.0])", ints(&[min])),
("toIntegerList([1e300])", ints(&[max])),
(
"toIntegerList([2.5, 9007199254740993])",
ints(&[2, 9_007_199_254_740_993]),
),
("toFloat('NaN')", float(nan)),
("toFloat('-NaN')", float(nan)),
("toFloat('nan')", NULL),
("toFloat('Infinity')", float(inf)),
("toFloat('-Infinity')", float(ninf)),
("toFloat('+Infinity')", float(inf)),
("toFloat('infinity')", NULL),
("toFloat('inf')", NULL),
("toFloat('Inf')", NULL),
("toFloat('-inf')", NULL),
("toFloat('1e400')", float(inf)),
("toFloat(0.0/0.0)", float(nan)),
("toFloat(1.0/0.0)", float(inf)),
("toFloatOrNull('NaN')", float(nan)),
("toFloatOrNull('nan')", NULL),
("toFloatOrNull('Infinity')", float(inf)),
("toFloatOrNull('inf')", NULL),
(
"toFloatList([0.0/0.0, 1.0/0.0])",
Want::Is(Value::List(
vec![Value::Float(nan), Value::Float(inf)].into(),
)),
),
]);
}
#[test]
fn conversions_match_adr_0168_supplementary_probes() {
let (max, min) = (i64::MAX, i64::MIN);
check_probes(vec![
("toInteger('-1e300')", OUT),
("toInteger('-9.3e18')", OUT),
("toInteger('1e400')", OUT),
("toInteger('-1e400')", OUT),
("toInteger(' 1e300 ')", OUT),
("toIntegerOrNull('1e400')", NULL),
("toIntegerOrNull('9.3e18')", NULL),
("toInteger('9223372036854775807')", int(max)),
("toInteger('9223372036854775808')", OUT),
("toIntegerOrNull('9223372036854775808')", NULL),
("toInteger('-9223372036854775808')", int(min)),
("toInteger('-9223372036854775809')", OUT),
("toInteger('9223372036854775807.0')", int(max)),
("toInteger('9223372036854775807.5')", OUT),
(
"toInteger('9223372036854775806.5')",
int(9_223_372_036_854_775_806),
),
("toInteger('-9223372036854775808.0')", int(min)),
("toInteger('-9223372036854775808.5')", OUT),
(
"toInteger('9007199254740993.5')",
int(9_007_199_254_740_993),
),
("toInteger('9.2e18')", int(9_200_000_000_000_000_000)),
("toInteger('-0.5')", int(0)),
("toInteger('1e-400')", int(0)),
("toInteger('1e3000000000')", int(max)),
("toInteger('+Infinity')", NULL),
("toInteger('-NaN')", NULL),
(
"toIntegerList([9223372036854775806])",
ints(&[9_223_372_036_854_775_806]),
),
(
"toIntegerList([-9223372036854775807])",
ints(&[-9_223_372_036_854_775_807]),
),
("toIntegerList([9.3e18, -1e300])", ints(&[max, min])),
(
"toIntegerList([9223372036854775807, -9223372036854775808])",
ints(&[max, min]),
),
("toFloat('+NaN')", float(f64::NAN)),
("toFloat('NAN')", NULL),
("toFloat('INFINITY')", NULL),
("toFloat('+inf')", NULL),
("toFloat('-1e400')", float(f64::NEG_INFINITY)),
("toFloatOrNull('-Infinity')", float(f64::NEG_INFINITY)),
("toFloatOrNull('Inf')", NULL),
(
"toIntegerList(vector([9007199254740993, 2], 2, INTEGER64))",
ints(&[9_007_199_254_740_992, 2]),
),
(
"toIntegerList(vector([2.5, 1e300], 2, FLOAT64))",
ints(&[2, max]),
),
]);
}
#[test]
fn decimal_integer_parse_is_exact_and_rejects_non_decimals() {
use DecimalInteger::{InRange, OutOfRange};
for (input, want) in [
("2.9", InRange(2)),
("1.7", InRange(1)),
("-2.9", InRange(-2)),
("+42", InRange(42)),
("007", InRange(7)),
("7.", InRange(7)),
(".5", InRange(0)),
("-.5", InRange(0)),
("1E3", InRange(1000)),
("1e+3", InRange(1000)),
("1234e-2", InRange(12)),
("0.000123e7", InRange(1230)),
("10000000000000000000", OutOfRange),
("-9223372036854775808e0", InRange(i64::MIN)),
("922337203685477580.75e1", OutOfRange),
("922337203685477580.70e1", InRange(i64::MAX)),
] {
assert_eq!(parse_decimal_integer(input), Some(want), "{input}");
}
for input in [
"", "+", "-", ".", "e5", "1e", "1e+", "1.2.3", "1e5e3", "--1", "+-1", "0x10", "1_000",
"1.5d", " 1", "NaN", "Infinity", "inf", "nan",
] {
assert_eq!(parse_decimal_integer(input), None, "{input:?}");
}
for input in ["1e99999999999999999999x", "1e-99999999999999999999x"] {
assert_eq!(parse_decimal_integer(input), None, "{input:?}");
}
for input in [
"1e3000000000",
"-1e3000000000",
"1e-3000000000",
"1e2147483648",
"0e99999999999999999999999",
"5e-99999999999999999999999",
"5e99999999999999999999999",
] {
assert_eq!(parse_decimal_integer(input), None, "{input:?}");
}
assert_eq!(
parse_decimal_integer("1e2147483647"),
Some(OutOfRange),
"1e2147483647"
);
}
#[test]
fn conversions_match_adr_0175_list_elements_and_string_forms() {
let (max, min) = (i64::MAX, i64::MIN);
let (nan, inf, ninf) = (f64::NAN, f64::INFINITY, f64::NEG_INFINITY);
check_probes(vec![
(
"toIntegerList(['1', 'NaN'])",
list(vec![Value::Int(1), Value::Null]),
),
(
"toFloatList(['1.5', 'NaN'])",
list(vec![Value::Float(1.5), Value::Float(nan)]),
),
("toIntegerList(['1e300'])", list(vec![Value::Null])),
(
"toIntegerList(['9223372036854775808', 'x', 1.5])",
list(vec![Value::Null, Value::Null, Value::Int(1)]),
),
(
"toIntegerList([' 7 ', '2.9', '-0.5'])",
list(vec![Value::Int(7), Value::Int(2), Value::Int(0)]),
),
(
"toFloatList(['Infinity', 'inf', ' 1.5 ', '1e400'])",
list(vec![
Value::Float(inf),
Value::Null,
Value::Float(1.5),
Value::Float(inf),
]),
),
(
"toIntegerList(['0x1F', '1_000', '1.5f'])",
list(vec![Value::Int(31), Value::Int(1000), Value::Int(1)]),
),
(
"toFloatList(['0x1p3', '1.5d'])",
list(vec![Value::Float(8.0), Value::Float(1.5)]),
),
(
"toIntegerList([true, 'true'])",
list(vec![Value::Int(1), Value::Null]),
),
(
"toFloatList([true, 'true'])",
list(vec![Value::Null, Value::Null]),
),
(
"toIntegerList(['1e3000000000'])",
list(vec![Value::Int(max)]),
),
("toIntegerList(['1e400'])", list(vec![Value::Null])),
(
"toFloatList(['1e3000000000'])",
list(vec![Value::Float(inf)]),
),
(
"toIntegerList(['9223372036854775807.5'])",
list(vec![Value::Null]),
),
(
"toIntegerList(['9223372036854775807.0'])",
list(vec![Value::Int(max)]),
),
(
"toFloatList(['0x1F', '1_000', '0o17'])",
list(vec![
Value::Float(31.0),
Value::Float(1000.0),
Value::Float(15.0),
]),
),
(
"toIntegerList(['nan', 'inf', '-Infinity'])",
list(vec![Value::Null, Value::Null, Value::Null]),
),
(
"toFloatList(['nan', 'inf', '-Infinity'])",
list(vec![Value::Null, Value::Null, Value::Float(ninf)]),
),
(
"toIntegerList([null, '42'])",
list(vec![Value::Null, Value::Int(42)]),
),
(
"toFloatList([null, '42'])",
list(vec![Value::Null, Value::Float(42.0)]),
),
(
"toIntegerList(['+1', '-0', '0001'])",
list(vec![Value::Int(1), Value::Int(0), Value::Int(1)]),
),
(
"toFloatList(['.5', '5.', '+.5', '1e3'])",
list(vec![
Value::Float(0.5),
Value::Float(5.0),
Value::Float(0.5),
Value::Float(1000.0),
]),
),
(
"toIntegerList(['.5', '5.', '1e3'])",
list(vec![Value::Int(0), Value::Int(5), Value::Int(1000)]),
),
(
"toIntegerList([true, false])",
list(vec![Value::Int(1), Value::Int(0)]),
),
(
"toFloatList([true, false])",
list(vec![Value::Null, Value::Null]),
),
("toIntegerList(['\\t7\\n'])", list(vec![Value::Int(7)])),
("toIntegerList([' 0x1F '])", list(vec![Value::Int(31)])),
("toFloatList([' 0x1p3 '])", list(vec![Value::Float(8.0)])),
(
"toIntegerList(['2L', '2f'])",
list(vec![Value::Null, Value::Null]),
),
(
"toFloatList(['2L', '2f'])",
list(vec![Value::Null, Value::Float(2.0)]),
),
("toInteger(' 1.5 ')", int(1)),
("toFloat(' 1.5 ')", float(1.5)),
("toIntegerOrNull(' 1.5 ')", int(1)),
("toFloatOrNull(' 1.5 ')", float(1.5)),
("toInteger('1.5f')", int(1)),
("toFloat('1.5f')", float(1.5)),
("toIntegerOrNull('1.5f')", int(1)),
("toFloatOrNull('1.5f')", float(1.5)),
("toInteger('1.5F')", int(1)),
("toFloat('1.5F')", float(1.5)),
("toIntegerOrNull('1.5F')", int(1)),
("toFloatOrNull('1.5F')", float(1.5)),
("toInteger('1.5d')", int(1)),
("toFloat('1.5d')", float(1.5)),
("toIntegerOrNull('1.5d')", int(1)),
("toFloatOrNull('1.5d')", float(1.5)),
("toInteger('1.5D')", int(1)),
("toFloat('1.5D')", float(1.5)),
("toIntegerOrNull('1.5D')", int(1)),
("toFloatOrNull('1.5D')", float(1.5)),
("toInteger('2f')", NULL),
("toFloat('2f')", float(2.0)),
("toIntegerOrNull('2f')", NULL),
("toFloatOrNull('2f')", float(2.0)),
("toInteger('2L')", NULL),
("toFloat('2L')", NULL),
("toIntegerOrNull('2L')", NULL),
("toFloatOrNull('2L')", NULL),
("toInteger('0x1F')", int(31)),
("toFloat('0x1F')", float(31.0)),
("toIntegerOrNull('0x1F')", int(31)),
("toFloatOrNull('0x1F')", float(31.0)),
("toInteger('-0x1F')", int(-31)),
("toFloat('-0x1F')", float(-31.0)),
("toIntegerOrNull('-0x1F')", int(-31)),
("toFloatOrNull('-0x1F')", float(-31.0)),
("toInteger('0o17')", int(15)),
("toFloat('0o17')", float(15.0)),
("toIntegerOrNull('0o17')", int(15)),
("toFloatOrNull('0o17')", float(15.0)),
("toInteger('017')", int(17)),
("toFloat('017')", float(17.0)),
("toIntegerOrNull('017')", int(17)),
("toFloatOrNull('017')", float(17.0)),
("toInteger('1_000')", int(1000)),
("toFloat('1_000')", float(1000.0)),
("toIntegerOrNull('1_000')", int(1000)),
("toFloatOrNull('1_000')", float(1000.0)),
("toInteger('1_000.5')", int(1000)),
("toFloat('1_000.5')", float(1000.5)),
("toIntegerOrNull('1_000.5')", int(1000)),
("toFloatOrNull('1_000.5')", float(1000.5)),
("toInteger('0x1p3')", NULL),
("toFloat('0x1p3')", float(8.0)),
("toIntegerOrNull('0x1p3')", NULL),
("toFloatOrNull('0x1p3')", float(8.0)),
("toInteger('0x1.8p1')", NULL),
("toFloat('0x1.8p1')", float(3.0)),
("toIntegerOrNull('0x1.8p1')", NULL),
("toFloatOrNull('0x1.8p1')", float(3.0)),
("toInteger('1e3000000000')", int(max)),
("toFloat('1e3000000000')", float(inf)),
("toIntegerOrNull('1e3000000000')", int(max)),
("toFloatOrNull('1e3000000000')", float(inf)),
("toInteger('-1e3000000000')", int(min)),
("toFloat('-1e3000000000')", float(ninf)),
("toIntegerOrNull('-1e3000000000')", int(min)),
("toFloatOrNull('-1e3000000000')", float(ninf)),
("toInteger('1e-3000000000')", int(0)),
("toFloat('1e-3000000000')", float(0.0)),
("toIntegerOrNull('1e-3000000000')", int(0)),
("toFloatOrNull('1e-3000000000')", float(0.0)),
("toInteger('\\t1.5\\t')", int(1)),
("toFloat('\\t1.5\\t')", float(1.5)),
("toIntegerOrNull('\\t1.5\\t')", int(1)),
("toFloatOrNull('\\t1.5\\t')", float(1.5)),
("toInteger('\\n1.5\\n')", int(1)),
("toFloat('\\n1.5\\n')", float(1.5)),
("toIntegerOrNull('\\n1.5\\n')", int(1)),
("toFloatOrNull('\\n1.5\\n')", float(1.5)),
("toInteger('1e-400')", int(0)),
("toFloat('1e-400')", float(0.0)),
("toIntegerOrNull('1e-400')", int(0)),
("toFloatOrNull('1e-400')", float(0.0)),
("toInteger('.5f')", int(0)),
("toFloat('.5f')", float(0.5)),
("toIntegerOrNull('.5f')", int(0)),
("toFloatOrNull('.5f')", float(0.5)),
("toInteger('0x')", NULL),
("toFloat('0x')", NULL),
("toInteger('0X1F')", NULL),
("toFloat('0X1F')", NULL),
("toInteger('+0x1F')", NULL),
("toFloat('+0x1F')", NULL),
("toInteger('0X1P3')", NULL),
("toFloat('0X1P3')", float(8.0)),
("toInteger('-1.5f')", int(-1)),
("toFloat('-1.5f')", float(-1.5)),
("toInteger('1.5e2f')", int(150)),
("toFloat('1.5e2f')", float(150.0)),
("toFloat(' Infinity ')", float(inf)),
("toFloat('NaN ')", float(nan)),
("toFloat('\\u00A01.5\\u00A0')", float(1.5)),
("toInteger('\\u00A07\\u00A0')", int(7)),
("toInteger('')", NULL),
("toFloat('')", NULL),
("toInteger('1 // :(')", int(1)),
("toInteger('-1 // :(')", int(-1)),
("toInteger('/* :( */ 1 /* :( */')", int(1)),
("toInteger('-/* :( */ 1 /* :( */')", int(-1)),
("toFloat('1 // 2')", float(1.0)),
("toInteger('1 1')", NULL),
("toInteger('1/*c*/2')", NULL),
("toInteger('1 /* unterminated')", NULL),
]);
}
#[test]
fn to_string_renders_temporals_at_full_precision() {
let t = relate_core::temporal::datetime_from_epoch(1_709_164_800, 0).unwrap();
let full = t.full_seconds_string();
assert_eq!(
to_string_value(Some(&Value::Temporal(t.clone()))),
Ok(Value::String(full.clone().into()))
);
let nested = Value::Map(BTreeMap::from([("at".to_string(), Value::Temporal(t))]).into());
assert_eq!(
to_string_value(Some(&nested)),
Ok(Value::String(format!("{{at: {full}}}").into()))
);
}
#[test]
fn to_string_list_accepts_a_vector_argument() {
check_probes(vec![(
"toStringList(vector([1, 2], 2, INTEGER64))",
Want::Is(Value::List(
vec![Value::String("1.0".into()), Value::String("2.0".into())].into(),
)),
)]);
}
#[test]
fn to_string_list_renders_non_scalar_elements_in_display_form() {
let g = Graph::default();
let graph = ExecGraph::read(&g);
let s = |v: &str| Value::String(v.into());
let list = |xs: Vec<Value>| Value::List(xs.into());
let mixed = || {
list(vec![
Value::Int(1),
list(vec![Value::Int(2), Value::Int(3)]),
Value::Null,
])
};
assert_eq!(
bi_tostringlist(&graph, "toStringList", vec![mixed()]),
Ok(list(vec![s("1"), s("[2, 3]"), s("null")]))
);
assert_eq!(
bi_tostringlist(&graph, "toStringList", vec![Value::Null]),
Ok(Value::Null)
);
assert!(bi_tostringlist(&graph, "toStringList", vec![Value::Int(1)]).is_err());
}
#[test]
fn tostring_rejects_a_second_argument() {
let g = Graph::default();
let graph = ExecGraph::read(&g);
for name in ["toString", "toStringOrNull"] {
let err = function(
&graph,
&Row::new(),
&BTreeMap::new(),
name,
false,
&[Expr::Int(7), Expr::Int(8)],
)
.unwrap_err();
assert!(
err.to_string().contains("InvalidNumberOfArguments"),
"{name}: {err}"
);
}
}
#[test]
fn arity_is_reported_before_any_argument_is_evaluated() {
let g = Graph::default();
let graph = ExecGraph::read(&g);
let boom = Expr::Function {
name: SmallName::from_name("no_such_function"),
distinct: false,
args: Vec::new(),
};
let string = |value: &str| Expr::String(value.to_string());
for (name, args) in [
("toString", vec![Expr::Int(1), boom.clone()]),
("toStringOrNull", vec![Expr::Int(1), boom.clone()]),
("split", vec![string("a,b"), string(","), boom.clone()]),
("split", vec![string("a,b")]),
("split", vec![string("a"), string(","), string("b")]),
] {
let err = function(&graph, &Row::new(), &BTreeMap::new(), name, false, &args).unwrap_err();
assert!(
err.to_string().contains("InvalidNumberOfArguments"),
"{name}: {err}"
);
}
}