From 2cb85a4f5922be57a43470bde828d8f92138de40 Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Thu, 28 May 2020 18:18:32 -0400 Subject: testing create/delete --- src/ed.rs | 8 +++++++- src/posts.rs | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/ed.rs b/src/ed.rs index b87b4a1..468e008 100644 --- a/src/ed.rs +++ b/src/ed.rs @@ -1,10 +1,15 @@ -use std::env; use std::fs; + +#[cfg(not(test))] +use std::env; +#[cfg(not(test))] use std::process; use chrono::prelude::*; +#[cfg(not(test))] use crate::error; + use crate::user; fn create_tmp_file() -> Result { @@ -16,6 +21,7 @@ fn create_tmp_file() -> Result { } } +#[cfg(not(test))] pub fn call(body: &str) -> String { // If they don't have $EDITOR set, just default to nano // instead of assuming vim or emacs. diff --git a/src/posts.rs b/src/posts.rs index c38c84f..4553b3e 100644 --- a/src/posts.rs +++ b/src/posts.rs @@ -1,10 +1,12 @@ use std::io; use crate::db; -use crate::ed; use crate::error; use crate::user; +#[cfg(not(test))] +use crate::ed; + // Make sure nobody encodes narsty characters // into a message to negatively affect other // users @@ -22,7 +24,13 @@ pub fn create() -> error::Result<()> { println!(); println!("Title of the new post: "); + #[cfg(test)] + let title = String::from("TEST_TITLE"); + + #[cfg(not(test))] let mut title = String::new(); + + #[cfg(not(test))] io::stdin().read_line(&mut title)?; let title = str_to_utf8(title.trim()); @@ -34,14 +42,20 @@ pub fn create() -> error::Result<()> { println!(); + #[cfg(not(test))] let body_raw = str_to_utf8(&ed::call("")); + + #[cfg(not(test))] let body = if body_raw.len() > 500 { &body_raw[..500] } else { &body_raw }; - let trimmed_body = body.trim(); + #[cfg(test)] + let body = String::from("TEST_BODY"); + + let trimmed_body = body.trim(); let user = &*user::NAME; let mut all = db::Posts::get_all(db::PATH); @@ -216,4 +230,19 @@ mod tests { assert_eq!(post.body, "TEST_BODY"); fs::rename("clinte_bak.json", db::PATH).unwrap(); } + + #[test] + fn test_create_delete() { + fs::copy(db::PATH, "clinte_bak.json").unwrap(); + create().unwrap(); + let all = db::Posts::get_all(db::PATH); + let post = all.get(1); + + assert_eq!(post.title, "TEST_TITLE"); + assert_eq!(post.body, "TEST_BODY"); + + delete_handler(2).unwrap(); + + fs::rename("clinte_bak.json", db::PATH).unwrap(); + } } -- cgit 1.4.1-2-gfad0