Lines
96.43 %
Functions
100 %
Branches
use forms::{Action, Field, Form, Kind, all, descriptor_sexpr};
const FIELDS: &[Field] = &[
Field {
key: "period",
label: "Period",
kind: Kind::Choice {
options: &[("month", "Month"), ("year", "Year \"all\"")],
default: Some("month"),
},
required: false,
key: "mode",
label: "Mode",
options: &[("all", "All"), ("any", "Any")],
default: None,
required: true,
];
const FORM: Form = Form {
name: "choice-test",
title: "Choice test",
fields: FIELDS,
action: Action::Query,
};
#[test]
fn a_choice_writes_its_options_and_default() {
assert_eq!(
descriptor_sexpr(&FORM),
concat!(
"(:name \"choice-test\" :title \"Choice test\" :action query :fields (",
"(:key \"period\" :label \"Period\" :kind choice ",
":options ((\"month\" \"Month\") (\"year\" \"Year \\\"all\\\"\")) :default \"month\" :required nil) ",
"(:key \"mode\" :label \"Mode\" :kind choice ",
":options ((\"all\" \"All\") (\"any\" \"Any\")) :required t)))"
)
);
}
fn the_choice_descriptor_matches_the_shared_fixture() {
let fixture = include_str!("fixtures/descriptor-choice.sexp");
assert_eq!(descriptor_sexpr(&FORM), fixture.trim_end_matches('\n'));
fn only_the_report_transaction_and_script_forms_emit_a_choice() {
fn emits(fields: &[Field]) -> bool {
fields.iter().any(|field| match field.kind {
Kind::Choice { .. } => true,
Kind::Rows { fields, .. } => emits(fields),
_ => false,
})
let names: Vec<&str> = all()
.iter()
.filter(|form| emits(form.fields))
.map(|form| form.name)
.collect();
names,
[
"transaction-create",
"transaction-edit",
"script-create",
"script-edit",
"report-activity",
"report-category"
]
fn a_text_check_leaves_a_choice_value_alone() {
let mut values = forms::FormValues::default();
values.set("period", "month");
values.set("mode", "any");
assert!(values.check(&FORM).is_empty());
fn a_split_tag_side_is_a_choice_whose_blank_key_means_both_legs() {
let rows = forms::TRANSACTION_CREATE
.fields
.find(|field| field.key == "split-tags")
.map(|field| field.kind);
let Some(Kind::Rows { fields, .. }) = rows else {
panic!("split-tags is not rows");
let side = fields.iter().find(|field| field.key == "side");
let Some(Kind::Choice { options, default }) = side.map(|field| field.kind) else {
panic!("side is not a choice");
let keys: Vec<&str> = options.iter().map(|(key, _)| *key).collect();
assert_eq!(keys, ["", "from", "to"]);
assert_eq!(default, Some(""));
let sexpr = descriptor_sexpr(&forms::TRANSACTION_CREATE);
assert!(
sexpr.contains(":options ((\"\" \"Both accounts\")"),
"{sexpr}"