mod common;
use common::*;
use simple_update_in::Value;
#[test]
fn expand_undefined_as_array() {
let actual = run(Value::Undefined, &[idx(1)], constant(n(1.0)));
assert_eq!(actual, arr(vec![hole(), n(1.0)]));
}
#[test]
fn expand_array_with_hole() {
let from = arr(vec![n(0.0), n(1.0)]);
let actual = run(from.clone(), &[idx(3)], constant(n(3.0)));
assert!(!from.ptr_eq(&actual));
assert_eq!(actual, arr(vec![n(0.0), n(1.0), hole(), n(3.0)]));
}
#[test]
fn fill_past_end_stores_hole_as_undefined() {
let from = arr(vec![n(0.0)]);
let actual = run(from, &[idx(2)], constant(n(3.0)));
let items = actual.as_array().unwrap();
assert!(matches!(items[1], Value::Undefined));
assert!(items[1].is_undefined());
}
#[test]
fn incompatible_type_convert_map_to_array() {
let from = obj(vec![("one", n(1.0))]);
let actual = run(from.clone(), &[idx(1)], constant(n(1.0)));
assert!(!from.ptr_eq(&actual));
assert_eq!(actual, arr(vec![hole(), n(1.0)]));
}
#[test]
fn incompatible_type_convert_string_to_array() {
let from = s("123");
let actual = run(from, &[idx(1)], constant(n(1.0)));
assert_eq!(actual, arr(vec![hole(), n(1.0)]));
}
#[test]
fn update_0_with_negative_0() {
let from = arr(vec![n(0.0)]);
let actual = run(from.clone(), &[idx(0)], constant(n(-0.0)));
assert!(!from.ptr_eq(&actual));
assert_eq!(actual, arr(vec![n(-0.0)]));
}
#[test]
fn update_nan_with_nan() {
let from = arr(vec![n(f64::NAN)]);
let actual = run(from.clone(), &[idx(0)], constant(n(f64::NAN)));
assert!(from.ptr_eq(&actual));
}
#[test]
fn change_across_type_is_not_no_op() {
let from = arr(vec![n(1.0)]);
let actual = run(from.clone(), &[idx(0)], constant(s("1")));
assert!(!from.ptr_eq(&actual));
assert_eq!(actual, arr(vec![s("1")]));
}
#[test]
fn null_root_with_key_coerces_to_map() {
let actual = run(Value::Null, &[key("x")], constant(n(1.0)));
assert_eq!(actual, obj(vec![("x", n(1.0))]));
}
#[test]
fn null_root_with_index_coerces_to_array() {
let actual = run(Value::Null, &[idx(1)], constant(n(1.0)));
assert_eq!(actual, arr(vec![hole(), n(1.0)]));
}
#[test]
fn null_intermediate_coerces_to_map() {
let from = obj(vec![("a", Value::Null)]);
let actual = run(from, &[key("a"), key("b")], constant(n(1.0)));
assert_eq!(actual, obj(vec![("a", obj(vec![("b", n(1.0))]))]));
}
#[test]
fn updater_sees_falsy_present_intermediate_not_undefined() {
use std::cell::Cell;
let from = obj(vec![("a", n(0.0))]);
let seen = Cell::new(Value::Undefined);
let actual = run(from, &[key("a"), key("b")], |v| {
seen.set(v);
n(1.0)
});
assert_eq!(seen.into_inner(), n(0.0));
assert_eq!(actual, obj(vec![("a", obj(vec![("b", n(1.0))]))]));
}