Skip to content

Commit

Permalink
Update hashbrown tests to work without ahash dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasbb committed Aug 17, 2023
1 parent 64e130d commit b207968
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 39 deletions.
12 changes: 0 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion serde_with/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ time_0_3 = ["dep:time_0_3"]
base64 = {version = "0.21.0", optional = true, default-features = false}
chrono_0_4 = {package = "chrono", version = "0.4.20", optional = true, default-features = false, features = ["serde"]}
doc-comment = {version = "0.3.3", optional = true}
hashbrown_0_14 = {package = "hashbrown", version = "0.14.0", optional = true, default-features = false, features = ["ahash", "serde"]}
hashbrown_0_14 = {package = "hashbrown", version = "0.14.0", optional = true, default-features = false, features = ["serde"]}
hex = {version = "0.4.3", optional = true, default-features = false}
indexmap_1 = {package = "indexmap", version = "1.8", optional = true, default-features = false, features = ["serde-1"]}
indexmap_2 = {package = "indexmap", version = "2.0", optional = true, default-features = false, features = ["serde"]}
Expand Down
92 changes: 66 additions & 26 deletions serde_with/tests/hashbrown_0_14.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ mod utils;
use crate::utils::{check_deserialization, check_error_deserialization, is_equal};
use core::iter::FromIterator;
use expect_test::expect;
use hashbrown_0_14::{HashMap, HashSet};
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr, Same};
use std::net::IpAddr;

type HashMap<K, V, S = fnv::FnvBuildHasher> = hashbrown_0_14::HashMap<K, V, S>;
type HashSet<V, S = fnv::FnvBuildHasher> = hashbrown_0_14::HashSet<V, S>;

#[test]
fn test_hashmap() {
#[serde_as]
Expand All @@ -26,13 +28,32 @@ fn test_hashmap() {
is_equal(
S([(1, 1), (3, 3), (111, 111)].iter().cloned().collect()),
expect![[r#"
{
"1": "1",
"111": "111",
"3": "3"
}"#]],
{
"1": "1",
"3": "3",
"111": "111"
}"#]],
);
is_equal(S(HashMap::default()), expect![[r#"{}"#]]);

#[serde_as]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct SStd(
#[serde_as(
as = "HashMap<DisplayFromStr, DisplayFromStr, core::hash::BuildHasherDefault<std::collections::hash_map::DefaultHasher>>"
)]
HashMap<u8, u32, core::hash::BuildHasherDefault<std::collections::hash_map::DefaultHasher>>,
);

// Normal
is_equal(
SStd([(1, 1)].iter().cloned().collect()),
expect![[r#"
{
"1": "1"
}"#]],
);
is_equal(SStd(HashMap::default()), expect![[r#"{}"#]]);
}

#[test]
Expand All @@ -45,15 +66,34 @@ fn test_hashset() {
is_equal(
S([1, 2, 3, 4, 5].iter().cloned().collect()),
expect![[r#"
[
"1",
"5",
"4",
"3",
"2"
]"#]],
[
"5",
"4",
"1",
"3",
"2"
]"#]],
);
is_equal(S(HashSet::default()), expect![[r#"[]"#]]);

#[serde_as]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct SStd(
#[serde_as(
as = "HashSet<DisplayFromStr, core::hash::BuildHasherDefault<std::collections::hash_map::DefaultHasher>>"
)]
HashSet<u32, core::hash::BuildHasherDefault<std::collections::hash_map::DefaultHasher>>,
);

// Normal
is_equal(
SStd([1].iter().cloned().collect()),
expect![[r#"
[
"1"
]"#]],
);
is_equal(SStd(HashSet::default()), expect![[r#"[]"#]]);
}

#[test]
Expand Down Expand Up @@ -120,8 +160,8 @@ fn duplicate_key_first_wins_hashmap() {
expect![[r#"
{
"1": 1,
"2": 2,
"3": 3
"3": 3,
"2": 2
}"#]],
);

Expand All @@ -131,8 +171,8 @@ fn duplicate_key_first_wins_hashmap() {
expect![[r#"
{
"1": 1,
"2": 1,
"3": 1
"3": 1,
"2": 1
}"#]],
);

Expand All @@ -156,8 +196,8 @@ fn prohibit_duplicate_key_hashmap() {
expect![[r#"
{
"1": 1,
"2": 2,
"3": 3
"3": 3,
"2": 2
}"#]],
);

Expand All @@ -167,8 +207,8 @@ fn prohibit_duplicate_key_hashmap() {
expect![[r#"
{
"1": 1,
"2": 1,
"3": 1
"3": 1,
"2": 1
}"#]],
);

Expand Down Expand Up @@ -213,13 +253,13 @@ fn duplicate_value_last_wins_hashset() {
1,
true
],
[
2,
false
],
[
3,
true
],
[
2,
false
]
]"#]],
);
Expand Down Expand Up @@ -249,8 +289,8 @@ fn prohibit_duplicate_value_hashset() {
S(HashSet::from_iter(vec![1, 2, 3, 4])),
expect![[r#"
[
1,
4,
1,
3,
2
]"#]],
Expand Down

0 comments on commit b207968

Please sign in to comment.