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