| 1 | use serde::Serialize; |
| 2 | use serde_derive::Serialize; |
| 3 | use std::cell::RefCell; |
| 4 | use std::collections::BTreeMap; |
| 5 | use std::fmt::Debug; |
| 6 | |
| 7 | fn test<T>(value: &T, expected: &str) |
| 8 | where |
| 9 | T: ?Sized + Serialize + Debug, |
| 10 | { |
| 11 | let mut out = Vec::new(); |
| 12 | let ser = &mut serde_json::Serializer::new(&mut out); |
| 13 | let result = serde_path_to_error::serialize(value, ser); |
| 14 | let path = result.unwrap_err().path().to_string(); |
| 15 | assert_eq!(path, expected); |
| 16 | } |
| 17 | |
| 18 | #[test] |
| 19 | fn test_refcell_already_borrowed() { |
| 20 | #[derive(Serialize, Debug)] |
| 21 | struct Outer<'a> { |
| 22 | k: Inner<'a>, |
| 23 | } |
| 24 | |
| 25 | #[derive(Serialize, Debug)] |
| 26 | struct Inner<'a> { |
| 27 | refcell: &'a RefCell<String>, |
| 28 | } |
| 29 | |
| 30 | let refcell = RefCell::new(String::new()); |
| 31 | let outer = Outer { |
| 32 | k: Inner { refcell: &refcell }, |
| 33 | }; |
| 34 | |
| 35 | let _borrowed = refcell.borrow_mut(); |
| 36 | test(&outer, "k.refcell"); |
| 37 | } |
| 38 | |
| 39 | #[test] |
| 40 | fn test_map_nonstring_key() { |
| 41 | fn singleton_map<K: Ord, V>(key: K, value: V) -> BTreeMap<K, V> { |
| 42 | let mut map = BTreeMap::new(); |
| 43 | map.insert(key, value); |
| 44 | map |
| 45 | } |
| 46 | |
| 47 | let map = singleton_map(b"", 0); |
| 48 | let map = singleton_map("k", map); |
| 49 | let map = singleton_map(100, map); |
| 50 | |
| 51 | test(&map, "100.k"); |
| 52 | } |