Repository: Geal/nom
Pull request #853 (2018-12-01 12:09:14)

Improved json test
This PR improves the readability of the json test by using multi-line string formatting. It also asserts on the output rather than using `println!`. 

One thing I'd like to fix but I'm not sure how, is the input string must end in a null byte '\0` or else the parsing fails. This behavior was carried over from the previous implementation. 



coveralls: 
[![Coverage Status](https://coveralls.io/builds/20398127/badge)](https://coveralls.io/builds/20398127)

Coverage decreased (-0.04%) to 87.543% when pulling **52aded733363a81b3620eaa632893658fad08e3c on JoshMcguigan:json-example** into **49a20d4f2bca4b893108096905bb91ac47193b91 on Geal:master**.


Patch:
diff --git a/README.md b/README.md
index fd9d454..51f5a9f 100644
--- a/README.md
+++ b/README.md
@@ -237,6 +237,7 @@ Here is a list of known projects using nom:
   * [GLSL](https://github.com/phaazon/glsl)
   * [Lua](https://github.com/doomrobo/nom-lua53)
   * [SQL](https://github.com/ms705/nom-sql)
+  * [Elm](https://github.com/cout970/Elm-interpreter)
 - Interface definition formats:
   * [Thrift](https://github.com/thehydroimpulse/thrust)
 - Audio, video and image formats:


Test patch:
diff --git a/tests/json.rs b/tests/json.rs
index 2d5ca04..bb62de7 100644
--- a/tests/json.rs
+++ b/tests/json.rs
@@ -23,34 +23,34 @@ named!(float<f32>, flat_map!(recognize_float, parse_to!(f32)));
 named!(
   string<&str>,
   delimited!(
-    tag!("\""),
+    char!('"'),
     //map_res!(escaped!(call!(alphanumeric), '\\', is_a!("\"n\\")), str::from_utf8),
     map_res!(
       escaped!(take_while1!(is_alphanumeric), '\\', one_of!("\"n\\")),
       str::from_utf8
     ),
-    tag!("\"")
+    char!('"')
   )
 );
 
 named!(
   array<Vec<JsonValue>>,
   ws!(delimited!(
-    tag!("["),
-    separated_list!(tag!(","), value),
-    tag!("]")
+    char!('['),
+    separated_list!(char!(','), value),
+    char!(']')
   ))
 );
 
 named!(
   key_value<(&str, JsonValue)>,
-  ws!(separated_pair!(string, tag!(":"), value))
+  ws!(separated_pair!(string, char!(':'), value))
 );
 
 named!(
   hash<HashMap<String, JsonValue>>,
   ws!(map!(
-    delimited!(tag!("{"), separated_list!(tag!(","), key_value), tag!("}")),
+    delimited!(char!('{'), separated_list!(char!(','), key_value), char!('}')),
     |tuple_vec| {
       let mut h: HashMap<String, JsonValue> = HashMap::new();
       for (k, v) in tuple_vec {
@@ -72,25 +72,34 @@ named!(
 );
 
 #[test]
-fn hash_test() {
-  let test = &b"  { \"a\"\t: 42,
-  \"b\": \"x\"
-  }\0";
-
-  //FIXME: top level value must be an object?
-  println!("{:?}", value(&test[..]).unwrap());
-  //assert!(false);
+fn json_object() {
+  let input =
+    r#"{
+      "a": 42,
+      "b": "x"
+    }\0"#;
+
+  let mut expected_map = HashMap::new();
+  expected_map.insert(String::from("a"), JsonValue::Num(42f32));
+  expected_map.insert(String::from("b"), JsonValue::Str(String::from("x")));
+  let expected = JsonValue::Object(expected_map);
+
+  assert_eq!(expected, value(input.as_bytes()).unwrap().1);
 }
 
 #[test]
-fn parse_example_test() {
-  let test = &b"  { \"a\"\t: 42,
-  \"b\": [ \"x\", \"y\", 12 ] ,
-  \"c\": { \"hello\" : \"world\"
-  }
-  }\0";
-
-  //FIXME: top level value must be an object?
-  println!("{:?}", value(&test[..]).unwrap());
-  //assert!(false);
+fn json_array() {
+  let input =
+    r#"[
+      42,
+      "x"
+    ]\0"#;
+
+  let expected_vec = vec![
+    JsonValue::Num(42f32),
+    JsonValue::Str(String::from("x"))
+  ];
+  let expected = JsonValue::Array(expected_vec);
+
+  assert_eq!(expected, value(input.as_bytes()).unwrap().1);
 }