|
@@ -7,7 +7,7 @@ use std::path;
|
7
|
7
|
|
8
|
8
|
use config::Config;
|
9
|
9
|
|
10
|
|
-#[derive(Debug)]
|
|
10
|
+#[derive(Debug, PartialEq)]
|
11
|
11
|
pub enum EntryKind {
|
12
|
12
|
Page,
|
13
|
13
|
Post,
|
|
@@ -45,9 +45,7 @@ pub fn read_entry_dir(cwd: &path::PathBuf) -> Vec<fs::DirEntry> {
|
45
|
45
|
}
|
46
|
46
|
}
|
47
|
47
|
|
48
|
|
-pub fn parse_entry(path: path::PathBuf) -> Entry {
|
49
|
|
- let contents = fs::read_to_string(&path).expect("Couldn't read post file");
|
50
|
|
-
|
|
48
|
+pub fn parse_entry(contents: &str, path: path::PathBuf) -> Entry {
|
51
|
49
|
lazy_static! {
|
52
|
50
|
static ref re_with_date: Regex =
|
53
|
51
|
Regex::new(r"^# (?P<title>.*) \| (?P<date>\d{4}-\d{2}-\d{2})\n\n(?s)(?P<body>.*)")
|
|
@@ -171,12 +169,13 @@ pub fn render_post_listing(
|
171
|
169
|
|
172
|
170
|
#[cfg(test)]
|
173
|
171
|
mod tests {
|
174
|
|
-
|
175
|
|
- use super::{render_post_listing, write_entry, write_entry_listing, Config, Entry, EntryKind};
|
176
|
172
|
use chrono::NaiveDate;
|
177
|
|
- use std::{env, fs};
|
178
|
173
|
use uuid::Uuid;
|
179
|
174
|
|
|
175
|
+ use std::{env, fs, path};
|
|
176
|
+
|
|
177
|
+ use super::*;
|
|
178
|
+
|
180
|
179
|
#[test]
|
181
|
180
|
fn test_render_post() {
|
182
|
181
|
let config = Config {
|
|
@@ -351,4 +350,32 @@ mod tests {
|
351
|
350
|
|
352
|
351
|
fs::remove_dir_all(temp_dir.join(&working_dir)).unwrap();
|
353
|
352
|
}
|
|
353
|
+
|
|
354
|
+ #[test]
|
|
355
|
+ fn test_parse_post_entry() {
|
|
356
|
+ let post = parse_entry(
|
|
357
|
+ "# Test Title | 2000-01-01\n\nThis is the body",
|
|
358
|
+ path::PathBuf::from("posts/one.md"),
|
|
359
|
+ );
|
|
360
|
+
|
|
361
|
+ assert_eq!(post.kind, EntryKind::Post);
|
|
362
|
+ assert_eq!(post.slug, "one");
|
|
363
|
+ assert_eq!(post.title, "Test Title");
|
|
364
|
+ assert_eq!(post.body, "This is the body");
|
|
365
|
+ assert_eq!(post.date, Some(NaiveDate::from_ymd(2000, 1, 1)));
|
|
366
|
+ }
|
|
367
|
+
|
|
368
|
+ #[test]
|
|
369
|
+ fn test_parse_page_entry() {
|
|
370
|
+ let post = parse_entry(
|
|
371
|
+ "# Test Title\n\nThis is the body",
|
|
372
|
+ path::PathBuf::from("pages/one.md"),
|
|
373
|
+ );
|
|
374
|
+
|
|
375
|
+ assert_eq!(post.kind, EntryKind::Page);
|
|
376
|
+ assert_eq!(post.slug, "one");
|
|
377
|
+ assert_eq!(post.title, "Test Title");
|
|
378
|
+ assert_eq!(post.body, "This is the body");
|
|
379
|
+ assert_eq!(post.date, None);
|
|
380
|
+ }
|
354
|
381
|
}
|