/
main.rs
74 lines (66 loc) · 1.72 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use std::io::{self, Write};
include!(concat!(env!("OUT_DIR"), "/templates.rs"));
use templates::*;
fn main() {
println!("### Page:");
page(&mut io::stdout()).unwrap();
for s in statics::STATICS {
println!("### /static/{} is {}:", s.name, s.mime);
io::stdout().write_all(s.content).unwrap();
}
}
#[test]
fn test_page_w_static() {
assert_eq!(
r2s(|o| page(o)),
"<html>\n \
<head>\n \
<title>Example with stylesheet</title>\n \
<link rel=\"stylesheet\" href=\"/static/style-o2rFo1lI.css\" \
type=\"text/css\"/>\n \
</head>\n \
<body>\n \
Hello world!\n \
</body>\n\
</html>\n"
);
}
#[test]
fn test_static_css_data() {
use std::str::from_utf8;
use templates::statics::style_css;
assert_eq!(
from_utf8(&style_css.content).unwrap(),
"body {\n background: white;\n color: black;\n}\n"
);
}
#[test]
fn test_get_static_by_name() {
use templates::statics::StaticFile;
assert_eq!(
StaticFile::get("style-o2rFo1lI.css").map(|s| s.name),
Some("style-o2rFo1lI.css")
)
}
#[test]
fn test_get_static_unknown() {
use templates::statics::StaticFile;
assert_eq!(StaticFile::get("foo-bar.css").map(|s| s.name), None)
}
#[test]
fn test_all_statics_known() {
use templates::statics::STATICS;
assert_eq!(
STATICS.iter().map(|s| s.name).collect::<Vec<_>>(),
["foo-JckCHvyv.css", "foo-R-7hhHLr.js", "style-o2rFo1lI.css"]
);
}
#[cfg(test)]
fn r2s<Call>(call: Call) -> String
where
Call: FnOnce(&mut Vec<u8>) -> io::Result<()>,
{
let mut buf = Vec::new();
call(&mut buf).unwrap();
String::from_utf8(buf).unwrap()
}