1
macro_rules! test_non_identifier {
2
    ($test_name:ident => $deserialize_method:ident($($deserialize_param:expr),*)) => {
3
        #[test]
4
100
        fn $test_name() {
5
            use serde::{Deserialize, Deserializer, de::Visitor, de::MapAccess};
6

            
7
            struct FieldVisitor;
8

            
9
            impl<'de> Visitor<'de> for FieldVisitor {
10
                type Value = FieldName;
11

            
12
                // GRCOV_EXCL_START
13
                fn expecting(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
14
                    fmt.write_str("an error")
15
                }
16
                // GRCOV_EXCL_STOP
17
            }
18

            
19
            struct FieldName;
20

            
21
            impl<'de> Deserialize<'de> for FieldName {
22
100
                fn deserialize<D: Deserializer<'de>>(deserializer: D)
23
100
                    -> Result<Self, D::Error>
24
100
                {
25
100
                    deserializer.$deserialize_method($($deserialize_param,)* FieldVisitor)
26
100
                }
27
            }
28

            
29
            struct StructVisitor;
30

            
31
            impl<'de> Visitor<'de> for StructVisitor {
32
                type Value = Struct;
33

            
34
                // GRCOV_EXCL_START
35
                fn expecting(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
36
                    fmt.write_str("a struct")
37
                }
38
                // GRCOV_EXCL_STOP
39

            
40
100
                fn visit_map<A: MapAccess<'de>>(self, mut map: A)
41
100
                    -> Result<Self::Value, A::Error>
42
100
                {
43
100
                    map.next_key::<FieldName>().map(|_| Struct)
44
100
                }
45
            }
46

            
47
            #[derive(Debug)]
48
            struct Struct;
49

            
50
            impl<'de> Deserialize<'de> for Struct {
51
100
                fn deserialize<D: Deserializer<'de>>(deserializer: D)
52
100
                    -> Result<Self, D::Error>
53
100
                {
54
100
                    deserializer.deserialize_struct("Struct", &[], StructVisitor)
55
100
                }
56
            }
57

            
58
100
            assert_eq!(
59
100
                ron::from_str::<Struct>("(true: 4)").unwrap_err().code,
60
100
                ron::Error::ExpectedIdentifier
61
100
            )
62
100
        }
63
    };
64
}
65

            
66
test_non_identifier! { test_bool => deserialize_bool() }
67
test_non_identifier! { test_i8 => deserialize_i8() }
68
test_non_identifier! { test_i16 => deserialize_i16() }
69
test_non_identifier! { test_i32 => deserialize_i32() }
70
test_non_identifier! { test_i64 => deserialize_i64() }
71
#[cfg(feature = "integer128")]
72
test_non_identifier! { test_i128 => deserialize_i128() }
73
test_non_identifier! { test_u8 => deserialize_u8() }
74
test_non_identifier! { test_u16 => deserialize_u16() }
75
test_non_identifier! { test_u32 => deserialize_u32() }
76
test_non_identifier! { test_u64 => deserialize_u64() }
77
#[cfg(feature = "integer128")]
78
test_non_identifier! { test_u128 => deserialize_u128() }
79
test_non_identifier! { test_f32 => deserialize_f32() }
80
test_non_identifier! { test_f64 => deserialize_f64() }
81
test_non_identifier! { test_char => deserialize_char() }
82
// Removed due to fix for #511 - string keys are allowed.
83
// test_non_identifier! { test_string => deserialize_string() }
84
// See comment above. If deserialize_str is to be added, it should give the same expected result as
85
// deserialize_string. deserialize_str and deserialize_string should be consistently implemented.
86
// test_non_identifier! { test_str => deserialize_str() }
87
test_non_identifier! { test_bytes => deserialize_bytes() }
88
test_non_identifier! { test_byte_buf => deserialize_byte_buf() }
89
test_non_identifier! { test_option => deserialize_option() }
90
test_non_identifier! { test_unit => deserialize_unit() }
91
test_non_identifier! { test_unit_struct => deserialize_unit_struct("") }
92
test_non_identifier! { test_newtype_struct => deserialize_newtype_struct("") }
93
test_non_identifier! { test_seq => deserialize_seq() }
94
test_non_identifier! { test_tuple => deserialize_tuple(0) }
95
test_non_identifier! { test_tuple_struct => deserialize_tuple_struct("", 0) }
96
test_non_identifier! { test_map => deserialize_map() }
97
test_non_identifier! { test_struct => deserialize_struct("", &[]) }
98
test_non_identifier! { test_enum => deserialize_enum("", &[]) }