2015-09-26 13:56:52 +12:00
|
|
|
#![feature(plugin)]
|
2015-02-13 17:46:02 +13:00
|
|
|
#![plugin(maud_macros)]
|
2014-12-17 21:11:56 +13:00
|
|
|
|
2014-12-18 19:49:49 +13:00
|
|
|
extern crate maud;
|
2014-12-17 21:11:56 +13:00
|
|
|
|
2015-09-26 13:56:52 +12:00
|
|
|
use std::fmt;
|
|
|
|
|
2014-12-17 21:11:56 +13:00
|
|
|
#[test]
|
2015-01-14 13:48:48 +13:00
|
|
|
fn literals() {
|
2015-09-01 18:26:50 +12:00
|
|
|
let mut s = String::new();
|
2015-09-03 11:03:45 +12:00
|
|
|
html!(s, "du\tcks" -23 3.14 '\n' "geese").unwrap();
|
2015-01-07 17:54:43 +13:00
|
|
|
assert_eq!(s, "du\tcks-233.14\ngeese");
|
2014-12-17 21:11:56 +13:00
|
|
|
}
|
2014-12-19 11:53:40 +13:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn escaping() {
|
2015-09-01 18:26:50 +12:00
|
|
|
let mut s = String::new();
|
2015-09-03 11:03:45 +12:00
|
|
|
html!(s, "<flim&flam>").unwrap();
|
2015-01-07 17:54:43 +13:00
|
|
|
assert_eq!(s, "<flim&flam>");
|
2014-12-19 11:53:40 +13:00
|
|
|
}
|
2015-01-07 17:43:37 +13:00
|
|
|
|
2015-01-14 13:48:48 +13:00
|
|
|
#[test]
|
|
|
|
fn semicolons() {
|
2015-09-01 18:26:50 +12:00
|
|
|
let mut s = String::new();
|
2015-09-03 11:03:45 +12:00
|
|
|
html!(s, {
|
2015-01-14 13:48:48 +13:00
|
|
|
"one";
|
|
|
|
"two";
|
|
|
|
"three";
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
"four";
|
2015-09-01 18:26:50 +12:00
|
|
|
}).unwrap();
|
2015-01-14 13:48:48 +13:00
|
|
|
assert_eq!(s, "onetwothreefour");
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:43:37 +13:00
|
|
|
#[test]
|
|
|
|
fn blocks() {
|
2015-09-01 18:26:50 +12:00
|
|
|
let mut s = String::new();
|
2015-09-03 11:03:45 +12:00
|
|
|
html!(s, {
|
2015-01-07 17:43:37 +13:00
|
|
|
"hello"
|
|
|
|
{
|
2015-01-14 13:48:48 +13:00
|
|
|
" ducks" " geese"
|
2015-01-07 17:43:37 +13:00
|
|
|
}
|
|
|
|
" swans"
|
2015-09-01 18:26:50 +12:00
|
|
|
}).unwrap();
|
2015-01-07 17:54:43 +13:00
|
|
|
assert_eq!(s, "hello ducks geese swans");
|
2015-01-07 17:43:37 +13:00
|
|
|
}
|
|
|
|
|
2015-01-14 13:48:48 +13:00
|
|
|
mod elements {
|
|
|
|
#[test]
|
|
|
|
fn simple() {
|
2015-09-01 18:26:50 +12:00
|
|
|
let mut s = String::new();
|
2015-09-03 11:03:45 +12:00
|
|
|
html!(s, p { b { "pickle" } "barrel" i { "kumquat" } }).unwrap();
|
2015-01-14 13:48:48 +13:00
|
|
|
assert_eq!(s, "<p><b>pickle</b>barrel<i>kumquat</i></p>");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn nesting() {
|
2015-09-01 18:26:50 +12:00
|
|
|
let mut s = String::new();
|
2015-09-03 11:03:45 +12:00
|
|
|
html!(s, html body div p sup "butts").unwrap();
|
2015-01-14 13:48:48 +13:00
|
|
|
assert_eq!(s, "<html><body><div><p><sup>butts</sup></p></div></body></html>");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty() {
|
2015-09-01 18:26:50 +12:00
|
|
|
let mut s = String::new();
|
2015-09-03 11:03:45 +12:00
|
|
|
html!(s, "pinkie" br/ "pie").unwrap();
|
2015-01-14 13:48:48 +13:00
|
|
|
assert_eq!(s, "pinkie<br>pie");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn attributes() {
|
2015-09-01 18:26:50 +12:00
|
|
|
let mut s = String::new();
|
2015-09-03 11:03:45 +12:00
|
|
|
html!(s, {
|
2015-01-14 13:48:48 +13:00
|
|
|
link rel="stylesheet" href="styles.css"/
|
|
|
|
section id="midriff" {
|
|
|
|
p class="hotpink" "Hello!"
|
|
|
|
}
|
2015-09-01 18:26:50 +12:00
|
|
|
}).unwrap();
|
2015-01-14 13:48:48 +13:00
|
|
|
assert_eq!(s, concat!(
|
|
|
|
r#"<link rel="stylesheet" href="styles.css">"#,
|
|
|
|
r#"<section id="midriff"><p class="hotpink">Hello!</p></section>"#));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_attributes() {
|
2015-09-01 18:26:50 +12:00
|
|
|
let mut s = String::new();
|
2015-09-03 11:03:45 +12:00
|
|
|
html!(s, div readonly? input type="checkbox" checked? /).unwrap();
|
2015-01-14 13:48:48 +13:00
|
|
|
assert_eq!(s, r#"<div readonly><input type="checkbox" checked></div>"#);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod splices {
|
2015-01-07 17:43:37 +13:00
|
|
|
#[test]
|
2015-01-14 13:48:48 +13:00
|
|
|
fn literals() {
|
2015-09-01 18:26:50 +12:00
|
|
|
let mut s = String::new();
|
2015-09-03 11:03:45 +12:00
|
|
|
html!(s, $"<pinkie>").unwrap();
|
2015-01-07 17:54:43 +13:00
|
|
|
assert_eq!(s, "<pinkie>");
|
2015-01-07 17:43:37 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-01-14 13:48:48 +13:00
|
|
|
fn raw_literals() {
|
2015-09-01 18:26:50 +12:00
|
|
|
let mut s = String::new();
|
2015-09-03 11:03:45 +12:00
|
|
|
html!(s, $$"<pinkie>").unwrap();
|
2015-01-07 17:54:43 +13:00
|
|
|
assert_eq!(s, "<pinkie>");
|
2015-01-07 17:43:37 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-01-14 13:48:48 +13:00
|
|
|
fn blocks() {
|
2015-09-01 18:26:50 +12:00
|
|
|
let mut s = String::new();
|
2015-09-03 11:03:45 +12:00
|
|
|
html!(s, {
|
2015-01-07 17:43:37 +13:00
|
|
|
${
|
|
|
|
let mut result = 1i32;
|
2015-02-06 14:50:47 +13:00
|
|
|
for i in 2..11 {
|
2015-01-07 17:43:37 +13:00
|
|
|
result *= i;
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
2015-09-01 18:26:50 +12:00
|
|
|
}).unwrap();
|
2015-01-07 17:54:43 +13:00
|
|
|
assert_eq!(s, "3628800");
|
2015-01-07 17:43:37 +13:00
|
|
|
}
|
|
|
|
|
2015-01-29 13:47:11 +13:00
|
|
|
#[test]
|
|
|
|
fn attributes() {
|
|
|
|
let rocks = true;
|
2015-09-01 18:26:50 +12:00
|
|
|
let mut s = String::new();
|
2015-09-03 11:03:45 +12:00
|
|
|
html!(s, {
|
2015-01-29 13:47:11 +13:00
|
|
|
input checked?=true /
|
|
|
|
input checked?=false /
|
|
|
|
input checked?=rocks /
|
|
|
|
input checked?=(!rocks) /
|
2015-09-01 18:26:50 +12:00
|
|
|
}).unwrap();
|
2015-01-29 13:47:11 +13:00
|
|
|
assert_eq!(s, concat!(
|
|
|
|
r#"<input checked>"#,
|
|
|
|
r#"<input>"#,
|
|
|
|
r#"<input checked>"#,
|
|
|
|
r#"<input>"#));
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:43:37 +13:00
|
|
|
static BEST_PONY: &'static str = "Pinkie Pie";
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn statics() {
|
2015-09-01 18:26:50 +12:00
|
|
|
let mut s = String::new();
|
2015-09-03 11:03:45 +12:00
|
|
|
html!(s, $BEST_PONY).unwrap();
|
2015-01-07 17:54:43 +13:00
|
|
|
assert_eq!(s, "Pinkie Pie");
|
2015-01-07 17:43:37 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-01-14 13:48:48 +13:00
|
|
|
fn closures() {
|
2015-01-07 17:43:37 +13:00
|
|
|
let best_pony = "Pinkie Pie";
|
2015-09-01 18:26:50 +12:00
|
|
|
let mut s = String::new();
|
2015-09-03 11:03:45 +12:00
|
|
|
html!(s, $best_pony).unwrap();
|
2015-01-07 17:54:43 +13:00
|
|
|
assert_eq!(s, "Pinkie Pie");
|
2015-01-07 17:43:37 +13:00
|
|
|
}
|
2015-01-17 20:43:51 +13:00
|
|
|
|
2015-01-18 20:18:21 +13:00
|
|
|
/// 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
|
2015-03-01 20:02:34 -05:00
|
|
|
adorableness: u32,
|
2015-01-18 20:18:21 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Creature {
|
2015-03-01 20:02:34 -05:00
|
|
|
fn repugnance(&self) -> u32 {
|
2015-01-18 20:18:21 +13:00
|
|
|
10 - self.adorableness
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn structs() {
|
|
|
|
let pinkie = Creature {
|
|
|
|
name: "Pinkie Pie",
|
|
|
|
adorableness: 9,
|
|
|
|
};
|
2015-09-01 18:26:50 +12:00
|
|
|
let mut s = String::new();
|
2015-09-03 11:03:45 +12:00
|
|
|
html!(s, {
|
2015-01-18 20:18:21 +13:00
|
|
|
"Name: " $pinkie.name ". Rating: " $pinkie.repugnance()
|
2015-09-01 18:26:50 +12:00
|
|
|
}).unwrap();
|
2015-01-18 20:18:21 +13:00
|
|
|
assert_eq!(s, "Name: Pinkie Pie. Rating: 1");
|
|
|
|
}
|
|
|
|
|
2015-01-17 20:43:51 +13:00
|
|
|
#[test]
|
|
|
|
fn nested_macro_invocation() {
|
|
|
|
let best_pony = "Pinkie Pie";
|
2015-09-01 18:26:50 +12:00
|
|
|
let mut s = String::new();
|
2015-09-03 11:03:45 +12:00
|
|
|
html!(s, $(format!("{}", best_pony))).unwrap();
|
2015-01-17 20:43:51 +13:00
|
|
|
assert_eq!(s, "Pinkie Pie");
|
|
|
|
}
|
2015-01-07 17:43:37 +13:00
|
|
|
}
|
2015-01-26 12:31:11 +13:00
|
|
|
|
2015-04-30 16:33:24 +12:00
|
|
|
#[test]
|
|
|
|
fn issue_13() {
|
|
|
|
let owned = String::from("yay");
|
2015-09-01 18:26:50 +12:00
|
|
|
let mut s = String::new();
|
2015-09-03 11:03:45 +12:00
|
|
|
html!(s, $owned).unwrap();
|
2015-09-01 18:26:50 +12:00
|
|
|
let _ = owned;
|
2015-04-30 16:33:24 +12:00
|
|
|
}
|
|
|
|
|
2015-02-27 09:27:45 +13:00
|
|
|
mod control {
|
|
|
|
#[test]
|
|
|
|
fn if_expr() {
|
|
|
|
for (number, &name) in (1..4).zip(["one", "two", "three"].iter()) {
|
2015-09-01 18:26:50 +12:00
|
|
|
let mut s = String::new();
|
2015-09-03 11:03:45 +12:00
|
|
|
html!(s, {
|
2015-09-12 13:53:14 +12:00
|
|
|
#if number == 1 {
|
2015-02-27 09:27:45 +13:00
|
|
|
"one"
|
2015-09-12 13:53:14 +12:00
|
|
|
} #else if number == 2 {
|
2015-02-27 09:27:45 +13:00
|
|
|
"two"
|
2015-09-12 13:53:14 +12:00
|
|
|
} #else if number == 3 {
|
2015-02-27 09:27:45 +13:00
|
|
|
"three"
|
2015-09-12 13:53:14 +12:00
|
|
|
} #else {
|
2015-02-27 09:27:45 +13:00
|
|
|
"oh noes"
|
|
|
|
}
|
2015-09-01 18:26:50 +12:00
|
|
|
}).unwrap();
|
2015-02-27 09:27:45 +13:00
|
|
|
assert_eq!(s, name);
|
|
|
|
}
|
|
|
|
}
|
2015-03-14 21:08:08 +13:00
|
|
|
|
2015-03-15 16:23:19 +13:00
|
|
|
#[test]
|
|
|
|
fn if_let() {
|
|
|
|
for &(input, output) in [(Some("yay"), "yay"), (None, "oh noes")].iter() {
|
2015-09-01 18:26:50 +12:00
|
|
|
let mut s = String::new();
|
2015-09-03 11:03:45 +12:00
|
|
|
html!(s, {
|
2015-09-12 13:53:14 +12:00
|
|
|
#if let Some(value) = input {
|
2015-03-15 16:23:19 +13:00
|
|
|
$value
|
2015-09-12 13:53:14 +12:00
|
|
|
} #else {
|
2015-03-15 16:23:19 +13:00
|
|
|
"oh noes"
|
|
|
|
}
|
2015-09-01 18:26:50 +12:00
|
|
|
}).unwrap();
|
2015-03-15 16:23:19 +13:00
|
|
|
assert_eq!(s, output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 21:08:08 +13:00
|
|
|
#[test]
|
|
|
|
fn for_expr() {
|
|
|
|
let ponies = ["Apple Bloom", "Scootaloo", "Sweetie Belle"];
|
2015-09-01 18:26:50 +12:00
|
|
|
let mut s = String::new();
|
2015-09-03 11:03:45 +12:00
|
|
|
html!(s, {
|
2015-09-12 13:53:14 +12:00
|
|
|
ul #for pony in &ponies {
|
2015-03-14 21:08:08 +13:00
|
|
|
li $pony
|
|
|
|
}
|
2015-09-01 18:26:50 +12:00
|
|
|
}).unwrap();
|
2015-03-14 21:08:08 +13:00
|
|
|
assert_eq!(s, concat!(
|
|
|
|
"<ul>",
|
|
|
|
"<li>Apple Bloom</li>",
|
|
|
|
"<li>Scootaloo</li>",
|
|
|
|
"<li>Sweetie Belle</li>",
|
|
|
|
"</ul>"));
|
|
|
|
}
|
2015-02-27 09:27:45 +13:00
|
|
|
}
|
2015-09-06 12:59:41 +12:00
|
|
|
|
|
|
|
#[test]
|
2015-09-12 14:21:01 +12:00
|
|
|
fn html_utf8() {
|
2015-09-07 19:43:48 +12:00
|
|
|
let mut buf = vec![];
|
2015-09-12 14:21:01 +12:00
|
|
|
html_utf8!(buf, p "hello").unwrap();
|
2015-09-06 12:59:41 +12:00
|
|
|
assert_eq!(buf, b"<p>hello</p>");
|
|
|
|
}
|
2015-09-15 17:47:32 +12:00
|
|
|
|
|
|
|
mod issue_10 {
|
|
|
|
#[test]
|
|
|
|
fn hyphens_in_element_names() {
|
|
|
|
let mut s = String::new();
|
|
|
|
html!(s, custom-element {}).unwrap();
|
|
|
|
assert_eq!(s, "<custom-element></custom-element>");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn hyphens_in_attribute_names() {
|
|
|
|
let mut s = String::new();
|
|
|
|
html!(s, this sentence-is="false" of-course? {}).unwrap();
|
|
|
|
assert_eq!(s, r#"<this sentence-is="false" of-course></this>"#);
|
|
|
|
}
|
|
|
|
}
|
2015-09-23 14:29:45 +12:00
|
|
|
|
2015-09-26 13:56:52 +12:00
|
|
|
#[test]
|
|
|
|
fn call() {
|
2015-09-23 14:29:45 +12:00
|
|
|
fn ducks(w: &mut fmt::Write) -> fmt::Result {
|
|
|
|
write!(w, "Ducks")
|
|
|
|
}
|
2015-09-26 13:56:52 +12:00
|
|
|
let mut s = String::new();
|
|
|
|
let swans = |yes|
|
|
|
|
if yes {
|
|
|
|
|w: &mut fmt::Write| write!(w, "Swans")
|
|
|
|
} else {
|
|
|
|
panic!("oh noes")
|
|
|
|
};
|
|
|
|
html!(s, {
|
|
|
|
#call ducks
|
|
|
|
#call (|w: &mut fmt::Write| write!(w, "Geese"))
|
|
|
|
#call swans(true)
|
|
|
|
}).unwrap();
|
|
|
|
assert_eq!(s, "DucksGeeseSwans");
|
2015-09-23 14:29:45 +12:00
|
|
|
}
|