|
@@ -29,3 +29,96 @@ pub fn write_post_listing(
|
29
|
29
|
render_post_listing(layout, post_listing_template, post_item_template, posts),
|
30
|
30
|
).expect("Unable to write file");
|
31
|
31
|
}
|
|
32
|
+
|
|
33
|
+mod tests {
|
|
34
|
+ #[allow(unused_imports)]
|
|
35
|
+ use super::*;
|
|
36
|
+ #[allow(unused_imports)]
|
|
37
|
+ use std::{env, fs};
|
|
38
|
+ #[allow(unused_imports)]
|
|
39
|
+ use uuid::Uuid;
|
|
40
|
+
|
|
41
|
+ #[test]
|
|
42
|
+ fn test_write_post() {
|
|
43
|
+ let temp_dir = env::temp_dir();
|
|
44
|
+ let working_dir = temp_dir.join(&Uuid::new_v4().to_string());
|
|
45
|
+ fs::create_dir(&working_dir).unwrap();
|
|
46
|
+ env::set_current_dir(&working_dir).unwrap();
|
|
47
|
+
|
|
48
|
+ let cwd = env::current_dir().unwrap();
|
|
49
|
+ fs::create_dir(cwd.join("public")).unwrap();
|
|
50
|
+
|
|
51
|
+ let layout = "<html><head><title>Test</title></head><body>{{ contents }}</body></html>";
|
|
52
|
+ let post_template = "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>";
|
|
53
|
+ let post = Post {
|
|
54
|
+ title: String::from("Hello world"),
|
|
55
|
+ body: String::from("Lorem ipsum dolor sit amet"),
|
|
56
|
+ slug: String::from("hello-world"),
|
|
57
|
+ };
|
|
58
|
+
|
|
59
|
+ write_post(&cwd, &layout, &post_template, &post);
|
|
60
|
+
|
|
61
|
+ let content =
|
|
62
|
+ fs::read_to_string(cwd.join("public").join("hello-world").join("index.html")).unwrap();
|
|
63
|
+
|
|
64
|
+ assert_eq!(
|
|
65
|
+ "<html><head><title>Test</title></head><body><article><h1>Hello wor\
|
|
66
|
+ ld</h1><div><p>Lorem ipsum dolor sit amet</p></div></article></body\
|
|
67
|
+ ></html>",
|
|
68
|
+ content.replace("\n", "")
|
|
69
|
+ );
|
|
70
|
+
|
|
71
|
+ fs::remove_dir_all(temp_dir.join(&working_dir)).unwrap();
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ #[test]
|
|
75
|
+ fn test_write_post_listing() {
|
|
76
|
+ let temp_dir = env::temp_dir();
|
|
77
|
+ let working_dir = temp_dir.join(&Uuid::new_v4().to_string());
|
|
78
|
+ fs::create_dir(&working_dir).unwrap();
|
|
79
|
+ env::set_current_dir(&working_dir).unwrap();
|
|
80
|
+
|
|
81
|
+ let cwd = env::current_dir().unwrap();
|
|
82
|
+ fs::create_dir(cwd.join("public")).unwrap();
|
|
83
|
+
|
|
84
|
+ let layout = "<html><head><title>Test</title></head><body>{{ contents }}</body></html>";
|
|
85
|
+ let post_listing_template = "<ul>{{ post_listing }}</ul>";
|
|
86
|
+ let post_item_template = "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>";
|
|
87
|
+ let posts = vec![
|
|
88
|
+ Post {
|
|
89
|
+ title: String::from("First post"),
|
|
90
|
+ body: String::from("lorem ipsum dolor sit amet"),
|
|
91
|
+ slug: String::from("first-post"),
|
|
92
|
+ },
|
|
93
|
+ Post {
|
|
94
|
+ title: String::from("Second post"),
|
|
95
|
+ body: String::from("lorem ipsum dolor sit amet"),
|
|
96
|
+ slug: String::from("second-post"),
|
|
97
|
+ },
|
|
98
|
+ Post {
|
|
99
|
+ title: String::from("Third post"),
|
|
100
|
+ body: String::from("lorem ipsum dolor sit amet"),
|
|
101
|
+ slug: String::from("third-post"),
|
|
102
|
+ },
|
|
103
|
+ ];
|
|
104
|
+ write_post_listing(
|
|
105
|
+ &cwd,
|
|
106
|
+ &layout,
|
|
107
|
+ &post_listing_template,
|
|
108
|
+ &post_item_template,
|
|
109
|
+ &posts,
|
|
110
|
+ );
|
|
111
|
+
|
|
112
|
+ assert_eq!(
|
|
113
|
+ "<html><head><title>Test</title></head><body><ul><li><a href=\"/fir\
|
|
114
|
+ st-post\">First post</a></li><li><a href=\"/second-post\">Second po\
|
|
115
|
+ st</a></li><li><a href=\"/third-post\">Third post</a></li></ul></bo\
|
|
116
|
+ dy></html>",
|
|
117
|
+ fs::read_to_string(&cwd.join("public").join("index.html"))
|
|
118
|
+ .unwrap()
|
|
119
|
+ .replace("\n", ""),
|
|
120
|
+ );
|
|
121
|
+
|
|
122
|
+ fs::remove_dir_all(temp_dir.join(&working_dir)).unwrap();
|
|
123
|
+ }
|
|
124
|
+}
|