maud/maud_macros/tests/splices.rs
Chris Wong f12efe4299 Rewrite everything to use Strings instead of writers
After fiddling around with zero-allocation solutions, I concluded that
all non-allocating approaches are too annoying to work with in realistic
code. Using closures leads to yak-shaving with lifetimes; and because
Iron needs to take ownership of the response body we often end up
cloning the input data anyway.

Removing this constraint has let me simplify the entire system, removing
a net 300 lines from the library. The `html!` macro no longer takes a
writer, and instead returns a `PreEscaped<String>`. This means that the
result of an `html!` can be spliced directly into another `html!`,
removing the need for the `impl Template` rigmarole.

To rub it in, benchmarks show the new code is in fact *faster* than it
was before. How lovely.
2016-09-23 19:22:22 +12:00

121 lines
2.5 KiB
Rust

#![feature(plugin)]
#![plugin(maud_macros)]
extern crate maud;
#[test]
fn literals() {
let s = html!(("<pinkie>")).into_string();
assert_eq!(s, "&lt;pinkie&gt;");
}
#[test]
fn raw_literals() {
use maud::PreEscaped;
let s = html!((PreEscaped("<pinkie>"))).into_string();
assert_eq!(s, "<pinkie>");
}
#[test]
fn blocks() {
let s = html!({
({
let mut result = 1i32;
for i in 2..11 {
result *= i;
}
result
})
}).into_string();
assert_eq!(s, "3628800");
}
#[test]
fn attributes() {
let alt = "Pinkie Pie";
let s = html!(img src="pinkie.jpg" alt=(alt) /).into_string();
assert_eq!(s, r#"<img src="pinkie.jpg" alt="Pinkie Pie">"#);
}
#[test]
fn empty_attributes() {
let rocks = true;
let s = html!({
input checked?(true) /
input checked?(false) /
input checked?(rocks) /
input checked?(!rocks) /
}).into_string();
assert_eq!(s, concat!(
r#"<input checked>"#,
r#"<input>"#,
r#"<input checked>"#,
r#"<input>"#));
}
static BEST_PONY: &'static str = "Pinkie Pie";
#[test]
fn statics() {
let s = html!((BEST_PONY)).into_string();
assert_eq!(s, "Pinkie Pie");
}
#[test]
fn locals() {
let best_pony = "Pinkie Pie";
let s = html!((best_pony)).into_string();
assert_eq!(s, "Pinkie Pie");
}
/// An example struct, for testing purposes only
struct Creature {
name: &'static str,
/// Rating out of 10, where:
/// * 0 is a naked mole rat with dysentery
/// * 10 is Sweetie Belle in a milkshake
adorableness: u32,
}
impl Creature {
fn repugnance(&self) -> u32 {
10 - self.adorableness
}
}
#[test]
fn structs() {
let pinkie = Creature {
name: "Pinkie Pie",
adorableness: 9,
};
let s = html!({
"Name: " (pinkie.name) ". Rating: " (pinkie.repugnance())
}).into_string();
assert_eq!(s, "Name: Pinkie Pie. Rating: 1");
}
#[test]
fn tuple_accessors() {
let a = ("ducks", "geese");
let s = html!((a.0)).into_string();
assert_eq!(s, "ducks");
}
#[test]
fn splice_with_path() {
mod inner {
pub fn name() -> &'static str {
"Maud"
}
}
let s = html!((inner::name())).into_string();
assert_eq!(s, "Maud");
}
#[test]
fn nested_macro_invocation() {
let best_pony = "Pinkie Pie";
let s = html!((format!("{}", best_pony))).into_string();
assert_eq!(s, "Pinkie Pie");
}