summaryrefslogtreecommitdiffstats
path: root/third_party/rust/serde_with/tests/serde_as/collections.rs
blob: dad84c497855d13c9eef1b935950d7bd772b48a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use super::*;
use fnv::{FnvHashMap, FnvHashSet};

/// Test that HashSets are also supported with non-default hashers.
#[test]
fn test_fnv_hashset() {
    #[serde_as]
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct S(#[serde_as(as = "FnvHashSet<DisplayFromStr>")] FnvHashSet<u32>);

    // Normal
    is_equal(
        S([1, 2, 3, 4, 5].iter().cloned().collect()),
        expect![[r#"
            [
              "5",
              "4",
              "1",
              "3",
              "2"
            ]"#]],
    );
    is_equal(S(FnvHashSet::default()), expect![[r#"[]"#]]);
}

/// Test that HashSets are also supported with non-default hashers.
#[test]
fn test_fnv_hashmap() {
    #[serde_as]
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct S(#[serde_as(as = "FnvHashMap<DisplayFromStr, DisplayFromStr>")] FnvHashMap<u8, u32>);

    // Normal
    is_equal(
        S([(1, 1), (3, 3), (111, 111)].iter().cloned().collect()),
        expect![[r#"
            {
              "1": "1",
              "3": "3",
              "111": "111"
            }"#]],
    );
    is_equal(S(FnvHashMap::default()), expect![[r#"{}"#]]);
}