Formatting gixes

This commit is contained in:
Timothy Warren 2020-02-11 16:42:33 -05:00
parent 6bfa7e53bf
commit a4cb3f9b0e
1 changed files with 40 additions and 45 deletions

View File

@ -388,8 +388,8 @@ impl JSON {
#[cfg(test)]
mod tests {
use super::JSONValue::{Array, False, Null, Number, Object, True};
use super::*;
use super::JSONValue::{Object, Array, Number, True, False, Null};
impl JSONValue {
fn unwrap_object(self) -> JSONMap {
@ -402,7 +402,7 @@ mod tests {
fn unwrap_array(self) -> Vec<JSONValue> {
match self {
JSONValue::Array(a) => a,
_ => panic!("Tried to unwrap a non-array")
_ => panic!("Tried to unwrap a non-array"),
}
}
}
@ -460,14 +460,7 @@ mod tests {
fn can_parse_array_of_keywords() {
let result = JSON::parse("[true,false,null]");
assert_eq!(
result,
Ok(Array(vec![
True,
False,
Null
]))
);
assert_eq!(result, Ok(Array(vec![True, False, Null])));
}
#[test]
@ -492,11 +485,7 @@ mod tests {
// Number
let res = JSON::parse("9.38083151965");
assert_eq!(
res,
Ok(Number(9.38083151965)),
"Failed to parse number"
);
assert_eq!(res, Ok(Number(9.38083151965)), "Failed to parse number");
// String
let res = JSON::parse(r#""/^$/""#);
@ -510,19 +499,12 @@ mod tests {
let res = JSON::parse("[1, 2, 3]");
assert_eq!(
res,
Ok(Array(vec![
Number(1f64),
Number(2f64),
Number(3f64)
]))
Ok(Array(vec![Number(1f64), Number(2f64), Number(3f64)]))
);
// Object array
let result = JSON::parse("[{}]");
assert_eq!(
result,
Ok(JSONValue::Array(vec![Object(HashMap::new())]))
);
assert_eq!(result, Ok(JSONValue::Array(vec![Object(HashMap::new())])));
}
#[test]
@ -548,12 +530,17 @@ mod tests {
let expected = Ok(Array(vec![Object(map)]));
assert_eq!(result, expected, "Failed on just number values: {:#?}", result);
assert_eq!(
result, expected,
"Failed on just number values: {:#?}",
result
);
}
#[test]
fn parse_weird_character_array() {
let result = JSON::parse(r#"["\"", "\\", "/", "\b", "\f", "\n", "\r", "\t", "\u0001", "\uface"]"#);
let result =
JSON::parse(r#"["\"", "\\", "/", "\b", "\f", "\n", "\r", "\t", "\u0001", "\uface"]"#);
let expected = Ok(Array(vec![
JSONValue::String(String::from("\"")),
JSONValue::String(String::from("\\")),
@ -573,7 +560,7 @@ mod tests {
#[test]
fn parse_full_json_example() {
let result = JSON::parse(
r#"[{
r#"[{
"a": 9.38083151965,
"b": 4e3,
"c": [1, 2, 3],
@ -595,37 +582,45 @@ r#"[{
let mut gmap: JSONMap = HashMap::new();
gmap.insert(String::from("h"), Null);
fmap.insert(String::from("g"), Object(gmap));
emap.insert(String::from("f"), Object(fmap));
map.insert(String::from("a"), Number(9.38083151965f64));
map.insert(String::from("b"), Number(4e3f64));
map.insert(String::from("c"), Array(vec![Number(1f64), Number(2f64), Number(3f64)]));
map.insert(
String::from("c"),
Array(vec![Number(1f64), Number(2f64), Number(3f64)]),
);
map.insert(String::from("d"), JSONValue::String(String::from("foo")));
map.insert(String::from("e"), Object(emap));
let i = vec![
JSONValue::String(String::from("\"")),
JSONValue::String(String::from("\\")),
JSONValue::String(String::from("/")),
JSONValue::String(String::from("\u{8}")),
JSONValue::String(String::from("\x0C")),
JSONValue::String(String::from("\n")),
JSONValue::String(String::from("\r")),
JSONValue::String(String::from("\t")),
JSONValue::String(String::from("\u{1}")),
JSONValue::String(String::from("\u{face}")),
];
map.insert(String::from("i"), Array(i));
map.insert(
String::from("i"),
Array(vec![
JSONValue::String(String::from("\"")),
JSONValue::String(String::from("\\")),
JSONValue::String(String::from("/")),
JSONValue::String(String::from("\u{8}")),
JSONValue::String(String::from("\x0C")),
JSONValue::String(String::from("\n")),
JSONValue::String(String::from("\r")),
JSONValue::String(String::from("\t")),
JSONValue::String(String::from("\u{1}")),
JSONValue::String(String::from("\u{face}")),
]),
);
assert!(result.is_ok(), format!("{:#?}", result));
let result_map = result.unwrap().unwrap_array()[0].clone().unwrap_object();
for (k, v) in &map {
assert_eq!(result_map.get(k).unwrap(), v, "HashMap Entry Differs: {:#?}, {:#?}", result_map.get(k).unwrap(), v);
assert_eq!(
result_map.get(k).unwrap(),
v,
"HashMap Entry Differs: {:#?}, {:#?}",
result_map.get(k).unwrap(),
v
);
}
}
}