-
Notifications
You must be signed in to change notification settings - Fork 33
/
main.rs
348 lines (312 loc) · 7.83 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#![allow(dead_code)] // Most templates here are only used in tests.
use std::io;
include!(concat!(env!("OUT_DIR"), "/templates.rs"));
use crate::templates::*;
fn main() {
page::page_html(&mut io::stdout(), "sample page").unwrap();
}
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()
}
#[test]
fn test_hello() {
assert_eq!(
r2s(|o| hello_html(o)),
"<h1>Hello World!</h1>\
\n<p>Note: Brackets and @ signs needs to be escaped: { ... }</p>\n"
);
}
#[test]
fn test_hello_args() {
assert_eq!(
r2s(|o| hello_args_html(o, "World")),
"<h1>Hello World!</h1>\n",
);
}
#[test]
fn test_hello_encodes_args() {
assert_eq!(
r2s(|o| hello_args_html(o, "encoded < & >")),
"<h1>Hello encoded < & >!</h1>\n"
);
}
#[test]
fn test_hello_args_two() {
assert_eq!(
r2s(|o| hello_args_two_html(o, 56, "prime", false)),
"<p class=\"foo\" data-n=\"56\">Is 56 a prime? false!</p>\n"
);
}
#[test]
fn test_hello_args_three() {
assert_eq!(
r2s(|o| hello_args_three_html(o, 56, &56, &56)),
"<p>56 56 56</p>\n"
);
}
#[test]
fn test_if_let_some() {
assert_eq!(
r2s(|o| if_let_html(o, Some("thing"))),
"<p> The item is thing </p>\n"
)
}
#[test]
fn test_if_let_none() {
assert_eq!(r2s(|o| if_let_html(o, None)), "<p> Got nothing </p>\n")
}
#[test]
fn test_if_let_destructure() {
assert_eq!(
r2s(|o| if_let_destructure_html(o, &Some((47, 11)))),
"<p> We have 47 and 11 </p>\n"
)
}
#[test]
fn test_if_else_if() {
let f = |n| r2s(|o| if_else_if_html(o, n));
assert_eq!(f(0), "There are none\n");
assert_eq!(f(1), "There is one\n");
assert_eq!(f(7), "There are 7\n");
}
#[test]
fn test_list() {
assert_eq!(
r2s(|o| list_html(o, &["foo", "bar"])),
"\n<ul>\n \n <li>foo</li>\n \n <li>bar</li>\n \n</ul>\n\n"
);
}
#[test]
fn test_list_empty() {
assert_eq!(r2s(|o| list_html(o, &[])), "\n<p>No items</p>\n\n");
}
#[test]
fn test_list_destructure() {
assert_eq!(
r2s(|o| list_destructure_html(o, &["foo", "bar"])),
"<ul>\n \n <li>0: foo</li>\n \n \
<li>1: bar</li>\n \n</ul>\n"
);
}
#[test]
fn test_list_destructure_2() {
assert_eq!(
r2s(|o| list_destructure_2_html(o)),
"\n <p>Rasmus is 44 years old.</p>\n\n \
<p>Mike is 36 years old.</p>\n\n"
);
}
#[test]
fn test_uselist() {
assert_eq!(
r2s(|o| uselist_html(o)),
"<h1>Two items</h1>\n\n\
<ul>\n \n <li>foo</li>\n \
\n <li>bar</li>\n \n</ul>\n\n\n\
<h2>No items</h2>\n\n\
<p>No items</p>\n\n\n"
);
}
#[test]
fn test_hello_utf8() {
assert_eq!(
r2s(|o| hello_utf8_html(o, "δ", "ε", "δ < ε", "δ < ε")),
"<p>δ < ε</p>\n\
<p>δ < ε</p>\n\
<p>δ < ε</p>\n\
<p>δ < ε</p>\n"
);
}
#[test]
fn test_comments() {
assert_eq!(
r2s(|o| comments_html(o)),
"<!-- this is a real HTML comment, which gets send to the client -->\n\
<p>This is visible</p>\n\n"
);
}
mod models {
use crate::templates::Html;
use std::fmt;
pub struct User<'a> {
pub name: &'a str,
pub email: &'a str,
}
impl<'a> User<'a> {
pub fn mailto(&self) -> Html<String> {
Html(format!("<a href=\"mailto:{0}\">{0}</a>", self.email))
}
}
impl<'a> fmt::Display for User<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.name)
}
}
}
#[test]
fn test_hello_fields() {
let user = models::User {
name: "Tom Puss",
email: "tom@example.nl",
};
assert_eq!(
r2s(|o| hello_fields_html(o, &user)),
"<h1>Hello Tom Puss!</h1>\n<p>Your email is \
tom@example.nl</p>\n"
);
}
#[test]
fn test_hello_method() {
let user = models::User {
name: "Tom Puss",
email: "tom@example.nl",
};
assert_eq!(
r2s(|o| hello_method_html(o, &user)),
"<h1>Hello Tom Puss!</h1>\n<p>Your email is \
<a href=\"mailto:tom@example.nl\">tom@example.nl</a></p>\n"
);
}
#[test]
fn test_hello_code() {
use templates::Html;
assert_eq!(
r2s(|o| hello_code_html(o, &"Paragraph:", &Html("<p>Hello.</p>"))),
"<h2>Paragraph:</h2>\n<p>Hello.</p>\n"
);
}
#[test]
fn test_for_loop() {
assert_eq!(
r2s(|o| for_loop_html(o, &vec!["Hello", "World"])),
"<h1>Looped paragraphs</h1>\n\n \
<p>Hello</p>\n\n <p>World</p>\n\n"
);
}
#[test]
fn test_for_destructure() {
let users = vec![
models::User {
name: "Tom Puss",
email: "tom@example.nl",
},
models::User {
name: "Heloise Walker",
email: "helwa@briarson.edu",
},
];
assert_eq!(
r2s(|o| for_destructure_html(o, &users)),
"<ul><li>Tom Puss</li><li>Heloise Walker</li></ul>\n",
)
}
#[test]
fn test_explicit_formatting() {
assert_eq!(
r2s(|o| explicit_formatting_html(o, 5.212432234, "one\ntwo")),
"<p>Value 1 is 5.2 (or really 5.212432234),\n\
while value 2 is "one\\ntwo".</p>\n"
);
}
#[test]
fn test_hello_use_templates() {
assert_eq!(
r2s(|o| hello_use_templates_html(o, &Html("<p>this is foo</p>"))),
"<h1>Hello World!</h1>\
\n<p>Note: Brackets and @ signs needs to be escaped: { ... }</p>\n\
\n<h2>foo</h2>\n<p>this is foo</p>\n\n"
);
}
#[test]
fn test_page_with_base() {
assert_eq!(
r2s(|o| page::page_html(o, "World")),
"<!doctype html>\
\n<html>\
\n <head><title>Hello World!</title>\
\n <meta property=\"og:description\" content=\"A simple example\"/>\
\n</head>\
\n <body>\
\n <h1>Hello World!</h1>\
\n \
\n <p>This is page content for World</p>\
\n\
\n </body>\
\n</html>\n\n"
);
}
#[test]
fn test_page_two() {
assert_eq!(
r2s(|o| page::page_two_html(o, 2019)),
"<!doctype html>\
\n<html lang=\"sv\">\
\n <head>\
\n <title>Year 2019 - Example page</title>\
\n </head>\
\n <body>\
\n <header>\
\n <h1>Year 2019</h1>\
\n \n <p>Welcome to this page about the year 2019.</p>\n\
\n </header>\n\
\n <main>\
\n <p>This is the main page content.</p>\
\n</main>\n\
\n <footer>\
\n <p>A simple example page.</p>\
\n </footer>\
\n </body>\
\n</html>\n\n",
)
}
#[test]
fn test_some_expressions() {
assert_eq!(
r2s(|o| some_expressions_html(o, "name")),
"<p>name</p>\
\n<p>name.name</p>\
\n<p>4</p>\
\n<p>name.len()</p>\
\n<p>-1</p>\
\n<p>1</p>\n"
)
}
#[test]
fn test_issue_66() {
assert_eq!(r2s(|o| issue_66_html(o)), "ABC\n");
}
#[test]
fn test_issue_68() {
assert_eq!(
r2s(|o| issue_68_html(o)),
"Hello!\n\nThe 0 number.\n\nThe 1 number.\n\nThe 2 number.\n\nGood bye!\n",
);
}
/// [Issue #106](https://github.com/kaj/ructe/issues/106)
#[test]
fn lifetimes() {
assert_eq!(
r2s(|o| with_lifetime_html(o, &["foo", "bar"])),
"\n <p>foo</p>\n\n <p>bar</p>\n\n",
);
}
/// [Issue #106](https://github.com/kaj/ructe/issues/106)
#[test]
fn lifetimes2() {
assert_eq!(
r2s(|o| with_lifetime2_html(o, &["foo", "bar"])),
"\n <p>foo</p>\n\n <p>bar</p>\n\n",
);
}
#[test]
fn test_list_join() {
assert_eq!(
r2s(|o| list_joins_html(o, &[2, 3, 7])),
"<p>Items: 2, 3, 7.</p>\n",
)
}