Skip to content

Commit fc3fc7e

Browse files
committed
Test case for source parse error
1 parent 4e655a5 commit fc3fc7e

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/constants.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use colored::*;
22
use std::fmt;
33

4-
#[derive(Debug)]
4+
// PartialEq is added for the sake of Test case that uses assert_eq
5+
#[derive(Debug, PartialEq)]
56
pub enum Message {
67
BadOption,
78
SOURCE1,

src/lib.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
pub mod constants;
22
pub mod ds;
33
mod process;
4-
4+
use constants::Message;
55
use ds::mismatch::Mismatch;
6-
pub fn compare_jsons(a: &str, b: &str) -> Result<Mismatch, serde_json::Error> {
7-
let value1 = serde_json::from_str(a)?;
8-
let value2 = serde_json::from_str(b)?;
6+
7+
pub fn compare_jsons(a: &str, b: &str) -> Result<Mismatch, Message> {
8+
let value1 = match serde_json::from_str(a) {
9+
Ok(val1) => val1,
10+
Err(_) => return Err(Message::JSON1),
11+
};
12+
let value2 = match serde_json::from_str(b) {
13+
Ok(val2) => val2,
14+
Err(_) => return Err(Message::JSON2),
15+
};
916
Ok(process::match_json(&value1, &value2))
1017
}
1118

@@ -139,4 +146,28 @@ mod tests {
139146
Mismatch::new(KeyNode::Nil, KeyNode::Nil, KeyNode::Nil)
140147
);
141148
}
149+
150+
#[test]
151+
fn parse_err_source_one() {
152+
let invalid_json1 = r#"{invalid: json}"#;
153+
let valid_json2 = r#"{"a":"b"}"#;
154+
match compare_jsons(invalid_json1, valid_json2) {
155+
Ok(_) => panic!("This shouldn't be an Ok"),
156+
Err(err) => {
157+
assert_eq!(Message::JSON1, err);
158+
},
159+
};
160+
}
161+
162+
#[test]
163+
fn parse_err_source_two() {
164+
let valid_json1 = r#"{"a":"b"}"#;
165+
let invalid_json2 = r#"{invalid: json}"#;
166+
match compare_jsons(valid_json1, invalid_json2) {
167+
Ok(_) => panic!("This shouldn't be an Ok"),
168+
Err(err) => {
169+
assert_eq!(Message::JSON2, err);
170+
},
171+
};
172+
}
142173
}

0 commit comments

Comments
 (0)