Lines
100 %
Functions
Branches
use forms::{
Field, Form, FormValues, Kind, Row, TRANSACTION_CREATE, TRANSACTION_EDIT, TRANSACTION_QUERY,
all,
};
const TAG_CELLS: [Field; 2] = [
Field {
key: "name",
label: "Name",
kind: Kind::Text {
multiline: false,
suggest: None,
},
required: false,
key: "value",
label: "Value",
multiline: true,
];
const TAGGED: Form = Form {
name: "tagged",
title: "Tagged",
fields: &[
key: "note",
label: "Note",
key: "tags",
label: "Tags",
kind: Kind::Rows {
fields: &TAG_CELLS,
nested_under: None,
],
action: forms::Action::Mutation,
fn pairs(items: &[(&str, &str)]) -> Vec<(String, String)> {
items
.iter()
.map(|(k, v)| ((*k).into(), (*v).into()))
.collect()
}
fn decode(form: &Form, items: &[(&str, &str)]) -> Result<FormValues, Vec<forms::FieldError>> {
FormValues::from_pairs(form, pairs(items))
fn whole_form_error(form: &Form, items: &[(&str, &str)]) -> String {
let errors = decode(form, items).expect_err("must be refused");
assert_eq!(errors.len(), 1);
assert_eq!(errors[0].field, "");
errors[0].message.clone()
fn fill(form: &Form) -> FormValues {
let mut values = FormValues::default();
for field in form.fields {
match field.kind {
Kind::Rows { fields, .. } => {
values.set_rows(field.key);
for n in 0..3 {
let row: Row = fields
.map(|f| (f.key.to_string(), format!("{}{n}", f.key)))
.collect();
values.push_row(field.key, row);
_ => values.set(field.key, &format!("{}-v", field.key)),
values
fn empty_rows(form: &Form) -> FormValues {
if matches!(field.kind, Kind::Rows { .. }) {
fn round_trip(form: &Form, values: &FormValues) {
let decoded = FormValues::from_pairs(form, values.to_pairs(form));
assert_eq!(decoded.as_ref(), Ok(values), "{}", form.name);
#[test]
fn round_trip_over_every_descriptor() {
let mut forms = all().to_vec();
forms.push(&TAGGED);
for form in forms {
round_trip(form, &fill(form));
round_trip(form, &empty_rows(form));
round_trip(form, &FormValues::default());
fn create_edit_and_query_are_covered() {
let names: Vec<_> = all().iter().map(|f| f.name).collect();
for form in [&TRANSACTION_CREATE, &TRANSACTION_EDIT, &TRANSACTION_QUERY] {
assert!(names.contains(&form.name));
fn bare_rows_key_is_the_presence_marker() {
let values = decode(&TAGGED, &[("tags", "")]).expect("decodes");
assert_eq!(values.rows("tags"), Some(&[][..]));
let missing = decode(&TAGGED, &[("note", "x")]).expect("decodes");
assert_eq!(missing.rows("tags"), None);
fn cells_without_marker_make_the_field_present() {
let values = decode(&TAGGED, &[("tags.0.name", "a")]).expect("decodes");
assert_eq!(values.rows("tags").map(<[Row]>::len), Some(1));
fn to_pairs_emits_marker_before_cells_and_skips_missing() {
values.set_rows("tags");
values.push_row("tags", Row::from([("name".into(), "n".into())]));
assert_eq!(
values.to_pairs(&TAGGED),
pairs(&[("tags", ""), ("tags.0.name", "n")])
);
assert!(FormValues::default().to_pairs(&TAGGED).is_empty());
fn marker_with_text_is_refused_at_its_key() {
let errors = decode(&TAGGED, &[("tags", "x")]).expect_err("refused");
assert_eq!(errors[0].field, "tags");
fn rows_are_ordered_by_index() {
let values = decode(&TAGGED, &[("tags.1.name", "b"), ("tags.0.name", "a")]).expect("decodes");
let names: Vec<_> = values
.rows("tags")
.unwrap_or_default()
.map(|r| r["name"].as_str())
assert_eq!(names, ["a", "b"]);
fn hidden_fields_decode_like_any_other() {
let values = decode(
&TRANSACTION_EDIT,
&[("id", "abc"), ("splits.0.hidden", "(:x 1)")],
)
.expect("decodes");
assert_eq!(values.text("id"), Some("abc"));
values.rows("splits").map(|r| r[0]["hidden"].as_str()),
Some("(:x 1)")
fn duplicate_address_is_refused() {
let message = whole_form_error(&TAGGED, &[("note", "a"), ("note", "b")]);
assert!(message.contains("duplicate"));
whole_form_error(&TAGGED, &[("tags", ""), ("tags", "")]);
whole_form_error(&TAGGED, &[("tags.0.name", "a"), ("tags.0.name", "b")]);
fn index_gap_is_refused_not_dropped() {
let message = whole_form_error(&TAGGED, &[("tags.0.name", "a"), ("tags.2.name", "c")]);
assert!(message.contains("missing"));
whole_form_error(&TAGGED, &[("tags.1.name", "b")]);
fn unknown_address_is_refused() {
whole_form_error(&TAGGED, &[("nope", "x")]);
whole_form_error(&TAGGED, &[("note.0.name", "x")]);
whole_form_error(&TAGGED, &[("tags.0.nope", "x")]);
whole_form_error(&TAGGED, &[("note.0", "x")]);
whole_form_error(&TAGGED, &[("tags.0.name.x", "x")]);
fn bad_index_is_refused() {
for index in ["", "01", "+1", "-1", "x", "1e1", "99999999999999999999999"] {
let name = format!("tags.{index}.name");
whole_form_error(&TAGGED, &[(name.as_str(), "x")]);
fn crlf_and_lone_cr_become_lf() {
&TAGGED,
&[("note", "a\r\nb\rc"), ("tags.0.value", "x\r\ny")],
assert_eq!(values.text("note"), Some("a\nb\nc"));
values.rows("tags").map(|r| r[0]["value"].as_str()),
Some("x\ny")
fn zero_cell_row_round_trips() {
values.push_row("tags", Row::new());
let encoded = values.to_pairs(&TAGGED);
encoded,
pairs(&[
("tags", ""),
("tags.0", ""),
("tags.1.name", "n"),
("tags.2", "")
])
round_trip(&TAGGED, &values);
fn row_marker_may_accompany_cells_and_counts_for_gaps() {
let values = decode(&TAGGED, &[("tags.0", ""), ("tags.0.name", "a")]).expect("decodes");
values.rows("tags").map(|r| r[0]["name"].as_str()),
Some("a")
whole_form_error(&TAGGED, &[("tags.0", ""), ("tags.2", "")]);
fn row_marker_with_text_is_refused_at_its_address() {
let errors = decode(&TAGGED, &[("tags.0", "x")]).expect_err("refused");
assert_eq!(errors[0].field, "tags.0");
fn duplicate_row_marker_is_refused() {
let message = whole_form_error(&TAGGED, &[("tags.0", ""), ("tags.0", "")]);