Formatting gixes
This commit is contained in:
parent
6bfa7e53bf
commit
a4cb3f9b0e
85
src/lib.rs
85
src/lib.rs
@ -388,8 +388,8 @@ impl JSON {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use super::JSONValue::{Array, False, Null, Number, Object, True};
|
||||||
use super::*;
|
use super::*;
|
||||||
use super::JSONValue::{Object, Array, Number, True, False, Null};
|
|
||||||
|
|
||||||
impl JSONValue {
|
impl JSONValue {
|
||||||
fn unwrap_object(self) -> JSONMap {
|
fn unwrap_object(self) -> JSONMap {
|
||||||
@ -402,7 +402,7 @@ mod tests {
|
|||||||
fn unwrap_array(self) -> Vec<JSONValue> {
|
fn unwrap_array(self) -> Vec<JSONValue> {
|
||||||
match self {
|
match self {
|
||||||
JSONValue::Array(a) => a,
|
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() {
|
fn can_parse_array_of_keywords() {
|
||||||
let result = JSON::parse("[true,false,null]");
|
let result = JSON::parse("[true,false,null]");
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(result, Ok(Array(vec![True, False, Null])));
|
||||||
result,
|
|
||||||
Ok(Array(vec![
|
|
||||||
True,
|
|
||||||
False,
|
|
||||||
Null
|
|
||||||
]))
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -492,11 +485,7 @@ mod tests {
|
|||||||
|
|
||||||
// Number
|
// Number
|
||||||
let res = JSON::parse("9.38083151965");
|
let res = JSON::parse("9.38083151965");
|
||||||
assert_eq!(
|
assert_eq!(res, Ok(Number(9.38083151965)), "Failed to parse number");
|
||||||
res,
|
|
||||||
Ok(Number(9.38083151965)),
|
|
||||||
"Failed to parse number"
|
|
||||||
);
|
|
||||||
|
|
||||||
// String
|
// String
|
||||||
let res = JSON::parse(r#""/^$/""#);
|
let res = JSON::parse(r#""/^$/""#);
|
||||||
@ -510,19 +499,12 @@ mod tests {
|
|||||||
let res = JSON::parse("[1, 2, 3]");
|
let res = JSON::parse("[1, 2, 3]");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
res,
|
res,
|
||||||
Ok(Array(vec![
|
Ok(Array(vec![Number(1f64), Number(2f64), Number(3f64)]))
|
||||||
Number(1f64),
|
|
||||||
Number(2f64),
|
|
||||||
Number(3f64)
|
|
||||||
]))
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Object array
|
// Object array
|
||||||
let result = JSON::parse("[{}]");
|
let result = JSON::parse("[{}]");
|
||||||
assert_eq!(
|
assert_eq!(result, Ok(JSONValue::Array(vec![Object(HashMap::new())])));
|
||||||
result,
|
|
||||||
Ok(JSONValue::Array(vec![Object(HashMap::new())]))
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -548,12 +530,17 @@ mod tests {
|
|||||||
|
|
||||||
let expected = Ok(Array(vec![Object(map)]));
|
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]
|
#[test]
|
||||||
fn parse_weird_character_array() {
|
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![
|
let expected = Ok(Array(vec![
|
||||||
JSONValue::String(String::from("\"")),
|
JSONValue::String(String::from("\"")),
|
||||||
JSONValue::String(String::from("\\")),
|
JSONValue::String(String::from("\\")),
|
||||||
@ -573,7 +560,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn parse_full_json_example() {
|
fn parse_full_json_example() {
|
||||||
let result = JSON::parse(
|
let result = JSON::parse(
|
||||||
r#"[{
|
r#"[{
|
||||||
"a": 9.38083151965,
|
"a": 9.38083151965,
|
||||||
"b": 4e3,
|
"b": 4e3,
|
||||||
"c": [1, 2, 3],
|
"c": [1, 2, 3],
|
||||||
@ -595,37 +582,45 @@ r#"[{
|
|||||||
let mut gmap: JSONMap = HashMap::new();
|
let mut gmap: JSONMap = HashMap::new();
|
||||||
|
|
||||||
gmap.insert(String::from("h"), Null);
|
gmap.insert(String::from("h"), Null);
|
||||||
|
|
||||||
fmap.insert(String::from("g"), Object(gmap));
|
fmap.insert(String::from("g"), Object(gmap));
|
||||||
|
|
||||||
emap.insert(String::from("f"), Object(fmap));
|
emap.insert(String::from("f"), Object(fmap));
|
||||||
|
|
||||||
map.insert(String::from("a"), Number(9.38083151965f64));
|
map.insert(String::from("a"), Number(9.38083151965f64));
|
||||||
map.insert(String::from("b"), Number(4e3f64));
|
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("d"), JSONValue::String(String::from("foo")));
|
||||||
map.insert(String::from("e"), Object(emap));
|
map.insert(String::from("e"), Object(emap));
|
||||||
|
|
||||||
let i = vec![
|
map.insert(
|
||||||
JSONValue::String(String::from("\"")),
|
String::from("i"),
|
||||||
JSONValue::String(String::from("\\")),
|
Array(vec![
|
||||||
JSONValue::String(String::from("/")),
|
JSONValue::String(String::from("\"")),
|
||||||
JSONValue::String(String::from("\u{8}")),
|
JSONValue::String(String::from("\\")),
|
||||||
JSONValue::String(String::from("\x0C")),
|
JSONValue::String(String::from("/")),
|
||||||
JSONValue::String(String::from("\n")),
|
JSONValue::String(String::from("\u{8}")),
|
||||||
JSONValue::String(String::from("\r")),
|
JSONValue::String(String::from("\x0C")),
|
||||||
JSONValue::String(String::from("\t")),
|
JSONValue::String(String::from("\n")),
|
||||||
JSONValue::String(String::from("\u{1}")),
|
JSONValue::String(String::from("\r")),
|
||||||
JSONValue::String(String::from("\u{face}")),
|
JSONValue::String(String::from("\t")),
|
||||||
];
|
JSONValue::String(String::from("\u{1}")),
|
||||||
|
JSONValue::String(String::from("\u{face}")),
|
||||||
map.insert(String::from("i"), Array(i));
|
]),
|
||||||
|
);
|
||||||
|
|
||||||
assert!(result.is_ok(), format!("{:#?}", result));
|
assert!(result.is_ok(), format!("{:#?}", result));
|
||||||
let result_map = result.unwrap().unwrap_array()[0].clone().unwrap_object();
|
let result_map = result.unwrap().unwrap_array()[0].clone().unwrap_object();
|
||||||
|
|
||||||
for (k, v) in &map {
|
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
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user