~gbmor/clinte

87bf08a5cfa640d92d8b9b1abcecf269d97c8773 — Ben Morrison 1 year, 1 month ago 3578c80
expanded testing
3 files changed, 54 insertions(+), 3 deletions(-)

M src/db.rs
M src/error.rs
M src/posts.rs
M src/db.rs => src/db.rs +5 -1
@@ 22,7 22,7 @@ pub struct Post {

#[derive(Debug, Deserialize, Serialize)]
pub struct Posts {
    pub posts: Vec<Post>,
    posts: Vec<Post>,
}

#[derive(Debug)]


@@ 90,6 90,10 @@ impl Posts {
            "Couldn't write data to clinte.json",
        );
    }

    pub fn posts(&self) -> Vec<Post> {
        self.posts.clone()
    }
}

#[cfg(test)]

M src/error.rs => src/error.rs +2 -1
@@ 14,7 14,8 @@ where
            if *conf::DEBUG {
                log::error!("--> {:?}", err);
            }
            std::process::exit(1);
            //std::process::exit(1);
            panic!("{:?}", err);
        }
    }
}

M src/posts.rs => src/posts.rs +47 -1
@@ 63,7 63,7 @@ pub fn display() -> error::Result<()> {
    let all = db::Posts::get_all(db::PATH);

    let mut postvec = Vec::new();
    all.posts.iter().enumerate().for_each(|(id, post)| {
    all.posts().iter().enumerate().for_each(|(id, post)| {
        let newpost = format!(
            "{}. {} -> by {}\n{}\n\n",
            id + 1,


@@ 97,10 97,15 @@ pub fn update_handler(id: usize) -> error::Result<()> {

    id_num_in -= 1;

    #[cfg(not(test))]
    let user = &*user::NAME;

    let mut all = db::Posts::get_all(db::PATH);
    let post = all.get(id_num_in);

    #[cfg(test)]
    let user = &post.author;

    if *user != post.author {
        println!();
        println!("Users don't match. Can't update post!");


@@ 108,6 113,10 @@ pub fn update_handler(id: usize) -> error::Result<()> {
        std::process::exit(1);
    }

    #[cfg(test)]
    let new_title = String::from("TEST_TITLE");

    #[cfg(not(test))]
    let mut new_title = String::new();

    println!("Updating post {}", id_num_in);


@@ 115,9 124,16 @@ pub fn update_handler(id: usize) -> error::Result<()> {
    println!("Current Title: {}", post.title);
    println!();
    println!("Enter new title:");

    #[cfg(not(test))]
    io::stdin().read_line(&mut new_title)?;

    #[cfg(test)]
    let body_raw = String::from("TEST_BODY");

    #[cfg(not(test))]
    let body_raw = str_to_utf8(&ed::call(&post.body));

    let body = if body_raw.len() > 500 {
        &body_raw[..500]
    } else {


@@ 171,3 187,33 @@ pub fn delete_handler(id: usize) -> error::Result<()> {

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::fs;

    #[test]
    fn test_str_to_utf8() {
        let lhs = "foobar";
        let rhs = str_to_utf8(lhs);
        assert_eq!(lhs, rhs);
    }

    #[test]
    fn display_doesnt_explode() {
        assert!(display().is_ok());
    }

    #[test]
    fn test_update_handler() {
        fs::copy(db::PATH, "clinte_bak.json").unwrap();
        update_handler(1).unwrap();
        let all = db::Posts::get_all(db::PATH);
        let post = all.get(0);
        assert_eq!(post.title, "TEST_TITLE");
        assert_eq!(post.body, "TEST_BODY");
        fs::rename("clinte_bak.json", db::PATH).unwrap();
    }
}