diff options
author | Sam Scott <sam.scott89@gmail.com> | 2017-03-12 14:44:44 -0400 |
---|---|---|
committer | Sam Scott <sam.scott89@gmail.com> | 2017-03-12 14:44:44 -0400 |
commit | bccdce1a3ac0dc0646f4ffa9bc0a09ea19ae58a5 (patch) | |
tree | 365d4561f44407720f24ec84690611096807c040 /tests | |
parent | 317b8b17f3e3656cdc64fc6435889d005aa9a8af (diff) |
Run rustfmt on code.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_deserialize.rs | 62 | ||||
-rw-r--r-- | tests/test_serialize.rs | 8 |
2 files changed, 51 insertions, 19 deletions
diff --git a/tests/test_deserialize.rs b/tests/test_deserialize.rs index 1e83589..0bc60ab 100644 --- a/tests/test_deserialize.rs +++ b/tests/test_deserialize.rs @@ -19,6 +19,8 @@ struct QueryParams { user_ids: Vec<u8>, } +// Compares a map generated by hash_to_map with the map returned by +// qs::from_str. All types are inferred by the compiler. macro_rules! map_test { ($string:expr, $($mapvars:tt)*) => { let expected_map = hash_to_map!(New $($mapvars)*); @@ -28,10 +30,11 @@ macro_rules! map_test { } +// Macro used to quickly generate a nested HashMap from a string. macro_rules! hash_to_map { - // Base case: a map with no inputs, do nothing + // Base case: a map with no inputs, do nothing. ($map:expr, ) => (); - //{} + //{} // This parses a single map entry, with a value explicitly an expression. ($map:expr, $k:tt[e $v:expr] $($rest:tt)*) => {{ $map.insert($k.to_owned(), $v.to_owned()); @@ -44,8 +47,8 @@ macro_rules! hash_to_map { hash_to_map!($map, $($rest)*); }}; - // This parses the first entry as a nested entry, and tail calls the remaining - // in rest. + // This parses the first entry as a nested entry, and tail calls the + // remaining in rest. ($map:expr, $k:tt[$($inner:tt)*] $($rest:tt)*) => {{ let mut inner_map = HashMap::new(); hash_to_map!(inner_map, $($inner)*); @@ -60,7 +63,7 @@ macro_rules! hash_to_map { hash_to_map!(map, $($rest)*); map }} - + } #[test] @@ -73,19 +76,38 @@ fn deserialize_struct() { city: "Carrot City".to_string(), postcode: "12345".to_string(), }, - user_ids: vec!(1,2,3,4), + user_ids: vec![1, 2, 3, 4], }; - let rec_params: QueryParams = qs::from_str("name=Acme&id=42&phone=12345&address[postcode]=12345&address[city]=Carrot+City&user_ids[0]=1&user_ids[1]=2&user_ids[2]=3&user_ids[3]=4").unwrap(); + // standard parameters + let rec_params: QueryParams = qs::from_str("\ + name=Acme&id=42&phone=12345&address[postcode]=12345&\ + address[city]=Carrot+City&user_ids[0]=1&user_ids[1]=2&\ + user_ids[2]=3&user_ids[3]=4") + .unwrap(); assert_eq!(rec_params, params); - let rec_params: QueryParams = qs::from_str("name=Acme&id=42&phone=12345&address[postcode]=12345&address[city]=Carrot+City&user_ids[]=1&user_ids[]=2&user_ids[]=3&user_ids[]=4").unwrap(); + + // unindexed arrays + let rec_params: QueryParams = qs::from_str("\ + name=Acme&id=42&phone=12345&address[postcode]=12345&\ + address[city]=Carrot+City&user_ids[]=1&user_ids[]=2&\ + user_ids[]=3&user_ids[]=4") + .unwrap(); + assert_eq!(rec_params, params); + + // ordering doesn't matter + let rec_params: QueryParams = qs::from_str("\ + address[city]=Carrot+City&user_ids[]=1&user_ids[]=2&\ + name=Acme&id=42&phone=12345&address[postcode]=12345&\ + user_ids[]=3&user_ids[]=4") + .unwrap(); assert_eq!(rec_params, params); } #[test] fn qs_test_simple() { -// test('parse()', function (t) { + // test('parse()', function (t) { // t.test('parses a simple string', function (st) { // st.deepEqual(qs.parse('0=foo'), { 0: 'foo' }); map_test!("0=foo", 0["foo"]); @@ -102,7 +124,8 @@ fn qs_test_simple() { // st.deepEqual(qs.parse('a[==]=23'), { a: { '==': '23' } }); map_test!("a[==]=23", "a"["=="[23]]); - // st.deepEqual(qs.parse('foo', { strictNullHandling: true }), { foo: null }); + // st.deepEqual(qs.parse('foo', { strictNullHandling: true }), + // { foo: null }); let none: Option<String> = Option::None; map_test!("foo", "foo"[none]); @@ -127,13 +150,15 @@ fn qs_test_simple() { // st.deepEqual(qs.parse('foo2=bar2&baz2='), { foo2: 'bar2', baz2: '' }); map_test!("foo2=bar2&baz2=", "foo2"["bar2"] "baz2"[""]); - // st.deepEqual(qs.parse('foo=bar&baz', { strictNullHandling: true }), { foo: 'bar', baz: null }); + // st.deepEqual(qs.parse('foo=bar&baz', { strictNullHandling: true }), { + // foo: 'bar', baz: null }); map_test!("foo=bar&baz", "foo"[e Some("bar".to_string())] "baz"[e None]); // st.deepEqual(qs.parse('foo=bar&baz'), { foo: 'bar', baz: '' }); map_test!("foo=bar&baz", "foo"["bar"] "baz"[""]); - // st.deepEqual(qs.parse('cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World'), { + // st.deepEqual(qs.parse('cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World'), + // { // cht: 'p3', // chd: 't:60,40', // chs: '250x100', @@ -146,20 +171,23 @@ fn qs_test_simple() { "chl"["Hello|World"] ); // st.end(); -// }); + // }); } #[test] fn qs_nesting() { - // t.deepEqual(qs.parse('a[b]=c'), { a: { b: 'c' } }, 'parses a single nested string'); - // map_test!("a[b]=c", "a"["b"["c"]]); + // t.deepEqual(qs.parse('a[b]=c'), { a: { b: 'c' } }, 'parses a single + // nested string'); + map_test!("a[b]=c", "a"["b"["c"]]); - // t.deepEqual(qs.parse('a[b][c]=d'), { a: { b: { c: 'd' } } }, 'parses a double nested string'); + // t.deepEqual(qs.parse('a[b][c]=d'), { a: { b: { c: 'd' } } }, 'parses a + // double nested string'); map_test!("a[b][c]=d", "a"["b"["c"["d"]]]); // t.deepEqual( // qs.parse('a[b][c][d][e][f][g][h]=i'), // { a: { b: { c: { d: { e: { f: { '[g][h]': 'i' } } } } } } }, // 'defaults to a depth of 5' // ); - // map_test!("a[b][c][d][e][f][g][h]=i", "a"["b"["c"["d"["e"["f"["[g][h]"["i"]]]]]]]); + map_test!("a[b][c][d][e][f][g][h]=i", + "a"["b"["c"["d"["e"["f"["[g][h]"["i"]]]]]]]); } diff --git a/tests/test_serialize.rs b/tests/test_serialize.rs index af66903..4a9964a 100644 --- a/tests/test_serialize.rs +++ b/tests/test_serialize.rs @@ -28,10 +28,14 @@ fn serialize_struct() { city: "Carrot City".to_string(), postcode: "12345".to_string(), }, - user_ids: vec!(1,2,3,4), + user_ids: vec![1, 2, 3, 4], }; - assert_eq!(qs::to_string(¶ms).unwrap(), urlencode("id=42&name=Acme&phone=12345&address[city]=Carrot+City&address[postcode]=12345&user_ids[0]=1&user_ids[1]=2&user_ids[2]=3&user_ids[3]=4")); + assert_eq!(qs::to_string(¶ms).unwrap(), + urlencode("\ + id=42&name=Acme&phone=12345&address[city]=Carrot+City&\ + address[postcode]=12345&user_ids[0]=1&user_ids[1]=2&\ + user_ids[2]=3&user_ids[3]=4")); } fn urlencode(input: &str) -> String { |