Lines
100 %
Functions
Branches
use forms::{Action, Field, Form, Kind};
use super::tests::{all_controls, error};
use super::*;
const FIELDS: &[Field] = &[
Field {
key: "period",
label: "Period",
kind: Kind::Choice {
options: &[("month", "Month"), ("year", "Year \"all\" <b>")],
default: Some("month"),
},
required: false,
key: "mode",
label: "Mode",
options: &[("all", "All"), ("any", "Any")],
default: None,
key: "scope",
label: "Scope",
options: &[("own", "Own")],
required: true,
];
static FORM: Form = Form {
name: "choice-test",
title: "Choice test",
fields: FIELDS,
action: Action::Query,
};
fn rendered(values: &FormValues, errors: &[FieldError]) -> askama::Result<String> {
render_form(&FORM, values, errors, "/x", &References::default())
}
fn squeezed(html: &str) -> String {
html.split_whitespace().collect::<Vec<_>>().join(" ")
fn value_of(html: &str, name: &str) -> Option<String> {
all_controls(html)
.iter()
.find(|c| c.attr("name") == Some(name))
.map(super::tests::Control::value)
#[test]
fn a_choice_renders_the_golden_select() -> askama::Result<()> {
let html = squeezed(&rendered(&FormValues::default(), &[])?);
assert!(
html.contains(concat!(
"<select name=\"period\"> ",
"<option value=\"month\" selected > Month </option> ",
"<option value=\"year\" > Year "all" <b> </option> ",
"</select ></label"
)),
"{html}"
);
Ok(())
fn a_label_with_markup_is_escaped() -> askama::Result<()> {
let html = rendered(&FormValues::default(), &[])?;
!html.contains("<b>") && !html.contains("\"all\" "),
fn the_loaded_key_is_selected_over_the_default() -> askama::Result<()> {
let mut values = FormValues::default();
values.set("period", "year");
assert_eq!(
value_of(&rendered(&values, &[])?, "period").as_deref(),
Some("year")
fn an_optional_choice_without_a_default_gets_an_empty_first_option() -> askama::Result<()> {
html.contains("<select name=\"mode\"> <option value=\"\" selected > </option>"),
fn a_required_choice_has_no_empty_option() -> askama::Result<()> {
html.contains("<select name=\"scope\"> <option value=\"own\""),
fn a_rejected_render_keeps_the_posted_key() -> askama::Result<()> {
values.set("mode", "any");
let html = rendered(&values, &[error("mode", "bad")])?;
assert_eq!(value_of(&html, "period").as_deref(), Some("year"));
assert_eq!(value_of(&html, "mode").as_deref(), Some("any"));