| 1 | use std::collections::HashMap; |
| 2 | |
| 3 | use ron::from_str; |
| 4 | use serde::Deserialize; |
| 5 | |
| 6 | #[derive(Deserialize)] |
| 7 | #[allow(dead_code)] |
| 8 | struct Newtype(i32); |
| 9 | |
| 10 | #[derive(Deserialize)] |
| 11 | #[allow(dead_code)] |
| 12 | struct Tuple(i32, i32); |
| 13 | |
| 14 | #[derive(Deserialize)] |
| 15 | #[allow(dead_code)] |
| 16 | struct Struct { |
| 17 | a: i32, |
| 18 | b: i32, |
| 19 | } |
| 20 | |
| 21 | #[derive(Deserialize)] |
| 22 | #[allow(dead_code)] |
| 23 | enum Enum { |
| 24 | Newtype(i32), |
| 25 | Tuple(i32, i32), |
| 26 | Struct { a: i32, b: i32 }, |
| 27 | } |
| 28 | |
| 29 | #[test] |
| 30 | fn test_trailing_comma_some() { |
| 31 | assert!(from_str::<Option<i32>>("Some(1)").is_ok()); |
| 32 | assert!(from_str::<Option<i32>>("Some(1,)").is_ok()); |
| 33 | assert!(from_str::<Option<i32>>("Some(1,,)").is_err()); |
| 34 | } |
| 35 | |
| 36 | #[test] |
| 37 | fn test_trailing_comma_tuple() { |
| 38 | assert!(from_str::<(i32, i32)>("(1,2)").is_ok()); |
| 39 | assert!(from_str::<(i32, i32)>("(1,2,)").is_ok()); |
| 40 | assert!(from_str::<(i32, i32)>("(1,2,,)").is_err()); |
| 41 | } |
| 42 | |
| 43 | #[test] |
| 44 | fn test_trailing_comma_list() { |
| 45 | assert!(from_str::<Vec<i32>>("[1,2]").is_ok()); |
| 46 | assert!(from_str::<Vec<i32>>("[1,2,]").is_ok()); |
| 47 | assert!(from_str::<Vec<i32>>("[1,2,,]").is_err()); |
| 48 | } |
| 49 | |
| 50 | #[test] |
| 51 | fn test_trailing_comma_map() { |
| 52 | assert!(from_str::<HashMap<i32, bool>>("{1:false,2:true}").is_ok()); |
| 53 | assert!(from_str::<HashMap<i32, bool>>("{1:false,2:true,}").is_ok()); |
| 54 | assert!(from_str::<HashMap<i32, bool>>("{1:false,2:true,,}").is_err()); |
| 55 | } |
| 56 | |
| 57 | #[test] |
| 58 | fn test_trailing_comma_newtype_struct() { |
| 59 | assert!(from_str::<Newtype>("(1)").is_ok()); |
| 60 | assert!(from_str::<Newtype>("(1,)").is_ok()); |
| 61 | assert!(from_str::<Newtype>("(1,,)").is_err()); |
| 62 | } |
| 63 | |
| 64 | #[test] |
| 65 | fn test_trailing_comma_tuple_struct() { |
| 66 | assert!(from_str::<Tuple>("(1,2)").is_ok()); |
| 67 | assert!(from_str::<Tuple>("(1,2,)").is_ok()); |
| 68 | assert!(from_str::<Tuple>("(1,2,,)").is_err()); |
| 69 | } |
| 70 | |
| 71 | #[test] |
| 72 | fn test_trailing_comma_struct() { |
| 73 | assert!(from_str::<Struct>("(a:1,b:2)").is_ok()); |
| 74 | assert!(from_str::<Struct>("(a:1,b:2,)").is_ok()); |
| 75 | assert!(from_str::<Struct>("(a:1,b:2,,)").is_err()); |
| 76 | } |
| 77 | |
| 78 | #[test] |
| 79 | fn test_trailing_comma_enum_newtype_variant() { |
| 80 | assert!(from_str::<Enum>("Newtype(1)").is_ok()); |
| 81 | assert!(from_str::<Enum>("Newtype(1,)").is_ok()); |
| 82 | assert!(from_str::<Enum>("Newtype(1,,)").is_err()); |
| 83 | } |
| 84 | |
| 85 | #[test] |
| 86 | fn test_trailing_comma_enum_tuple_variant() { |
| 87 | assert!(from_str::<Enum>("Tuple(1,2)").is_ok()); |
| 88 | assert!(from_str::<Enum>("Tuple(1,2,)").is_ok()); |
| 89 | assert!(from_str::<Enum>("Tuple(1,2,,)").is_err()); |
| 90 | } |
| 91 | |
| 92 | #[test] |
| 93 | fn test_trailing_comma_enum_struct_variant() { |
| 94 | assert!(from_str::<Enum>("Struct(a:1,b:2)").is_ok()); |
| 95 | assert!(from_str::<Enum>("Struct(a:1,b:2,)").is_ok()); |
| 96 | assert!(from_str::<Enum>("Struct(a:1,b:2,,)").is_err()); |
| 97 | } |