Simplify test calls, autoformat
This commit is contained in:
parent
a2ecaef5ee
commit
ecb3e231ee
77
src/lib.rs
77
src/lib.rs
@ -297,11 +297,7 @@ impl JSON {
|
|||||||
|
|
||||||
// If there are numeric digits attempt to parse the digits as a number
|
// If there are numeric digits attempt to parse the digits as a number
|
||||||
if n > start {
|
if n > start {
|
||||||
let mut end = if n < self.chars.len() {
|
let mut end = if n < self.chars.len() { n } else { max };
|
||||||
n
|
|
||||||
} else {
|
|
||||||
max
|
|
||||||
};
|
|
||||||
|
|
||||||
// Hack to remove non-number characters
|
// Hack to remove non-number characters
|
||||||
if !self.chars[end].is_ascii_digit() {
|
if !self.chars[end].is_ascii_digit() {
|
||||||
@ -313,8 +309,8 @@ impl JSON {
|
|||||||
match str.parse::<f64>() {
|
match str.parse::<f64>() {
|
||||||
Ok(number) => {
|
Ok(number) => {
|
||||||
self.increment(str.len());
|
self.increment(str.len());
|
||||||
return Ok(Some(JSONValue::Number(number)))
|
return Ok(Some(JSONValue::Number(number)));
|
||||||
},
|
}
|
||||||
Err(e) => Err(ParseError::ExpectedDigit(format!("'{}', {:#?}", str, e))),
|
Err(e) => Err(ParseError::ExpectedDigit(format!("'{}', {:#?}", str, e))),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -395,12 +391,10 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_keyword() {
|
fn parse_keyword() {
|
||||||
let mut parser = JSON::new(r#""foobarbaz""#);
|
let res = JSON::new(r#""foobarbaz""#).parse_keyword("true", JSONValue::True);
|
||||||
let res = JSON::parse_keyword(&mut parser, "true", JSONValue::True);
|
|
||||||
assert_eq!(res, Ok(None));
|
assert_eq!(res, Ok(None));
|
||||||
|
|
||||||
let mut parser = JSON::new("true");
|
let res = JSON::new("true").parse_keyword("true", JSONValue::True);
|
||||||
let res = JSON::parse_keyword(&mut parser, "true", JSONValue::True);
|
|
||||||
assert_eq!(res, Ok(Some(JSONValue::True)));
|
assert_eq!(res, Ok(Some(JSONValue::True)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -413,41 +407,34 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_string() {
|
fn parse_string() {
|
||||||
let mut parser = JSON::new(r#""\t""#);
|
let res = JSON::new(r#""\t""#).parse_string();
|
||||||
let res = JSON::parse_string(&mut parser);
|
|
||||||
assert_eq!(res, Ok(Some(JSONValue::String(String::from("\t")))));
|
assert_eq!(res, Ok(Some(JSONValue::String(String::from("\t")))));
|
||||||
|
|
||||||
let mut parser = JSON::new(r#""\u203d""#);
|
let res = JSON::new(r#""\u203d""#).parse_string();
|
||||||
let res = JSON::parse_string(&mut parser);
|
|
||||||
assert_eq!(res, Ok(Some(JSONValue::String(String::from("‽")))));
|
assert_eq!(res, Ok(Some(JSONValue::String(String::from("‽")))));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_empty_array() {
|
fn parse_empty_array() {
|
||||||
let mut parser = JSON::new("[]");
|
let res = JSON::new("[]").parse_value();
|
||||||
let res = JSON::parse_value(&mut parser);
|
|
||||||
assert_eq!(res, Ok(JSONValue::Array(vec![])));
|
assert_eq!(res, Ok(JSONValue::Array(vec![])));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_number() {
|
fn parse_number() {
|
||||||
// This function works like I think, right?
|
// 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::new(r#""foo""#).parse_number();
|
||||||
let res = JSON::parse_number(&mut parser);
|
|
||||||
assert_eq!(res, Ok(None));
|
assert_eq!(res, Ok(None));
|
||||||
|
|
||||||
let mut parser = JSON::new("3.14159");
|
let res = JSON::new("3.14159").parse_number();
|
||||||
let res = JSON::parse_number(&mut parser);
|
|
||||||
assert_eq!(res, Ok(Some(JSONValue::Number(3.14159f64))));
|
assert_eq!(res, Ok(Some(JSONValue::Number(3.14159f64))));
|
||||||
|
|
||||||
let mut parser = JSON::new("3e4");
|
let res = JSON::new("3e4").parse_number();
|
||||||
let res = JSON::parse_number(&mut parser);
|
|
||||||
assert_eq!(res, Ok(Some(JSONValue::Number(3e4f64))));
|
assert_eq!(res, Ok(Some(JSONValue::Number(3e4f64))));
|
||||||
|
|
||||||
let mut parser = JSON::new("1.234,");
|
let res = JSON::new("1.234,").parse_number();
|
||||||
let res = JSON::parse_number(&mut parser);
|
|
||||||
assert_eq!(res, Ok(Some(JSONValue::Number(1.234f64))));
|
assert_eq!(res, Ok(Some(JSONValue::Number(1.234f64))));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -467,8 +454,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_object() {
|
fn parse_object() {
|
||||||
let mut parser = JSON::new(r#"{"foo": "bar"}"#);
|
let result = JSON::new(r#"{"foo": "bar"}"#).parse_object();
|
||||||
let result = JSON::parse_object(&mut parser);
|
|
||||||
|
|
||||||
let mut hash_map: HashMap<String, JSONValue> = HashMap::new();
|
let mut hash_map: HashMap<String, JSONValue> = HashMap::new();
|
||||||
hash_map.insert(String::from("foo"), JSONValue::String(String::from("bar")));
|
hash_map.insert(String::from("foo"), JSONValue::String(String::from("bar")));
|
||||||
@ -488,19 +474,37 @@ mod tests {
|
|||||||
|
|
||||||
// Number
|
// Number
|
||||||
let res = JSON::parse("9.38083151965");
|
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
|
// String
|
||||||
let res = JSON::parse(r#""/^$/""#);
|
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
|
// Number array
|
||||||
let res = JSON::parse("[1, 2, 3]");
|
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
|
// Object array
|
||||||
let result = JSON::parse("[{}]");
|
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]
|
#[test]
|
||||||
@ -512,8 +516,10 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn can_parse_arbitrary_json() {
|
fn can_parse_arbitrary_json() {
|
||||||
let result = JSON::parse(r#"[{ "a": 9.38083151965, "b": 4e3 }]"#);
|
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(
|
let result = JSON::parse(
|
||||||
r#"[{
|
r#"[{
|
||||||
@ -529,7 +535,8 @@ mod tests {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"i": ["\"", "\\", "/", "\b", "\f", "\n", "\r", "\t", "\u0001", "\uface"]
|
"i": ["\"", "\\", "/", "\b", "\f", "\n", "\r", "\t", "\u0001", "\uface"]
|
||||||
}]"#);
|
}]"#,
|
||||||
|
);
|
||||||
assert!(result.is_ok(), format!("{:#?}", result));
|
assert!(result.is_ok(), format!("{:#?}", result));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user