compmd 0.13.0

The CompMD processor: parses, validates and expands CompMD (Component Markdown) documents. In development.
Documentation
//! CEL's optional types in the profile (spec ยง8.2): an optional made, selected or indexed into, its methods and
//! macros, and the errors that are left.

use super::{Failed, Refused, V, check};
use serde_json::{Value as Json, json};

#[test]
fn an_optional_is_made_and_read() {
    check(&[
        ("optional.of(2).value()", V(json!(2))),
        ("optional.of(2).hasValue()", V(json!(true))),
        ("optional.none().hasValue()", V(json!(false))),
        ("optional.none().orValue(\"x\")", V(json!("x"))),
        ("optional.of(1).orValue(2)", V(json!(1))),
        ("optional.none().or(optional.of(3)).value()", V(json!(3))),
        ("optional.of(1).or(optional.of(3)).value()", V(json!(1))),
        ("optional.of(1) == optional.of(1)", V(json!(true))),
        ("optional.none() == optional.none()", V(json!(true))),
        ("optional.of(\"a\")", V(json!("a"))),
        ("optional.none()", V(Json::Null)),
    ]);
}

#[test]
fn a_field_or_an_element_that_isnt_there_is_none() {
    check(&[
        ("md.doc.?title.orValue(\"untitled\")", V(json!("Release"))),
        ("md.doc.?subtitle.orValue(\"none\")", V(json!("none"))),
        ("md.doc.?subtitle.hasValue()", V(json!(false))),
        ("rows[?1].value()", V(json!(2.0))),
        ("rows[?5].hasValue()", V(json!(false))),
        ("rows[?-1].hasValue()", V(json!(false))),
        ("{\"a\": 1}[?\"b\"].orValue(0)", V(json!(0))),
        ("{\"a\": 1}.?a.value()", V(json!(1))),
    ]);
}

#[test]
fn a_select_or_an_index_on_an_optional_reads_its_value_and_stays_optional() {
    check(&[
        ("md.?doc.title.value()", V(json!("Release"))),
        // A method isn't lifted: `optMap` calls it on the value.
        ("md.doc.?subtitle.size()", Refused(18)),
        (
            "md.doc.?subtitle.optMap(s, s.size()).hasValue()",
            V(json!(false)),
        ),
        ("md.doc.?title.optMap(s, s.size()).value()", V(json!(7))),
        ("md.data.?sales[0].region.value()", V(json!("eu"))),
        ("md.?nothing.title.hasValue()", V(json!(false))),
        ("md.data.sales[?1].?note.value()", V(json!("late"))),
        ("md.data.sales[?0].?note.hasValue()", V(json!(false))),
    ]);
}

#[test]
fn opt_map_and_opt_flat_map_keep_none_as_none() {
    check(&[
        ("optional.of(2).optMap(x, x * 3).value()", V(json!(6))),
        (
            "optional.none().optMap(x, x * 3).hasValue()",
            V(json!(false)),
        ),
        (
            "optional.of(2).optFlatMap(x, x > 1 ? optional.of(x) : optional.none()).value()",
            V(json!(2)),
        ),
        (
            "rows[?0].optFlatMap(r, r > 1.0 ? optional.of(r) : optional.none()).hasValue()",
            V(json!(false)),
        ),
    ]);
}

#[test]
fn what_is_still_an_error() {
    check(&[
        // No value to give.
        ("optional.none().value()", Failed(17)),
        // Not an optional, or not of the right kind.
        ("rows.optMap(x, x)", Refused(6)),
        ("optional.of(1).optFlatMap(x, x)", Refused(16)),
        ("optional.of(1).map(x, x)", Refused(16)),
        ("count.?a", Refused(8)),
        ("rows[?\"a\"]", Refused(5)),
        ("optional.some(1)", Refused(10)),
        ("1.hasValue()", Refused(3)),
        ("optional.of(1).?a", Refused(17)),
        // At the start of an optional read, as at any other operand's.
        ("md.doc.?title + 1", Refused(1)),
        ("rows[?0] + 1", Refused(1)),
    ]);
}

#[test]
fn what_the_checker_cant_tell_fails_when_evaluated() {
    // A list of an optional and an int is a list of `dyn`.
    check(&[
        ("[optional.of(1), 1][1].optMap(x, x)", Failed(24)),
        ("[optional.of(1), 1][0].optMap(x, x).value()", V(json!(1))),
        (
            "optional.of(1).optFlatMap(x, [x, optional.none()][0])",
            Failed(16),
        ),
        ("[1][?[\"a\", 1][0]]", Failed(4)),
    ]);
}

#[test]
fn none_read_into_is_none() {
    check(&[
        ("optional.none().?a.hasValue()", V(json!(false))),
        ("optional.none()[?0].hasValue()", V(json!(false))),
    ]);
}