From b0408fe1b7e7b06b5724eff340bece4a24792e22 Mon Sep 17 00:00:00 2001 From: hijackthe2 <2948278083@qq.com> Date: Tue, 24 Oct 2023 17:13:36 +0800 Subject: [PATCH] tests: add unit test case for service/src/upgrade.rs test type transformation between struct FailoverPolicy and String/&str --- service/src/upgrade.rs | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/service/src/upgrade.rs b/service/src/upgrade.rs index 2503d13ee34..7c55433af91 100644 --- a/service/src/upgrade.rs +++ b/service/src/upgrade.rs @@ -15,7 +15,7 @@ use crate::Result; pub enum UpgradeMgrError {} /// FUSE fail-over policies. -#[derive(PartialEq, Eq)] +#[derive(PartialEq, Eq, Debug)] pub enum FailoverPolicy { /// Flush pending requests. Flush, @@ -86,3 +86,41 @@ pub mod fusedev_upgrade { Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_failover_policy() { + assert_eq!( + FailoverPolicy::try_from("flush").unwrap(), + FailoverPolicy::Flush + ); + assert_eq!( + FailoverPolicy::try_from("resend").unwrap(), + FailoverPolicy::Resend + ); + + let strs = vec!["flash", "Resend"]; + for s in strs.clone().into_iter() { + assert!(FailoverPolicy::try_from(s).is_err()); + } + + let str = String::from("flush"); + assert_eq!( + FailoverPolicy::try_from(&str).unwrap(), + FailoverPolicy::Flush + ); + let str = String::from("resend"); + assert_eq!( + FailoverPolicy::try_from(&str).unwrap(), + FailoverPolicy::Resend + ); + + let strings: Vec = strs.into_iter().map(|s| s.to_owned()).collect(); + for s in strings.iter() { + assert!(FailoverPolicy::try_from(s).is_err()); + } + } +}