From ecb3e231ee640f22b2853b1e9544e218d21ee8e9 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Mon, 10 Feb 2020 19:29:24 -0500 Subject: [PATCH] Simplify test calls, autoformat --- src/lib.rs | 81 +++++++++++++++++++++++++++++------------------------- 1 file changed, 44 insertions(+), 37 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0ff2169..4338326 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -255,7 +255,7 @@ impl JSON { let start = self.i; // If it doesn't start with 0-9 or a minus sign, it's probably not a number - if ! (self.chars[start].is_ascii_digit() || self.chars[start] == '-') { + if !(self.chars[start].is_ascii_digit() || self.chars[start] == '-') { return Ok(None); } @@ -297,11 +297,7 @@ impl JSON { // If there are numeric digits attempt to parse the digits as a number if n > start { - let mut end = if n < self.chars.len() { - n - } else { - max - }; + let mut end = if n < self.chars.len() { n } else { max }; // Hack to remove non-number characters if !self.chars[end].is_ascii_digit() { @@ -313,8 +309,8 @@ impl JSON { match str.parse::() { Ok(number) => { self.increment(str.len()); - return Ok(Some(JSONValue::Number(number))) - }, + return Ok(Some(JSONValue::Number(number))); + } Err(e) => Err(ParseError::ExpectedDigit(format!("'{}', {:#?}", str, e))), } } else { @@ -395,12 +391,10 @@ mod tests { #[test] fn parse_keyword() { - let mut parser = JSON::new(r#""foobarbaz""#); - let res = JSON::parse_keyword(&mut parser, "true", JSONValue::True); + let res = JSON::new(r#""foobarbaz""#).parse_keyword("true", JSONValue::True); assert_eq!(res, Ok(None)); - let mut parser = JSON::new("true"); - let res = JSON::parse_keyword(&mut parser, "true", JSONValue::True); + let res = JSON::new("true").parse_keyword("true", JSONValue::True); assert_eq!(res, Ok(Some(JSONValue::True))); } @@ -413,41 +407,34 @@ mod tests { #[test] fn parse_string() { - let mut parser = JSON::new(r#""\t""#); - let res = JSON::parse_string(&mut parser); + let res = JSON::new(r#""\t""#).parse_string(); assert_eq!(res, Ok(Some(JSONValue::String(String::from("\t"))))); - let mut parser = JSON::new(r#""\u203d""#); - let res = JSON::parse_string(&mut parser); + let res = JSON::new(r#""\u203d""#).parse_string(); assert_eq!(res, Ok(Some(JSONValue::String(String::from("‽"))))); } #[test] fn parse_empty_array() { - let mut parser = JSON::new("[]"); - let res = JSON::parse_value(&mut parser); + let res = JSON::new("[]").parse_value(); assert_eq!(res, Ok(JSONValue::Array(vec![]))); } #[test] fn parse_number() { // This function works like I think, right? - assert_ne!(','.is_ascii_digit(), true); + assert_eq!(','.is_ascii_digit(), false); - let mut parser = JSON::new(r#""foo""#); - let res = JSON::parse_number(&mut parser); + let res = JSON::new(r#""foo""#).parse_number(); assert_eq!(res, Ok(None)); - let mut parser = JSON::new("3.14159"); - let res = JSON::parse_number(&mut parser); + let res = JSON::new("3.14159").parse_number(); assert_eq!(res, Ok(Some(JSONValue::Number(3.14159f64)))); - let mut parser = JSON::new("3e4"); - let res = JSON::parse_number(&mut parser); + let res = JSON::new("3e4").parse_number(); assert_eq!(res, Ok(Some(JSONValue::Number(3e4f64)))); - let mut parser = JSON::new("1.234,"); - let res = JSON::parse_number(&mut parser); + let res = JSON::new("1.234,").parse_number(); assert_eq!(res, Ok(Some(JSONValue::Number(1.234f64)))); } @@ -467,8 +454,7 @@ mod tests { #[test] fn parse_object() { - let mut parser = JSON::new(r#"{"foo": "bar"}"#); - let result = JSON::parse_object(&mut parser); + let result = JSON::new(r#"{"foo": "bar"}"#).parse_object(); let mut hash_map: HashMap = HashMap::new(); hash_map.insert(String::from("foo"), JSONValue::String(String::from("bar"))); @@ -488,19 +474,37 @@ mod tests { // Number let res = JSON::parse("9.38083151965"); - assert_eq!(res, Ok(JSONValue::Number(9.38083151965)), "Failed to parse number"); + assert_eq!( + res, + Ok(JSONValue::Number(9.38083151965)), + "Failed to parse number" + ); // String let res = JSON::parse(r#""/^$/""#); - assert_eq!(res, Ok(JSONValue::String(String::from("/^$/"))), "Failed to parse string"); + assert_eq!( + res, + Ok(JSONValue::String(String::from("/^$/"))), + "Failed to parse string" + ); // Number array let res = JSON::parse("[1, 2, 3]"); - assert_eq!(res, Ok(JSONValue::Array(vec![JSONValue::Number(1f64), JSONValue::Number(2f64), JSONValue::Number(3f64)]))); + assert_eq!( + res, + Ok(JSONValue::Array(vec![ + JSONValue::Number(1f64), + JSONValue::Number(2f64), + JSONValue::Number(3f64) + ])) + ); // Object array let result = JSON::parse("[{}]"); - assert_eq!(result, Ok(JSONValue::Array(vec![JSONValue::Object(HashMap::new())]))); + assert_eq!( + result, + Ok(JSONValue::Array(vec![JSONValue::Object(HashMap::new())])) + ); } #[test] @@ -512,11 +516,13 @@ mod tests { #[test] fn can_parse_arbitrary_json() { let result = JSON::parse(r#"[{ "a": 9.38083151965, "b": 4e3 }]"#); - assert!(result.is_ok(), format!("Failed on just number values: {:#?}", result)); - + assert!( + result.is_ok(), + format!("Failed on just number values: {:#?}", result) + ); let result = JSON::parse( - r#"[{ +r#"[{ "a": 9.38083151965, "b": 4e3, "c": [1, 2, 3], @@ -529,7 +535,8 @@ mod tests { } }, "i": ["\"", "\\", "/", "\b", "\f", "\n", "\r", "\t", "\u0001", "\uface"] -}]"#); +}]"#, + ); assert!(result.is_ok(), format!("{:#?}", result)); } }