about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--src/db.rs6
-rw-r--r--src/error.rs3
-rw-r--r--src/posts.rs48
3 files changed, 54 insertions, 3 deletions
diff --git a/src/db.rs b/src/db.rs
index 02cd776..20ad5a1 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -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)]
diff --git a/src/error.rs b/src/error.rs
index aa21021..ee4511c 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -14,7 +14,8 @@ where
             if *conf::DEBUG {
                 log::error!("--> {:?}", err);
             }
-            std::process::exit(1);
+            //std::process::exit(1);
+            panic!("{:?}", err);
         }
     }
 }
diff --git a/src/posts.rs b/src/posts.rs
index b286469..c38c84f 100644
--- a/src/posts.rs
+++ b/src/posts.rs
@@ -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();
+    }
+}
'oid'>9e751bb8 ^
a654e4ec ^





e5c11a51 ^






















a654e4ec ^


e5c11a51 ^
a654e4ec ^
204dae92 ^


201458e3 ^
204dae92 ^









201458e3 ^
bd5d3936 ^
204dae92 ^



201458e3 ^
204dae92 ^










bd5d3936 ^






1c2d788b ^
bd5d3936 ^



1c2d788b ^
bd5d3936 ^





























a654e4ec ^



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