use super::documents::{declare_document, DocTree, TagRule, Vocabulary};
use super::element::{attributes, elements};
use super::{Declared, Family};
pub use super::{fragment, json, El, Json, Value};
elements! {
testplan: children;
condition: leaf;
fixture: leaf;
step: leaf;
}
attributes! {
pub trait TestPlanAttrs {
title; device; note;
id; quantity; value; min; max; unit; kind; channel;
fixture;
command;
read;
equals; settle_ms; timeout_ms;
condition;
verifies;
}
}
pub(crate) const FAMILY: Family = Family { module: "testplans", roots: &["testplan"], declare };
pub const TESTPLAN_FIELDS: &[&str] = &["title", "device", "note"];
pub const CONDITION_FIELDS: &[&str] = &["id", "quantity", "value", "min", "max", "unit", "note"];
pub const FIXTURE_FIELDS: &[&str] = &["id", "kind", "unit", "channel", "note"];
pub const STEP_FIELDS: &[&str] =
&["id", "channel", "fixture", "command", "read", "min", "max", "equals", "unit", "settleMs", "timeoutMs", "condition", "verifies", "note"];
const LISTS: &[(&str, &str)] = &[("condition", "conditions"), ("fixture", "fixtures"), ("step", "steps")];
const TESTPLAN: Vocabulary = Vocabulary {
format: "testplan",
noun: "a test plan",
root: "testplan",
tags: &[
TagRule::new("testplan", &[]).attrs(TESTPLAN_FIELDS),
TagRule::new("condition", &["testplan"]).key("id").required(&["id", "quantity"]).attrs(CONDITION_FIELDS),
TagRule::new("fixture", &["testplan"]).key("id").required(&["id", "kind"]).attrs(FIXTURE_FIELDS),
TagRule::new("step", &["testplan"]).key("id").required(&["id"]).attrs(STEP_FIELDS),
],
from_tree: testplan_of,
};
fn declare(root: El) -> Result<Declared, String> {
declare_document(&root, &TESTPLAN)
}
fn testplan_of(t: &DocTree) -> Result<Json, String> {
let mut out = t.record();
for (tag, field) in LISTS {
let items: Vec<Json> = t.children.iter().filter(|c| c.tag == *tag).map(DocTree::record).collect();
if !items.is_empty() {
out.set(field, Json::Arr(items));
}
}
for s in t.children.iter().filter(|c| c.tag == "step") {
let at = match s.attr_of("id") {
Some(id) => format!("step().id({})", id.text()),
None => "step()".into(),
};
if let Some(v) = s.attr_of("verifies") {
let refs = matches!(v, Json::Arr(items) if items.iter().all(|r| matches!(r, Json::Str(s) if !s.is_empty())));
if !refs {
return Err(format!("{at} verifies is a list of refs to requirements, not {}", v.text()));
}
}
if let Some(v) = s.attr_of("command") {
if !matches!(v, Json::Obj(_)) {
return Err(format!("{at} command is one record in the channel format's shape ({{ verb: … }}), not {}", v.text()));
}
}
}
Ok(out)
}