From d907b0f3f688ac6417b8d5bec894f513ff5aa55d Mon Sep 17 00:00:00 2001 From: Valere Date: Thu, 19 Dec 2024 15:50:05 +0100 Subject: [PATCH] fixup: Use assert_json_snapshot properly without serde first --- .../src/deserialized_responses.rs | 121 ++++++------------ ...__tests__snapshot_test_algorithm_info.snap | 6 +- ..._tests__snapshot_test_encryption_info.snap | 6 +- 3 files changed, 45 insertions(+), 88 deletions(-) diff --git a/crates/matrix-sdk-common/src/deserialized_responses.rs b/crates/matrix-sdk-common/src/deserialized_responses.rs index 890f1645fc0..0833cf3f4e9 100644 --- a/crates/matrix-sdk-common/src/deserialized_responses.rs +++ b/crates/matrix-sdk-common/src/deserialized_responses.rs @@ -906,7 +906,7 @@ impl From for SyncTimelineEvent { #[cfg(test)] mod tests { - use std::{collections::BTreeMap, vec}; + use std::collections::BTreeMap; use assert_matches::assert_matches; use insta::{assert_json_snapshot, with_settings}; @@ -1321,91 +1321,49 @@ mod tests { #[test] fn snapshot_test_verification_level() { - let level = VerificationLevel::VerificationViolation; - assert_json_snapshot! { - serde_json::to_value(&level).unwrap(), - } - - let level = VerificationLevel::UnsignedDevice; - assert_json_snapshot! { - serde_json::to_value(&level).unwrap(), - } - - let level = VerificationLevel::None(DeviceLinkProblem::InsecureSource); - assert_json_snapshot! { - serde_json::to_value(&level).unwrap(), - } - - let level = VerificationLevel::None(DeviceLinkProblem::MissingDevice); - assert_json_snapshot! { - serde_json::to_value(&level).unwrap(), - } - - let level = VerificationLevel::UnverifiedIdentity; - assert_json_snapshot! { - serde_json::to_value(&level).unwrap() - } + assert_json_snapshot!(VerificationLevel::VerificationViolation); + assert_json_snapshot!(VerificationLevel::UnsignedDevice); + assert_json_snapshot!(VerificationLevel::None(DeviceLinkProblem::InsecureSource)); + assert_json_snapshot!(VerificationLevel::None(DeviceLinkProblem::MissingDevice)); + assert_json_snapshot!(VerificationLevel::UnverifiedIdentity); } #[test] fn snapshot_test_verification_states() { - let states = vec![ - VerificationState::Unverified(VerificationLevel::UnsignedDevice), - VerificationState::Unverified(VerificationLevel::VerificationViolation), - VerificationState::Unverified(VerificationLevel::None( - DeviceLinkProblem::InsecureSource, - )), - VerificationState::Unverified(VerificationLevel::None( - DeviceLinkProblem::MissingDevice, - )), - VerificationState::Verified, - ]; - - for state in states { - assert_json_snapshot! { - serde_json::to_value(&state).unwrap(), - } - } + assert_json_snapshot!(VerificationState::Unverified(VerificationLevel::UnsignedDevice)); + assert_json_snapshot!(VerificationState::Unverified( + VerificationLevel::VerificationViolation + )); + assert_json_snapshot!(VerificationState::Unverified(VerificationLevel::None( + DeviceLinkProblem::InsecureSource, + ))); + assert_json_snapshot!(VerificationState::Unverified(VerificationLevel::None( + DeviceLinkProblem::MissingDevice, + ))); + assert_json_snapshot!(VerificationState::Verified); } #[test] fn snapshot_test_shield_states() { - let state = ShieldState::None; - assert_json_snapshot! { - serde_json::to_value(&state).unwrap(), - } - - let state = - ShieldState::Red { code: ShieldStateCode::UnverifiedIdentity, message: "a message" }; - assert_json_snapshot! { - serde_json::to_value(&state).unwrap(), - } - - let state = ShieldState::Grey { + assert_json_snapshot!(ShieldState::None); + assert_json_snapshot!(ShieldState::Red { + code: ShieldStateCode::UnverifiedIdentity, + message: "a message" + }); + assert_json_snapshot!(ShieldState::Grey { code: ShieldStateCode::AuthenticityNotGuaranteed, message: "authenticity of this message cannot be guaranteed", - }; - assert_json_snapshot! { - serde_json::to_value(&state).unwrap(), - } + }); } #[test] fn snapshot_test_shield_codes() { - let codes = vec![ - ShieldStateCode::AuthenticityNotGuaranteed, - ShieldStateCode::UnknownDevice, - ShieldStateCode::UnsignedDevice, - ShieldStateCode::UnverifiedIdentity, - ShieldStateCode::SentInClear, - ShieldStateCode::VerificationViolation, - ]; - - for code in codes { - assert_json_snapshot! { - serde_json::to_value(code).unwrap(), - } - } + assert_json_snapshot!(ShieldStateCode::AuthenticityNotGuaranteed); + assert_json_snapshot!(ShieldStateCode::UnknownDevice); + assert_json_snapshot!(ShieldStateCode::UnsignedDevice); + assert_json_snapshot!(ShieldStateCode::UnverifiedIdentity); + assert_json_snapshot!(ShieldStateCode::SentInClear); + assert_json_snapshot!(ShieldStateCode::VerificationViolation); } #[test] @@ -1415,14 +1373,13 @@ mod tests { map.insert(DeviceKeyAlgorithm::Ed25519, "claimedclaimeded25519".to_owned()); let info = AlgorithmInfo::MegolmV1AesSha2 { curve25519_key: "curvecurvecurve".into(), - sender_claimed_keys: map, + sender_claimed_keys: BTreeMap::from([ + (DeviceKeyAlgorithm::Curve25519, "claimedclaimedcurve25519".to_owned()), + (DeviceKeyAlgorithm::Ed25519, "claimedclaimeded25519".to_owned()), + ]), }; - with_settings!({sort_maps =>true}, { - assert_json_snapshot! { - serde_json::to_value(&info).unwrap(), - } - }) + assert_json_snapshot!(info) } #[test] @@ -1438,10 +1395,8 @@ mod tests { }; with_settings!({sort_maps =>true}, { - assert_json_snapshot! { - serde_json::to_value(&info).unwrap(), - } - }); + assert_json_snapshot!(info) + }) } #[test] @@ -1481,6 +1436,8 @@ mod tests { }; with_settings!({sort_maps =>true}, { + // We use directly the serde_json formatter here, because of a bug in insta + // not serializing custom BTreeMap key enum https://github.com/mitsuhiko/insta/issues/689 assert_json_snapshot! { serde_json::to_value(&room_event).unwrap(), } diff --git a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_algorithm_info.snap b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_algorithm_info.snap index 9b4a8b46750..94277ee0deb 100644 --- a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_algorithm_info.snap +++ b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_algorithm_info.snap @@ -1,13 +1,13 @@ --- source: crates/matrix-sdk-common/src/deserialized_responses.rs -expression: "serde_json::to_value(&info).unwrap()" +expression: info --- { "MegolmV1AesSha2": { "curve25519_key": "curvecurvecurve", "sender_claimed_keys": { - "curve25519": "claimedclaimedcurve25519", - "ed25519": "claimedclaimeded25519" + "ed25519": "claimedclaimeded25519", + "curve25519": "claimedclaimedcurve25519" } } } diff --git a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_encryption_info.snap b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_encryption_info.snap index cc548f41f0a..96e5c2b2765 100644 --- a/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_encryption_info.snap +++ b/crates/matrix-sdk-common/src/snapshots/matrix_sdk_common__deserialized_responses__tests__snapshot_test_encryption_info.snap @@ -1,15 +1,15 @@ --- source: crates/matrix-sdk-common/src/deserialized_responses.rs -expression: "serde_json::to_value(&info).unwrap()" +expression: info --- { + "sender": "@alice:localhost", + "sender_device": "ABCDEFGH", "algorithm_info": { "MegolmV1AesSha2": { "curve25519_key": "curvecurvecurve", "sender_claimed_keys": {} } }, - "sender": "@alice:localhost", - "sender_device": "ABCDEFGH", "verification_state": "Verified" }